<commit_before>{CompositeDisposable} = require 'atom'
marked = require('marked');
fs = require('fs')

#TODO: Retrieve from the environment or from the server process
ELIXIR_VERSION = '1.1'

module.exports =
class ElixirAutocompleteProvider
  selector: ".source.elixir"
  disableForSelector: '.source.elixir .comment'
  server: null
  inclusionPriority: 1
  excludeLowerPriority: true

  constructor: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-text-editor', 'atom-elixir:autocomplete-tab', ->
      editor = atom.workspace.getActiveTextEditor()
      atom.commands.dispatch(atom.views.getView(editor), 'autocomplete-plus:cancel')
      atom.commands.dispatch(atom.views.getView(editor), 'snippets:next-tab-stop')
    @subscriptions.add(atom.config.observe('autocomplete-plus.minimumWordLength', (@minimumWordLength) => ))

  dispose: ->
    @subscriptions.dispose()

  setServer: (server) ->
    @server = server

    # This is a hack until descriptionHTML is suported. See:
    # https://github.com/atom/autocomplete-plus/issues/423
    replaceUpdateDescription()

  getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix, activatedManually}) ->
    prefix = getPrefix(editor, bufferPosition)

    return if !activatedManually && prefix == ""

    #TODO: maybe we should have our own configuration for that
    # return unless prefix?.length >= @minimumWordLength

    new Promise (resolve) =>
      editor   = atom.workspace.getActiveTextEditor()
      line     = editor.getCursorBufferPosition().row + 1
      tmpFile  = @createTempFile(editor.buffer.getText())

      @server.getCodeCompleteSuggestions prefix, tmpFile, line, (result) ->
        fs.unlink(tmpFile)
        suggestions = result.split('\n')

        hint = suggestions[0]
        suggestions = suggestions[1...]
        module_prefix = ''
        modules_to_add = []

        is_prefix_a_function_call = !!(prefix.match(/\.[^A-Z][^\.]*$/) || prefix.match(/^[^A-Z:][^\.]*$/))

        if prefix != '' && !is_prefix_a_function_call
          prefix_modules = prefix.split('.')[...-1]
          hint_modules   = hint.split('.')[...-1]<commit_msg>Autocomple have to be manually activated when inside strings or comments<commit_after>{CompositeDisposable} = require 'atom'
marked = require('marked');
fs = require('fs')

#TODO: Retrieve from the environment or from the server process
ELIXIR_VERSION = '1.1'

module.exports =
class ElixirAutocompleteProvider
  selector: ".source.elixir"
  # disableForSelector: '.source.elixir .comment'
  server: null
  inclusionPriority: 1
  excludeLowerPriority: true

  constructor: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-text-editor', 'atom-elixir:autocomplete-tab', ->
      editor = atom.workspace.getActiveTextEditor()
      atom.commands.dispatch(atom.views.getView(editor), 'autocomplete-plus:cancel')
      atom.commands.dispatch(atom.views.getView(editor), 'snippets:next-tab-stop')
    @subscriptions.add(atom.config.observe('autocomplete-plus.minimumWordLength', (@minimumWordLength) => ))

  dispose: ->
    @subscriptions.dispose()

  setServer: (server) ->
    @server = server

    # This is a hack until descriptionHTML is suported. See:
    # https://github.com/atom/autocomplete-plus/issues/423
    replaceUpdateDescription()

  getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix, activatedManually}) ->
    scopeChain = scopeDescriptor.getScopeChain()
    editorElement = atom.views.getView(editor)
    if scopeChain.match(/\.string\.quoted\./) || scopeChain.match(/\.comment/)
      unless (activatedManually || 'autocomplete-active' in editorElement.classList)
        return

    prefix = getPrefix(editor, bufferPosition)

    return if !activatedManually && prefix == ""

    #TODO: maybe we should have our own configuration for that
    # return unless prefix?.length >= @minimumWordLength

    new Promise (resolve) =>
      editor   = atom.workspace.getActiveTextEditor()
      line     = editor.getCursorBufferPosition().row + 1
      tmpFile  = @createTempFile(editor.buffer.getText())

      @server.getCodeCompleteSuggestions prefix, tmpFile, line, (result) ->
        fs.unlink(tmpFile)
        suggestions = result.split('\n')

        hint = suggestions[0]
        suggestions = suggestions[1...]
        module_prefix = ''
        modules_to_add = []

        is_prefix_a_function_call = !!(prefix.match(/\.[^A-Z][^\.]*$/) || prefix.match(/^[^A-Z:][^\.]*$/))

        if prefix != '' && !is_prefix_a_function_call
          prefix_modules = prefix.split('.')[...-1]
          hint_modules   = hint.split('.')[...-1]
{'fraction_non_alphanumeric': 0.13831376734258272, 'fraction_numerical': 0.004695837780149413, 'mean_word_length': 3.5628042843232715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 2, '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': '4318534', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1361, 'n_words': 276}