From 97d791f7283ff165c52b73bc95ffac0a1e446965 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 29 Jul 2016 13:42:09 -0700 Subject: [PATCH] Deprecate grammar override API on GrammarRegistry --- spec/grammars-spec.coffee | 33 +++++++++++++++++++++++-------- src/grammar-registry.coffee | 39 ++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/spec/grammars-spec.coffee b/spec/grammars-spec.coffee index a36a10170..7dcff8bcd 100644 --- a/spec/grammars-spec.coffee +++ b/spec/grammars-spec.coffee @@ -2,6 +2,7 @@ path = require 'path' fs = require 'fs-plus' temp = require 'temp' GrammarRegistry = require '../src/grammar-registry' +Grim = require 'grim' describe "the `grammars` global", -> beforeEach -> @@ -85,14 +86,6 @@ describe "the `grammars` global", -> expect(atom.grammars.selectGrammar(filePath, filePathContents).name).toBe "Ruby" expect(fs.read).not.toHaveBeenCalled() - it "allows the default grammar to be overridden for a path", -> - filePath = '/foo/bar/file.js' - expect(atom.grammars.selectGrammar(filePath).name).not.toBe 'Ruby' - atom.grammars.setGrammarOverrideForPath(filePath, 'source.ruby') - expect(atom.grammars.selectGrammar(filePath).name).toBe 'Ruby' - atom.grammars.clearGrammarOverrideForPath(filePath) - expect(atom.grammars.selectGrammar(filePath).name).not.toBe 'Ruby' - describe "when multiple grammars have matching fileTypes", -> it "selects the grammar with the longest fileType match", -> grammarPath1 = temp.path(suffix: '.json') @@ -155,3 +148,27 @@ describe "the `grammars` global", -> grammar = atom.grammars.selectGrammar('foo.js') atom.grammars.removeGrammar(grammar) expect(atom.grammars.selectGrammar('foo.js').name).not.toBe grammar.name + + describe "grammar overrides", -> + it "logs deprecations and uses the TextEditorRegistry", -> + editor = null + + waitsForPromise -> + atom.workspace.open('sample.js').then (e) -> editor = e + + runs -> + spyOn(Grim, 'deprecate') + + atom.grammars.setGrammarOverrideForPath(editor.getPath(), 'source.ruby') + expect(Grim.deprecate.callCount).toBe 1 + expect(editor.getGrammar().name).toBe 'Ruby' + + expect(atom.grammars.grammarOverrideForPath(editor.getPath())).toBe('source.ruby') + expect(Grim.deprecate.callCount).toBe 2 + + atom.grammars.clearGrammarOverrideForPath(editor.getPath(), 'source.ruby') + expect(Grim.deprecate.callCount).toBe 3 + expect(editor.getGrammar().name).toBe 'JavaScript' + + expect(atom.grammars.grammarOverrideForPath(editor.getPath())).toBe(undefined) + expect(Grim.deprecate.callCount).toBe 4 diff --git a/src/grammar-registry.coffee b/src/grammar-registry.coffee index 87fa9b9a3..1876e1d74 100644 --- a/src/grammar-registry.coffee +++ b/src/grammar-registry.coffee @@ -3,6 +3,7 @@ _ = require 'underscore-plus' FirstMate = require 'first-mate' Token = require './token' fs = require 'fs-plus' +Grim = require 'grim' PathSplitRegex = new RegExp("[/.]") @@ -44,8 +45,6 @@ class GrammarRegistry extends FirstMate.GrammarRegistry # Extended: Returns a {Number} representing how well the grammar matches the # `filePath` and `contents`. getGrammarScore: (grammar, filePath, contents) -> - return Infinity if @grammarOverrideForPath(filePath) is grammar.scopeName - contents = fs.readFileSync(filePath, 'utf8') if not contents? and fs.isFileSync(filePath) score = @getGrammarPathScore(grammar, filePath) @@ -93,36 +92,40 @@ class GrammarRegistry extends FirstMate.GrammarRegistry lines = contents.split('\n') grammar.firstLineRegex.testSync(lines[0..numberOfNewlinesInRegex].join('\n')) - # Public: Get the grammar override for the given file path. + # Deprecated: Get the grammar override for the given file path. # # * `filePath` A {String} file path. # - # Returns a {Grammar} or undefined. + # Returns a {String} such as `"source.js"`. grammarOverrideForPath: (filePath) -> - @grammarOverridesByPath[filePath] + Grim.deprecate 'Use atom.textEditors.getGrammarOverride(editor) instead' + if editor = getEditorForPath(filePath) + atom.textEditors.getGrammarOverride(editor) - # Public: Set the grammar override for the given file path. + # Deprecated: Set the grammar override for the given file path. # # * `filePath` A non-empty {String} file path. # * `scopeName` A {String} such as `"source.js"`. # - # Returns a {Grammar} or undefined. + # Returns undefined setGrammarOverrideForPath: (filePath, scopeName) -> - if filePath - @grammarOverridesByPath[filePath] = scopeName + Grim.deprecate 'Use atom.textEditors.setGrammarOverride(editor, scopeName) instead' + if editor = getEditorForPath(filePath) + atom.textEditors.setGrammarOverride(editor, scopeName) + return - # Public: Remove the grammar override for the given file path. + # Deprecated: Remove the grammar override for the given file path. # # * `filePath` A {String} file path. # # Returns undefined. clearGrammarOverrideForPath: (filePath) -> - delete @grammarOverridesByPath[filePath] - undefined + Grim.deprecate 'Use atom.textEditors.clearGrammarOverride(editor) instead' + if editor = getEditorForPath(filePath) + atom.textEditors.clearGrammarOverride(editor) + return - # Public: Remove all grammar overrides. - # - # Returns undefined. - clearGrammarOverrides: -> - @grammarOverridesByPath = {} - undefined +getEditorForPath = (filePath) -> + if filePath? + atom.workspace.getTextEditors().find (editor) -> + editor.getPath() is filePath