<commit_before>_ = require 'underscore'

File = require 'fs'
App  = require 'app'
Pane = require 'pane'
activeWindow = App.activeWindow

ace = require 'ace/ace'

{EditSession} = require 'ace/edit_session'
{UndoManager} = require 'ace/undomanager'

module.exports =
class Editor extends Pane
  filename: null

  position: 'main'
  html: '<div id="editor"></div>'

  keymap:
    'Command-S'       : 'save'
    'Command-Shift-S' : 'saveAs'
    'Command-C'       : 'copy'
    'Command-X'       : 'cut'
    'Command-R'       : 'eval'
    'Command-/'       : 'toggleComment'
    'Command-['       : 'outdent'
    'Command-]'       : 'indent'
    'Alt-F'           : 'forwardWord'
    'Alt-B'           : 'backWord'
    'Alt-D'           : 'deleteWord'
    'Alt-Shift-,'     : 'home'
    'Alt-Shift-.'     : 'end'
    'Ctrl-L'          : 'consolelog'

  sessions: {}

  initialize: ->
    @ace = ace.edit "editor"
    @ace.setTheme require "ace/theme/twilight"
    @ace.getSession().setUseSoftTabs true
    @ace.getSession().setTabSize 2
    @ace.pane = this
    @ace.setShowInvisibles(true)

    @ace.getSession().on 'change', ->
      activeWindow.setDirty true

    el = document.body
    el.addEventListener 'DOMNodeInsertedIntoDocument', =>
      @resize()
    el.addEventListener 'DOMNodeRemovedFromDocument', =>
      @resize()

  save: ->
    return @saveAs() if not @filename

    File.write @filename, @code()
    @sessions[@filename] = @ace.getSession()
    activeWindow.setDirty false
    @ace._emit 'save', { @filename }

  open: (path) ->
    path = App.openPanel() if not path
    return if not path
    @filename = path

    if File.isDirectory @filename
      File.changeWorkingDirectory @filename
      activeWindow.setTitle _.last @filename.split '/'
      @ace.setSession @newSession()
      activeWindow.setDirty false
    else
      if /png|jpe?g|gif/i.test @filename
        App.openURL @filename
      else
        activeWindow.setTitle _.last @filename.split '/'
        @sessions[@filename] or= @newSession File.read @filename
        @ace.setSession @sessions[@filename]
        activeWindow.setDirty false
    @ace._emit 'open', { @filename }

  saveAs: ->
    if file = App.savePanel()
      @filename = file
      activeWindow.setTitle _.last @filename.split '/'
      @save()

  code: ->
    @ace.getSession().getValue()
<commit_msg>Remove trailing whitespace on save. Fixes #18.<commit_after>_ = require 'underscore'

File = require 'fs'
App  = require 'app'
Pane = require 'pane'
activeWindow = App.activeWindow

ace = require 'ace/ace'

{EditSession} = require 'ace/edit_session'
{UndoManager} = require 'ace/undomanager'

module.exports =
class Editor extends Pane
  filename: null

  position: 'main'
  html: '<div id="editor"></div>'

  keymap:
    'Command-S'       : 'save'
    'Command-Shift-S' : 'saveAs'
    'Command-C'       : 'copy'
    'Command-X'       : 'cut'
    'Command-R'       : 'eval'
    'Command-/'       : 'toggleComment'
    'Command-['       : 'outdent'
    'Command-]'       : 'indent'
    'Alt-F'           : 'forwardWord'
    'Alt-B'           : 'backWord'
    'Alt-D'           : 'deleteWord'
    'Alt-Shift-,'     : 'home'
    'Alt-Shift-.'     : 'end'
    'Ctrl-L'          : 'consolelog'

  sessions: {}

  initialize: ->
    @ace = ace.edit "editor"
    @ace.setTheme require "ace/theme/twilight"
    @ace.getSession().setUseSoftTabs true
    @ace.getSession().setTabSize 2
    @ace.pane = this
    @ace.setShowInvisibles(true)

    @ace.getSession().on 'change', ->
      activeWindow.setDirty true

    el = document.body
    el.addEventListener 'DOMNodeInsertedIntoDocument', =>
      @resize()
    el.addEventListener 'DOMNodeRemovedFromDocument', =>
      @resize()

  save: ->
    return @saveAs() if not @filename

    @removeTrailingWhitespace()
    File.write @filename, @code()   
    @sessions[@filename] = @ace.getSession()
    activeWindow.setDirty false
    @ace._emit 'save', { @filename }

  open: (path) ->
    path = App.openPanel() if not path
    return if not path
    @filename = path

    if File.isDirectory @filename
      File.changeWorkingDirectory @filename
      activeWindow.setTitle _.last @filename.split '/'
      @ace.setSession @newSession()
      activeWindow.setDirty false
    else
      if /png|jpe?g|gif/i.test @filename
        App.openURL @filename
      else
        activeWindow.setTitle _.last @filename.split '/'
        @sessions[@filename] or= @newSession File.read @filename
        @ace.setSession @sessions[@filename]
        activeWindow.setDirty false
    @ace._emit 'open', { @filename }

  saveAs: ->
    if file = App.savePanel()
      @filename = file
      activeWindow.setTitle _.last @filename.split '/'
      @save()

  code: ->
    @ace.getSession().getValue()

  removeTrailingWhitespace: ->
    @ace.replaceAll "",
      needle: "[ \t]+$"
      regExp: true
      wrap: true

{'fraction_non_alphanumeric': 0.1416904859126174, 'fraction_numerical': 0.0008166598611678236, 'mean_word_length': 2.4695467422096318, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, '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': '1631119', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1589, 'n_words': 347}