<commit_before>import {
  Component, TextPropertyEditor, isArrayEqual
} from 'substance'

import { getSyntaxTokens } from '../engine/expressionHelpers'

export default
class CodeEditor extends Component {

  render($$) {
    let el = $$('div').addClass('sc-code-editor')
    // the source code
    const path = this.props.path
    const commands = this.props.commands
    const excludedCommands = this.props.excludedCommands
    const markers = this._getMarkers()
    let content = $$(TextPropertyEditor, {
      // TextPropertyEditor props
      name: this.props.name,
      path,
      withoutBreak: this.props.multiline,
      multiLine: this.props.multiline,
      // Surface props
      commands,
      excludedCommands,
      markers,
      handleTab: false
    }).ref('contentEditor')
      // EXPERIMENTAL: adding 2 spaces if at begin of line
      .on('tab', this._onTabKey)
      .on('escape', this._onEscapeKey)
    content.addClass('se-content')
    el.append(content)
    return el
  }

  _getMarkers() {
    const path = this.props.path
    return getSyntaxTokens(path, this.props.tokens)
  }

  _onEscapeKey() {
    this.send('escape')
  }

  _onTabKey() {
    const editorSession = this.context.editorSession
    const head = this._getCurrentLineHead()
    // console.log('head', head)
    if (/^\s*$/.exec(head)) {
      editorSession.transaction((tx) => {
        tx.insertText('  ')
      })
    }
  }

  _insertNewLine() {
    const editorSession = this.context.editorSession
    const indent = this._getCurrentIndent() || ''
    editorSession.transaction((tx) => {
      tx.insertText('\n' + indent)
    })
  }

  _getCurrentIndent() {<commit_msg>Fix CodeEditor.<commit_after>import {
  Component, TextPropertyEditor, isArrayEqual
} from 'substance'

import { getSyntaxTokens } from '../engine/expressionHelpers'

export default
class CodeEditor extends Component {

  render($$) {
    let el = $$('div').addClass('sc-code-editor')
    // the source code
    const path = this.props.path
    const commands = this.props.commands
    const excludedCommands = this.props.excludedCommands
    const markers = this._getMarkers()
    let content = $$(TextPropertyEditor, {
      // TextPropertyEditor props
      name: this.props.name,
      path,
      withoutBreak: this.props.multiline,
      multiLine: this.props.multiline,
      // Surface props
      commands,
      excludedCommands,
      markers,
      handleTab: false
    }).ref('contentEditor')
      // EXPERIMENTAL: adding 2 spaces if at begin of line
      .on('tab', this._onTabKey)
    content.addClass('se-content')
    el.append(content)
    return el
  }

  _getMarkers() {
    const path = this.props.path
    return getSyntaxTokens(path, this.props.tokens)
  }

  _onTabKey() {
    const editorSession = this.context.editorSession
    const head = this._getCurrentLineHead()
    // console.log('head', head)
    if (/^\s*$/.exec(head)) {
      editorSession.transaction((tx) => {
        tx.insertText('  ')
      })
    }
  }

  _insertNewLine() {
    const editorSession = this.context.editorSession
    const indent = this._getCurrentIndent() || ''
    editorSession.transaction((tx) => {
      tx.insertText('\n' + indent)
    })
  }

  _getCurrentIndent() {
{'fraction_non_alphanumeric': 0.1270378345124577, 'fraction_numerical': 0.0006151953245155337, 'mean_word_length': 2.8124267291910905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686051', 'n_tokens_mistral': 986, 'n_tokens_neox': 984, 'n_words': 207}