How to enable "Go to symbol" with a custom language in vscode? - visual-studio-code

I have made a custom language extension and I would like to enable the "Go To Symbol" feature. I have tried to follow the guidelines here, but I'm still kind of lost.
I think all I need to do is implement a DocumentSymbolProvider, but I'm not really sure how to go about it.
UPDATE
The example language server docs point to a repo that is deprecated. It is replaced with this one, which is much more complex. I think the simplest example I can find there is the lsp-sample, but it doesn't use a DocumentSymbolProvider.
I've found other repos that do use symbol providers, but they are a bit overwhelming. I can't figure out what needs to go where (for example, do I need both a client and a server? Some packages only seem to use an extension.ts without both client and server folders).
All I really want to do in my language is detect lines that start with # and show them in the Go to Symbol pane. I'd love to see a simple tutorial of this.

Do I need both a client and a server?
Not necessarily. As the language support docs you linked show, there's usually two options: a "direct implementation" and one using the Language Server Protocol. For the former, you don't need a client / server architecture. Language Servers have the advantage of being editor-agnostic, you can theoretically use them in any editor that implements the protocol. A direct implementation is limited to usage in VSCode.
All I really want to do in my language is detect lines that start with # and show them in the Go to Symbol pane.
Here's the extension.ts for a very simple example of a "direct implementation" for this:
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(
{language: "foo"}, new FooDocumentSymbolProvider()
));
}
class FooDocumentSymbolProvider implements vscode.DocumentSymbolProvider {
public provideDocumentSymbols(document: vscode.TextDocument,
token: vscode.CancellationToken): Thenable<vscode.SymbolInformation[]> {
return new Promise((resolve, reject) => {
var symbols = [];
for (var i = 0; i < document.lineCount; i++) {
var line = document.lineAt(i);
if (line.text.startsWith("#")) {
symbols.push({
name: line.text.substr(1),
kind: vscode.SymbolKind.Field,
location: new vscode.Location(document.uri, line.range)
})
}
}
resolve(symbols);
});
}
}
You will also need to add this to the package.json generated by the VSCode extension template. It registers the foo language and activates the extension when a file with the .foo extension is opened:
"activationEvents": [
"onLanguage:foo"
],
"contributes": {
"languages": [{
"id": "foo",
"extensions": [".foo"]
}]
}
Here it is in action:

Related

VS Code extension: setting custom semantic token colors

Is it possible to modify the styling of semantic token modifiers received from LSP
inside an extension without the need to create custom themes?
I am able to use editor.semanticTokenColorCustomizations in my settings.json file and add the custom rules I want, but this setting is not available for configurationDefaults in the package.json file for a VS Code extension.
So the following snippet does work in settings.json, while the same does not work in package.json for an extension under the configurationDefaults field.
"editor.semanticTokenColorCustomizations": {
"enabled": true,
"rules": {
"*.declaration": {
"bold": true
},
"*.definition": {
"italic": true
},
"*.readonly": "#ff0000"
}
}
Is there another way?
Ideally, I would like to change both token types and token modifiers
for the language I introduce with the extension, but I don't want to create custom themes a user would need to use to get proper highlighting.
Note: I am forced to stick with the token types and modifiers supported by the language-client provided by Microsoft. Those are defined in the LSP specification.
Edit: I use LSP with semantic tokens to get the token types and modifiers of a file. This should be similar to using TextMate grammar.
The problem I have, is applying correct styling/highlighting to those tokens. Since the language client limits the usable tokens, I apply a mapping between tokens of my language and the default LSP ones.
Meaning: token modifier declaration is in fact bold in my markup language
You can introduce all your custom semantic tokens without the need to restrict yourself to the built-in ones. Personally I prefer the way proposed in the official sample file:
semantic-tokens-sample.
As for the styling, you can easily modify an extension incl. semantic token colors via the package.json file as follows.
{
...
"editor.semanticHighlighting.enabled": true, // not necessary, just make sure it is not disabled
"contributes": {
"semanticTokenTypes": [ // not necessary if you use own parsing with "DocumentSemanticTokensProvider"
{
"id": "myToken",
"superType": "myToken",
"description": "myToken"
}
],
"configurationDefaults": {
"editor.semanticTokenColorCustomizations": {
"rules": {
"comment": "#969896",
"string": "#B5BD68",
"myToken": "#323232" // custom
}
}
}
}
}
For that I personally introduced myToken in the legend in an extension.ts file.
To check if your semantic token logic is working, you can use the
[view/Command Palette/>Developer: Inspect Editor Tokens ans Scopes] functionality that will reveal what semantic scope is attached to your keyword, if any.
If the provided code is not working for you, check your package.json and make sure the language settings are all correct:
settings that could be of relevance for you:
{
...
"activationEvents": ["onLanguage:myLanguage"], // make sure your extension is activated
"contributes": {"languages": [{"id": "myLanguage", "extensions": [".myLang"], "configuration": "./language-configuration.json"}]}
}
Furthermore check if your User / Workspace settings are interfering with your package.json settings.

Go to definition, go to implementation, autogenerate import for Ember

Im using Ember with VS Code.
What I need is to generate import string on a fly when I encounter dependency. For example I write someting like:
#tracked isLarge = false;
But I don’t have “#tracked” imported yet. So the otion could be to set the coursor on #tracked, press something like “Action + .” and pick “generate import”. It should generate import string:
import { tracked } from '#ember/tracking';
But it doesn’t work out of the box. How can I do that?
UPDATE: the same question about:
go to definition
go to implementation
cmd+click to navigate to implementation/component
You can use the extension My Code Actions
You can create actions that just insert the text independent of an error.
"my-code-actions.actions": {
"[javascript]": {
"import tracked": {
"where": "insertAfter",
"insertFind": "^import",
"text": "import { tracked } from '#ember/tracking';\n"
}
}
}
The key combo to use is the Code Action combo: Ctrl+.
If you get a diagnostic (PROBLEM panel, and squiggle) you can use that to further customize the action and you can use text from the diagnostics message.
I'm current adding the possibility to make multiple edits in an action and to use further customization and generalization.
"Ember Language Server" brings some solution. But it works mostly with library code that has .d.ts typings.
In case of custom JS code it still doesn't work.
So there is no straight solution. Only 2 ways:
Write .d.ts typing for custom code JS files
Move project to typescript

VSCode extension: disable repeating things in code completion

I am creating a language extension for VSCode using Java and the LSP4J library. It is something like this.
But I have a problem - if the user presses Ctrl+Space, and the language server returns an empty list, VSCode will still offers its options - things that are already in the code. How can I get it to display something like "No suggestions" instead?
The text-based completion you're seeing there can be disabled with the "editor.wordBasedSuggestions" setting.
Extensions can change the default value of a setting for a particular language by contributing configurationDefaults in package.json:
"contributes": {
"configurationDefaults": {
"[lang]": {
"editor.wordBasedSuggestions": false
}
}
}
Where lang is the ID of the language in question.
If the language server sends back an empty list you could add an artificial entry with text: "No suggestions" to the completion list.

Set language of a text document in a VSCode extension

I have a Visual Studio Code extension where I try to open a
virtual editor:
vscode.workspace.openTextDocument(vscode.Uri.parse(previewSchema + ":" + path))
context.subscriptions.push(extractHibernateLogCommand, vscode.Disposable.from(
vscode.workspace.registerTextDocumentContentProvider(previewSchema, hibernateExtractorProvider)
));
Those documents are always language:plain-text. Is it possible to change this programmatically to "SQL" to have the correct highlighting?
Full code
Since VSCode 1.28 (September 2018), it's also possible to set the language mode for a document after it has been created using languages.setTextDocumentLanguage():
Set (and change) the language that is associated with the given document.
Note that calling this function will trigger the onDidCloseTextDocument event followed by the onDidOpenTextDocument event.
Here's a simple example that opens a document containing {} and sets the language to JSON:
vscode.workspace.openTextDocument({content: "{}"}).then(document => {
vscode.window.showTextDocument(document);
vscode.languages.setTextDocumentLanguage(document, "json");
});
I found a solution by myself:
let options: Object = {
content: string,
language: "sql"
};
vscode.workspace.openTextDocument(options).then(doc => {
vscode.window.showTextDocument(doc, vscode.ViewColumn.One);
}, err => {
vscode.window.showErrorMessage(err);
});
A solution when using TextDocumentContentProvider seems not to be possible.
The commit with my change
Open the Command Palette (View->Command Palette)
run "Configure language specific settings"
In the Select Language drop down there should be a setting for SQL

Where are the docs on how to add symbol support for a language to Visual Studio Code?

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