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

前端 未结 7 586
暗喜
暗喜 2021-01-31 02:17

I want my .ejs files to have html syntax, however, it always opens the files as Plain Text.

In sublime you can choose \"Open all with current extension as...\" then ch

7条回答
  •  生来不讨喜
    2021-01-31 02:45

    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'
      }
    ]
    ...
    

提交回复
热议问题