Default syntax in case of conflicting file extensions


#1

When two language packages define syntax and snippets for files with the same file extension, what determines the precedence?

For example, both language-ruby and language-ruby-on-rails are available by default, as they are included in the so-called Core Package set, and the two packages share the .rb file extension.

How can I make sure that Atom will by default treat .rb file as, say, source.ruby.rails instead of source.ruby files in my projects?

[Incidentally, I have two Rails projects (folders) open in Atom. In one of them, Ruby is the default grammar for .rb files, but it is Rails for the other project. And I have no idea of why…]

Thanks,
Giuseppe


#2

There are a couple different ways that Atom determines the type of the file, not just the file extension. There is also the “first line match” that detects things like shebang lines. First line matches take precedence (last I checked) over file extensions. And user selection or user rules take precedence over first line matches.


#3

I cannot seem to find documentation on this.

I am assuming that “user selection” corresponds to the manual choice of a grammar (Edit -> Select Grammar). It works, but only for the currently open file, not for the entire project.

Is a “user rule” a higher-level method? Could you please point out the relevant documentation on how to inspect/set such rules?


#4

That’s because there isn’t documentation of this currently that I’ve seen. Yes, user selection is the manual selection of a grammar. User rules are something that was added to Atom very recently in this Pull Request:

This added the ability to add entries like this to your config.cson to create user rules for grammar selection:

'*':
  core:
    customFileTypes:
      'source.ruby': ['Cheffile', 'this-is-also-ruby']
      'source.cpp': ['h']

#5

Thanks!
This indeed works at a global level.

On the other hand, is there a way to set defaults on a per-project basis?

I tried to apply your suggestion in the context of the project-manager package, by removing customFileTypes from config.cson and writing the following in projects.cson:

Railing:
  title: "My Rails project"
  paths: [
    "/Users/giuseppe/dev/rails/railing"
  ]
  settings:
    "*"
      core:
        customFileTypes:
          "source.ruby.rails": [
            "rb"
          ]

Rubyist:
  title: "My Ruby Project"
  paths: [
    "/Users/giuseppe/dev/ruby/rubyist"
  ]
  settings:
    "*"
      core:
        customFileTypes:
          "source.ruby": [
            "rb"
          ]

But now the Rails grammar takes precedence even for the plain Ruby project.


#6

Atom doesn’t natively support per-project settings. Unfortunately, I don’t use the project-manager package, so I don’t know how to make this work with that.

On the other hand, I’ve submitted a PR to add documentation for the new feature: