Build GrammarRegistry in Atom environment constructor

As part of this, we no longer serialize/deserialize the grammar registry
instance, but instead just restore the grammarOverridesByPath state.
Ultimately I’d like to store these overrides on the editor instead.
This commit is contained in:
Nathan Sobo
2015-09-28 19:05:35 -06:00
parent c1f5d6b8bf
commit 27762866c0
5 changed files with 14 additions and 28 deletions

View File

@@ -226,13 +226,13 @@ describe "the `atom` global", ->
describe "::unloadEditorWindow()", ->
it "saves the serialized state of the window so it can be deserialized after reload", ->
workspaceState = atom.workspace.serialize()
syntaxState = atom.grammars.serialize()
grammarsState = {grammarOverridesByPath: atom.grammars.grammarOverridesByPath}
projectState = atom.project.serialize()
atom.unloadEditorWindow()
expect(atom.state.workspace).toEqual workspaceState
expect(atom.state.grammars).toEqual syntaxState
expect(atom.state.grammars).toEqual grammarsState
expect(atom.state.project).toEqual projectState
expect(atom.saveSync).toHaveBeenCalled()

View File

@@ -24,18 +24,9 @@ describe "the `grammars` global", ->
atom.packages.deactivatePackages()
atom.packages.unloadPackages()
describe "serialization", ->
it "remembers grammar overrides by path", ->
filePath = '/foo/bar/file.js'
expect(atom.grammars.selectGrammar(filePath).name).not.toBe 'Ruby'
atom.grammars.setGrammarOverrideForPath(filePath, 'source.ruby')
grammars2 = atom.deserializers.deserialize(atom.grammars.serialize())
grammars2.addGrammar(grammar) for grammar in atom.grammars.grammars when grammar isnt atom.grammars.nullGrammar
expect(grammars2.selectGrammar(filePath).name).toBe 'Ruby'
describe ".selectGrammar(filePath)", ->
it "always returns a grammar", ->
registry = new GrammarRegistry()
registry = new GrammarRegistry(config: atom.config)
expect(registry.selectGrammar().scopeName).toBe 'text.plain.null-grammar'
it "selects the text.plain grammar over the null grammar", ->

View File

@@ -771,7 +771,7 @@ describe "PackageManager", ->
atom.packages.unloadPackages()
GrammarRegistry = require '../src/grammar-registry'
atom.grammars = window.syntax = new GrammarRegistry()
atom.grammars = window.syntax = new GrammarRegistry(config: atom.config)
jasmine.restoreDeprecationsSnapshot()
it "activates all the packages, and none of the themes", ->

View File

@@ -201,6 +201,9 @@ class Atom extends Model
Clipboard = require './clipboard'
@clipboard = new Clipboard()
GrammarRegistry = require './grammar-registry'
@grammars = new GrammarRegistry({@config})
reset: ->
@config.reset()
@@ -237,7 +240,6 @@ class Atom extends Model
@loadTime = null
GrammarRegistry = require './grammar-registry'
{devMode, safeMode, resourcePath} = @getLoadSettings()
configDirPath = @getConfigDirPath()
@@ -254,7 +256,10 @@ class Atom extends Model
@registerViewProviders()
document.head.appendChild(new StylesElement)
@grammars = @deserializers.deserialize(@state.grammars ? @state.syntax) ? new GrammarRegistry()
if grammarOverridesByPath = @state.grammars?.grammarOverridesByPath
@grammars.grammarOverridesByPath = grammarOverridesByPath
@disposables.add @packages.onDidActivateInitialPackages => @watchThemes()
Project = require './project'
@@ -626,7 +631,7 @@ class Atom extends Model
return if not @project
@storeWindowBackground()
@state.grammars = @grammars.serialize()
@state.grammars = {grammarOverridesByPath: @grammars.grammarOverridesByPath}
@state.project = @project.serialize()
@state.workspace = @workspace.serialize()
@packages.deactivatePackages()

View File

@@ -14,19 +14,9 @@ PathSplitRegex = new RegExp("[/.]")
# language-specific comment regexes. See {::getProperty} for more details.
module.exports =
class GrammarRegistry extends FirstMate.GrammarRegistry
@deserialize: ({grammarOverridesByPath}) ->
grammarRegistry = new GrammarRegistry()
grammarRegistry.grammarOverridesByPath = grammarOverridesByPath
grammarRegistry
atom.deserializers.add(this)
constructor: ->
constructor: ({@config}={}) ->
super(maxTokensPerLine: 100)
serialize: ->
{deserializer: @constructor.name, @grammarOverridesByPath}
createToken: (value, scopes) -> new Token({value, scopes})
# Extended: Select a grammar for the given file path and file contents.
@@ -70,7 +60,7 @@ class GrammarRegistry extends FirstMate.GrammarRegistry
pathScore = -1
fileTypes = grammar.fileTypes
if customFileTypes = atom.config.get('core.customFileTypes')?[grammar.scopeName]
if customFileTypes = @config.get('core.customFileTypes')?[grammar.scopeName]
fileTypes = fileTypes.concat(customFileTypes)
for fileType, i in fileTypes