Only use the first line when determine the syntax via file contents

This commit is contained in:
Corey Johnson
2013-02-20 12:38:49 -08:00
parent 27f44fbfd7
commit e0212ba02b
2 changed files with 7 additions and 1 deletions

View File

@@ -12,6 +12,10 @@ describe "the `syntax` global", ->
filePath = require.resolve("fixtures/shebang")
expect(syntax.grammarForFilePath(filePath).name).toBe "Ruby"
it "only use the first line to determine the syntax", ->
fileContent = "first-line\n<html>"
expect(syntax.grammarForFilePath("dummy.coffee", fileContent).name).toBe "CoffeeScript"
it "doesn't read the file when the file contents are specified", ->
filePath = require.resolve("fixtures/shebang")
filePathContents = fs.read(filePath)

View File

@@ -43,7 +43,9 @@ class Syntax
catch e
null
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(fileContents)
if fileContents
firstLine = fileContents.match(/^.*$/m)
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(firstLine)
grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]