mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Atom packages can load cson grammars and TextMate packages can't
This commit is contained in:
@@ -76,3 +76,23 @@ describe "AtomPackage", ->
|
||||
window.loadPackage("package-with-module")
|
||||
expect(config.get('package-with-module.numbers.one')).toBe 1
|
||||
expect(config.get('package-with-module.numbers.two')).toBe 2
|
||||
|
||||
describe "when the package has a grammars directory", ->
|
||||
grammar = null
|
||||
beforeEach ->
|
||||
spyOn(syntax, 'addGrammar')
|
||||
spyOn(syntax, 'addProperties')
|
||||
window.loadPackage("package-with-a-cson-grammar")
|
||||
expect(syntax.addGrammar).toHaveBeenCalled()
|
||||
grammar = syntax.addGrammar.argsForCall[0][0]
|
||||
|
||||
it "loads the grammar and correctly parses a keyword", ->
|
||||
expect(grammar.scopeName).toBe "source.alot"
|
||||
{tokens} = grammar.tokenizeLine("this is alot of code")
|
||||
expect(tokens[1]).toEqual value: "alot", scopes: ["source.alot", "keyword.alot"]
|
||||
|
||||
it "loads the properties included in the grammar files", ->
|
||||
expect(syntax.addProperties).toHaveBeenCalled()
|
||||
[selector, properties] = syntax.addProperties.argsForCall[0]
|
||||
expect(selector).toBe ".source.alot"
|
||||
expect(properties).toEqual {editor: increaseIndentPattern: '^a'}
|
||||
|
||||
@@ -258,14 +258,3 @@ 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(project.resolve("packages/package-with-a-cson-grammar.tmbundle"))
|
||||
pack.load()
|
||||
grammar = pack.grammars[0]
|
||||
expect(grammar).toBeTruthy()
|
||||
expect(grammar.scopeName).toBe "source.alot"
|
||||
{tokens} = grammar.tokenizeLine("this is alot of code")
|
||||
expect(tokens[1]).toEqual value: "alot", scopes: ["source.alot", "keyword.alot"]
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
'name': 'Really'
|
||||
'scope': 'source.alot'
|
||||
'tabTrigger': 'really'
|
||||
'content': 'I really like $1 alot$0'
|
||||
@@ -1,6 +1,11 @@
|
||||
'fileTypes': ['alot']
|
||||
'fileTypes': ['alot', 'foobizbang']
|
||||
'name': 'Alot'
|
||||
'scopeName': 'source.alot'
|
||||
|
||||
'properties':
|
||||
'editor':
|
||||
'increaseIndentPattern': '^a'
|
||||
|
||||
'patterns': [
|
||||
{
|
||||
'captures':
|
||||
@@ -1,3 +1,4 @@
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
Package = require 'package'
|
||||
fs = require 'fs-utils'
|
||||
_ = require 'underscore'
|
||||
@@ -15,6 +16,7 @@ class AtomPackage extends Package
|
||||
@loadMetadata()
|
||||
@loadKeymaps()
|
||||
@loadStylesheets()
|
||||
@loadGrammars()
|
||||
if @deferActivation = @metadata.activationEvents?
|
||||
@registerDeferredDeserializers()
|
||||
else
|
||||
@@ -43,6 +45,20 @@ class AtomPackage extends Package
|
||||
for stylesheetPath in fs.list(stylesheetDirPath)
|
||||
requireStylesheet(stylesheetPath)
|
||||
|
||||
loadGrammars: ->
|
||||
grammarsDirPath = fs.join(@path, 'grammars')
|
||||
for grammarPath in fs.list(grammarsDirPath)
|
||||
continue unless fs.extension(grammarPath) in ['.cson', '.json']
|
||||
grammarContent = fs.readObject(grammarPath)
|
||||
grammar = new TextMateGrammar(grammarContent)
|
||||
syntax.addGrammar(grammar)
|
||||
@loadPropertiesFromGrammar(grammarContent.scopeName, grammarContent.properties)
|
||||
|
||||
loadPropertiesFromGrammar: (scopeSelector, properties) ->
|
||||
return unless properties
|
||||
cssSelector = syntax.cssSelectorFromScopeSelector(scopeSelector)
|
||||
syntax.addProperties(cssSelector, properties)
|
||||
|
||||
activate: ->
|
||||
if @deferActivation
|
||||
@subscribeToActivationEvents()
|
||||
|
||||
@@ -125,4 +125,13 @@ class Syntax
|
||||
else
|
||||
element[0]
|
||||
|
||||
cssSelectorFromScopeSelector: (scopeSelector) ->
|
||||
scopeSelector.split(', ').map((commaFragment) ->
|
||||
commaFragment.split(' ').map((spaceFragment) ->
|
||||
spaceFragment.split('.').map((dotFragment) ->
|
||||
'.' + dotFragment.replace(/\+/g, '\\+')
|
||||
).join('')
|
||||
).join(' ')
|
||||
).join(', ')
|
||||
|
||||
_.extend(Syntax.prototype, EventEmitter)
|
||||
|
||||
@@ -8,7 +8,7 @@ CSON = require 'cson'
|
||||
module.exports =
|
||||
class TextMateGrammar
|
||||
@readFromPath: (path) ->
|
||||
fs.readObject(path)
|
||||
fs.readPlist(path)
|
||||
|
||||
name: null
|
||||
fileTypes: null
|
||||
|
||||
@@ -10,15 +10,6 @@ class TextMatePackage extends Package
|
||||
@testName: (packageName) ->
|
||||
/(\.|_|-)tmbundle$/.test(packageName)
|
||||
|
||||
@cssSelectorFromScopeSelector: (scopeSelector) ->
|
||||
scopeSelector.split(', ').map((commaFragment) ->
|
||||
commaFragment.split(' ').map((spaceFragment) ->
|
||||
spaceFragment.split('.').map((dotFragment) ->
|
||||
'.' + dotFragment.replace(/\+/g, '\\+')
|
||||
).join('')
|
||||
).join(' ')
|
||||
).join(', ')
|
||||
|
||||
constructor: ->
|
||||
super
|
||||
@preferencesPath = fs.join(@path, "Preferences")
|
||||
@@ -66,12 +57,12 @@ class TextMatePackage extends Package
|
||||
|
||||
for grammar in @getGrammars()
|
||||
if properties = @propertiesFromTextMateSettings(grammar)
|
||||
selector = @cssSelectorFromScopeSelector(grammar.scopeName)
|
||||
selector = syntax.cssSelectorFromScopeSelector(grammar.scopeName)
|
||||
scopedProperties.push({selector, properties})
|
||||
|
||||
for {scope, settings} in @getTextMatePreferenceObjects()
|
||||
if properties = @propertiesFromTextMateSettings(settings)
|
||||
selector = @cssSelectorFromScopeSelector(scope) if scope?
|
||||
selector = syntax.cssSelectorFromScopeSelector(scope) if scope?
|
||||
scopedProperties.push({selector, properties})
|
||||
|
||||
scopedProperties
|
||||
@@ -101,6 +92,3 @@ class TextMatePackage extends Package
|
||||
foldEndPattern: textMateSettings.foldingStopMarker
|
||||
)
|
||||
{ editor: editorProperties } if _.size(editorProperties) > 0
|
||||
|
||||
cssSelectorFromScopeSelector: (scopeSelector) ->
|
||||
@constructor.cssSelectorFromScopeSelector(scopeSelector)
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
'name': 'Indent'
|
||||
'scope': 'source.gfm'
|
||||
'settings':
|
||||
'increaseIndentPattern': '^\\s*([\\*\\+-])[ \\t]+'
|
||||
@@ -7,6 +7,11 @@
|
||||
'mkdown'
|
||||
'ron'
|
||||
]
|
||||
|
||||
'properties':
|
||||
'editor':
|
||||
'increaseIndentPattern': '^\\s*([\\*\\+-])[ \\t]+'
|
||||
|
||||
'patterns': [
|
||||
{
|
||||
'match': '(?:^|\\s)(\\*\\*[^\\*]+\\*\\*)'
|
||||
@@ -1,14 +1,13 @@
|
||||
fs = require 'fs-utils'
|
||||
TextMatePackage = require 'text-mate-package'
|
||||
|
||||
describe "GitHub Flavored Markdown grammar", ->
|
||||
grammar = null
|
||||
|
||||
beforeEach ->
|
||||
spyOn(syntax, "addGrammar")
|
||||
pack = new TextMatePackage(fs.resolveOnLoadPath("gfm.tmbundle"))
|
||||
pack.load()
|
||||
grammar = pack.grammars[0]
|
||||
spyOn(syntax, "addGrammar").andCallThrough()
|
||||
window.loadPackage("gfm")
|
||||
expect(syntax.addGrammar).toHaveBeenCalled()
|
||||
grammar = syntax.addGrammar.argsForCall[0][0]
|
||||
|
||||
it "parses the grammar", ->
|
||||
expect(grammar).toBeTruthy()
|
||||
@@ -91,7 +91,7 @@ module.exports =
|
||||
async.eachSeries(paths, loadSnippetFile, done)
|
||||
|
||||
translateTextmateSnippet: ({ scope, name, content, tabTrigger }) ->
|
||||
scope = TextMatePackage.cssSelectorFromScopeSelector(scope) if scope
|
||||
scope = syntax.cssSelectorFromScopeSelector(scope) if scope
|
||||
scope ?= '*'
|
||||
snippetsByScope = {}
|
||||
snippetsByName = {}
|
||||
|
||||
@@ -281,21 +281,6 @@ describe "Snippets extension", ->
|
||||
expect(console.warn).toHaveBeenCalled()
|
||||
expect(console.warn.calls.length).toBe 1
|
||||
|
||||
it "loads CSON snippets from TextMate packages", ->
|
||||
jasmine.unspy(snippets, 'loadTextMateSnippets')
|
||||
spyOn(console, 'warn')
|
||||
snippets.loaded = false
|
||||
snippets.loadAll()
|
||||
|
||||
waitsFor "CSON snippets to load", 5000, -> snippets.loaded
|
||||
|
||||
runs ->
|
||||
snippet = syntax.getProperty(['.source.alot'], 'snippets.really')
|
||||
expect(snippet).toBeTruthy()
|
||||
expect(snippet.prefix).toBe 'really'
|
||||
expect(snippet.name).toBe 'Really'
|
||||
expect(snippet.body).toBe "I really like alot"
|
||||
|
||||
describe "snippet body parser", ->
|
||||
it "breaks a snippet body into lines, with each line containing tab stops at the appropriate position", ->
|
||||
bodyTree = snippets.getBodyParser().parse """
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
fs = require 'fs-utils'
|
||||
TextMatePackage = require 'text-mate-package'
|
||||
|
||||
describe "TOML grammar", ->
|
||||
@@ -6,9 +5,9 @@ describe "TOML grammar", ->
|
||||
|
||||
beforeEach ->
|
||||
spyOn(syntax, "addGrammar")
|
||||
pack = new TextMatePackage(fs.resolveOnLoadPath("toml.tmbundle"))
|
||||
pack.load()
|
||||
grammar = pack.grammars[0]
|
||||
window.loadPackage("toml")
|
||||
expect(syntax.addGrammar).toHaveBeenCalled()
|
||||
grammar = syntax.addGrammar.argsForCall[0][0]
|
||||
|
||||
it "parses the grammar", ->
|
||||
expect(grammar).toBeTruthy()
|
||||
Reference in New Issue
Block a user