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:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…