Simplify grammar selection and its specs

This commit is contained in:
Corey Johnson & Nathan Sobo
2013-03-21 15:45:41 -06:00
committed by Nathan Sobo
parent bfd2667c51
commit dacb00ed67
3 changed files with 19 additions and 23 deletions

View File

@@ -2048,13 +2048,13 @@ describe "EditSession", ->
describe "when the 'grammars-loaded' event is triggered on the syntax global", ->
it "reloads the edit session's grammar and re-tokenizes the buffer if it changes", ->
editSession.destroy()
grammarToReturn = syntax.grammarByFileTypeSuffix('txt')
grammarToReturn = syntax.grammarByPath('a.txt')
spyOn(syntax, 'selectGrammar').andCallFake -> grammarToReturn
editSession = project.buildEditSession('sample.js', autoIndent: false)
expect(editSession.lineForScreenRow(0).tokens.length).toBe 1
grammarToReturn = syntax.grammarByFileTypeSuffix('js')
grammarToReturn = syntax.grammarByPath('a.js')
syntax.trigger 'grammars-loaded'
expect(editSession.lineForScreenRow(0).tokens.length).toBeGreaterThan 1

View File

@@ -2,11 +2,12 @@ fs = require 'fs-utils'
describe "the `syntax` global", ->
describe ".selectGrammar(filePath)", ->
it "uses the filePath's extension to load the correct grammar", ->
expect(syntax.selectGrammar("file.js").name).toBe "JavaScript"
it "uses the filePath's base name if there is no extension", ->
expect(syntax.selectGrammar("Rakefile").name).toBe "Ruby"
it "can use the filePath to load the correct grammar based on the grammar's filetype", ->
expect(syntax.selectGrammar("file.js").name).toBe "JavaScript" # based on extension (.js)
expect(syntax.selectGrammar("/tmp/.git/config").name).toBe "Git Config" # based on end of the path (.git/config)
expect(syntax.selectGrammar("Rakefile").name).toBe "Ruby" # based on the file's basename (Rakefile)
expect(syntax.selectGrammar("curb").name).toBe "Plain Text"
expect(syntax.selectGrammar("/hu.git/config").name).toBe "Plain Text"
it "uses the filePath's shebang line if the grammar cannot be determined by the extension or basename", ->
filePath = require.resolve("fixtures/shebang")
@@ -29,11 +30,6 @@ describe "the `syntax` global", ->
expect(syntax.selectGrammar(filePath, filePathContents).name).toBe "Ruby"
expect(fs.read).not.toHaveBeenCalled()
it "uses the grammar's fileType as a suffix of the full filePath if the grammar cannot be determined by shebang line", ->
expect(syntax.selectGrammar("/tmp/.git/config").name).toBe "Git Config"
it "uses plain text if no grammar can be found", ->
expect(syntax.selectGrammar("this-is-not-a-real-file").name).toBe "Plain Text"
describe ".getProperty(scopeDescriptor)", ->
it "returns the property with the most specific scope selector", ->

View File

@@ -5,6 +5,8 @@ Specificity = require 'specificity'
fs = require 'fs-utils'
EventEmitter = require 'event-emitter'
NullGrammar = require 'null-grammar'
nodePath = require 'path'
pathSplitRegex = new RegExp("[#{nodePath.sep}.]")
module.exports =
class Syntax
@@ -25,19 +27,17 @@ class Syntax
selectGrammar: (filePath, fileContents) ->
return @grammarsByFileType["txt"] ? @nullGrammar unless filePath
@grammarByFirstLineRegex(filePath, fileContents) ?
@grammarByPath(filePath) ?
@grammarsByFileType["txt"] ?
@nullGrammar
extension = fs.extension(filePath)?[1..]
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarByFirstLineRegex(filePath, fileContents) or
@grammarsByFileType[extension] or
@grammarByFileTypeSuffix(filePath) or
@grammarsByFileType["txt"] ? @nullGrammar
grammarByFileTypeSuffix: (filePath) ->
grammarByPath: (path) ->
pathComponents = path.split(pathSplitRegex)
for fileType, grammar of @grammarsByFileType
return grammar if _.endsWith(filePath, fileType)
fileTypeComponents = fileType.split(pathSplitRegex)
pathSuffix = pathComponents[-fileTypeComponents.length..-1]
return grammar if _.isEqual(pathSuffix, fileTypeComponents)
grammarByFirstLineRegex: (filePath, fileContents) ->
try