Handle ties between custom and built-in grammar file types

This commit is contained in:
Max Brunsfeld
2015-07-29 13:58:54 -07:00
parent 1ee382a4cb
commit aa8fb391aa
2 changed files with 18 additions and 3 deletions

View File

@@ -138,11 +138,23 @@ describe "the `grammars` global", ->
expect(-> atom.grammars.selectGrammar(null, '')).not.toThrow()
expect(-> atom.grammars.selectGrammar(null, null)).not.toThrow()
describe "when the user has custom grammar filetypes", ->
it "considers the custom filetypes as well as those defined in the grammar", ->
describe "when the user has custom grammar file types", ->
it "considers the custom file types as well as those defined in the grammar", ->
atom.config.set('core.fileTypesByScope', 'source.ruby': ['Cheffile'])
expect(atom.grammars.selectGrammar('build/Cheffile', 'cookbook "postgres"').scopeName).toBe 'source.ruby'
it "favors user-defined file types over built-in ones of equal length", ->
atom.config.set('core.fileTypesByScope',
'source.coffee': ['Rakefile'],
'source.ruby': ['Cakefile']
)
expect(atom.grammars.selectGrammar('Rakefile', '').scopeName).toBe 'source.coffee'
expect(atom.grammars.selectGrammar('Cakefile', '').scopeName).toBe 'source.ruby'
it "favors grammars with matching first-line-regexps even if custom file types match the file", ->
atom.config.set('core.fileTypesByScope', 'source.ruby': ['bootstrap'])
expect(atom.grammars.selectGrammar('bootstrap', '#!/usr/bin/env node').scopeName).toBe 'source.js'
describe ".removeGrammar(grammar)", ->
it "removes the grammar, so it won't be returned by selectGrammar", ->
grammar = atom.grammars.selectGrammar('foo.js')

View File

@@ -74,11 +74,14 @@ class GrammarRegistry extends FirstMate.GrammarRegistry
if customFileTypes = atom.config.get('core.fileTypesByScope')?[grammar.scopeName]
fileTypes = fileTypes.concat(customFileTypes)
for fileType in fileTypes
for fileType, i in fileTypes
fileTypeComponents = fileType.toLowerCase().split(PathSplitRegex)
pathSuffix = pathComponents[-fileTypeComponents.length..-1]
if _.isEqual(pathSuffix, fileTypeComponents)
pathScore = Math.max(pathScore, fileType.length)
if i >= grammar.fileTypes.length
pathScore += 0.5
pathScore
grammarMatchesContents: (grammar, contents) ->