<commit_before>{Function} = require 'loophole'

module.exports =
class Provider

  manager: null
  force: false
  # automcomplete-plus
  selector: '.source.js'
  disableForSelector: '.source.js .comment'
  inclusionPriority: 1
  excludeLowerPriority: true

  init: (manager) ->
    @manager = manager

  isValidPrefix: (prefix) ->
    return true if prefix[prefix.length - 1] is '\.'
    return false if prefix[prefix.length - 1]?.match(/;|\s/)
    if prefix.length > 1
      prefix = '_' + prefix
    try (new Function("var " + prefix))()
    catch e then return false
    return true

  checkPrefix: (prefix) ->
    return '' if prefix.match(/(\s|;|\.|\"|\')$/) or prefix.replace(/\s/g, '').length is 0
    prefix

  getPrefix: (editor, bufferPosition) ->
    regexp = /(([\$\w]+[\w-]*)|([.:;'"[{( ]+))$/g
    line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])
    line.match(regexp)?[0]

  getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix}) ->
    return [] unless @manager.client
    tempPrefix = @getPrefix(editor, bufferPosition) or prefix
    if !@isValidPrefix(tempPrefix) and !@force
      return []
    prefix = @checkPrefix(tempPrefix)

    that = this

    return new Promise (resolve) ->
      that.manager.client.update(editor.getURI(), editor.getText()).then =>
        that.manager.client.completions(editor.getURI(), {line: bufferPosition.row, ch: bufferPosition.column}).then (data) =>
          if !data.completions.length
            resolve([])
            return

          suggestionsArr = []

          for obj, index in data.completions
            obj = that.manager.helper.formatTypeCompletion(obj)

            description = if obj.doc then obj.doc else null
            url = if obj.url then obj.doc else null

            suggestionsArr.push {
              text: obj.name
              replacementPrefix: prefix
              className: null
              type: obj._typeSelf
              leftLabel: obj.leftLabel
              snippet: obj._snippet
              description: description
              descriptionMoreURL: url
            }
          resolve(suggestionsArr)
        , (err) ->
          console.log err

  forceCompletion: ->
    @force = true
    atom.commands.dispatch(atom.views.getView(atom.workspace.getActiveTextEditor()), 'autocomplete-plus:activate');
    @force = false

  addSelector: (selector) ->
    @selector = @selector + ',' + selector

  removeSelector: (selector) ->
    @selector = @selector.replace(',' + selector, '')<commit_msg>set href to url<commit_after>{Function} = require 'loophole'

module.exports =
class Provider

  manager: null
  force: false
  # automcomplete-plus
  selector: '.source.js'
  disableForSelector: '.source.js .comment'
  inclusionPriority: 1
  excludeLowerPriority: true

  init: (manager) ->
    @manager = manager

  isValidPrefix: (prefix) ->
    return true if prefix[prefix.length - 1] is '\.'
    return false if prefix[prefix.length - 1]?.match(/;|\s/)
    if prefix.length > 1
      prefix = '_' + prefix
    try (new Function("var " + prefix))()
    catch e then return false
    return true

  checkPrefix: (prefix) ->
    return '' if prefix.match(/(\s|;|\.|\"|\')$/) or prefix.replace(/\s/g, '').length is 0
    prefix

  getPrefix: (editor, bufferPosition) ->
    regexp = /(([\$\w]+[\w-]*)|([.:;'"[{( ]+))$/g
    line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])
    line.match(regexp)?[0]

  getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix}) ->
    return [] unless @manager.client
    tempPrefix = @getPrefix(editor, bufferPosition) or prefix
    if !@isValidPrefix(tempPrefix) and !@force
      return []
    prefix = @checkPrefix(tempPrefix)

    that = this

    return new Promise (resolve) ->
      that.manager.client.update(editor.getURI(), editor.getText()).then =>
        that.manager.client.completions(editor.getURI(), {line: bufferPosition.row, ch: bufferPosition.column}).then (data) =>
          if !data.completions.length
            resolve([])
            return

          suggestionsArr = []

          for obj, index in data.completions
            obj = that.manager.helper.formatTypeCompletion(obj)

            description = if obj.doc then obj.doc else null
            url = if obj.url then obj.url else null

            suggestionsArr.push {
              text: obj.name
              replacementPrefix: prefix
              className: null
              type: obj._typeSelf
              leftLabel: obj.leftLabel
              snippet: obj._snippet
              description: description
              descriptionMoreURL: url
            }
          resolve(suggestionsArr)
        , (err) ->
          console.log err

  forceCompletion: ->
    @force = true
    atom.commands.dispatch(atom.views.getView(atom.workspace.getActiveTextEditor()), 'autocomplete-plus:activate');
    @force = false

  addSelector: (selector) ->
    @selector = @selector + ',' + selector

  removeSelector: (selector) ->
    @selector = @selector.replace(',' + selector, '')
{'fraction_non_alphanumeric': 0.1351457840819543, 'fraction_numerical': 0.0027580772261623326, 'mean_word_length': 2.472640218878249, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1983957', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1421, 'n_words': 349}