Changing a files "meta" scope


#1

Is it possible to change a file’s ‘meta’ scope from within the editor?

For example, in the rails-snippets package, it limits the scope snippets to files with the meta.rails.model scope. However, I have model files that don’t get that scope for some reason and so the snippets no longer work which can be a bit of a productivity killer once the snippets are in muscle memory.

Also, where can I find the place that determines the scopes for a file so I can see if there is a possible resolution to the underlying issue in this case?


#2

I managed to find the definition for the meta.rails.model scope here https://github.com/atom/language-ruby-on-rails/blob/master/grammars/ruby%20on%20rails.cson#L71

I suppose my next question is whether grammars can only be specified based on content regexes or whether file names/locations can be taken into account?

In my case the default grammar definition will miss the vast majority of my models as they don’t directly inherit from ActiveRecord::Base. Being able to specify that any files matching app/models/**/*.rb should include the meta.rails.model grammar would solve that issue and ensure that snippets don’t randomly stop working in files in which they are needed.


#3

The closest I’ve got is the following in my ~/atom/init.coffee:

atom.workspace.observeTextEditors (editor) ->
  editorPath = editor.getPath()
  ext = path.extname(editorPath)
  if ext == '.rb'
    if /app\/(models)/.test(editorPath)
      # editor.setGrammar(atom.grammars.grammarForScopeName('meta.rails.model'))

However, I need to be able to set the editor to a specific pattern within the grammar to enable the snippets. Is there a method for that?


#4

What do you mean with this?

There’s also the file-types package set a grammar for specific extensions. Does that do what you want?


#5

Typically, you want to declare snippets for the top-level scope for a grammar unless you have a specific reason for restricting it further. It’s just easier that way :grinning: For example, if I want a snippet to work in Ruby code, I would declare something like this:

'.source.ruby':
  Before:
    prefix: 'bef'
    body: 'before { $0 }'

Now, yes, technically this snippet is only for RSpec specifications. But declaring it as a Ruby snippet is good enough.

Also, typically the top-level scope for a grammar is either .source.language or .text.language, indicating whether the language is for source code or text: .source.ruby or .text.html. Of course, these are more like guidelines … than actual rules per se … so they’re broken sometimes. But they’re always two entries … and meta has a specific meaning when it comes to scopes (see here) … so I definitely wouldn’t restrict my snippets to just that.

Anyway … I need to run off … I hope that gets you started.


#6

Thanks! So it appears the core issue with snippets is that the rails-snippets package is limiting it’s snippets based on the meta scopes provided by the language-ruby-on-rails package.

This still doesn’t solve the problem that my model files don’t get the correct meta scope but at least snippets would be working if the above was fixed.


#7

The term is “scope” not “meta scope”. A specific section of the text in a file may be marked with the scope of meta, but an entire file is only given a “scope”.


#8

Thanks for the responses. I think what tripped me up was the “official” ruby-on-rails language package where the model snippets are tied to a specific “meta scope”: https://github.com/atom/language-ruby-on-rails/blob/master/snippets/language-ruby-on-rails.cson#L399

The snippets in the rails-snippets package work ok as they are on the “source.ruby.rails” scope but it seems as though there was possibly a conflict or something where they were also not working in my model files until after restarting Atom.