Give first line regex the required amount of lines

Certain bundles require multi-line matches in the firstLineMatch
value so count the number of newlines in the regex and only test
the regex against only those lines.
This commit is contained in:
Kevin Sawicki
2013-02-20 15:11:07 -08:00
parent 881efd9c5d
commit 1db21c91cc
2 changed files with 29 additions and 6 deletions

View File

@@ -39,13 +39,30 @@ class Syntax
grammarByFirstLineRegex: (filePath, fileContents) ->
try
fileContents = fs.read(filePath) unless fileContents?
fileContents ?= fs.read(filePath)
catch e
null
return
if fileContents
firstLine = fileContents.match(/^.*$/m)
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(firstLine)
return unless fileContents
lines = fileContents.split('\n')
_.find @grammars, (grammar) ->
regex = grammar.firstLineRegex
return unless regex?
escaped = false
numberOfNewlinesInRegex = 0
for character in regex.source
switch character
when '\\'
escaped = !escaped
when 'n'
numberOfNewlinesInRegex++ if escaped
escaped = false
else
escaped = false
regex.test(lines[0..numberOfNewlinesInRegex].join('\n'))
grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]