Deprecate grammar override API on GrammarRegistry

This commit is contained in:
Max Brunsfeld
2016-07-29 13:42:09 -07:00
parent e2e245aca1
commit 97d791f728
2 changed files with 46 additions and 26 deletions

View File

@@ -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