首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何告诉sublime-text语法高亮工具同时捕获单引号和双引号?

如何告诉sublime-text语法高亮工具同时捕获单引号和双引号?
EN

Stack Overflow用户
提问于 2020-03-13 15:39:47
回答 1查看 266关注 0票数 0

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

代码语言:javascript
复制
%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

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

代码语言:javascript
复制
    - match: '"'
      scope: punctuation.definition.string.begin.example-c
      push: double_quoted_string

代码语言:javascript
复制
  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

如果我在代码中添加以下代码:

代码语言:javascript
复制
    - match: ''''
      scope: punctuation.definition.string.begin.example-c
      push: single_quoted_string

代码语言:javascript
复制
  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

当双引号包含在单引号内时,结果不会很好:

我怎么才能修复它呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-17 03:48:08

将您的规则更改为:

代码语言:javascript
复制
- match: "'"
  scope: punctuation.definition.string.begin.example-c
  push: single_quoted_string

还有这个

代码语言:javascript
复制
  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既可以处理单引号,也可以处理双引号,所以如果需要在正则表达式中使用其中一个,请使用另一个将正则表达式括起来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60666584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档