在VS Code中,“normal code/text”使用的是什么TextMate范围?

huangapple go评论70阅读模式
英文:

What TextMate scope is used for "normal code/text" in VS Code?

问题

我正在尝试为一种语言创建TextMate语法,该语言中的任何字符串都可以包含插值代码部分。因此,在任何字符串中,位于<%%>之间的内容都被视为“正常”代码,它可以再次包含字符串。

我想要将这个语法用于VS Code扩展。

我的多行字符串由<<->>括起来。

我认为在任意深度处理这种情况可能不太可能,所以我希望它能在简单的情况下工作,嵌套的深度最多为某个深度。

以下是以下代码的模式会是什么样子的?

<<
这是一些字符串
<%这是正常代码
  <<
  这是更多<%正常代码%>字符串<%更多代码%>
  >>%>
当然还有更多字符串
>>

我尝试使用以下myLanguage.tmGrammar.yaml

name: myLanguage
scopeName: source.myLanguage

patterns:
  - begin: <<
    end: ">>"
    name: string.quoted.double
    patterns:
        - begin: <%
          end: "%>"
          include: source.myLanguage

  - begin: <%
    end: "%>"
    name: variable  # <--- "default code"的名称是什么?
    patterns:
        - begin: <<
          end: ">>"
          include: source.myLanguage

它基本上有效,但我需要使用“正常”代码而不是keyword突出显示。

英文:

I'm trying to create a TextMate grammar for a language that can have interpolated code sections in any string. So everything between <% and %> inside any string is "normal" code, which can have strings again.
I want to use the grammar for a VS Code extension.

My multi-line strings are enclosed by <<->>.

I think it is not possible to handle this case for arbitrary depth, so I would be happy to get it to work for simple cases with nesting up to some depth.

What would the patterns look like for the following code?

<<
This is some string
<%This is normal code
  <<
  This is more <%Normal code%>string <%More code%>
  >>%>
And of course more string
>>

I tried to use the following myLanguage.tmGrammar.yaml:

name: myLanguage
scopeName: source.myLanguage

patterns:
  - begin: <<
    end: ">>"
    name: string.quoted.double
    patterns:
        - begin: <%
          end: "%>"
          include: source.myLanguage

  - begin: <%
    end: "%>"
    name: variable  # <--- What is the name for "default code"?
    patterns:
        - begin: <<
          end: ">>"
          include: source.myLanguage

在VS Code中,“normal code/text”使用的是什么TextMate范围?

It mostly works, but I need to use "normal" code instead of keyword highlighting.

答案1

得分: 0

我遵循了建议,并查看了JavaScript的TestMate语法,并为我的语言调整了反引号字符串。

以下是生成的语法:

name: myLanguage
scopeName: source.myLanguage

patterns:
  - include: '#strings'

repository:
  strings:
    begin: '<<'
    end: '>>'
    name: string.quoted.double
    patterns:
      - include: '#interpolation'
  interpolation:
    begin: '<%'
    end: '%>'
    contentName: source.myLanguage
    name: meta.embedded
    patterns:
      - include: $self

正常代码部分被突出显示为普通文本。
<%-%>块内的“正常代码”部分上,我能够进行更多的突出显示。

英文:

I followed the advice and looked at the TestMate grammar of JavaScript and adapted the backtick strings for my language.

Here is the resulting grammar:

name: myLanguage
scopeName: source.myLanguage

patterns:
  - include: &#39;#strings&#39;

repository:
  strings:
    begin: &#39;&lt;&lt;&#39;
    end: &#39;&gt;&gt;&#39;
    name: string.quoted.double
    patterns:
      - include: &#39;#interpolation&#39;
  interpolation:
    begin: &#39;&lt;%&#39;
    end: &#39;%&gt;&#39;
    contentName: source.myLanguage
    name: meta.embedded
    patterns:
      - include: $self

在VS Code中,“normal code/text”使用的是什么TextMate范围?

The normal code part is highlighted as normal text.
Expanding on this I was able to do more highlighting within the "normal code" sections inside &lt;%-&amp;&gt;-blocks.

huangapple
  • 本文由 发表于 2023年6月29日 00:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定