Как сказать подсвечивателю синтаксиса текста для захвата одинарных и двойных кавычек? - PullRequest
0 голосов
/ 13 марта 2020

В возвышенном тексте 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

. По умолчанию он захватывает двойные кавычки через

    - 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

Результат не go хорошо, когда двойная кавычка внутри одной цитаты:

Syntax Result

Как это исправить?

1 Ответ

0 голосов
/ 16 марта 2020

Измените свои правила следующим образом:

- 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 может обрабатывать как одинарные, так и двойные кавычки, поэтому, если вам нужно использовать одно или другое в регулярное выражение, используйте другой, чтобы окружить регулярное выражение.

...