Sublime Forum

Syntax Highlighting, Filter Scopes

#1

In textMate, I can specify the scopes that the matches are injected to with: "injectionSelector": "L:meta.embedded.block.hsx - (source.js, source.css)", that means, inject into meta.embedded.block.hsx scope, but not source.js and not source.css .

How can I achieve something similar with sublime syntax?
I need my match to apply to all HTML content, except when the there is a script tag with JavaScript (source.js) or style tag with CSS (source.css).

0 Likes

#2

text.html.basic - source.js - source.css would work. Following a similar style to your TextMate example should also work: text.html.basic - (source.js, source.css)

1 Like

Does Sublime Text support the TextMate injection selector?
#3

Could you give me an example of how to apply this in practice, everything I tried has not worked.

0 Likes

#4

ST has no injectionSelector (at least I’ve never seen people use it and now ST has been moved to its own syntax definition format) but https://www.sublimetext.com/docs/syntax.html#inheritance

0 Likes

#5

Yeah, sorry, I was only helping out with the scope selector, didn’t pay much attention to what it was for. See also

0 Likes

#6

In this example, from (https://github.com/digitallyinduced/sublime-text-hsx/blob/main/Haskell-HSX.sublime-syntax)

  hsx:
    - match: ""
      push: Packages/HTML/HTML.sublime-syntax
      with_prototype:
        - match: \{
          pop: true # pop ^ HTML
          embed: Haskell-Improved.sublime-syntax
          escape: \}

The { is matched in all of the HTML code, including inside script and style tags:

<script>
function a() {
   // The brackets are matched, and Haskell syntax is embeded.
}
</script>

Is there a clean way to solve this issue? So the {, } are only matched in the text.html.basic scope, and not source.js or source.css, or is the only option to fork Packages/HTML/HTML.sublime-syntax?

0 Likes

#7

No need to fork it, just create a syntax which extends it

0 Likes

#8

Could you please link to example, or another .sublime-syntax that extends another syntax?
I have a hard time finding examples on how to use extends.

0 Likes

#10

I’m making some progress with:

%YAML 1.2
---
name: HSX
scope: source.hsx
version: 2
hidden: true

extends: Packages/HTML/HTML.sublime-syntax

contexts:
  entities:
    - meta_append: true
    - include: hsx-interpolation

  hsx-interpolation:
    - match: ({)
      captures:
        0: punctuation.section.braces.begin.hsx
      embed: Haskell-Improved.sublime-syntax
      escape: (})
      escape_captures:
        0: punctuation.section.braces.end.hsx

0 Likes

#11

Embedding Haskell this way results in no closing } being supported anywhere in the embedded code. Not sure if it is needed?

In order to support interpolation within html tags, you’d probably want to extend the following contexts (illustrated using HTML (JSP) from ST’s default Packages):

The following contexts are used within string values. Hence clear_scope: 1 should be applied to remove the string scope.

Note: If interpolation is to be supported within <script> tags or in inline-scripts or inline-styles (style="{...}" you’d also need to extend CSS/JS after the scheme of HTML (JSP)

0 Likes