3

I would like to add symbol support for PowerShell to VS Code but I'm not finding any docs on the code.visualstudio.com/docs site.

Also, is it possible to do this for a language like PowerShell that, for the moment, will only work on Windows? Is there a way to light up symbol support on Windows only?

BTW I've added a bunch of PowerShell snippets that I'm in the process of trying to get integrated into VS Code. Any help on how to get these snippets into the product would be appreciated as well? I did submit an issue on the snippets, suggesting that the team put these into VS Code.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369

1 Answers1

2

There is currently no documentation for the plugin API. It's too early for this as the API is still changing with every minor release. The VSCode team is focused on providing a stable plugin API. There will be a documentation about it when it's done.

Nevertheless it is already possible to add a new language plugin or extending an exisiting one. Take a look on this short description on how to add declaration support for a new language: Create Custom Language in Visual Studio Code

You could add symbol support in a similar way. What you need is something like an abstract syntax tree builder for powershell scripts and an application or a javascript module that is able to process a JSON request in order to provide the correct symbols. An example request for outline support is this:

{
  "seq":442,
  "type":"request",
  "command":"navbar",
  "arguments":
  {
    "file":"c:/Users/C/Documents/projects/MyProject/MyFile.xxx"
  }
}

A response could look like that:

{
"seq":442,
"type":"response",
"command":"navbar",
"request_seq":442,
"success":true,
"body":[
    {
        "text":"TObjA",
        "kind":"class",
        "kindModifiers":"",
        "spans":[
            {
            "start":{
                "line":10,
                "offset":3
            },
            "end":{
                "line":16,
                "offset":4
            }
            }
        ],
        "childItems":[

        ]
    },
    {
        "text":"DoSomething",
        "kind":"method",
        "kindModifiers":"",
        "spans":[
            {
            "start":{
                "line":20,
                "offset":1
            },
            "end":{
                "line":27,
                "offset":4
            }
            }
        ],
        "childItems":[

        ]
    },
]
}

I'm not sure what do you mean with "symbol support". Is it something like "jump to symbol inside the current file" using CTRL+Shift+O? Then you are looking for outlineSupport. Is it something like "find a symbol in any file" using CTRL+P, #? Then you are looking for navigateTypesSupport. Copy the needed .js file from the vs.langauage.csharp.o folder to the vs.langauage.powershell folder and register the support in powershellMain.js as it is done in omnisharpMain.js.

If you want to register the new support only on Windows then you can do it like this:

var isWin = /^win/.test(process.platform);
if(isWin)
  monaco.Modes.NavigateTypesSupport.register('powershell', new navigateTypesSupport_1.default(ModelService, server));

I hope this helps for the moment. Don't forget to save your changed plugins in a different folder. VSCode often deletes changes in the plugin folders on update.

Community
  • 1
  • 1
Wosi
  • 41,986
  • 17
  • 75
  • 82
  • Thanks! By symbol support I was referring to Ctrl+P, @ and @:. PowerShell has had AST support since V3 e.g. `{function foo { 'hello world'}}.Ast.FindAll({param($ast) $ast -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true) | Foreach { "$($_.Name) Start:$($_.Extent.StartLineNumber):$($_.Extent.StartColumnNumber) End:$($_.Extent.EndLineNumber):$($_.Extent.EndColumnNumber)" } ` outputs `foo Start:1:2 End:1:31`. So I think PowerShell itself can do most of the heavy lifting for symbol support. But how does VSC handle features (PowerShell) that only run on Windows? – Keith Hill Sep 14 '15 at 16:09
  • OK, you are are looking for OutlineSupport. I suggest to write a wrapper `.exe` for working with the PowerShell AST. The wrapper communicates with VSCode using JSON via StdIn/StdOut. MS did the same for TypeScript until V0.7. Download and _save_ V0.6 here: C:\Users\C\AppData\Local\Temp\VSCode-win32-1.zip\resources\app\plugins\vs.language.typescript\bin\win Take a look at `typeScriptServiceClient.js` in that version to see how the communication to the `Node.exe` process is being established. – Wosi Sep 14 '15 at 17:20
  • If you register the outlineSupport only when you are on Windows then it is disabled on all other platforms. By using a stand alone application you have no problems to use platform dependent APIs. This generally works great. But you have to deal with BufferSyncSupport. Take a look on how it is implemented for TypeScript in V0.6 and implement it the same way for your PowerShell-Service-wrapper. BufferSyncSupport is needed to synchronize your wrapper application with the current editor content (which has not been saved since introducing new methods). – Wosi Sep 14 '15 at 17:27