Support packages with CSON grammars

This commit is contained in:
Kevin Sawicki 2013-02-09 12:41:51 -08:00
parent 4619e1847f
commit 75a9bce848
3 changed files with 29 additions and 3 deletions

View File

@ -1,4 +1,5 @@
TextMateGrammar = require 'text-mate-grammar'
TextMatePackage = require 'text-mate-package'
plist = require 'plist'
fs = require 'fs'
_ = require 'underscore'
@ -257,3 +258,14 @@ describe "TextMateGrammar", ->
{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"]
describe "when the grammar is CSON", ->
it "loads the grammar and correctly parses a keyword", ->
spyOn(syntax, 'addGrammar')
pack = new TextMatePackage(fixturesProject.resolve("packages/package-with-a-cson-grammar.tmbundle"))
pack.load()
grammar = pack.grammars[0]
expect(grammar).toBeTruthy()
expect(grammar.scopeName).toBe "source.alot"
{tokens, ruleStack} = grammar.tokenizeLine("this is alot of code")
expect(tokens[1]).toEqual value: "alot", scopes: ["source.alot", "keyword.alot"]

View File

@ -0,0 +1,11 @@
'fileTypes': ['alot']
'name': 'Alot'
'scopeName': 'source.alot'
'patterns': [
{
'captures':
'0':
'name': 'keyword.alot'
'match': 'alot'
}
]

View File

@ -9,9 +9,12 @@ module.exports =
class TextMateGrammar
@readFromPath: (path) ->
grammarContent = null
plist.parseString fs.read(path), (e, data) ->
throw new Error(e) if e
grammarContent = data[0]
if fs.extension(path) is '.cson'
grammarContent = fs.readObject(path)
else
plist.parseString fs.read(path), (e, data) ->
throw new Error(e) if e
grammarContent = data[0]
throw new Error("Failed to load grammar at path `#{path}`") unless grammarContent
grammarContent