-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
sampleServer.ts
78 lines (68 loc) · 2.33 KB
/
sampleServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import {
CodeAction, CodeActionKind, Command, createConnection, Diagnostic, DiagnosticSeverity, Position, Range, TextDocumentEdit,
TextDocuments, TextDocumentSyncKind, TextEdit
} from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';
const connection = createConnection();
connection.console.info(`Sample server running in node ${process.version}`);
const documents = new TextDocuments(TextDocument);
documents.listen(connection);
connection.onInitialize(() => {
return {
capabilities: {
codeActionProvider: true,
textDocumentSync: {
openClose: true,
change: TextDocumentSyncKind.Incremental
},
executeCommandProvider: {
commands: ['sample.fixMe']
}
}
};
});
function validate(document: TextDocument): void {
connection.sendDiagnostics({
uri: document.uri,
version: document.version,
diagnostics: [
Diagnostic.create(Range.create(0, 0, 0, 10), 'Something is wrong here', DiagnosticSeverity.Warning)
]
});
}
documents.onDidOpen((event) => {
validate(event.document);
});
documents.onDidChangeContent((event) => {
validate(event.document);
});
connection.onCodeAction((params) => {
const textDocument = documents.get(params.textDocument.uri);
if (textDocument === undefined) {
return undefined;
}
const title = 'With User Input';
return [CodeAction.create(title, Command.create(title, 'sample.fixMe', textDocument.uri), CodeActionKind.QuickFix)];
});
connection.onExecuteCommand(async (params) => {
if (params.command !== 'sample.fixMe' || params.arguments === undefined) {
return;
}
const textDocument = documents.get(params.arguments[0]);
if (textDocument === undefined) {
return;
}
const newText = typeof params.arguments[1] === 'string' ? params.arguments[1] : 'Eclipse';
connection.workspace.applyEdit({
documentChanges: [
TextDocumentEdit.create({ uri: textDocument.uri, version: textDocument.version }, [
TextEdit.insert(Position.create(0, 0), newText)
])
]
});
});
connection.listen();