mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Add TextMateBundle which handles all bundle loading
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
TextMateBundle = require 'text-mate-bundle'
|
||||
plist = require 'plist'
|
||||
fs = require 'fs'
|
||||
_ = require 'underscore'
|
||||
@@ -8,17 +9,7 @@ describe "TextMateGrammar", ->
|
||||
|
||||
beforeEach ->
|
||||
coffeePlist = fs.read(require.resolve 'CoffeeScriptBundle.tmbundle/Syntaxes/CoffeeScript.tmLanguage')
|
||||
plist.parseString coffeePlist, (err, data) ->
|
||||
grammar = new TextMateGrammar(data[0])
|
||||
|
||||
describe ".loadFromBundles()", ->
|
||||
it "creates grammars for all plist files in all bundles' Syntaxes directories", ->
|
||||
TextMateGrammar.loadFromBundles()
|
||||
coffeeGrammar = TextMateGrammar.grammarForExtension("coffee")
|
||||
rubyGrammar = TextMateGrammar.grammarForExtension("rb")
|
||||
|
||||
expect(coffeeGrammar.name).toBe "CoffeeScript"
|
||||
expect(rubyGrammar.name).toBe "Ruby"
|
||||
grammar = TextMateBundle.grammarForFileName("hello.coffee")
|
||||
|
||||
describe ".getLineTokens(line, currentRule)", ->
|
||||
describe "when the entire line matches a single pattern with no capture groups", ->
|
||||
@@ -118,4 +109,4 @@ describe "TextMateGrammar", ->
|
||||
expect(tokens[5]).toEqual value: '}', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"]
|
||||
expect(tokens[6]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","string.quoted.double.coffee","punctuation.definition.string.end.coffee"]
|
||||
expect(tokens[7]).toEqual value: '}', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"]
|
||||
expect(tokens[8]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.end.coffee"]
|
||||
expect(tokens[8]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.end.coffee"]
|
||||
|
||||
@@ -7,13 +7,13 @@ Project = require 'project'
|
||||
Directory = require 'directory'
|
||||
File = require 'file'
|
||||
RootView = require 'root-view'
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
TextMateBundle = require 'text-mate-bundle'
|
||||
fs = require 'fs'
|
||||
require 'window'
|
||||
$native.showDevTools()
|
||||
|
||||
requireStylesheet "jasmine.css"
|
||||
TextMateGrammar.loadFromBundles()
|
||||
TextMateBundle.loadAll()
|
||||
|
||||
defaultTitle = document.title
|
||||
pathsWithSubscriptions = null
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
AceAdaptor = require 'ace-adaptor'
|
||||
Range = require 'range'
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
TextMateBundle = require 'text-mate-bundle'
|
||||
_ = require 'underscore'
|
||||
|
||||
module.exports =
|
||||
@@ -15,7 +15,8 @@ class LanguageMode
|
||||
constructor: (@editSession) ->
|
||||
@buffer = @editSession.buffer
|
||||
@aceMode = @requireAceMode()
|
||||
@grammar = TextMateGrammar.grammarForExtension(@editSession.buffer.getExtension())
|
||||
|
||||
@grammar = TextMateBundle.grammarForFileName(@editSession.buffer.getBaseName())
|
||||
@aceAdaptor = new AceAdaptor(@editSession)
|
||||
|
||||
_.adviseBefore @editSession, 'insertText', (text) =>
|
||||
|
||||
37
src/app/text-mate-bundle.coffee
Normal file
37
src/app/text-mate-bundle.coffee
Normal file
@@ -0,0 +1,37 @@
|
||||
_ = require 'underscore'
|
||||
fs = require 'fs'
|
||||
plist = require 'plist'
|
||||
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
|
||||
|
||||
module.exports =
|
||||
class TextMateBundle
|
||||
@grammarsByFileType: {}
|
||||
@bundles: []
|
||||
|
||||
@loadAll: ->
|
||||
for bundlePath in fs.list(require.resolve("bundles"))
|
||||
@registerBundle(new TextMateBundle(bundlePath))
|
||||
|
||||
@registerBundle: (bundle)->
|
||||
@bundles.push(bundle)
|
||||
|
||||
for grammar in bundle.grammars
|
||||
for fileType in grammar.fileTypes
|
||||
@grammarsByFileType[fileType] = grammar
|
||||
|
||||
@grammarForFileName: (fileName) ->
|
||||
extension = fs.extension(fileName)[1...]
|
||||
@grammarsByFileType[extension] or @grammarsByFileType["txt"]
|
||||
|
||||
grammars: null
|
||||
|
||||
constructor: (bundlePath) ->
|
||||
@grammars = []
|
||||
syntaxesPath = fs.join(bundlePath, "Syntaxes")
|
||||
if fs.exists(syntaxesPath)
|
||||
for syntaxPath in fs.list(syntaxesPath)
|
||||
@grammars.push TextMateGrammar.loadFromPath(syntaxPath)
|
||||
|
||||
|
||||
@@ -4,36 +4,18 @@ plist = require 'plist'
|
||||
|
||||
module.exports =
|
||||
class TextMateGrammar
|
||||
@grammarsByExtension: {}
|
||||
|
||||
@loadFromBundles: ->
|
||||
for bundlePath in fs.list(require.resolve("bundles"))
|
||||
syntaxesPath = fs.join(bundlePath, "Syntaxes")
|
||||
continue unless fs.exists(syntaxesPath)
|
||||
for path in fs.list(syntaxesPath)
|
||||
grammar = @loadGrammarFromPath(path)
|
||||
@registerGrammar(grammar)
|
||||
|
||||
@loadGrammarFromPath: (path) ->
|
||||
@loadFromPath: (path) ->
|
||||
grammar = null
|
||||
plist.parseString fs.read(path), (e, data) ->
|
||||
throw new Error(e) if e
|
||||
grammar = new TextMateGrammar(data[0])
|
||||
grammar
|
||||
|
||||
@registerGrammar: (grammar) ->
|
||||
for extension in grammar.extensions
|
||||
@grammarsByExtension[extension] = grammar
|
||||
|
||||
@grammarForExtension: (extension) ->
|
||||
@grammarsByExtension[extension] or @grammarsByExtension["txt"]
|
||||
|
||||
name: null
|
||||
repository: null
|
||||
initialRule: null
|
||||
|
||||
constructor: ({ @name, fileTypes, scopeName, patterns, repository }) ->
|
||||
@extensions = fileTypes
|
||||
constructor: ({ @name, @fileTypes, scopeName, patterns, repository }) ->
|
||||
@initialRule = new Rule(this, {scopeName, patterns})
|
||||
@repository = {}
|
||||
for name, data of repository
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# the DOM window.
|
||||
|
||||
Native = require 'native'
|
||||
TextMateGrammar = require 'text-mate-grammar'
|
||||
TextMateBundle = require 'text-mate-bundle'
|
||||
fs = require 'fs'
|
||||
_ = require 'underscore'
|
||||
$ = require 'jquery'
|
||||
@@ -31,7 +31,7 @@ windowAdditions =
|
||||
false
|
||||
$(window).focus()
|
||||
atom.windowOpened this
|
||||
TextMateGrammar.loadFromBundles()
|
||||
TextMateBundle.loadAll()
|
||||
|
||||
shutdown: ->
|
||||
@rootView.deactivate()
|
||||
|
||||
Reference in New Issue
Block a user