如何告诉 sublime-text 语法荧光笔捕获单引号和双引号?

How to tell sublime-text syntax highlighter to capture both single quotes and double quotes?

在 sublime-text 3 中,从 Tools > Developer > New Syntax ... 的菜单中,我找到了这个默认代码

%YAML 1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
file_extensions:
  - ec
scope: source.example-c
contexts:
  main:
    # Strings begin and end with quotes, and use backslashes as an escape
    # character
    - match: '"'
      scope: punctuation.definition.string.begin.example-c
      push: double_quoted_string

    # Comments begin with a '//' and finish at the end of the line
    - match: '//'
      scope: punctuation.definition.comment.example-c
      push: line_comment

    # Keywords are if, else for and while.
    # Note that blackslashes don't need to be escaped within single quoted
    # strings in YAML. When using single quoted strings, only single quotes
    # need to be escaped: this is done by using two single quotes next to each
    # other.
    - match: '\b(if|else|for|while)\b'
      scope: keyword.control.example-c

    # Numbers
    - match: '\b(-)?[0-9.]+\b'
      scope: constant.numeric.example-c

  double_quoted_string:
    - meta_scope: string.quoted.double.example-c
    - match: '\.'
      scope: constant.character.escape.example-c
    - match: '"'
      scope: punctuation.definition.string.end.example-c
      pop: true

  line_comment:
    - meta_scope: comment.line.example-c
    - match: $
      pop: true

通过它默认捕获的是双引号via

    - match: '"'
      scope: punctuation.definition.string.begin.example-c
      push: double_quoted_string

  double_quoted_string:
    - meta_scope: string.quoted.double.example-c
    - match: '\.'
      scope: constant.character.escape.example-c
    - match: '"'
      scope: punctuation.definition.string.end.example-c
      pop: true

如果我在我的代码中添加这个:

    - match: ''''
      scope: punctuation.definition.string.begin.example-c
      push: single_quoted_string

  single_quoted_string:
    - meta_scope: string.quoted.single.example-c
    - match: '\.'
      scope: constant.character.escape.example-c
    - match: '"'
      scope: punctuation.definition.string.end.example-c
      pop: true

双引号在单引号内时效果不佳:

我该如何解决?

将您的规则更改为:

- match: "'"
  scope: punctuation.definition.string.begin.example-c
  push: single_quoted_string

还有这个

  single_quoted_string:
    - meta_scope: string.quoted.single.example-c
    - match: '\.'
      scope: constant.character.escape.example-c
    - match: "'"
      scope: punctuation.definition.string.end.example-c
      pop: true

YAML 可以处理单引号和双引号,因此如果您需要在正则表达式中使用一个或另一个,请使用另一个将正则表达式括起来。