Skip to content

Commit b241d78

Browse files
authored
Add code action to open json schema (redhat-developer#395)
* add code action to open json schema Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Fix eslint warning, fix review comments Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Fix tests Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * simplify code actions implementation Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * add code action to open schema only if 'showDocument' supported Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Fix tests Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * fix test Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * Add supported protocol version in readme Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
1 parent 19df4f0 commit b241d78

29 files changed

+581
-107
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ To run the image you can use:
234234
docker run -it quay.io/redhat-developer/yaml-language-server:latest
235235
```
236236

237+
## Language Server Protocol version
238+
239+
`yaml-language-server` use `vscode-languageserver@7.0.0` which implements [LSP 3.16](https://github.com/Microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md)
240+
237241
## Clients
238242

239243
This repository only contains the server implementation. Here are some known clients consuming this server:

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
"jsonc-parser": "^2.2.1",
4141
"request-light": "^0.2.4",
4242
"vscode-json-languageservice": "^3.10.0",
43-
"vscode-languageserver": "^5.2.1",
43+
"vscode-languageserver": "^7.0.0",
4444
"vscode-languageserver-textdocument": "^1.0.1",
45-
"vscode-languageserver-types": "^3.15.1",
45+
"vscode-languageserver-types": "^3.16.0",
4646
"vscode-nls": "^4.1.2",
4747
"vscode-uri": "^2.1.1",
4848
"yaml-language-server-parser": "next"
@@ -53,6 +53,7 @@
5353
"@types/node": "^12.11.7",
5454
"@types/prettier": "2.0.2",
5555
"@types/sinon": "^9.0.5",
56+
"@types/sinon-chai": "^3.2.5",
5657
"@typescript-eslint/eslint-plugin": "^3.2.0",
5758
"@typescript-eslint/parser": "^3.2.0",
5859
"chai": "^4.2.0",

src/commands.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
export enum YamlCommands {
7+
JUMP_TO_SCHEMA = 'jumpToSchema',
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { ExecuteCommandParams } from 'vscode-languageserver';
7+
8+
export interface CommandHandler {
9+
(...args: unknown[]): void;
10+
}
11+
12+
export class CommandExecutor {
13+
private commands = new Map<string, CommandHandler>();
14+
executeCommand(params: ExecuteCommandParams): void {
15+
if (this.commands.has(params.command)) {
16+
const handler = this.commands.get(params.command);
17+
return handler(...params.arguments);
18+
}
19+
throw new Error(`Command '${params.command}' not found`);
20+
}
21+
22+
registerCommand(commandId: string, handler: CommandHandler): void {
23+
this.commands.set(commandId, handler);
24+
}
25+
}
26+
27+
export const commandExecutor = new CommandExecutor();

src/languageserver/handlers/languageHandlers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55
import { FoldingRange } from 'vscode-json-languageservice';
66
import {
7+
CodeAction,
8+
CodeActionParams,
79
CompletionList,
810
DidChangeWatchedFilesParams,
911
DocumentFormattingParams,
@@ -12,7 +14,7 @@ import {
1214
DocumentOnTypeFormattingParams,
1315
DocumentSymbolParams,
1416
FoldingRangeParams,
15-
IConnection,
17+
Connection,
1618
TextDocumentPositionParams,
1719
} from 'vscode-languageserver';
1820
import { DocumentSymbol, Hover, SymbolInformation, TextEdit } from 'vscode-languageserver-types';
@@ -27,7 +29,7 @@ export class LanguageHandlers {
2729
private validationHandler: ValidationHandler;
2830

2931
constructor(
30-
private readonly connection: IConnection,
32+
private readonly connection: Connection,
3133
languageService: LanguageService,
3234
yamlSettings: SettingsState,
3335
validationHandler: ValidationHandler
@@ -45,6 +47,7 @@ export class LanguageHandlers {
4547
this.connection.onCompletion((textDocumentPosition) => this.completionHandler(textDocumentPosition));
4648
this.connection.onDidChangeWatchedFiles((change) => this.watchedFilesHandler(change));
4749
this.connection.onFoldingRanges((params) => this.foldingRangeHandler(params));
50+
this.connection.onCodeAction((params) => this.codeActionHandler(params));
4851
this.connection.onDocumentOnTypeFormatting((params) => this.formatOnTypeHandler(params));
4952
}
5053

@@ -165,4 +168,13 @@ export class LanguageHandlers {
165168

166169
return this.languageService.getFoldingRanges(textDocument, this.yamlSettings.capabilities.textDocument.foldingRange);
167170
}
171+
172+
codeActionHandler(params: CodeActionParams): CodeAction[] | undefined {
173+
const textDocument = this.yamlSettings.documents.get(params.textDocument.uri);
174+
if (!textDocument) {
175+
return;
176+
}
177+
178+
return this.languageService.getCodeAction(textDocument, params);
179+
}
168180
}

src/languageserver/handlers/notificationHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { IConnection } from 'vscode-languageserver';
5+
import { Connection } from 'vscode-languageserver';
66
import { CustomSchemaProvider } from '../../languageservice/services/yamlSchemaService';
77
import { LanguageService, SchemaConfiguration } from '../../languageservice/yamlLanguageService';
88
import {
@@ -20,7 +20,7 @@ export class NotificationHandlers {
2020
private settingsHandler: SettingsHandler;
2121

2222
constructor(
23-
private readonly connection: IConnection,
23+
private readonly connection: Connection,
2424
languageService: LanguageService,
2525
yamlSettings: SettingsState,
2626
settingsHandler: SettingsHandler

src/languageserver/handlers/requestHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { IConnection } from 'vscode-languageserver';
5+
import { Connection } from 'vscode-languageserver';
66
import { MODIFICATION_ACTIONS, SchemaAdditions, SchemaDeletions } from '../../languageservice/services/yamlSchemaService';
77
import { LanguageService } from '../../languageservice/yamlLanguageService';
88
import { SchemaModificationNotification } from '../../requestTypes';
99

1010
export class RequestHandlers {
1111
private languageService: LanguageService;
12-
constructor(private readonly connection: IConnection, languageService: LanguageService) {
12+
constructor(private readonly connection: Connection, languageService: LanguageService) {
1313
this.languageService = languageService;
1414
}
1515

src/languageserver/handlers/settingsHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
import { xhr, configure as configureHttpRequests } from 'request-light';
6-
import { DidChangeConfigurationParams, DocumentFormattingRequest, IConnection } from 'vscode-languageserver';
6+
import { DidChangeConfigurationParams, DocumentFormattingRequest, Connection } from 'vscode-languageserver';
77
import { isRelativePath, relativeToAbsolutePath } from '../../languageservice/utils/paths';
88
import { checkSchemaURI, JSON_SCHEMASTORE_URL, KUBERNETES_SCHEMA_URL } from '../../languageservice/utils/schemaUrls';
99
import { LanguageService, LanguageSettings } from '../../languageservice/yamlLanguageService';
@@ -12,7 +12,7 @@ import { ValidationHandler } from './validationHandlers';
1212

1313
export class SettingsHandler {
1414
constructor(
15-
private readonly connection: IConnection,
15+
private readonly connection: Connection,
1616
private readonly languageService: LanguageService,
1717
private readonly yamlSettings: SettingsState,
1818
private readonly validationHandler: ValidationHandler

src/languageserver/handlers/validationHandlers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Red Hat, Inc. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { IConnection } from 'vscode-languageserver';
5+
import { Connection } from 'vscode-languageserver';
66
import { TextDocument } from 'vscode-languageserver-textdocument';
77
import { Diagnostic } from 'vscode-languageserver-types';
88
import { isKubernetesAssociatedDocument } from '../../languageservice/parser/isKubernetes';
@@ -14,7 +14,7 @@ export class ValidationHandler {
1414
private languageService: LanguageService;
1515
private yamlSettings: SettingsState;
1616

17-
constructor(private readonly connection: IConnection, languageService: LanguageService, yamlSettings: SettingsState) {
17+
constructor(private readonly connection: Connection, languageService: LanguageService, yamlSettings: SettingsState) {
1818
this.languageService = languageService;
1919
this.yamlSettings = yamlSettings;
2020

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { ExecuteCommandParams, Connection } from 'vscode-languageserver';
7+
import { CommandExecutor } from '../commandExecutor';
8+
9+
export class WorkspaceHandlers {
10+
constructor(private readonly connection: Connection, private readonly commandExecutor: CommandExecutor) {}
11+
12+
registerHandlers(): void {
13+
this.connection.onExecuteCommand((params) => this.executeCommand(params));
14+
}
15+
16+
private executeCommand(params: ExecuteCommandParams): void {
17+
return this.commandExecutor.executeCommand(params);
18+
}
19+
}

0 commit comments

Comments
 (0)