Parse tab-stop positions correctly when there are multiple on a line

This commit is contained in:
Nathan Sobo
2012-06-22 11:44:58 -06:00
parent 1710ecc8a9
commit ae2b686802
3 changed files with 27 additions and 17 deletions

View File

@@ -99,15 +99,16 @@ describe "Snippets extension", ->
it "can parse snippets with tabstops", ->
snippets = Snippets.snippetsParser.parse """
# this line intentially left blank.
snippet t1 "Test snippet 1"
then go back to here: ($2)
first go here: ($1)
snippet t1 "Snippet with tab stops"
go here next:($2) and finally go here:($3)
go here first:($1)
endsnippet
"""
snippet = snippets['t1']
expect(snippet.body).toBe """
then go back to here: ()
first go here: ()
go here next:() and finally go here:()
go here first:()\n
"""
expect(snippet.tabStops).toEqual [[1, 16], [0, 23]]
expect(snippet.tabStops).toEqual [[1, 15], [0, 14], [0, 37]]

View File

@@ -1,18 +1,25 @@
_ = require 'underscore'
Point = require 'point'
module.exports =
class Snippet
constructor: ({@bodyPosition, @prefix, @description, body}) ->
@body = @extractTabStops(body)
extractTabStops: (body) ->
extractTabStops: (bodyLines) ->
tabStopsByIndex = {}
bodyText = []
for element in body
if element.index
tabStopsByIndex[element.index] = element.position.subtract(@bodyPosition)
else
bodyText.push(element)
[row, column] = [0, 0]
for bodyLine in bodyLines
for segment in bodyLine
if _.isNumber(segment)
tabStopsByIndex[segment] = new Point(row, column)
else
bodyText.push(segment)
column += segment.length
bodyText.push('\n')
row++; column = 0
@tabStops = []
for index in _.keys(tabStopsByIndex).sort()

View File

@@ -19,13 +19,15 @@ start = 'snippet'
prefix = prefix:[A-Za-z0-9_]+ { return prefix.join(''); }
string = ['] body:[^']* ['] { return body.join(''); }
/ ["] body:[^"]* ["] { return body.join(''); }
beforeBody = [ ]* '\n' { return new Point(line, 0); } // return start position of body: body begins on next line, so don't subtract 1 from line
body = (tabStop / bodyText)*
bodyText = body:bodyCharacter+ { return body.join(''); }
bodyCharacter = !(end / tabStop) char:. { return char; }
tabStop = '$' index:[0-9]+ { return { index: index, position: new Point(line - 1, column - 1) }; }
body = bodyLine+
bodyLine = content:(tabStop / bodyText)* '\n' { return content; }
bodyText = text:bodyChar+ { return text.join(''); }
bodyChar = !(end / tabStop) char:[^\n] { return char; }
tabStop = '$' index:[0-9]+ { return parseInt(index); }
end = '\nendsnippet'
end = 'endsnippet'
ws = ([ \n] / comment)+
comment = '#' [^\n]*