Force grammar usage for file extension

Hi,

I maintain the language-smarty-php package available here: https://github.com/martin-damien/language-smarty-php.

As you can see in the grammar.cson file, smarty files ends with .tpl or .smarty. But since some times, .tpl files are not associated to the package anymore but with language-html (who define .tpl too).

Is there any way to force .tpl association (letting language-html handling it when when not installed) ?

Or at least is there a way to force association in init.coffee or config.cson ?

Thanks,

Damien

This doesn’t say it can be used for your explicit use-case, but you can give it a shot: https://atom.io/packages/file-types

Thanks @dannyfritz but I just tryed and the detected grammar is still language-html

In init.coffee there is a sample in comments:

atom.workspace.observeTextEditors (editor) ->
  editor.onDidSave ->
    console.log "Saved! #{editor.getPath()}"

Does someone know how to detect the file extension and how to force grammar for the current editor?

Have you tried something along the lines of the code in this post?

You could use the IO.js path module to get the extension and decide whether to change/force the grammar at that point.

1 Like

Thank you @leedohm!

Using this example, I was able to do what I want adding this piece of code to init.coffee:

path = require 'path'

atom.workspace.observeTextEditors (editor) ->
    if path.extname(editor.getPath()) == ".tpl"
        editor.setGrammar(atom.grammars.grammarForScopeName('source.smarty'))

This is a good fix for the problem, but maybe it would be usefull to be set natively (selecting grammar for specific file extensions) in Atom.

Thanks,

Damien

2 Likes

The request for a feature to override grammars is being tracked here: