Tab stops are associated with anchors so we can jump to them event when the buffer changes

This commit is contained in:
Nathan Sobo
2012-06-22 12:07:20 -06:00
parent ae2b686802
commit 5c6e94ec74
4 changed files with 23 additions and 7 deletions

View File

@@ -47,8 +47,13 @@ describe "Snippets extension", ->
expect(buffer.lineForRow(1)).toBe "go here first:()"
expect(buffer.lineForRow(2)).toBe "var quicksort = function () {"
expect(editor.getCursorScreenPosition()).toEqual [1, 15]
editor.trigger keydownEvent('tab', target: editor[0])
expect(editor.getCursorScreenPosition()).toEqual [0, 14]
editor.insertText 'abc'
editor.trigger keydownEvent('tab', target: editor[0])
expect(editor.getCursorScreenPosition()).toEqual [0, 40]
describe "when the letters preceding the cursor don't match a snippet", ->
it "inserts a tab as normal", ->
@@ -108,7 +113,7 @@ describe "Snippets extension", ->
snippet = snippets['t1']
expect(snippet.body).toBe """
go here next:() and finally go here:()
go here first:()\n
go here first:()
"""
expect(snippet.tabStops).toEqual [[1, 15], [0, 14], [0, 37]]

View File

@@ -239,6 +239,11 @@ class EditSession
@anchors.push(anchor)
anchor
addAnchorAtBufferPosition: (bufferPosition) ->
anchor = @addAnchor()
anchor.setBufferPosition(bufferPosition)
anchor
removeAnchor: (anchor) ->
_.remove(@anchors, anchor)

View File

@@ -11,18 +11,19 @@ class Snippet
bodyText = []
[row, column] = [0, 0]
for bodyLine in bodyLines
for bodyLine, i in bodyLines
lineText = []
for segment in bodyLine
if _.isNumber(segment)
tabStopsByIndex[segment] = new Point(row, column)
else
bodyText.push(segment)
lineText.push(segment)
column += segment.length
bodyText.push('\n')
bodyText.push(lineText.join(''))
row++; column = 0
@tabStops = []
for index in _.keys(tabStopsByIndex).sort()
@tabStops.push tabStopsByIndex[index]
bodyText.join('')
bodyText.join('\n')

View File

@@ -34,6 +34,7 @@ module.exports =
class SnippetsSession
tabStopAnchors: null
constructor: (@editSession, @snippetsByExtension) ->
expandSnippet: ->
@@ -43,15 +44,19 @@ class SnippetsSession
@editSession.selectToBeginningOfWord()
@activeSnippetStartPosition = @editSession.getCursorBufferPosition()
@editSession.insertText(@activeSnippet.body)
@placeTabStopAnchors()
@setTabStopIndex(0) if @activeSnippet.tabStops.length
true
else
false
placeTabStopAnchors: ->
@tabStopAnchors = @activeSnippet.tabStops.map (position) =>
@editSession.addAnchorAtBufferPosition(position)
goToNextTabStop: ->
return false unless @activeSnippet
@setTabStopIndex(@tabStopIndex + 1)
setTabStopIndex: (@tabStopIndex) ->
tabStopPosition = @activeSnippet.tabStops[@tabStopIndex].subtract(@activeSnippetStartPosition)
@editSession.setCursorBufferPosition(tabStopPosition)
@editSession.setCursorBufferPosition(@tabStopAnchors[@tabStopIndex].getBufferPosition())