Resolve ${group:/command} style pattern names

These are used by the todo package to include the lower cased
version of the matched capture index in the pattern scope name.
This commit is contained in:
Kevin Sawicki 2013-06-06 12:37:45 -07:00
parent 42d70f888e
commit 7313407d04
2 changed files with 34 additions and 7 deletions

View File

@ -511,6 +511,20 @@ describe "TextMateGrammar", ->
expect(tokens[0].value).toBe "forever and ever"
expect(tokens[0].scopes).toEqual ["source", "text"]
describe "${capture:/command} style pattern names", ->
lines = null
beforeEach ->
atom.activatePackage('ruby-tmbundle', sync: true)
atom.activatePackage('todo-tmbundle', sync: true)
grammar = syntax.selectGrammar('main.rb')
lines = grammar.tokenizeLines "# TODO be nicer"
it "replaces the number with the capture group and translates the text", ->
tokens = lines[0]
expect(tokens[2].value).toEqual "TODO"
expect(tokens[2].scopes).toEqual ["source.ruby", "comment.line.number-sign.ruby", "storage.type.class.todo"]
describe "language-specific integration tests", ->
lines = null

View File

@ -422,16 +422,29 @@ class Pattern
else
[this]
resolveScopeName: (line, captureIndices) ->
resolvedScopeName = @scopeName.replace /\${(\d+):\/(downcase|upcase)}/, (match, index, command) ->
capture = captureIndices[parseInt(index)]
if capture?
replacement = line.substring(capture.start, capture.end)
switch command
when 'downcase' then replacement.toLowerCase()
when 'upcase' then replacement.toUpperCase()
else replacement
else
match
resolvedScopeName.replace /\$(\d+)/, (match, index) ->
capture = captureIndices[parseInt(index)]
if capture?
line.substring(capture.start, capture.end)
else
match
handleMatch: (stack, line, captureIndices) ->
scopes = scopesFromStack(stack)
if @scopeName and not @popRule
patternScope = @scopeName.replace /\$\d+/, (match) ->
capture = captureIndices[parseInt(match.substring(1))]
if capture?
line.substring(capture.start, capture.end)
else
match
scopes.push(patternScope)
scopes.push(@resolveScopeName(line, captureIndices))
if @captures
tokens = @getTokensForCaptureIndices(line, _.clone(captureIndices), scopes, stack)