<commit_before>{$$} = require 'space-pen'
Range = require 'range'
SelectList = require 'select-list'

module.exports =
class AutocompleteView extends SelectList
  @activate: (rootView) ->
    rootView.eachEditor (editor) ->
      new AutocompleteView(editor) if editor.attached and not editor.mini

  @viewClass: -> "autocomplete #{super} popover-list"

  editor: null
  currentBuffer: null
  wordList: null
  wordRegex: /\w+/g
  originalSelectionBufferRange: null
  originalCursorPosition: null
  aboveCursor: false
  filterKey: 'word'

  initialize: (@editor) ->
    super

    @handleEvents()
    @setCurrentBuffer(@editor.getBuffer())

  itemForElement: (match) ->
    $$ ->
      @li match.word

  handleEvents: ->
    @editor.on 'editor:path-changed', => @setCurrentBuffer(@editor.getBuffer())
    @editor.command 'autocomplete:attach', => @attach()

    @miniEditor.preempt 'textInput', (e) =>
      text = e.originalEvent.data
      unless text.match(@wordRegex)
        @confirmSelection()
        @editor.insertText(text)
        false

  setCurrentBuffer: (@currentBuffer) ->

  selectItem: (item) ->
    super

    match = @getSelectedElement()
    @replaceSelectedTextWithMatch(match) if match

  selectNextItem: ->
    super

    false

  selectPreviousItem: ->
    super

    false

  buildWordList: () ->
    wordHash = {}
    matches = @currentBuffer.getText().match(@wordRegex)
    wordHash[word] ?= true for word in (matches or [])

    @wordList = Object.keys(wordHash)

  confirmed: (match) ->
    @editor.getSelection().clear()
    @cancel()
    return unless match
    @replaceSelectedTextWithMatch match
    position = @editor.getCursorBufferPosition()
    @editor.setCursorBufferPosition([position.row, position.column + match.suffix.length])

  cancelled: ->
    super

    @editor.abort()
    @editor.setSelectedBufferRange(@originalSelectionBufferRange)
    @editor.rootView()?.focus() if @miniEditor.isFocused

  attach: ->
    @editor.transact()

    @aboveCursor = false
    @originalSelectionBufferRange = @editor.getSelection().getBufferRange()
    @originalCursorPosition = @editor.getCursorScreenPosition()

    @buildWordList()
    matches = @findMatchesForCurrentSelection()
    @setArray(matches)

    if matches.length is 1
      @confirmSelection()
    else
      @editor.appendToLinesView(this)
      @setPosition()
      @miniEditor.focus()

  detach: ->
    super

    @editor.off(".autocomplete")
    @editor.focus()

  setPosition: ->
    { left, top } = @editor.pixelPositionForScreenPosition(@originalCursorPosition)
<commit_msg>fix autocomplete and specs<commit_after>{$$} = require 'space-pen'
Range = require 'range'
SelectList = require 'select-list'

module.exports =
class AutocompleteView extends SelectList
  @activate: (rootView) ->
    rootView.eachEditor (editor) ->
      new AutocompleteView(editor) if editor.attached and not editor.mini

  @viewClass: -> "autocomplete #{super} popover-list"

  editor: null
  currentBuffer: null
  wordList: null
  wordRegex: /\w+/g
  originalSelectionBufferRange: null
  originalCursorPosition: null
  aboveCursor: false
  filterKey: 'word'

  initialize: (@editor) ->
    super

    @handleEvents()
    @setCurrentBuffer(@editor.getBuffer())

  itemForElement: (match) ->
    $$ ->
      @li match.word

  handleEvents: ->
    @editor.on 'editor:path-changed', => @setCurrentBuffer(@editor.getBuffer())
    @editor.command 'autocomplete:attach', => @attach()

    @miniEditor.preempt 'textInput', (e) =>
      text = e.originalEvent.data
      unless text.match(@wordRegex)
        @confirmSelection()
        @editor.insertText(text)
        false

  setCurrentBuffer: (@currentBuffer) ->

  selectItem: (item) ->
    super

    match = @getSelectedElement()
    @replaceSelectedTextWithMatch(match) if match

  selectNextItem: ->
    super

    false

  selectPreviousItem: ->
    super

    false

  buildWordList: () ->
    wordHash = {}
    matches = @currentBuffer.getText().match(@wordRegex)
    wordHash[word] ?= true for word in (matches or [])

    @wordList = Object.keys(wordHash)

  confirmed: (match) ->
    @editor.getSelection().clear()
    @cancel()
    return unless match
    @replaceSelectedTextWithMatch match
    position = @editor.getCursorBufferPosition()
    @editor.setCursorBufferPosition([position.row, position.column + match.suffix.length])

  cancelled: ->
    super

    @editor.abort()
    @editor.setSelectedBufferRange(@originalSelectionBufferRange)
    @editor.rootView()?.focus() if @miniEditor.isFocused

  attach: ->
    @editor.transact()

    @aboveCursor = false
    @originalSelectionBufferRange = @editor.getSelection().getBufferRange()
    @originalCursorPosition = @editor.getCursorScreenPosition()

    @buildWordList()
    matches = @findMatchesForCurrentSelection()
    @setArray(matches)

    if matches.length is 1
      @confirmSelection()
    else
      @editor.appendToLinesView(this)
      @setPosition()
      @miniEditor.focus()

  detach: ->
    super

    @editor.off(".autocomplete")
    @editor.focus()

  setPosition: ->
    { left, top } = @editor.pixelPositionForScreenPosition(@originalCursorPosition)
{'fraction_non_alphanumeric': 0.12509682416731216, 'fraction_numerical': 0.0003872966692486445, 'mean_word_length': 3.510917030567686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, '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': '5082066', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1493, 'n_words': 303}