Parse nested snippet placeholders

This commit is contained in:
Nathan Sobo
2013-01-07 16:49:48 -07:00
parent 60c89f8b32
commit 62d7273069
2 changed files with 25 additions and 13 deletions

View File

@@ -1,10 +1,15 @@
body = content:(tabStop / bodyText)* { return content; }
bodyText = text:bodyChar+ { return text.join(''); }
bodyChar = !tabStop char:. { return char; }
bodyContent = content:(tabStop / bodyContentText)* { return content; }
bodyContentText = text:bodyContentChar+ { return text.join(''); }
bodyContentChar = !tabStop char:. { return char; }
placeholderContent = content:(tabStop / placeholderContentText)* { return content; }
placeholderContentText = text:placeholderContentChar+ { return text.join(''); }
placeholderContentChar = !tabStop char:[^}] { return char; }
tabStop = simpleTabStop / tabStopWithPlaceholder
simpleTabStop = '$' index:[0-9]+ {
return { index: parseInt(index), content: [] };
}
tabStopWithPlaceholder = '${' index:[0-9]+ ':' content:[^}]* '}' {
return { index: parseInt(index), content: [content.join('')] };
tabStopWithPlaceholder = '${' index:[0-9]+ ':' content:placeholderContent '}' {
return { index: parseInt(index), content: content };
}

View File

@@ -216,16 +216,23 @@ describe "Snippets extension", ->
describe "Snippets parser", ->
it "breaks a snippet body into lines, with each line containing tab stops at the appropriate position", ->
bodyTree = Snippets.parser.parse """
go here next:($2) and finally go here:(${3:here!})
go here first:($1)
the quick brown $1fox ${2:jumped ${3:over}
}the ${4:lazy} dog
"""
expect(bodyTree).toEqual [
"go here next:(",
{ index: 2, content: [] },
") and finally go here:(",
{ index: 3, content: ["here!"] },
")\ngo here first:(",
"the quick brown ",
{ index: 1, content: [] },
")"
"fox ",
{
index: 2,
content: [
"jumped ",
{ index: 3, content: ["over"]},
"\n"
],
}
"the "
{ index: 4, content: ["lazy"] },
" dog"
]