简体   繁体   English

在 vscode 扩展中获取语法范围/标记

[英]Get grammar scopes / tokens in vscode extension

Having defined TextMate grammar for text highlighting of a new language in Visual Studio Code, I'm now writing a DefinitionProvider.在 Visual Studio Code 中为新语言的文本突出显示定义了 TextMate 语法后,我现在正在编写一个 DefinitionProvider。 Is it possible to access the grammar tokens of the document in my extension code?是否可以在我的扩展代码中访问文档的语法标记?

The signature of the extension method in a DefinitionProvider is: DefinitionProvider 中扩展方法的签名是:

provideDefinition(
    document: vscode.TextDocument,
    position: vscode.Position,
    token: vscode.CancellationToken,
  )

And I can't find a way to get any tokens from the document instance...而且我找不到从文档实例中获取任何标记的方法...

Any help is highly appreciated =)非常感谢任何帮助 =)

EDIT: Let me rephrase that: Is it possible to use the already defined TextMate grammar to navigate the code in the extension code?编辑:让我重新表述一下:是否可以使用已经定义的 TextMate 语法来导航扩展代码中的代码? I would like to not write a parser on my own...我不想自己写一个解析器......

There is a service that does this now: Using my library:现在有一项服务可以做到这一点:使用我的图书馆:

import TextmateLanguageService from 'vscode-textmate-languageservice';

export async function activate(context: vscode.ExtensionContext) {
    const selector: vscode.DocumentSelector = 'custom';
    const textmateService = new TextmateLanguageService('custom', context);
    const textmateTokenService = await textmateService.initTokenService();
    const textDocument = vscode.window.activeTextEditor!.document;
    const tokens = textmateTokenService.fetch(textDocument);
};

https://github.com/vsce-toolroom/vscode-textmate-languageservice#tokenization https://github.com/vsce-toolroom/vscode-textmate-languageservice#tokenization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM