<commit_before>###
  todo:

    - put search replace
    - fix setSoftWrap it goes back to off when you reopen the settings

###


class Ace extends KDView

  constructor:(options, file)->

    super options, file
    @lastSavedContents = ""
    @appStorage = @getSingleton('mainController').getAppStorageSingleton 'Ace', '1.0'

  setDomElement:(cssClass)->

    @domElement = $ "<figure class='kdview'><div id='editor#{@getId()}' class='code-wrapper'></div></figure>"

  viewAppended:->

    @hide()
    @appStorage.fetchStorage (storage)=>
      require ['ace/ace'], (ace)=>
        @fetchContents (err, contents)=>
          notification?.destroy()
          @editor = ace.edit "editor#{@getId()}"
          @prepareEditor()
          @utils.defer => @emit "ace.ready"
          @setContents contents if contents
          @editor.gotoLine 0
          @focus()
          @show()

  prepareEditor:->

    @setTheme()
    @setSyntax()
    @setEditorListeners()
    @appStorage.fetchStorage (storage)=>
      @setUseSoftTabs         @appStorage.getValue('useSoftTabs')         ? yes
      @setShowGutter          @appStorage.getValue('showGutter')          ? yes
      @setUseWordWrap         @appStorage.getValue('useWordWrap')         ? no
      @setShowPrintMargin     @appStorage.getValue('showPrintMargin')     ? no
      @setHighlightActiveLine @appStorage.getValue('highlightActiveLine') ? yes
      @setShowInvisibles      @appStorage.getValue('showInvisibles')      ? no
      @setSoftWrap            @appStorage.getValue('softWrap')            or 'off'
      @setFontSize            @appStorage.getValue('fontSize')            ? 12
      @setTabSize             @appStorage.getValue('tabSize')             ? 4

  setEditorListeners:->

    @editor.getSession().selection.on 'changeCursor', (cursor)=>
      @emit "ace.change.cursor", @editor.getSession().getSelection().getCursor()

    @editor.commands.addCommand
      name    : 'find'
      bindKey :
        win   : 'Ctrl-F'
        mac   : 'Command-F'
      exec    : => @showFindReplaceView no

    @editor.commands.addCommand
      name    : 'replace'
      bindKey :
        win   : 'Ctrl-Shift-F'
        mac   : 'Command-Shift-F'
      exec    : => @showFindReplaceView yes

    @editor.commands.addCommand
      name    : 'save'
      bindKey :
        win   : 'Ctrl-S'
        mac   : 'Command-S'
      exec    : => @requestSave()

    @editor.commands.addCommand
      name    : 'save as'
      bindKey :
        win   : 'Ctrl-Shift-S'
        mac   : 'Command-Shift-S'
      exec    : => @requestSaveAs()

    @editor.commands.addCommand
      name    : 'compileAndRun'
      bindKey :
        win   : 'Ctrl-Shift-C'
        mac   : 'Command-Shift-C'
      exec    : => @getDelegate().compileAndRun()

    file = @getData()

    file.on "fs.save.finished", (err,res)=>
      unless err
        @notify "Successfully saved!", "success"
        @lastSavedContents = @lastContentsSentForSave

    file.on "fs.save.started", =><commit_msg>ace: Added Cmd+Shift+P key combo to preview file.<commit_after>###
  todo:

    - put search replace
    - fix setSoftWrap it goes back to off when you reopen the settings

###


class Ace extends KDView

  constructor:(options, file)->

    super options, file
    @lastSavedContents = ""
    @appStorage = @getSingleton('mainController').getAppStorageSingleton 'Ace', '1.0'

  setDomElement:(cssClass)->

    @domElement = $ "<figure class='kdview'><div id='editor#{@getId()}' class='code-wrapper'></div></figure>"

  viewAppended:->

    @hide()
    @appStorage.fetchStorage (storage)=>
      require ['ace/ace'], (ace)=>
        @fetchContents (err, contents)=>
          notification?.destroy()
          @editor = ace.edit "editor#{@getId()}"
          @prepareEditor()
          @utils.defer => @emit "ace.ready"
          @setContents contents if contents
          @editor.gotoLine 0
          @focus()
          @show()

  prepareEditor:->

    @setTheme()
    @setSyntax()
    @setEditorListeners()
    @appStorage.fetchStorage (storage)=>
      @setUseSoftTabs         @appStorage.getValue('useSoftTabs')         ? yes
      @setShowGutter          @appStorage.getValue('showGutter')          ? yes
      @setUseWordWrap         @appStorage.getValue('useWordWrap')         ? no
      @setShowPrintMargin     @appStorage.getValue('showPrintMargin')     ? no
      @setHighlightActiveLine @appStorage.getValue('highlightActiveLine') ? yes
      @setShowInvisibles      @appStorage.getValue('showInvisibles')      ? no
      @setSoftWrap            @appStorage.getValue('softWrap')            or 'off'
      @setFontSize            @appStorage.getValue('fontSize')            ? 12
      @setTabSize             @appStorage.getValue('tabSize')             ? 4

  setEditorListeners:->

    @editor.getSession().selection.on 'changeCursor', (cursor)=>
      @emit "ace.change.cursor", @editor.getSession().getSelection().getCursor()

    @editor.commands.addCommand
      name    : 'find'
      bindKey :
        win   : 'Ctrl-F'
        mac   : 'Command-F'
      exec    : => @showFindReplaceView no

    @editor.commands.addCommand
      name    : 'replace'
      bindKey :
        win   : 'Ctrl-Shift-F'
        mac   : 'Command-Shift-F'
      exec    : => @showFindReplaceView yes

    @editor.commands.addCommand
      name    : 'save'
      bindKey :
        win   : 'Ctrl-S'
        mac   : 'Command-S'
      exec    : => @requestSave()

    @editor.commands.addCommand
      name    : 'save as'
      bindKey :
        win   : 'Ctrl-Shift-S'
        mac   : 'Command-Shift-S'
      exec    : => @requestSaveAs()

    @editor.commands.addCommand
      name    : 'compileAndRun'
      bindKey :
        win   : 'Ctrl-Shift-C'
        mac   : 'Command-Shift-C'
      exec    : => @getDelegate().compileAndRun()

    @editor.commands.addCommand
      name    : 'preview'
      bindKey :
        win   : 'Ctrl-Shift-P'
        mac   : 'Command-Shift-P'
      exec    : => @getDelegate().preview()

    file = @getData()

    file.on "fs.save.finished", (err,res)=>
      unless err
        @notify "Successfully saved!", "success"
        @lastSavedContents = @lastContentsSentForSave

    file.on "fs.save.started", =>
{'fraction_non_alphanumeric': 0.13369669765950626, 'fraction_numerical': 0.0019236934915036871, 'mean_word_length': 2.175063613231552, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, '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': '5211934', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1855, 'n_words': 361}