jjzjj

javascript - 在 VS Code 中斜体化 JavaScript 的保留关键字

coder 2024-12-07 原文

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

具体来说,我想将所有 JavaScript 的保留关键字设为斜体。我已经通过以下设置成功完成了 98% 的工作(剩下的内容包含评论)。

不幸的是,有一些规则我没能找到:

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

有什么想法吗?

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"
    }
  },
]
  },
};

最佳答案

事实证明,在 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.functionpackagevariable.other.readwrite 结合,但这些范围很广并将包括许多其他结果。

话虽如此,下面列出了在 VS Code 中将所有 JavaScript 保留关键字斜体化所需的规则(还包括注释和 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": ""
    }
  }
]
  }

关于javascript - 在 VS Code 中斜体化 JavaScript 的保留关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51110201/

有关javascript - 在 VS Code 中斜体化 JavaScript 的保留关键字的更多相关文章

  1. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  2. ruby - Ruby 的 AST 中的 'send' 关键字是什么意思? - 2

    我正在尝试学习Ruby词法分析器和解析器(whitequarkparser)以了解更多有关从Ruby脚本进一步生成机器代码的过程。在解析以下Ruby代码字符串时。defadd(a,b)returna+bendputsadd1,2它导致以下S表达式符号。s(:begin,s(:def,:add,s(:args,s(:arg,:a),s(:arg,:b)),s(:return,s(:send,s(:lvar,:a),:+,s(:lvar,:b)))),s(:send,nil,:puts,s(:send,nil,:add,s(:int,1),s(:int,3))))任何人都可以向我解释生成的

  3. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  4. ruby - 为什么 return 关键字会导致我的 'if block' 出现问题? - 2

    下面的代码工作正常:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson)do|key,oldv,newv|ifkey==:aoldvelsifkey==:bnewvelsekeyendendputskerson.inspect但是如果我在“ifblock”中添加return,我会得到一个错误:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson

  5. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

  6. ruby - 在 Ruby 中跳过额外的关键字参数 - 2

    我定义了一个方法:defmethod(one:1,two:2)[one,two]end当我这样调用它时:methodone:'one',three:'three'我得到:ArgumentError:unknownkeyword:three我不想从散列中一个一个地提取所需的键或排除额外的键。除了像这样定义方法之外,有没有办法规避这种行为:defmethod(one:1,two:2,**other)[one,two,other]end 最佳答案 如果不想写**other中的other,可以省略。defmethod(one:1,two:2

  7. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  8. ruby-on-rails - 我将 Rails3 与 tinymce 一起使用。如何呈现用户关闭浏览器javascript然后输入xss? - 2

    我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如

  9. ruby - 使用 Selenium WebDriver 启用/禁用 javascript - 2

    出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=

  10. ruby - 是否有可能在 Ruby 中以哈希的形式访问关键字参数? - 2

    我知道我能做到:classParentdefinitialize(args)args.eachdo|k,v|instance_variable_set("@#{k}",v)endendendclassA但我想使用关键字参数来更清楚地说明可以接受哪个散列键方法(并进行验证表明不支持此键)。所以我可以写:classAdefinitialize(param1:3,param2:4)@param1=param1@param2=param2endend但是有没有可能写一些更短的东西而不是@x=x;@y=y;...从传递的关键字参数初始化实例变量?是否可以访问作为哈希传递的关键字参数?

随机推荐