Skip to content

Commit

Permalink
Add support to restart Language Server
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Sharma <[email protected]>
  • Loading branch information
esmasth committed Jul 24, 2024
1 parent e63b66e commit e770fa2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ by the extension, except pull diagnostics and commands.

See `pyang` documentation corresponding to installation for expected features.

See: [Programmatic Language Features][programmatic], [Language Server Extension Guide][lseg]
See: [Programmatic Language Features][programmatic],
[Language Server Extension Guide][lseg]

## Requirements

The extension requires a `pyang` version installation that supports LSP. At the
time of writing, only a work in progress `pyang` supports `--lsp` argument,
which is used to execute it as an LSP server.
time of writing, only a work in progress `pyang` supports `--lsp --no-env-path`
arguments, which are used to execute it as an LSP server.

## Extension Settings

Expand All @@ -52,22 +53,34 @@ This extension contributes the following settings:
* `pyang.debug.server.port`
> LSP Server Port, when LSP is used via TCP socket.
## Extension Commands

This extension contributes the following commands:

* `pyang.client.restart`
> `pyang: Restart Language Server` restarts the language client and in turn
reinstantiates the language server. When `pyang.debug.server.enable` is set,
it merely reinitiates the TCP connection and hence TCP server should already
be initiated.

## Known Issues

Missing `pyang` or older installations that do not meet requirements are not
handled gracefully.

Change of configuration requires reload of editor for application.
Change of some settings e.g., CLI arguments, requires reload of manual reload of
the editor. `pyang: Restart Language Server` command reuses the settings from
the last startup.

For issues pertaining to the language server, please refer to documentation for
`pyang`.

## Unknown Issues
## Release Notes

The extension has only been verified with VS Code, hence issues may exist with
other VS Code compatible editors/IDE.
### 0.2.0

## Release Notes
* Added support for older VS Code >= 1.65.0
* Added command `pyang: Restart Language Server`

### 0.1.0

Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pyang",
"displayName": "pyang",
"version": "0.1.0",
"version": "0.2.0",
"publisher": "sid-sharma",
"description": "YANG Language Support using pyang",
"author": {
Expand All @@ -15,10 +15,13 @@
],
"pricing": "Free",
"engines": {
"vscode": "^1.88.0"
"vscode": "^1.65.0"
},
"license": "MIT",
"activationEvents": [],
"activationEvents": [
"onLanguage:yang",
"onCommand:pyang.client.restart"
],
"main": "./dist/extension",
"contributes": {
"languages": [
Expand All @@ -41,6 +44,13 @@
"path": "./syntaxes/yang.tmLanguage.json"
}
],
"commands": [
{
"command": "pyang.client.restart",
"title": "Restart Language Server",
"category": "pyang"
}
],
"configuration": {
"type": "object",
"title": "pyang",
Expand Down
47 changes: 31 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,40 @@ import {
} from 'vscode-languageclient/node';

let client: LanguageClient;
const restartCmd = 'pyang.client.restart'
const config = vscode.workspace.getConfiguration('pyang');
const debug = config.get<boolean>('debug.server.enable', false)
const debug_host = config.get<string>('debug.server.host', "127.0.0.1")
const debug_port = config.get<number>('debug.server.port', 2087)
const extra_args = config.get<string>('cli.args', '')
const debugHost = config.get<string>('debug.server.host', "127.0.0.1")
const debugPort = config.get<number>('debug.server.port', 2087)
const extraArgs = config.get<string>('cli.args', '')

export function activate(context: vscode.ExtensionContext) {
let clientOptions: LanguageClientOptions = {
const clientOptions: LanguageClientOptions = {
documentSelector: ['yang'],
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.yang')
}
};
const clientId = { id: 'pyang', name: 'pyang Language Server' };
client = debug
? getSocketLanguageClient(clientId, clientOptions)
: getStdioLanguageClient(clientId, clientOptions, context);
: getStdioLanguageClient(clientId, clientOptions);

client.start();
vscode.window.showInformationMessage(debug
? 'pyang: LSP socket mode'
: 'pyang: LSP stdio mode');

client.onReady().then(() => {
vscode.window.showInformationMessage(debug
? 'pyang: LSP socket mode'
: 'pyang: LSP stdio mode');

});

const restartCmdHandler = async () => {
await client.stop();
client.start();
};

context.subscriptions.push(vscode.commands.registerCommand(restartCmd, restartCmdHandler));
}

export function deactivate(): Thenable<void> | undefined {
Expand All @@ -42,14 +58,13 @@ export function deactivate(): Thenable<void> | undefined {
function getStdioLanguageClient(
clientId: { id: string, name: string },
clientOptions: LanguageClientOptions,
context: vscode.ExtensionContext
): LanguageClient {
const lsp_command = 'pyang';
let lsp_args = [
'--lsp',
'--no-env-path',
];
lsp_args = lsp_args.concat(extra_args.split(' '))
lsp_args = lsp_args.concat(extraArgs.split(' '))
const serverOptions: ServerOptions = {
run: {
command: lsp_command,
Expand All @@ -73,13 +88,13 @@ function getSocketLanguageClient(
clientId: { id: string, name: string },
clientOptions: LanguageClientOptions,
): LanguageClient {
let connectionInfo = {
port: debug_port,
host: debug_host
const connectionInfo = {
port: debugPort,
host: debugHost
};
let serverOptions: ServerOptions = () => {
let socket = net.connect(connectionInfo);
let result: StreamInfo = {
const serverOptions: ServerOptions = () => {
const socket = net.connect(connectionInfo);
const result: StreamInfo = {
writer: socket,
reader: socket
};
Expand Down
1 change: 1 addition & 0 deletions test/yang/test-d.yang
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module test-d {
namespace "urn:test-c";
}

0 comments on commit e770fa2

Please sign in to comment.