使用VS-Code-Extension通过命令更改文本 [英] Change text by Command with VS-Code-Extension

查看:117
本文介绍了使用VS-Code-Extension通过命令更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现第二种代码格式.这种格式应该可以通过其他命令执行.

I want to implement a second code formatting. This formatting should be executable by an additional command.

我已经注册了 DocumentFormattingEditProvider ,这很好.

I have already registered a DocumentFormattingEditProvider and that is fine.

vscode.languages.registerDocumentFormattingEditProvider({ language: 'sosse' }, {
    provideDocumentFormattingEdits(document: vscode.TextDocument) {
        return formattingAction(document);
    },
});

但是在我的情况下,我需要第二个格式化操作,由命令执行.我考虑过使用:

But in my case I need a second formatting action for one-liners, which is executed by an command. I thought about using:

vscode.commands.registerCommand(command, callback)

但是我不知道如何访问和更改文档.

But I don't know how to access and change the document.

推荐答案

但是我不知道如何访问和更改文档.

But I don't know how to access and change the document.

我认为正是您要寻找的 registerCommand()的特殊变体: registerTextEditorCommand().从 API文档:

There's a special variant of registerCommand() that I think is exactly what you're looking for: registerTextEditorCommand(). From the API docs:

文本编辑器命令与普通命令不同,因为它们仅在调用该命令时在活动的编辑器中执行.另外,编辑器命令的命令处理程序有权访问活动的编辑器和编辑生成器.

这意味着 callback 传递了 TextEditorEdit .

That means the callback gets passed an instance of a TextEditor as well as a TextEditorEdit.

这篇关于使用VS-Code-Extension通过命令更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆