Open .rb files with RoR grammar by default instead of Ruby


#1

Hey,

I’ve been trying to figure out how to make all .rb files to open with the Ruby on Rails grammar by default rather than Ruby. I know the ctrl+shift+L trick but it’s only on a file by file basis. I’ve also had a look at the file types package but didn’t understand how to set it up for that particular case.

Thanks for your help!


#2

You can indeed use the file-types package for something like that.
To set it up you have to open your config.cson:

  • File - “Open your Config” on Windows
  • Edit - “Open your Config” on Linux (probably OS X as well)

And then add this to the file:

'file-types':
  'rb': 'source.ruby.rails'

#3

It works! Awesome, thanks.


#4

Actually, no, it doesn’t work. Only the files I previously manually changed to Ruby on Rails open automatically with that grammar.

What am I doing wrong?

Thanks


#5

Weird, it does work for me on .h files which Atom highlighted as C instead of C++. So I don’t really know what’s wrong.


I think part of the underlying problem is that ‘rb’ is in the fileTypes of both the ruby and ruby-on-rails packages (same with ‘h’ for C and C++), so I’m not entirely sure how that is supposed to work. It doesn’t make much sense to have the extension in both languages if Atom always loads the first/last/whatever one anyway.

A package as file-types kind of resolves that by always selecting a set grammar. But I don’t think that completely solves the issue if you are frequently switching between e.g. Ruby and Ruby on Rails projects or files.
Ideally, shouldn’t Atom (or a package, but there are such conflicts in the core language packages as well) maybe select a grammar based on the file extension and, when there are multiple possibilities defined, the contents of the file? I don’t really know how this is done in other editors/IDE’s though so maybe I’m wrong on that.


#6

Atom does have a system for selecting a grammar based on the contents of the file. You can see it in action on Unix-style shell scripts which have no extension but contain a shebang line.

But there are a number of ways this experience could be improved. Check out the first-mate package for how Atom works currently and if you have suggestions for improving it, please chime in!


#7

AFAIK, for shell scripts that is accomplished by the ‘firstLineMatch’ key in the grammar.cson file. That approach is not really applicable for a whole lot of other languages though. Does Atom do anything more than that in terms of content checking? If so, I will have to investigate this a bit more. :smile:


#8

Might be useful to allow cascading grammars, @leedohm. Many syntaxes are built on top of other syntaxes, such as SLIM accepting valid ruby within certain characters. Others just add specific styles, such as jUnit or Rspec.

Might be worth discussing in a different topic?


#9

As @abe points out, Atom already supports cascading grammars:


#10

Doh. Thank you. I’ll go nag/open pull requests against the languages I use to start making use of it