From 2b51a2ce733eb4dcaf678091a6cd95320f2be79e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 8 Jan 2013 16:15:24 -0800 Subject: [PATCH] Test firstLineRegex first when finding grammar This is required for the property-list bundle to highlight .plist files that maybe in XML or non-XML formats. Also specify the cached buffer disk contents to grammarForFilePath so fs.read doesn't need to be called again if the contents are already read. --- spec/app/syntax-spec.coffee | 9 +++++++++ src/app/language-mode.coffee | 6 ++++-- src/app/project.coffee | 4 ++-- src/app/syntax.coffee | 10 +++++----- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index 60b65b0e9..d15eb5c74 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -1,3 +1,5 @@ +fs = require 'fs' + describe "the `syntax` global", -> describe ".grammarForFilePath(filePath)", -> it "uses the filePath's extension to load the correct grammar", -> @@ -10,6 +12,13 @@ describe "the `syntax` global", -> filePath = require.resolve("fixtures/shebang") expect(syntax.grammarForFilePath(filePath).name).toBe "Ruby" + it "doesn't read the file when the file contents are specified", -> + filePath = require.resolve("fixtures/shebang") + filePathContents = fs.read(filePath) + spyOn(fs, 'read').andCallThrough() + expect(syntax.grammarForFilePath(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.grammarForFilePath("/tmp/.git/config").name).toBe "Git Config" diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 8c6e21b89..79e9bc461 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -48,10 +48,12 @@ class LanguageMode false reloadGrammar: -> + path = @buffer.getPath() + pathContents = @buffer.cachedDiskContents if @buffer.project? - @grammar = @buffer.project.grammarForFilePath(@buffer.getPath()) + @grammar = @buffer.project.grammarForFilePath(path, pathContents) else - @grammar = syntax.grammarForFilePath(@buffer.getPath()) + @grammar = syntax.grammarForFilePath(path, pathContents) isQuote: (string) -> /'|"/.test(string) diff --git a/src/app/project.coffee b/src/app/project.coffee index 1bbb56ceb..fa6e9f8cc 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -45,8 +45,8 @@ class Project grammarOverrideForPath: (path) -> syntax.grammarForScopeName(@grammarOverridesByPath[path]) - grammarForFilePath: (path) -> - @grammarOverrideForPath(path) or syntax.grammarForFilePath(path) + grammarForFilePath: (path, contents) -> + @grammarOverrideForPath(path) or syntax.grammarForFilePath(path, contents) getPath: -> @rootDirectory?.path diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index 8b3f1a9c5..1ff5e8663 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -20,15 +20,15 @@ class Syntax @grammarsByFileType[fileType] = grammar @grammarsByScopeName[grammar.scopeName] = grammar - grammarForFilePath: (filePath) -> + grammarForFilePath: (filePath, fileContents) -> return @grammarsByFileType["txt"] unless filePath extension = fs.extension(filePath)?[1..] if filePath and extension.length == 0 extension = fs.base(filePath) - @grammarsByFileType[extension] or - @grammarByFirstLineRegex(filePath) or + @grammarByFirstLineRegex(filePath, fileContents) or + @grammarsByFileType[extension] or @grammarByFileTypeSuffix(filePath) or @grammarsByFileType["txt"] @@ -36,9 +36,9 @@ class Syntax for fileType, grammar of @grammarsByFileType return grammar if _.endsWith(filePath, fileType) - grammarByFirstLineRegex: (filePath) -> + grammarByFirstLineRegex: (filePath, fileContents) -> try - fileContents = fs.read(filePath) + fileContents = fs.read(filePath) unless fileContents? catch e null