<commit_before>$ = require 'jquery'
AutocompleteView = require 'autocomplete/lib/autocomplete-view'
Autocomplete = require 'autocomplete/lib/autocomplete'
Buffer = require 'buffer'
Editor = require 'editor'
RootView = require 'root-view'

describe "Autocomplete", ->
  beforeEach ->
    window.rootView = new RootView
    rootView.open('sample.js')
    rootView.simulateDomAttachment()

  describe "@activate()", ->
    it "activates autocomplete on all existing and future editors (but not on autocomplete's own mini editor)", ->
      spyOn(AutocompleteView.prototype, 'initialize').andCallThrough()
      autocompletePackage = window.loadPackage("autocomplete")
      expect(AutocompleteView.prototype.initialize).not.toHaveBeenCalled()

      leftEditor = rootView.getActiveView()
      rightEditor = leftEditor.splitRight()

      leftEditor.trigger 'autocomplete:attach'
      expect(leftEditor.find('.autocomplete')).toExist()
      expect(rightEditor.find('.autocomplete')).not.toExist()

      expect(AutocompleteView.prototype.initialize).toHaveBeenCalled()

      autoCompleteView = leftEditor.find('.autocomplete').view()
      autoCompleteView.trigger 'core:cancel'
      expect(leftEditor.find('.autocomplete')).not.toExist()

      rightEditor.trigger 'autocomplete:attach'
      expect(rightEditor.find('.autocomplete')).toExist()

describe "AutocompleteView", ->
  autocomplete = null
  editor = null
  miniEditor = null

  beforeEach ->
    window.rootView = new RootView
    editor = new Editor(editSession: fixturesProject.buildEditSession('sample.js'))
    window.loadPackage('autocomplete')
    autocomplete = new AutocompleteView(editor)
    miniEditor = autocomplete.miniEditor

  describe 'autocomplete:attach event', ->
    it "shows autocomplete view and focuses its mini-editor", ->
      expect(editor.find('.autocomplete')).not.toExist()

      editor.trigger "autocomplete:attach"
      expect(editor.find('.autocomplete')).toExist()
      expect(autocomplete.editor.isFocused).toBeFalsy()
      expect(autocomplete.miniEditor.isFocused).toBeTruthy()

    describe "when no text is selected", ->
      it 'autocompletes word when there is only a prefix', ->
        editor.getBuffer().insert([10,0] ,"extra:s:extra")
        editor.setCursorBufferPosition([10,7])
        autocomplete.attach()

        expect(editor.lineForBufferRow(10)).toBe "extra:sort:extra"
        expect(editor.getCursorBufferPosition()).toEqual [10,10]
        expect(editor.getSelection().getBufferRange()).toEqual [[10,7], [10,10]]

        expect(autocomplete.list.find('li').length).toBe 2
        expect(autocomplete.list.find('li:eq(0)')).toHaveText('sort')
        expect(autocomplete.list.find('li:eq(1)')).toHaveText('shift')
<commit_msg>Eliminate fixturesProject global. Use project global instead.<commit_after>$ = require 'jquery'
AutocompleteView = require 'autocomplete/lib/autocomplete-view'
Autocomplete = require 'autocomplete/lib/autocomplete'
Buffer = require 'buffer'
Editor = require 'editor'
RootView = require 'root-view'

describe "Autocomplete", ->
  beforeEach ->
    window.rootView = new RootView
    rootView.open('sample.js')
    rootView.simulateDomAttachment()

  describe "@activate()", ->
    it "activates autocomplete on all existing and future editors (but not on autocomplete's own mini editor)", ->
      spyOn(AutocompleteView.prototype, 'initialize').andCallThrough()
      autocompletePackage = window.loadPackage("autocomplete")
      expect(AutocompleteView.prototype.initialize).not.toHaveBeenCalled()

      leftEditor = rootView.getActiveView()
      rightEditor = leftEditor.splitRight()

      leftEditor.trigger 'autocomplete:attach'
      expect(leftEditor.find('.autocomplete')).toExist()
      expect(rightEditor.find('.autocomplete')).not.toExist()

      expect(AutocompleteView.prototype.initialize).toHaveBeenCalled()

      autoCompleteView = leftEditor.find('.autocomplete').view()
      autoCompleteView.trigger 'core:cancel'
      expect(leftEditor.find('.autocomplete')).not.toExist()

      rightEditor.trigger 'autocomplete:attach'
      expect(rightEditor.find('.autocomplete')).toExist()

describe "AutocompleteView", ->
  autocomplete = null
  editor = null
  miniEditor = null

  beforeEach ->
    window.rootView = new RootView
    editor = new Editor(editSession: project.buildEditSession('sample.js'))
    window.loadPackage('autocomplete')
    autocomplete = new AutocompleteView(editor)
    miniEditor = autocomplete.miniEditor

  describe 'autocomplete:attach event', ->
    it "shows autocomplete view and focuses its mini-editor", ->
      expect(editor.find('.autocomplete')).not.toExist()

      editor.trigger "autocomplete:attach"
      expect(editor.find('.autocomplete')).toExist()
      expect(autocomplete.editor.isFocused).toBeFalsy()
      expect(autocomplete.miniEditor.isFocused).toBeTruthy()

    describe "when no text is selected", ->
      it 'autocompletes word when there is only a prefix', ->
        editor.getBuffer().insert([10,0] ,"extra:s:extra")
        editor.setCursorBufferPosition([10,7])
        autocomplete.attach()

        expect(editor.lineForBufferRow(10)).toBe "extra:sort:extra"
        expect(editor.getCursorBufferPosition()).toEqual [10,10]
        expect(editor.getSelection().getBufferRange()).toEqual [[10,7], [10,10]]

        expect(autocomplete.list.find('li').length).toBe 2
        expect(autocomplete.list.find('li:eq(0)')).toHaveText('sort')
        expect(autocomplete.list.find('li:eq(1)')).toHaveText('shift')

{'fraction_non_alphanumeric': 0.1319870176703931, 'fraction_numerical': 0.007933645870897944, 'mean_word_length': 4.28789323164919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6318709', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1547, 'n_words': 276}