rascal

Setting up a Menu for Code Generation in 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


Solution

  • 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 Contributions.

    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)