Use the file's first line to determine the grammar.

Only when it cannot be determined by the file's extension fails. Fixes #75
Esse commit está contido em:
Corey Johnson
2012-11-01 17:47:20 -07:00
commit 83329567b9
4 arquivos alterados com 32 adições e 2 exclusões
+15
Ver Arquivo
@@ -21,3 +21,18 @@ describe "TextMateBundle", ->
spyOn(console, 'warn')
expect(-> new TextMateBundle(bundlePath)).not.toThrow()
expect(console.warn).toHaveBeenCalled()
describe ".grammarForFilePath(filePath)", ->
it "uses the filePath's extension to load the correct grammar", ->
expect(TextMateBundle.grammarForFilePath("file.js").name).toBe "JavaScript"
it "uses the filePath's base name if there is no extension", ->
expect(TextMateBundle.grammarForFilePath("Rakefile").name).toBe "Ruby"
it "uses the filePath's shebang line if the grammar cannot be determined by the extension or basename", ->
filePath = require.resolve("fixtures/shebang")
expect(TextMateBundle.grammarForFilePath(filePath).name).toBe "Ruby"
it "uses plain text if no grammar can be found", ->
filePath = require.resolve("this-is-not-a-real-file")
expect(TextMateBundle.grammarForFilePath(filePath).name).toBe "Plain Text"
+3
Ver Arquivo
@@ -0,0 +1,3 @@
#!/usr/bin/ruby
puts "America – fuck yeah!"
+11 -1
Ver Arquivo
@@ -10,6 +10,7 @@ class TextMateBundle
@grammarsByScopeName: {}
@preferencesByScopeSelector: {}
@bundles: []
@grammars: []
@loadAll: ->
globalBundles = fs.list(require.resolve("bundles"))
@@ -27,6 +28,7 @@ class TextMateBundle
@preferencesByScopeSelector[scopeSelector] = preferences
for grammar in bundle.grammars
@grammars.push(grammar)
for fileType in grammar.fileTypes
@grammarsByFileType[fileType] = grammar
@grammarsByScopeName[grammar.scopeName] = grammar
@@ -36,7 +38,15 @@ class TextMateBundle
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarsByFileType[extension] or @grammarsByFileType["txt"]
@grammarsByFileType[extension] or @grammarByShebang(filePath) or @grammarsByFileType["txt"]
@grammarByShebang: (filePath) ->
try
firstLine = fs.read(filePath).match(/.*/)[0]
catch e
null
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(firstLine)
@grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]
+3 -1
Ver Arquivo
@@ -16,10 +16,12 @@ class TextMateGrammar
scopeName: null
repository: null
initialRule: null
firstLineRegex: null
constructor: ({ @name, @fileTypes, @scopeName, patterns, repository, @foldingStopMarker}) ->
constructor: ({ @name, @fileTypes, @scopeName, patterns, repository, @foldingStopMarker, firstLineMatch}) ->
@initialRule = new Rule(this, {@scopeName, patterns})
@repository = {}
@firstLineRegex = new OnigRegExp(firstLineMatch) if firstLineMatch
for name, data of repository
@repository[name] = new Rule(this, data)