Store grammars on the syntax global

This commit is contained in:
Nathan Sobo
2012-12-31 18:11:11 -06:00
parent 4f50133fd8
commit 188d8f8604
8 changed files with 71 additions and 101 deletions

View File

@@ -17,7 +17,7 @@ class LanguageMode
constructor: (@editSession) ->
@buffer = @editSession.buffer
@grammar = TextMateBundle.grammarForFilePath(@buffer.getPath())
@grammar = syntax.grammarForFilePath(@buffer.getPath())
@bracketAnchorRanges = []
_.adviseBefore @editSession, 'insertText', (text) =>

View File

@@ -2,14 +2,50 @@ _ = require 'underscore'
jQuery = require 'jquery'
Specificity = require 'specificity'
{$$} = require 'space-pen'
fs = require 'fs'
module.exports =
class Syntax
constructor: ->
@grammars = []
@grammarsByFileType = {}
@grammarsByScopeName = {}
@globalProperties = {}
@scopedPropertiesIndex = 0
@scopedProperties = []
@propertiesBySelector = {}
addGrammar: (grammar) ->
@grammars.push(grammar)
for fileType in grammar.fileTypes
@grammarsByFileType[fileType] = grammar
@grammarsByScopeName[grammar.scopeName] = grammar
grammarForFilePath: (filePath) ->
return @grammarsByFileType["txt"] unless filePath
extension = fs.extension(filePath)?[1..]
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarsByFileType[extension] or
@grammarByShebang(filePath) or
@grammarByFileTypeSuffix(filePath) or
@grammarsByFileType["txt"]
grammarByFileTypeSuffix: (filePath) ->
for fileType, grammar of @grammarsByFileType
return grammar if _.endsWith(filePath, fileType)
grammarByShebang: (filePath) ->
try
fileContents = fs.read(filePath)
catch e
null
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(fileContents)
grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]
addProperties: (args...) ->
selector = args.shift() if args.length > 1

View File

@@ -9,50 +9,13 @@ module.exports =
class TextMateBundle
@grammarsByFileType: {}
@grammarsByScopeName: {}
@preferencesByScopeSelector: {}
@grammars: []
@load: (name)->
@load: (name) ->
bundle = new TextMateBundle(require.resolve(name))
for scopeSelector, preferences of bundle.getPreferencesByScopeSelector()
if @preferencesByScopeSelector[scopeSelector]?
_.extend(@preferencesByScopeSelector[scopeSelector], preferences)
else
@preferencesByScopeSelector[scopeSelector] = preferences
for grammar in bundle.grammars
@grammars.push(grammar)
for fileType in grammar.fileTypes
@grammarsByFileType[fileType] = grammar
@grammarsByScopeName[grammar.scopeName] = grammar
syntax.addGrammar(grammar) for grammar in bundle.grammars
bundle
@grammarForFilePath: (filePath) ->
return @grammarsByFileType["txt"] unless filePath
extension = fs.extension(filePath)?[1...]
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarsByFileType[extension] or @grammarByShebang(filePath) or @grammarByFileTypeSuffix(filePath) or @grammarsByFileType["txt"]
@grammarByFileTypeSuffix: (filePath) ->
for fileType, grammar of @grammarsByFileType
return grammar if _.endsWith(filePath, fileType)
@grammarByShebang: (filePath) ->
try
fileContents = fs.read(filePath)
catch e
null
_.find @grammars, (grammar) -> grammar.firstLineRegex?.test(fileContents)
@grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]
grammars: null
constructor: (@path) ->
@@ -64,25 +27,5 @@ class TextMateBundle
catch e
console.warn "Failed to load grammar at path '#{syntaxPath}'", e
getPreferencesByScopeSelector: ->
return {} unless fs.exists(@getPreferencesPath())
preferencesByScopeSelector = {}
for preferencePath in fs.list(@getPreferencesPath())
plist.parseString fs.read(preferencePath), (e, data) ->
if e
console.warn "Failed to parse preference at path '#{preferencePath}'", e
else
{ scope, settings } = data[0]
return unless scope
for scope in scope.split(',')
scope = $.trim(scope)
continue unless scope
preferencesByScopeSelector[scope] = _.extend(preferencesByScopeSelector[scope] ? {}, settings)
preferencesByScopeSelector
getSyntaxesPath: ->
fs.join(@path, "Syntaxes")
getPreferencesPath: ->
fs.join(@path, "Preferences")

View File

@@ -72,8 +72,7 @@ class TextMateGrammar
else if name == "$self" or name == "$base"
@initialRule
else
TextMateBundle = require 'text-mate-bundle'
TextMateBundle.grammarForScopeName(name)?.initialRule
syntax.grammarForScopeName(name)?.initialRule
class Rule
grammar: null

View File

@@ -71,4 +71,4 @@ class TextMatePackage extends Package
{ editor: editorProperties } if _.size(editorProperties) > 0
cssSelectorFromScopeSelector: (scopeSelector) ->
@constructor.cssSelectorFromScopeSelector(scopeSelector)
@constructor.cssSelectorFromScopeSelector(scopeSelector)