Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code

栏目: IT技术 · 发布时间: 4年前

内容简介:Hi everyone! I’m Justin and I am currently an intern on the PowerShell team. One of my projects was to add PowerShell semantic highlighting support in VS Code allowing for more accurate highlighting in the editor. I’m excited to share that the first iterat

Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code

Hi everyone! I’m Justin and I am currently an intern on the PowerShell team. One of my projects was to add PowerShell semantic highlighting support in VS Code allowing for more accurate highlighting in the editor. I’m excited to share that the first iteration has been released.

Getting started

Great news! You don’t have to do anything to get this feature except for making sure you have at least the v2020.7.0 version of the PowerShell Preview extension for Visual Studio Code .

IMPORTANT

You have to use a theme that supports Semantic Highlighting. All the inbox themes support it and the PowerShell ISE theme supports it but it’s not guaranteed that every theme will. If you don’t see any difference in highlighting, the theme you’re using probably doesn’t support it. Open an issue on the theme you’re using to support Semantic Highlighting.

For theme authors: Supporting Semantic Highlighting

If you are a theme author, make sure to add {semanticHighlighting: true} to the theme.json file of your VS Code theme.

For a more complete guide into supporting Semantic Highlighting in your theme, please look at:

The rest of this blog post will discuss the shortcomings of the old syntax highlighting mechanism and how semantic highlighting addresses those issues.

Syntax Highlighting

Currently, the syntax highlighting support for PowerShell scripts in VS Code leverages TextMate grammars , which are mappings of regular expressions to tokens. For instance, to identify control keywords, something like the following would be used

{
    name = 'keyword.control.untitled';
    match = 'b(if|while|for|return)b';
}

However, there are some limitations with regular expressions and their ability to recognize different syntax patterns. Since TextMate grammars rely on these expressions, there are many complex and context-dependent tokens these grammars are unable to parse, leading to inconsistent or incorrect highlighting. Just skim through the issues in the EditorSyntax repo , our TextMate grammar.

Here are a few examples where syntax highlighting fails in tokenizing a PowerShell script.

Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code

Semantic Highlighting

To solve those cases (and many other ones) we use the PowerShell tokenizer which describes the tokens more accurately than regular expressions can, while also always being up-to-date with the language grammar. The only problem is that the tokens generated by the PowerShell tokenizer do not align perfectly to the semantic token types predefined by VS Code. The semantic token types provided by VS Code are:

  • namespace
  • type, class, enum, interface, struct, typeParameter
  • parameter, variable, property, enumMember, event
  • function, member, macro
  • label
  • comment, string, keyword, number, regexp, operator

On the other hand, there are over 100 PowerShell token kinds and also manytoken flags that can modify those types.

The main task (aside from setting up a semantic tokenization handler) was to create a mapping from PowerShell tokens to VS Code semantic token types. The result of enabling semantic highlighting can be seen below.

Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code

If we compare the semantic highlighting to the highlighting in PowerShell ISE, we can see they are quite similar (in tokenization, not color).

Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code

Next Steps

Although semantic highlighting does a better job than syntax highlighting in identifying tokens, there remain some cases that can still be improved at the PowerShell layer.

In Example 5, for instance, while the enum does have better highlighting, the name and members of the enums are highlighted identically. This occurs because PowerShell tokenizes them all of them the same way (as identifiers with a token flags denoting that they are member names meaning that the semantic highlighting has no way to differentiate them.

How to Provide Feedback

If you experience any issues or have comments on improvement, please raise an issue in PowerShell/vscode-powershell . Since this was just released, any feedback will be greatly appreciated.

Justin Chen PowerShell Team


以上所述就是小编给大家介绍的《Semantic Highlighting in the PowerShell Preview extension for Visual Studio Code》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

写给大家看的设计书(第3版)

写给大家看的设计书(第3版)

[美] Robin Williams / 苏金国、刘亮 / 人民邮电出版社 / 2009-1 / 49.00元

这本书出自一位世界级设计师之手。复杂的设计原理在书中凝炼为亲密性、对齐、重复和对比4 个基本原则。作者以其简洁明快的风格,将优秀设计所必须遵循的这4 个基本原则及其背后的原理通俗易懂地展现在读者面前。本书包含大量的示例,让你了解怎样才能按照自己的方式设计出美观且内容丰富的产品。 此书适用于各行各业需要从事设计工作的读者,也适用于有经验的设计人员。一起来看看 《写给大家看的设计书(第3版)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具