Update autocomplete position when populating list

Previously if the list was displayed above the cursor
position and the list was filtered to decrease in size
it would be displayed several rows above the cursor as
it shrinks.

Now the position is updated each time the list is populated
so it will always use the latest height when calculating
the position in the editor to place the select list at.
This commit is contained in:
Kevin Sawicki 2013-01-02 11:35:53 -08:00
parent 6d50abb6ce
commit 8a10c48ad2
2 changed files with 35 additions and 7 deletions

View File

@ -364,6 +364,26 @@ describe "Autocomplete", ->
expect(autocompleteBottom).toBe cursorPixelPosition.top
expect(autocomplete.position().left).toBe cursorPixelPosition.left
it "updates the position when the list is filtered and the height of the list decreases", ->
editor.setCursorScreenPosition([11, 0])
editor.insertText('s')
editor.setCursorScreenPosition([11, 0])
cursorPixelPosition = editor.pixelPositionForScreenPosition(editor.getCursorScreenPosition())
autocomplete.attach()
expect(autocomplete.parent()).toExist()
autocompleteBottom = autocomplete.position().top + autocomplete.outerHeight()
expect(autocompleteBottom).toBe cursorPixelPosition.top
expect(autocomplete.position().left).toBe cursorPixelPosition.left
miniEditor.textInput('sh')
window.advanceClock(autocomplete.inputThrottle)
expect(autocomplete.parent()).toExist()
autocompleteBottom = autocomplete.position().top + autocomplete.outerHeight()
expect(autocompleteBottom).toBe cursorPixelPosition.top
expect(autocomplete.position().left).toBe cursorPixelPosition.left
describe ".cancel()", ->
it "clears the mini-editor and unbinds autocomplete event handlers for move-up and move-down", ->
autocomplete.attach()

View File

@ -16,6 +16,8 @@ class Autocomplete extends SelectList
wordList: null
wordRegex: /\w+/g
originalSelectionBufferRange: null
originalCursorPosition: null
aboveCursor: false
undoCount: 0
filterKey: 'word'
@ -82,8 +84,9 @@ class Autocomplete extends SelectList
attach: ->
@undoCount = 0
@aboveCursor = false
@originalSelectionBufferRange = @editor.getSelection().getBufferRange()
originalCursorPosition = @editor.getCursorScreenPosition()
@originalCursorPosition = @editor.getCursorScreenPosition()
@buildWordList()
matches = @findMatchesForCurrentSelection()
@ -93,7 +96,7 @@ class Autocomplete extends SelectList
@confirmSelection()
else
@editor.appendToLinesView(this)
@setPosition(originalCursorPosition)
@setPosition()
@miniEditor.focus()
detach: ->
@ -102,19 +105,19 @@ class Autocomplete extends SelectList
@editor.off(".autocomplete")
@editor.focus()
setPosition: (originalCursorPosition) ->
{ left, top } = @editor.pixelPositionForScreenPosition(originalCursorPosition)
setPosition: ->
{ left, top } = @editor.pixelPositionForScreenPosition(@originalCursorPosition)
height = @outerHeight()
potentialTop = top + @editor.lineHeight
potentialBottom = potentialTop - @editor.scrollTop() + height
potentialBottom = potentialTop - @editor.scrollTop() + height
if potentialBottom > @editor.outerHeight()
if @aboveCursor or potentialBottom > @editor.outerHeight()
@aboveCursor = true
@css(left: left, top: top - height, bottom: 'inherit')
else
@css(left: left, top: potentialTop, bottom: 'inherit')
findMatchesForCurrentSelection: ->
selection = @editor.getSelection()
{prefix, suffix} = @prefixAndSuffixOfSelection(selection)
@ -157,3 +160,8 @@ class Autocomplete extends SelectList
suffix = match[0][suffixOffset..] if range.end.isGreaterThan(selectionRange.end)
{prefix, suffix}
populateList: ->
super()
@setPosition()