How to turn off snippets in Atom?

2019-03-27 18:04发布

问题:

I've recently started using Atom. One problem I've run into is that there are too many / ambiguous snippets defined for Ruby. This makes tab completion worse, as you sometimes get a bit of irrelevant code instead of the name you wanted. I'm wondering how to turn off a specific snippet from the "Language Ruby" package, or failing that turning off all snippets. Preferably without disabling the Ruby package entirely.

回答1:

Sadly, there's currently no built-in feature for this kind of thing.

Until some filter feature is added to the snippets package, the only way to access the snippets is to monkey-patch the package from your init script.

For instance something like that will allow you to filter the snippets returned for a given editor at runtime:

# we need a reference to the snippets package
snippetsPackage = require(atom.packages.getLoadedPackage('snippets').path)

# we need a reference to the original method we'll monkey patch
__oldGetSnippets = snippetsPackage.getSnippets

snippetsPackage.getSnippets = (editor) ->
  snippets = __oldGetSnippets.call(this, editor)

  # we're only concerned by ruby files
  return snippets unless editor.getGrammar().scopeName is 'source.ruby'

  # snippets is an object where keys are the snippets's prefixes and the values
  # the snippets objects
  console.log snippets

  newSnippets = {}
  excludedPrefixes = ['your','prefixes','exclusion','list']

  for prefix, snippet of snippets
    newSippets[prefix] = snippet unless prefix in excludedPrefixes   

  newSnippets