Support grammar fileTypes that contains more than extensions

The Git bundle registers fileTypes with multiple path segments
such as '.git/config'
This commit is contained in:
Kevin Sawicki
2012-12-18 17:48:22 -08:00
parent 28d28a5206
commit 2256962129
3 changed files with 13 additions and 1 deletions

View File

@@ -36,6 +36,9 @@ describe "TextMateBundle", ->
filePath = require.resolve("fixtures/shebang")
expect(TextMateBundle.grammarForFilePath(filePath).name).toBe "Ruby"
it "uses the grammar's fileType as a suffix of the full filePath if the grammar cannot be determined by shebang line", ->
expect(TextMateBundle.grammarForFilePath("/tmp/.git/config").name).toBe "Git Config"
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"

View File

@@ -36,11 +36,17 @@ class TextMateBundle
@grammarsByScopeName[grammar.scopeName] = grammar
@grammarForFilePath: (filePath) ->
return @grammarsByFileType["txt"] unless filePath
extension = fs.extension(filePath)?[1...]
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarsByFileType[extension] or @grammarByShebang(filePath) or @grammarsByFileType["txt"]
@grammarsByFileType[extension] or @grammarByShebang(filePath) or @grammarByFileTypeSuffix(filePath) or @grammarsByFileType["txt"]
@grammarByFileTypeSuffix: (filePath) ->
for fileType, grammar of @grammarsByFileType
return grammar if _.endsWith(filePath, fileType)
@grammarByShebang: (filePath) ->
try

View File

@@ -82,3 +82,6 @@ _.mixin
@pendingNextTickFns.push(fn)
@messageChannel.port2.postMessage(0)
endsWith: (string, suffix) ->
string.indexOf(suffix, string.length - suffix.length) isnt -1