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:
@@ -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"
|
||||
externo
+3
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/ruby
|
||||
|
||||
puts "America – fuck yeah!"
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário