【问题标题】:Sublime Text 3 syntax definition: how to capture a multiline C-style block comment?Sublime Text 3 语法定义:如何捕获多行 C 样式的块注释?
【发布时间】:2026-02-15 20:10:02
【问题描述】:

所以目标块是这样的:

/* some comments
   more
   end  */

/**/ 之间的任何内容都被视为评论。我当前的规则只捕获单行 cmets:

match: \/\*(\w|\s|\W|\n|\r\n)*\*\/

我知道 Sublime Text 使用 Oniguruma,但我不确定如何匹配多行。

【问题讨论】:

  • ST3自带的语法定义有什么问题?
  • 这适用于还没有 ST 语法定义的自定义文件类型。对于 cmets,它使用 C 风格的块注释。
  • 然后从C语法定义中复制一份?这就是我在为新语言添加语法定义时所做的。
  • 嗯。好点子。看起来.sublime-package 只是一个 zip 文件,所以我现在正在研究它。

标签: regex sublimetext sublimetext3


【解决方案1】:

我在使用 ST3 时遇到了同样的问题,但使用了 .sublime-syntax 文件。不同之处在于,在我的语言中,block cmets 以!**! 为界。内联注释以! 开头。

这是我使用的解决方案:

contexts:
  # The prototype context is prepended to all contexts but those setting
  # meta_include_prototype: false.
  prototype:
    - include: comments
  comments:
    # Block comments begin with !* and ends with *!
    - match: '!\*'
      scope: punctuation.definition.comment.c
      push: 
        - meta_scope: comment.block.c
        - match: '\*!'
          pop: true
    # Inline comments begin with a '!' and finish at the end of the line.
    - match: '![^\*]'
      scope: punctuation.definition.comment.c
      push:
        # This is an anonymous context push for brevity.
        - meta_scope: comment.line.double-slash.c
        - match: $\n?
          pop: true

【讨论】:

    【解决方案2】:

    好的。所以Joachim Pileborg 的评论很有帮助。以下是我的做法。

    方法

    1- 安装PackageDev 包。

    2- 将相关包复制到一个临时目录并解压。对于C,我这样做了:

    cp /Applications/Sublime Text.app/Contents/MacOS/Packages/C++.sublime-package ~/tmp
    cd ~/tmp
    unzip C++.sublime-package
    

    解压会创建很多*.tmLanguage文件。

    3- 在 Sublime 中打开 C.tmLanguage

    4- 从命令面板中选择Build with: covert to ... YAML (Block Style),这将创建C.YAML-tmLanguage

    5- 然后打开YAML 文件并复制您需要的正则表达式。

    回答

    对于 C 样式的块,此规则有效:

    - begin: /\*
      captures:
        '0':
          name: punctuation.definition.comment.mn
      end: \*/
      name: comment.block.c
    

    【讨论】: