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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

代码之髓

代码之髓

[日] 西尾泰和 / 曾一鸣 / 人民邮电出版社 / 2014-8 / 45.00元

《代码之髓:编程语言核心概念》作者从编程语言设计的角度出发,围绕语言中共通或特有的核心概念,通过语言演变过程中的纵向比较和在多门语言中的横向比较,清晰地呈现了程序设计语言中函数、类型、作用域、类、继承等核心知识。本书旨在帮助读者更好地理解各种概念是因何而起,并在此基础上更好地判断为何使用、何时使用及怎样使用。同时,在阅读本书后,读者对今后不断出现的新概念的理解能力也将得到提升。 《代码之髓:......一起来看看 《代码之髓》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

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

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具