mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-09 00:30:14 +03:00
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.
This commit is contained in:
parent
1872335adf
commit
2b51a2ce73
@ -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"
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user