diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 754ac051a..f6db6702a 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -2049,7 +2049,7 @@ describe "EditSession", -> it "reloads the edit session's grammar and re-tokenizes the buffer if it changes", -> editSession.destroy() grammarToReturn = syntax.grammarByFileTypeSuffix('txt') - spyOn(syntax, 'grammarForFilePath').andCallFake -> grammarToReturn + spyOn(syntax, 'selectGrammar').andCallFake -> grammarToReturn editSession = project.buildEditSession('sample.js', autoIndent: false) expect(editSession.lineForScreenRow(0).tokens.length).toBe 1 diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 3c2bd2024..63a9daa6a 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -2038,7 +2038,7 @@ describe "Editor", -> editor.edit(project.buildEditSession(path)) expect(editor.getGrammar().name).toBe 'Plain Text' - jsGrammar = syntax.grammarForFilePath('/tmp/js.js') + jsGrammar = syntax.selectGrammar('/tmp/js.js') expect(jsGrammar.name).toBe 'JavaScript' project.addGrammarOverrideForPath(path, jsGrammar) @@ -2067,7 +2067,7 @@ describe "Editor", -> expect(eventHandler).not.toHaveBeenCalled() - jsGrammar = syntax.grammarForFilePath('/tmp/js.js') + jsGrammar = syntax.selectGrammar('/tmp/js.js') project.addGrammarOverrideForPath(path, jsGrammar) editor.reloadGrammar() diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index 017f3f85b..b655d3f6f 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -1,39 +1,39 @@ fs = require 'fs-utils' describe "the `syntax` global", -> - describe ".grammarForFilePath(filePath)", -> + describe ".selectGrammar(filePath)", -> it "uses the filePath's extension to load the correct grammar", -> - expect(syntax.grammarForFilePath("file.js").name).toBe "JavaScript" + expect(syntax.selectGrammar("file.js").name).toBe "JavaScript" it "uses the filePath's base name if there is no extension", -> - expect(syntax.grammarForFilePath("Rakefile").name).toBe "Ruby" + expect(syntax.selectGrammar("Rakefile").name).toBe "Ruby" it "uses the filePath's shebang line if the grammar cannot be determined by the extension or basename", -> filePath = require.resolve("fixtures/shebang") - expect(syntax.grammarForFilePath(filePath).name).toBe "Ruby" + expect(syntax.selectGrammar(filePath).name).toBe "Ruby" it "uses the number of newlines in the first line regex to determine the number of lines to test against", -> fileContent = "first-line\n" - expect(syntax.grammarForFilePath("dummy.coffee", fileContent).name).toBe "CoffeeScript" + expect(syntax.selectGrammar("dummy.coffee", fileContent).name).toBe "CoffeeScript" fileContent = '' - expect(syntax.grammarForFilePath("grammar.tmLanguage", fileContent).name).toBe "Plain Text" + expect(syntax.selectGrammar("grammar.tmLanguage", fileContent).name).toBe "Plain Text" fileContent += '\n' - expect(syntax.grammarForFilePath("grammar.tmLanguage", fileContent).name).toBe "Property List (XML)" + expect(syntax.selectGrammar("grammar.tmLanguage", fileContent).name).toBe "Property List (XML)" 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(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.grammarForFilePath("/tmp/.git/config").name).toBe "Git Config" + expect(syntax.selectGrammar("/tmp/.git/config").name).toBe "Git Config" it "uses plain text if no grammar can be found", -> - expect(syntax.grammarForFilePath("this-is-not-a-real-file").name).toBe "Plain Text" + 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/spec/app/text-mate-grammar-spec.coffee b/spec/app/text-mate-grammar-spec.coffee index aee8b00a3..8d421b142 100644 --- a/spec/app/text-mate-grammar-spec.coffee +++ b/spec/app/text-mate-grammar-spec.coffee @@ -8,7 +8,7 @@ describe "TextMateGrammar", -> grammar = null beforeEach -> - grammar = syntax.grammarForFilePath("hello.coffee") + grammar = syntax.selectGrammar("hello.coffee") describe ".tokenizeLine(line, ruleStack)", -> describe "when the entire line matches a single pattern with no capture groups", -> @@ -31,7 +31,7 @@ describe "TextMateGrammar", -> describe "when the line doesn't match any patterns", -> it "returns the entire line as a single simple token with the grammar's scope", -> - textGrammar = syntax.grammarForFilePath('foo.txt') + textGrammar = syntax.selectGrammar('foo.txt') {tokens} = textGrammar.tokenizeLine("abc def") expect(tokens.length).toBe 1 @@ -108,14 +108,14 @@ describe "TextMateGrammar", -> describe "when the line matches no patterns", -> it "does not infinitely loop", -> - grammar = syntax.grammarForFilePath("sample.txt") + grammar = syntax.selectGrammar("sample.txt") {tokens} = grammar.tokenizeLine('hoo') expect(tokens.length).toBe 1 expect(tokens[0]).toEqual value: 'hoo', scopes: ["text.plain", "meta.paragraph.text"] describe "when the line matches a pattern with a 'contentName'", -> it "creates tokens using the content of contentName as the token name", -> - grammar = syntax.grammarForFilePath("sample.txt") + grammar = syntax.selectGrammar("sample.txt") {tokens} = grammar.tokenizeLine('ok, cool') expect(tokens[0]).toEqual value: 'ok, cool', scopes: ["text.plain", "meta.paragraph.text"] @@ -243,18 +243,18 @@ describe "TextMateGrammar", -> expect(tokens[1].value).toBe " a singleLineComment" it "does not loop infinitely (regression)", -> - grammar = syntax.grammarForFilePath("hello.js") + grammar = syntax.selectGrammar("hello.js") {tokens, ruleStack} = grammar.tokenizeLine("// line comment") {tokens, ruleStack} = grammar.tokenizeLine(" // second line comment with a single leading space", ruleStack) describe "when inside a C block", -> it "correctly parses a method. (regression)", -> - grammar = syntax.grammarForFilePath("hello.c") + grammar = syntax.selectGrammar("hello.c") {tokens, ruleStack} = grammar.tokenizeLine("if(1){m()}") expect(tokens[5]).toEqual value: "m", scopes: ["source.c", "meta.block.c", "meta.function-call.c", "support.function.any-method.c"] it "correctly parses nested blocks. (regression)", -> - grammar = syntax.grammarForFilePath("hello.c") + grammar = syntax.selectGrammar("hello.c") {tokens, ruleStack} = grammar.tokenizeLine("if(1){if(1){m()}}") expect(tokens[5]).toEqual value: "if", scopes: ["source.c", "meta.block.c", "keyword.control.c"] expect(tokens[10]).toEqual value: "m", scopes: ["source.c", "meta.block.c", "meta.block.c", "meta.function-call.c", "support.function.any-method.c"] @@ -263,7 +263,7 @@ describe "TextMateGrammar", -> it "aborts tokenization", -> spyOn(console, 'error') window.loadPackage("package-with-infinite-loop-grammar") - grammar = syntax.grammarForFilePath("something.package-with-infinite-loop-grammar") + grammar = syntax.selectGrammar("something.package-with-infinite-loop-grammar") {tokens} = grammar.tokenizeLine("abc") expect(tokens[0].value).toBe "a" expect(tokens[1].value).toBe "bc" diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index af461e30d..7f59e33e8 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -18,9 +18,9 @@ class LanguageMode pathContents = @buffer.cachedDiskContents previousGrammar = @grammar if @buffer.project? - @grammar = @buffer.project.grammarForFilePath(path, pathContents) + @grammar = @buffer.project.selectGrammar(path, pathContents) else - @grammar = syntax.grammarForFilePath(path, pathContents) + @grammar = syntax.selectGrammar(path, pathContents) throw new Error("No grammar found for path: #{path}") unless @grammar previousGrammar isnt @grammar diff --git a/src/app/project.coffee b/src/app/project.coffee index 678a4f2e7..b42abbca9 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, contents) -> - @grammarOverrideForPath(path) or syntax.grammarForFilePath(path, contents) + selectGrammar: (path, contents) -> + @grammarOverrideForPath(path) or syntax.selectGrammar(path, contents) getPath: -> @rootDirectory?.path diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index 04d2d02ff..0ab0e3a8a 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -21,7 +21,7 @@ class Syntax @grammarsByFileType[fileType] = grammar @grammarsByScopeName[grammar.scopeName] = grammar - grammarForFilePath: (filePath, fileContents) -> + selectGrammar: (filePath, fileContents) -> return @grammarsByFileType["txt"] unless filePath extension = fs.extension(filePath)?[1..]