From db4420e068f6f56b55e11eb8e72c03e7f6c375cb Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 21 Mar 2013 16:54:49 -0600 Subject: [PATCH] Move grammar overrides to syntax (no serialization yet) --- spec/app/editor-spec.coffee | 12 +++--------- spec/app/project-spec.coffee | 5 +---- spec/app/syntax-spec.coffee | 7 +++++++ spec/spec-helper.coffee | 1 + src/app/grammar-view.coffee | 4 ++-- src/app/language-mode.coffee | 5 +---- src/app/project.coffee | 18 ++---------------- src/app/syntax.coffee | 16 +++++++++++++++- .../status-bar/spec/status-bar-spec.coffee | 5 ++--- 9 files changed, 34 insertions(+), 39 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 63a9daa6a..48647ef19 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -2036,14 +2036,10 @@ describe "Editor", -> it "updates all the rendered lines when the grammar changes", -> editor.edit(project.buildEditSession(path)) - expect(editor.getGrammar().name).toBe 'Plain Text' - jsGrammar = syntax.selectGrammar('/tmp/js.js') - expect(jsGrammar.name).toBe 'JavaScript' - - project.addGrammarOverrideForPath(path, jsGrammar) + syntax.setGrammarOverrideForPath(path, 'source.js') expect(editor.reloadGrammar()).toBeTruthy() - expect(editor.getGrammar()).toBe jsGrammar + expect(editor.getGrammar().name).toBe 'JavaScript' tokenizedBuffer = editor.activeEditSession.displayBuffer.tokenizedBuffer line0 = tokenizedBuffer.lineForScreenRow(0) @@ -2067,10 +2063,8 @@ describe "Editor", -> expect(eventHandler).not.toHaveBeenCalled() - jsGrammar = syntax.selectGrammar('/tmp/js.js') - project.addGrammarOverrideForPath(path, jsGrammar) + syntax.setGrammarOverrideForPath(path, 'source.js') editor.reloadGrammar() - expect(eventHandler).toHaveBeenCalled() describe ".replaceSelectedText()", -> diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 205b5b188..48f483fc5 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -260,9 +260,6 @@ describe "Project", -> range: [[1, 3], [1, 5]] describe "serialization", -> - it "restores the project path and grammar overrides", -> - jsGrammar = _.find syntax.grammars, (grammar) -> grammar.name is 'JavaScript' - project.addGrammarOverrideForPath('/a/b.txt', jsGrammar) + it "restores the project path", -> newProject = Project.deserialize(project.serialize()) expect(newProject.getPath()).toBe project.getPath() - expect(newProject.grammarOverrideForPath('/a/b.txt')).toBe jsGrammar diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index f2490dcbb..d800eb37f 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -30,6 +30,13 @@ describe "the `syntax` global", -> expect(syntax.selectGrammar(filePath, filePathContents).name).toBe "Ruby" expect(fs.read).not.toHaveBeenCalled() + it "allows the default grammar to be overridden for a path", -> + path = '/foo/bar/file.js' + expect(syntax.selectGrammar(path).name).not.toBe 'Ruby' + syntax.setGrammarOverrideForPath(path, 'source.ruby') + expect(syntax.selectGrammar(path).name).toBe 'Ruby' + syntax.clearGrammarOverrideForPath(path) + expect(syntax.selectGrammar(path).name).not.toBe 'Ruby' describe ".getProperty(scopeDescriptor)", -> it "returns the property with the most specific scope selector", -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 9ac81274d..27ada9ad4 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -41,6 +41,7 @@ beforeEach -> spyOn(atom, 'saveWindowState') spyOn(atom, 'getSavedWindowState').andReturn(null) $native.setWindowState('') + syntax.clearGrammarOverrides() # used to reset keymap after each spec bindingSetsToRestore = _.clone(keymap.bindingSets) diff --git a/src/app/grammar-view.coffee b/src/app/grammar-view.coffee index 22963375b..5c1638057 100644 --- a/src/app/grammar-view.coffee +++ b/src/app/grammar-view.coffee @@ -48,9 +48,9 @@ class GrammarView extends SelectList confirmed: (grammar) -> @cancel() if grammar is @autoDetect - project.removeGrammarOverrideForPath(@path) + syntax.clearGrammarOverrideForPath(@path) else - project.addGrammarOverrideForPath(@path, grammar) + syntax.setGrammarOverrideForPath(@path, grammar.scopeName) @editor.reloadGrammar() attach: -> diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 7f59e33e8..dc13fae18 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -17,10 +17,7 @@ class LanguageMode path = @buffer.getPath() pathContents = @buffer.cachedDiskContents previousGrammar = @grammar - if @buffer.project? - @grammar = @buffer.project.selectGrammar(path, pathContents) - else - @grammar = syntax.selectGrammar(path, pathContents) + @grammar = syntax.selectGrammar(path, pathContents) throw new Error("No grammar found for path: #{path}") unless @grammar previousGrammar isnt @grammar diff --git a/src/app/project.coffee b/src/app/project.coffee index b42abbca9..d6651e727 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -13,7 +13,7 @@ class Project registerDeserializer(this) @deserialize: (state) -> - new Project(state.path, state.grammarOverridesByPath) + new Project(state.path) tabLength: 2 softTabs: true @@ -21,9 +21,8 @@ class Project rootDirectory: null editSessions: null ignoredPathRegexes: null - grammarOverridesByPath: null - constructor: (path, @grammarOverridesByPath={}) -> + constructor: (path) -> @setPath(path) @editSessions = [] @buffers = [] @@ -31,23 +30,10 @@ class Project serialize: -> deserializer: 'Project' path: @getPath() - grammarOverridesByPath: @grammarOverridesByPath destroy: -> editSession.destroy() for editSession in @getEditSessions() - addGrammarOverrideForPath: (path, grammar) -> - @grammarOverridesByPath[path] = grammar.scopeName - - removeGrammarOverrideForPath: (path) -> - delete @grammarOverridesByPath[path] - - grammarOverrideForPath: (path) -> - syntax.grammarForScopeName(@grammarOverridesByPath[path]) - - selectGrammar: (path, contents) -> - @grammarOverrideForPath(path) or syntax.selectGrammar(path, contents) - getPath: -> @rootDirectory?.path diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index a80aba43e..9ce8fd71a 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -14,6 +14,7 @@ class Syntax @grammars = [] @grammarsByFileType = {} @grammarsByScopeName = {} + @grammarOverridesByPath = {} @globalProperties = {} @scopedPropertiesIndex = 0 @scopedProperties = [] @@ -25,13 +26,26 @@ class Syntax @grammarsByFileType[fileType] = grammar @grammarsByScopeName[grammar.scopeName] = grammar + setGrammarOverrideForPath: (path, scopeName) -> + @grammarOverridesByPath[path] = scopeName + + clearGrammarOverrideForPath: (path) -> + delete @grammarOverridesByPath[path] + + clearGrammarOverrides: -> + @grammarOverridesByPath = {} + selectGrammar: (filePath, fileContents) -> return @grammarsByFileType["txt"] ? @nullGrammar unless filePath - @grammarByFirstLineRegex(filePath, fileContents) ? + @grammarOverrideForPath(filePath) ? + @grammarByFirstLineRegex(filePath, fileContents) ? @grammarByPath(filePath) ? @grammarsByFileType["txt"] ? @nullGrammar + grammarOverrideForPath: (path) -> + @grammarsByScopeName[@grammarOverridesByPath[path]] + grammarByPath: (path) -> pathComponents = path.split(pathSplitRegex) for fileType, grammar of @grammarsByFileType diff --git a/src/packages/status-bar/spec/status-bar-spec.coffee b/src/packages/status-bar/spec/status-bar-spec.coffee index 006885331..85180426b 100644 --- a/src/packages/status-bar/spec/status-bar-spec.coffee +++ b/src/packages/status-bar/spec/status-bar-spec.coffee @@ -178,10 +178,9 @@ describe "StatusBar", -> describe "when the editor's grammar changes", -> it "displays the new grammar of the editor", -> - textGrammar = _.find syntax.grammars, (grammar) -> grammar.name is 'Plain Text' - project.addGrammarOverrideForPath(editor.getPath(), textGrammar) + syntax.setGrammarOverrideForPath(editor.getPath(), 'text.plain') editor.reloadGrammar() - expect(statusBar.find('.grammar-name').text()).toBe textGrammar.name + expect(statusBar.find('.grammar-name').text()).toBe 'Plain Text' describe "when clicked", -> it "toggles the editor:select-grammar event", ->