rascal

How do you point to current file when creating a lens for Rascal in VSCode?


Is there a way to point to the current file (by location) with its LanguageServer running, such that, you can:


Solution

  • The way to pass in parameters to the handler of a lense is to first extend the Command language with a new command, like so:

    data Command = myCommand(loc aParameter);
    

    Next the lense detector collects all the lenses for a file, and binds all the parameters. This is an example that uses / to collect locations of expressions:

    rel[loc,Command] myLenses(start[Program] input) = {<e@\loc, myCommand(e@\loc, title="do something with this exp")> | /Exp e := input};
    

    Finally the command handler executes the command when triggered:

    value myHandler(myCommand(loc e) {
         // Do something with e
         return ("result": true);
    }
    

    Then you register the lenses and the handler function as a contribution.

    Small parse trees or abstract subtract trees can be parameters to commands directly so you won't have to reparse, however if you have many large editors open it's better to pass locations and parse again for the sake of memory leaks.