From dacb00ed678d93c8d136689d2eea41d70ce41e00 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 21 Mar 2013 15:45:41 -0600 Subject: [PATCH] Simplify grammar selection and its specs --- spec/app/edit-session-spec.coffee | 4 ++-- spec/app/syntax-spec.coffee | 16 ++++++---------- src/app/syntax.coffee | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index f6db6702a..7afe8d888 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -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 diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index b655d3f6f..f2490dcbb 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -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", -> diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index 90c040429..a80aba43e 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -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