diff --git a/spec/app/autocomplete-spec.coffee b/spec/app/autocomplete-spec.coffee index 5054d0de0..3cc735d5b 100644 --- a/spec/app/autocomplete-spec.coffee +++ b/spec/app/autocomplete-spec.coffee @@ -17,20 +17,15 @@ describe "Autocomplete", -> afterEach -> autocomplete.remove() - describe 'autocomplete:toggle event', -> + describe 'autocomplete:show event', -> it "shows autocomplete view and focuses its mini-editor", -> expect($(document).find('#autocomplete')).not.toExist() - editor.trigger "autocomplete:toggle" + editor.trigger "autocomplete:attach" expect($(document).find('#autocomplete')).toExist() expect(autocomplete.editor.isFocused).toBeFalsy() expect(autocomplete.miniEditor.isFocused).toBeTruthy() - miniEditor.trigger "autocomplete:toggle" - expect($(document).find('#autocomplete')).not.toExist() - expect(autocomplete.editor.isFocused).toBeTruthy() - expect(autocomplete.miniEditor.isFocused).toBeFalsy() - describe "when no text is selected", -> it 'autocompletes word when there is only a prefix', -> editor.buffer.insert([10,0] ,"extra:s:extra") @@ -186,7 +181,19 @@ describe "Autocomplete", -> expect(autocomplete.find('li:eq(0)')).toHaveClass('selected') expect(autocomplete.find('li:eq(1)')).not.toHaveClass('selected') - describe "when the mini-editor receives text input", -> + describe "when the mini-editor's buffer changes", -> + describe "when text is removed from the mini-editor", -> + it "reloads the match list based on the mini-editor's text", -> + editor.buffer.insert([10,0] ,"t") + editor.setCursorBufferPosition([10,0]) + autocomplete.attach() + + expect(autocomplete.matchesList.find('li').length).toBe 8 + miniEditor.textInput('c') + expect(autocomplete.matchesList.find('li').length).toBe 3 + miniEditor.backspace() + expect(autocomplete.matchesList.find('li').length).toBe 8 + describe "when the text contains only word characters", -> it "narrows the list of completions with the fuzzy match algorithm", -> editor.buffer.insert([10,0] ,"t") diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 6b30ac670..d00eb58ec 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -51,6 +51,20 @@ describe "Editor", -> expect(newEditor.editSessions[0]).toEqual(editor.editSessions[0]) expect(newEditor.editSessions[0]).not.toBe(editor.editSessions[0]) + describe "editor-open event", -> + it 'only triggers an editor-open event when it is first added to the DOM', -> + openHandler = jasmine.createSpy('openHandler') + editor.on 'editor-open', openHandler + + editor.simulateDomAttachment() + expect(openHandler).toHaveBeenCalled() + [event, eventEditor] = openHandler.argsForCall[0] + expect(eventEditor).toBe editor + + openHandler.reset() + editor.simulateDomAttachment() + expect(openHandler).not.toHaveBeenCalled() + describe "text rendering", -> it "creates a line element for each line in the buffer with the html-escaped text of the line", -> expect(editor.lines.find('.line').length).toEqual(buffer.numLines()) diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 3038816b1..1e126047d 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -131,3 +131,5 @@ $.fn.textInput = (data) -> event = jQuery.event.fix(event) $(this).trigger(event) +$.fn.simulateDomAttachment = -> + $('').append(this) \ No newline at end of file diff --git a/src/app/autocomplete.coffee b/src/app/autocomplete.coffee index 7231b7de1..80b7429dc 100644 --- a/src/app/autocomplete.coffee +++ b/src/app/autocomplete.coffee @@ -33,13 +33,13 @@ class Autocomplete extends View @editor.on 'buffer-path-change', => @setCurrentBuffer(@editor.buffer) @editor.on 'before-remove', => @currentBuffer?.off '.autocomplete' - @editor.on 'autocomplete:toggle', => @attach() - @on 'autocomplete:toggle', => @detach() + @editor.on 'autocomplete:attach', => @attach() @on 'autocomplete:confirm', => @confirm() @on 'autocomplete:cancel', => @cancel() - @miniEditor.buffer.on 'change', => - @filterMatchList() if @parent()[0] + @miniEditor.buffer.on 'change', (e) => + return unless @parent()[0] + @filterMatchList() @miniEditor.preempt 'move-up', => @selectPreviousMatch() @@ -122,13 +122,14 @@ class Autocomplete extends View if (prefix.length + suffix.length) > 0 currentWord = prefix + @editor.getSelectedText() + suffix - @matches = (match for match in @wordMatches(prefix, suffix) when match.word != currentWord) + @baseMatches = (match for match in @wordMatches(prefix, suffix) when match.word != currentWord) else - @matches = [] - @renderMatchList() + @baseMatches = [] + + @filterMatchList() filterMatchList: -> - @matches = fuzzyFilter(@matches, @miniEditor.buffer.getText(), key: 'word') + @matches = fuzzyFilter(@baseMatches, @miniEditor.getText(), key: 'word') @renderMatchList() renderMatchList: -> diff --git a/src/app/editor.coffee b/src/app/editor.coffee index a793275f7..97af57427 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -201,11 +201,14 @@ class Editor extends View else @gutter.addClass('drop-shadow') - @on 'attach', => + @on 'attach', (e) => + return if @attached + @attached = true @calculateDimensions() @hiddenInput.width(@charWidth) @setMaxLineLength() if @softWrap @focus() if @isFocused + @trigger 'editor-open', [this] rootView: -> @parents('#root-view').view() diff --git a/src/app/keymaps/autocomplete.coffee b/src/app/keymaps/autocomplete.coffee index 329924173..6ff9cc724 100644 --- a/src/app/keymaps/autocomplete.coffee +++ b/src/app/keymaps/autocomplete.coffee @@ -1,5 +1,5 @@ window.keymap.bindKeys '.editor', - 'escape': 'autocomplete:toggle' + 'escape': 'autocomplete:attach' window.keymap.bindKeys '#autocomplete .editor', 'enter': 'autocomplete:confirm' diff --git a/src/app/window.coffee b/src/app/window.coffee index 44afa68f1..d7295d15d 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -21,8 +21,8 @@ windowAdditions = $(document).on 'keydown', @_handleKeyEvent startup: (path) -> - @attachRootView(path) @loadUserConfiguration() + @attachRootView(path) $(window).on 'close', => @close() $(window).on 'beforeunload', => @shutdown() @@ -53,7 +53,7 @@ windowAdditions = try require atom.userConfigurationPath if fs.exists(atom.userConfigurationPath) catch error - console.error "Failed to load `#{atom.userConfigurationPath}`", error + console.error "Failed to load `#{atom.userConfigurationPath}`", error.message, error @showConsole() requireStylesheet: (path) ->