mirror of
https://github.com/atom/atom.git
synced 2026-01-22 21:38:10 -05:00
Rename grammarForFilePath to selectGrammar
The old name was misleading because it the decision is based on the file contents in some circumstances.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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<html>"
|
||||
expect(syntax.grammarForFilePath("dummy.coffee", fileContent).name).toBe "CoffeeScript"
|
||||
expect(syntax.selectGrammar("dummy.coffee", fileContent).name).toBe "CoffeeScript"
|
||||
|
||||
fileContent = '<?xml version="1.0" encoding="UTF-8"?>'
|
||||
expect(syntax.grammarForFilePath("grammar.tmLanguage", fileContent).name).toBe "Plain Text"
|
||||
expect(syntax.selectGrammar("grammar.tmLanguage", fileContent).name).toBe "Plain Text"
|
||||
|
||||
fileContent += '\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
|
||||
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", ->
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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..]
|
||||
|
||||
Reference in New Issue
Block a user