<commit_before>configManager = require './config-manager'
fsUtil        = require './fs-util'
pluginManager = require './plugin-manager'

module.exports =
  ###
  @name getTextEditorById
  @param {String} id
  @returns {Editor|null}
  ###
  getTextEditorById: (id) ->
    for editor in atom.workspace.getTextEditors()
      return editor if editor.id?.toString() is id.toString()

    return null

  ###
  @name compile
  @param {Editor} editor
  @returns {String} Compiled code
  ###
  compile: (editor) ->
    language = pluginManager.getLanguageByScope editor.getGrammar().scopeName

    code = @getSelectedCode editor

    return code unless language?

    for preCompiler in language.preCompilers
      code = preCompiler code, editor

    for compiler in language.compilers
      code = compiler code, editor

    for postCompiler in language.postCompilers
      code = postCompiler code, editor

    return code

  ###
  @name getSelectedCode
  @param {Editor} editor
  @returns {String} Selected text
  ###
  getSelectedCode: (editor) ->
    range = editor.getSelectedBufferRange()
    text  =
      if range.isEmpty()
        editor.getText()
      else
        editor.getTextInBufferRange range

    return text

  ###
  @name compileToFile
  @param {Editor} editor
  ###
  compileToFile: (editor) ->
    try
      srcPath = editor.getPath()

      return unless fsUtil.isPathInSrc srcPath

      text     = @compile editor
      destPath = fsUtil.resolvePath editor.getPath()

      unless atom.project.contains(destPath)
        atom.notifications.addError "Compile-compile: Failed to compile to file",
          detail: "Cannot write outside of project root"

      destPath = fsUtil.toExt destPath, 'js'
      fsUtil.writeFile destPath, text

    catch e
      atom.notifications.addError "Compile-compile: Failed to compile to file",
        detail: e.stack

  compileOrStack: (editor) ->
    try
      text = @compile editor
    catch e
      text = e.stack

    return text

  renderAndSave: (editor, sourceEditor, shouldWriteToFile = true) ->
    text = @compileOrStack sourceEditor

    editor.setText text

    @compileToFile(sourceEditor) if shouldWriteToFile

  buildCoffeeCompileEditor: (sourceEditor) ->
    previewEditor = atom.workspace.buildTextEditor()

    shouldCompileToFile = sourceEditor? and fsUtil.isPathInSrc(sourceEditor.getPath()) and
      pluginManager.isEditorLanguageSupported(sourceEditor, true)

    previewEditor.disposables.add(
      sourceEditor.onDidSave =>
        shouldWriteToFile = shouldCompileToFile and configManager.get('compileOnSave') and not
            configManager.get('compileOnSaveWithoutPreview')

        @renderAndSave previewEditor, sourceEditor, shouldWriteToFile
    )

    # set editor grammar to correct language
    grammar = atom.grammars.selectGrammar pluginManager.getCompiledScopeByEditor(sourceEditor)
    previewEditor.setGrammar grammar

    if shouldCompileToFile and (configManager.get('compileOnSave') or
        configManager.get('compileOnSaveWithoutPreview'))
      @compileToFile sourceEditor

    # HACK: Override TextBuffer saveAs function
    previewEditor.getBuffer().saveAs = ->

    # HACK: Override getURI and getTitle
    previewEditor.getTitle = -> "Compiled #{sourceEditor?.getTitle() or ''}".trim()
    previewEditor.getURI   = -> "coffeecompile://editor/#{sourceEditor.id}"

    return previewEditor<commit_msg>Disable prompt to save when closing preview pane<commit_after>configManager = require './config-manager'
fsUtil        = require './fs-util'
pluginManager = require './plugin-manager'

module.exports =
  ###
  @name getTextEditorById
  @param {String} id
  @returns {Editor|null}
  ###
  getTextEditorById: (id) ->
    for editor in atom.workspace.getTextEditors()
      return editor if editor.id?.toString() is id.toString()

    return null

  ###
  @name compile
  @param {Editor} editor
  @returns {String} Compiled code
  ###
  compile: (editor) ->
    language = pluginManager.getLanguageByScope editor.getGrammar().scopeName

    code = @getSelectedCode editor

    return code unless language?

    for preCompiler in language.preCompilers
      code = preCompiler code, editor

    for compiler in language.compilers
      code = compiler code, editor

    for postCompiler in language.postCompilers
      code = postCompiler code, editor

    return code

  ###
  @name getSelectedCode
  @param {Editor} editor
  @returns {String} Selected text
  ###
  getSelectedCode: (editor) ->
    range = editor.getSelectedBufferRange()
    text  =
      if range.isEmpty()
        editor.getText()
      else
        editor.getTextInBufferRange range

    return text

  ###
  @name compileToFile
  @param {Editor} editor
  ###
  compileToFile: (editor) ->
    try
      srcPath = editor.getPath()

      return unless fsUtil.isPathInSrc srcPath

      text     = @compile editor
      destPath = fsUtil.resolvePath editor.getPath()

      unless atom.project.contains(destPath)
        atom.notifications.addError "Compile-compile: Failed to compile to file",
          detail: "Cannot write outside of project root"

      destPath = fsUtil.toExt destPath, 'js'
      fsUtil.writeFile destPath, text

    catch e
      atom.notifications.addError "Compile-compile: Failed to compile to file",
        detail: e.stack

  compileOrStack: (editor) ->
    try
      text = @compile editor
    catch e
      text = e.stack

    return text

  renderAndSave: (editor, sourceEditor, shouldWriteToFile = true) ->
    text = @compileOrStack sourceEditor

    editor.setText text

    @compileToFile(sourceEditor) if shouldWriteToFile

  buildCoffeeCompileEditor: (sourceEditor) ->
    previewEditor = atom.workspace.buildTextEditor()

    shouldCompileToFile = sourceEditor? and fsUtil.isPathInSrc(sourceEditor.getPath()) and
      pluginManager.isEditorLanguageSupported(sourceEditor, true)

    previewEditor.disposables.add(
      sourceEditor.onDidSave =>
        shouldWriteToFile = shouldCompileToFile and configManager.get('compileOnSave') and not
            configManager.get('compileOnSaveWithoutPreview')

        @renderAndSave previewEditor, sourceEditor, shouldWriteToFile
    )

    # set editor grammar to correct language
    grammar = atom.grammars.selectGrammar pluginManager.getCompiledScopeByEditor(sourceEditor)
    previewEditor.setGrammar grammar

    if shouldCompileToFile and (configManager.get('compileOnSave') or
        configManager.get('compileOnSaveWithoutPreview'))
      @compileToFile sourceEditor

    # HACK: Override TextBuffer saveAs function
    previewEditor.getBuffer().saveAs = ->

    # HACK: Override getURI and getTitle
    previewEditor.getTitle = -> "Compiled #{sourceEditor?.getTitle() or ''}".trim()
    previewEditor.getURI   = -> "coffeecompile://editor/#{sourceEditor.id}"

    # Should never prompt to save on preview editor
    previewEditor.shouldPromptToSave = -> false

    return previewEditor
{'fraction_non_alphanumeric': 0.08896234550158091, 'fraction_numerical': 0.0, 'mean_word_length': 3.300988875154512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '179206', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1885, 'n_words': 502}