【问题标题】:Italicize JavaScript's reserved keywords in VS Code斜体化 VS Code 中 JavaScript 的保留关键字
【发布时间】:2018-06-29 22:39:46
【问题描述】:

我正在尝试通过TextMate language grammars 使用Visual Studio Code's theme settings 创建自定义语法样式。

具体来说,我想将 JavaScript 的所有保留关键字都用斜体表示。通过下面的设置,我已经成功完成了 98% 的路径(剩余部分包括 cmets)。

很遗憾,我找不到一些规则:

  1. storage 包含粗箭头符号,我确实不想 想要包含它。我试图更具体,如下面的设置所示,但无法找到constructorconst 的更具体设置。此外,"storage.type.function" 是我能找到的最明确的函数设置(function 关键字需要,但它包含粗箭头)。
  2. keyword 包含逻辑运算符等字符,我再次想要包含这些字符。 keyword.operator 是文本运算符所必需的(例如 ininstanceof),但包括字符运算符。
  3. 我找不到eval(严格禁止)或package(未使用的未来关键字)的规则。

有什么想法吗?

const settings = {
  "editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": [
          // TODO: missing keywords: package, eval

      // all comment types
      "comment",

      // true, false, null
      "constant.language",

      // import, from, export, default, return, if, for, break, continue, try, catch, finally,
      // throw, default, yield, await
      "keyword.control",

      // TODO: remove operator symbols
      // in, void, delete, instanceof
      "keyword.operator",

      // debugger
      "keyword.other",

      // new
      "keyword.operator.new",

      // enum
      "storage.type.enum",

      // super, this, arguments
      "variable.language",

      // attributes in html, jsx, etc.
      "entity.other.attribute-name",

      // TODO: remove storage.type after finding explicit for constructor, const, let, var
      "storage.type",

      // static, extends, async, private, public, implements
      "storage.modifier",

      // TODO: exclude fat arrow
      // function
      "storage.type.function",

      // class
      "storage.type.class",

      // interface
      "storage.type.interface",
    ],
    "settings": {
      "fontStyle": "italic"
    }
  },
]
  },
};

【问题讨论】:

    标签: javascript visual-studio-code textmate vscode-settings


    【解决方案1】:

    事实证明,在 VS Code 中,您可以轻松找到所需的范围。

    使用 ctrl/cmd + shift + p 打开命令搜索并搜索 Developer: Inspect TM scopes。然后,您可以单击要查找范围的任何符号/单词的左侧。


    回答我自己的问题:

    1. 到目前为止,constletvarfunction 关键字本身没有明确的范围(storage.type.function 包括保留字和箭头)。然而,函数箭头有一个明确的范围:storage.type.function.arrow。这允许我们包含整个 storage 范围,然后明确排除箭头。
    2. keyword.operator.expression 是仅表示为单词的运算符所需的范围。
    3. evalpackage 的特定范围尚不可用。您可以将evalsupport.function 定位,将packagevariable.other.readwrite 定位,但这些范围很广,将包括许多其他结果。

    话虽如此,下面列出了在 VS Code 中将所有 JavaScript 保留关键字斜体化所需的规则(还包括 cmets 和 jsx/html 属性):

    "editor.tokenColorCustomizations": {
    "textMateRules": [
      {
        "scope": [
          // all comment types
          "comment",
    
          // true, false, null
          "constant.language",
    
          // import, from, export, default, return, if, for, break, continue, try, catch, finally,
          // throw, default, yield, await
          "keyword.control",
    
          // in, void, delete, instanceof
          "keyword.operator.expression",
    
          // debugger
          "keyword.other",
    
          // new
          "keyword.operator.new",
    
          // super, this, arguments
          "variable.language",
    
          // attributes in html, jsx, etc.
          "entity.other.attribute-name",
    
          // static, extends, async, private, public, implements
          // constructor, const, let, var, enum, class, function, interface
          // no explicit scopes for constructor, const, let, var
          // also no explicit scope for function without the arrow
          // therefore we enable all storage and explictly exclude the arrow in another scope
          "storage",
    
          // // no explicit scope for the eval keyword yet
          // // can be targeted with the scope below, but scope is too broad
          // "support.function",
    
          // // no explicit scope for the package keyword yet
          // // can be targeted with the scope below, but scope is too broad
          // "variable.other.readwrite",
        ],
        "settings": {
          "fontStyle": "italic"
        }
      },
      {
        "scope": [
          // function keyword does not have an explicit scope without the arrow
          // therefore we explictly exclude the function arrow from being italicized
          "storage.type.function.arrow",
        ],
        "settings": {
          "fontStyle": ""
        }
      }
    ]
      }
    

    【讨论】:

    猜你喜欢
    • 2012-01-24
    • 2021-08-15
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多