'use babel'

export default {
  Hack: null,
  activate: function() {
    require('atom-package-deps').install()
  },
  consumeHack: function(Hack) {
    this.Hack = Hack
  },
  provideAutoComplete: function() {
    const Path = require('path')
    const Score = require('sb-string_score')
    window.tmp = Score
    const Regex = {
      isClass: / class$/,
      label: /(\w+)/,
      prefix: /((:\w[\w:-]+)|(\$\w+)|::(\w+)|(\w+))$/
    }
    const Provider = {
      inclusionPriority: 100,
      excludeLowerPriority: true,
      selector: '.source.cpp, .source.hack, .source.php',
      disableForSelector: '.comment',
      parseSuggestion: (suggestion, prefix) => {
        const spaceIndex = suggestion.indexOf(' ')
        suggestion = [suggestion.slice(0, spaceIndex), suggestion.slice(spaceIndex + 1)]

        let type = 'property'
        let label = ''
        let snippet = null
        let text = null

        if (suggestion[1] === 'class') {
          type = 'class'
          label = 'class'
        } else {
          type = suggestion[0].substr(0, 1) === '$' ? 'variable' : 'property'
          label = Regex.label.exec(suggestion[1])[0] || ''
        }

        if (label === 'function') {
          type = 'function'
        }
        if (type === 'function') {
          const regex = /(\$\w+)/g
          let match
          let number = 0

          snippet = []
          while ((match = regex.exec(suggestion[1])) !== null) {
            ++ number
            snippet.push(`\${${number}:${match[0]}}`)
          }
          snippet = suggestion[0] + '(' + snippet.join(', ') + ')${' + (++number) + '}'
        } else {
          text = suggestion[0]
        }

        return {
          type,
          leftLabel: label,
          description: suggestion[1],
          text, snippet,
          replacementPrefix: prefix,
          score: Score(suggestion[0], prefix)
        }
      },
      getSuggestions: ({editor, bufferPosition}) => {
        if (this.Hack === null || !this.Hack.connected) {
          return []
        }

        const textBuffer = editor.getBuffer()
        const text = editor.getText()
        const index = textBuffer.characterIndexForPosition(bufferPosition)

        return this.Hack.exec(atom.config.get('Atom-Hack.hackExecutablePath'), ['--auto-complete'], {
          stdin: text.substr(0, index) + 'AUTO332' + text.substr(index),
          cwd: Path.dirname(editor.getPath())
        }).then(function(contents) {
          const toReturn = []
          let prefix = (Regex.prefix.exec(editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])) || [])
          prefix = prefix[2] || prefix[3] || prefix[4] || prefix[5] || ''
          contents.substr(0, contents.length - 1).split("\n").forEach(entry => {
            if (entry.length) {
              toReturn.push(Provider.parseSuggestion(entry, prefix))
            }
          })
          return toReturn.sort(function(a, b) {
            return b.score - a.score
          })
        }).catch(err => {
          atom.notifications.addError(err.message, {dismissible: true})
          return []
        })
      }
    }
    return Provider
  }
}

@@ -640,17 +640,22 @@
 stion: (
-s
+givenS
 uggestio
@@ -697,17 +697,22 @@
 Index = 
-s
+givenS
 uggestio
@@ -734,16 +734,22 @@
         
+const 
 suggesti
@@ -754,17 +754,22 @@
 tion = %5B
-s
+givenS
 uggestio
@@ -792,17 +792,22 @@
 Index), 
-s
+givenS
 uggestio
@@ -829,16 +829,23 @@
 dex + 1)
+ %7C%7C '_'
 %5D%0A%0A     

Add support for latest HH