英文:
Setting up a Menu for Code Generation in VSCode
问题
如何在Visual Studio Code(VSCode)中创建一个菜单以便于代码生成?是否有推荐的资源或代码存储库,提供有关实现此功能的示例或指南?
英文:
How can I create a menu in Visual Studio Code (VSCode) to facilitate code generation? Are there any recommended sources or code repositories that provide examples or guidance on implementing this functionality
答案1
得分: 1
VScode不是一个“菜单”驱动的用户界面。因此,通常有其他交互方式。最好的方法是注册一个“命令”,这样它将出现在命令面板中。
从Rascal中注册命令的最简单方法是将“lenses”插入到DSL编辑器中。如果您单击这样的镜头,将激活一个命令,您可以直接在Rascal中处理它。
这里有一个演示 链接
但这涉及到注册镜头和执行器:
// 扩展可能命令的集合,包括可能的参数:
data Command = doSomething(start[Program] program);
// 检测放置镜头的位置:
rel[loc,Command] myLenses(start[Program] input)
= {<input@\loc, doSomething(input /* 命令的任何参数 */, title="这是用户看到的内容!")>};
// 一个命令处理程序:
value myCommandHandler(doSomething(start[Program] input)) {
... 在这里执行任何操作,可能使用util::IDEServices中的功能...
return ("result": true);
}
这些myCommandHandler
和myLenses
函数将被注册为语言的Contribution
。
如果代码生成器向现有文件添加代码,使用util::IDEServices::registerDocumentEdits
是有意义的,因为它将与打开的编辑器和撤销堆栈集成。否则,您可以使用IO
模块(writeFile
)将文件直接写入磁盘。
英文:
VScode is not a "menu" driven user-interface. So typically there are other ways of interaction. The best thing is to register a "command", such it will appear in the Command Palette.
From Rascal the most easy-to-use feature for registering commands is to insert lenses
into the editor for the DSL. If you click on such a lense a command will be activated that you can handle in Rascal directly.
There is a demo here
But it amounts to registering lenses and the executors:
// extend the set of possible commands, with possible parameters:
data Command = doSomething(start[Program] program);
// detect places to put lenses:
rel[loc,Command] myLenses(start[Program] input)
= {<input@\loc, doSomething(input /* any parameter to the command */, title="This is what the user sees!")>};
// a command handler:
value myCommandHandler(doSomething(start[Program] input)) {
... do anything here, possible using features from util::IDEServices...
return ("result": true);
}
And those myCommandHandler
and myLenses
functions would be registered with the language as Contribution
s.
If the code generator adds code to existing files, it makes sense to use util::IDEServices::registerDocumentEdits
, because that will integrate with open editors and the undo stack. Otherwise you can just write files to disk using the IO
module (writeFile
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论