mirror of
https://github.com/atom/atom.git
synced 2026-02-14 08:35:11 -05:00
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:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user