How do I make a default syntax by filetype in Atom text editor?

对着背影说爱祢 提交于 2019-12-02 17:37:01
Kyle Kelley

Easy mode: include

If your language really is just HTML, you can set up a simple package to handle this.

Create a package called langugage-ejs and in grammars/ejs.cson you can include HTML as having the patterns you care about:

'fileTypes': [
  'ejs'
]

'name': 'Embedded JavaScript'

'patterns': [
  {
    'include': 'source.html'
  }
]

'scopeName': 'source.ejs'

language-ipynb certainly does this by extending JSON.

What about my template tags?

In reality though, you have template tags on top of HTML that you would want to make the editor recognize. The best example I can find is for erb (Embedded Ruby templates). It sources from HTML but also adds on other tags as shown in this snippet:

...
'patterns': [
  {
    'begin': '<%+#'
    'captures':
      '0':
        'name': 'punctuation.definition.comment.erb'
    'end': '%>'
    'name': 'comment.block.erb'
  }
  {
    'begin': '<%+(?!>)[-=]?'
    'captures':
      '0':
        'name': 'punctuation.section.embedded.ruby'
    'end': '-?%>'
    'name': 'source.ruby.rails.embedded.html'
    'patterns': [
      {
        'captures':
          '1':
            'name': 'punctuation.definition.comment.ruby'
        'match': '(#).*?(?=-?%>)'
        'name': 'comment.line.number-sign.ruby'
      }
      {
        'include': 'source.ruby.rails'
      }
    ]
  }
  {
    'include': 'text.html.basic'
  }
]
...
timseal

This is now in Atom core, you don't need that file-types package any more.

Where I work, we use .phl for a certain kind of PHP file. To tell Atom about this, edit config.cson like so:

core:
    customFileTypes:
      "text.html.php": [
        "phl"
      ]
    themes: [
// snip

You can find this file at ~/.atom/config.cson, or from the Settings window click the "Open Config Folder" button.

It's easy to get wrong, as evidenced by the many errors people made on the GitHub issue for the feature.

Ábel Hegedüs

Edit: since the time I wrote this answer, this functionality has been added to Atom core, see this answer for details.

file-types Atom package

(https://atom.io/packages/file-types)

It does exactly what the title question asks for: you can define new file extensions for existing languages with a simple edit of the config.cson file.

Jesse

For individual files you can use the Grammar Selector; Ctrl+Shift+L to set which language you're using on that file. Not the same as auto-detect but useful for those times when you're using a file that you don't want to set a default for.

It looks likes there's a brand new package has been released for this. https://atom.io/packages/language-ejs.

For example if you want to open all .jsx files with javascript syntax, you need to add this to your config.cson

"*":
  core:
    customFileTypes:
      "source.js": [
         "jsx"
      ]

This maps all .jsx files to open with js syntax.

As Dave Andersen mentions in a buried comment, it is now possible to do this without any extra packages and it's documented here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!