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卷)-排序和查找(英文影印版)

(美)Donald E.Knuth / 清华大学出版社 / 2002-9 / 85.00元

《计算机程序设计艺术排序和查找(第3卷)(第2版)》内容简介:这是对第3卷的头一次修订,不仅是对经典计算机排序和查找技术的最全面介绍,而且还对第1卷中的数据结构处理技术作了进一步的扩充,通盘考虑了将大小型数据库和内外存储器。它遴选了一些经过反复检验的计算机方法,并对其效率做了定量分析。第3卷的突出特点是对“最优排序”一节作了修订,对排列论原理与通用散列法作了全新讨论。一起来看看 《计算机程序设计艺术(第3卷)-排序和查找(英文影印版)》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具