@volar/test-utils 2.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +31 -0
- package/index.d.ts +36 -0
- package/index.js +209 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Johnson Chu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @volar/test-utils
|
|
2
|
+
|
|
3
|
+
This module provides a simple way to start a language server and interact with it. It exports a function `startLanguageServer` which starts a language server and returns a handle to interact with it.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
First, import the module and start the language server:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { startLanguageServer } from '@volar/test-utils';
|
|
11
|
+
|
|
12
|
+
const serverHandle = startLanguageServer('path/to/server/module');
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The `startLanguageServer` function takes the path to the server module as a string and optionally a current working directory as a string or URL.
|
|
16
|
+
|
|
17
|
+
The returned server handle provides several methods to interact with the language server:
|
|
18
|
+
|
|
19
|
+
- `initialize(rootUri: string, initializationOptions: InitializationOptions)`: Initializes the language server.
|
|
20
|
+
- `openTextDocument(fileName: string, languageId: string)`: Opens a text document.
|
|
21
|
+
- `openUntitledTextDocument(content: string, languageId: string)`: Opens an untitled text document.
|
|
22
|
+
- `closeTextDocument(uri: string)`: Closes a text document.
|
|
23
|
+
- Various `send*Request` methods: Send language-related requests to the server.
|
|
24
|
+
|
|
25
|
+
For example, to open a text document and send a completion request:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
await serverHandle.initialize('file:///path/to/workspace', {});
|
|
29
|
+
const document = await serverHandle.openTextDocument('path/to/file', 'typescript');
|
|
30
|
+
const completions = await serverHandle.sendCompletionRequest(document.uri, { line: 0, character: 0 });
|
|
31
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import * as _ from '@volar/language-server/node';
|
|
3
|
+
import * as cp from 'child_process';
|
|
4
|
+
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
5
|
+
export type LanguageServerHandle = ReturnType<typeof startLanguageServer>;
|
|
6
|
+
export declare function startLanguageServer(serverModule: string, cwd?: string | URL): {
|
|
7
|
+
process: cp.ChildProcess;
|
|
8
|
+
connection: _.ProtocolConnection;
|
|
9
|
+
initialize(rootUri: string, initializationOptions: _.InitializationOptions): Promise<_.InitializeResult<any>>;
|
|
10
|
+
openTextDocument(fileName: string, languageId: string): Promise<TextDocument>;
|
|
11
|
+
openUntitledTextDocument(content: string, languageId: string): Promise<TextDocument>;
|
|
12
|
+
closeTextDocument(uri: string): Promise<void>;
|
|
13
|
+
sendCompletionRequest(uri: string, position: _.Position): Promise<_.CompletionList | null>;
|
|
14
|
+
sendCompletionResolveRequest(item: _.CompletionItem): Promise<_.CompletionItem>;
|
|
15
|
+
sendDocumentDiagnosticRequest(uri: string): Promise<_.DocumentDiagnosticReport>;
|
|
16
|
+
sendHoverRequest(uri: string, position: _.Position): Promise<_.Hover | null>;
|
|
17
|
+
sendDocumentFormattingRequest(uri: string, options: _.FormattingOptions): Promise<_.TextEdit[] | null>;
|
|
18
|
+
sendRenameRequest(uri: string, position: _.Position, newName: string): Promise<_.WorkspaceEdit | null>;
|
|
19
|
+
sendPrepareRenameRequest(uri: string, position: _.Position): Promise<_.PrepareRenameResult | null>;
|
|
20
|
+
sendFoldingRangesRequest(uri: string): Promise<_.FoldingRange[] | null>;
|
|
21
|
+
sendDocumentSymbolRequest(uri: string): Promise<_.DocumentSymbol[] | _.SymbolInformation[] | null>;
|
|
22
|
+
sendDocumentColorRequest(uri: string): Promise<_.ColorInformation[]>;
|
|
23
|
+
sendDefinitionRequest(uri: string, position: _.Position): Promise<_.Definition | _.LocationLink[] | null>;
|
|
24
|
+
sendTypeDefinitionRequest(uri: string, position: _.Position): Promise<_.Definition | _.LocationLink[] | null>;
|
|
25
|
+
sendReferencesRequest(uri: string, position: _.Position, context: _.ReferenceContext): Promise<_.Location[] | null>;
|
|
26
|
+
sendSignatureHelpRequest(uri: string, position: _.Position): Promise<_.SignatureHelp | null>;
|
|
27
|
+
sendSelectionRangesRequest(uri: string, positions: _.Position[]): Promise<_.SelectionRange[] | null>;
|
|
28
|
+
sendCodeActionsRequest(uri: string, range: _.Range, context: _.CodeActionContext): Promise<(_.CodeAction | _.Command)[] | null>;
|
|
29
|
+
sendCodeActionResolveRequest(codeAction: _.CodeAction): Promise<_.CodeAction>;
|
|
30
|
+
sendExecuteCommandRequest(command: string, args?: any[]): Promise<any>;
|
|
31
|
+
sendSemanticTokensRequest(uri: string): Promise<_.SemanticTokens | null>;
|
|
32
|
+
sendSemanticTokensRangeRequest(uri: string, range: _.Range): Promise<_.SemanticTokens | null>;
|
|
33
|
+
sendColorPresentationRequest(uri: string, color: _.Color, range: _.Range): Promise<_.ColorPresentation[]>;
|
|
34
|
+
sendDocumentLinkRequest(uri: string): Promise<_.DocumentLink[] | null>;
|
|
35
|
+
sendDocumentLinkResolveRequest(link: _.DocumentLink): Promise<_.DocumentLink>;
|
|
36
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startLanguageServer = void 0;
|
|
4
|
+
const _ = require("@volar/language-server/node");
|
|
5
|
+
const assert = require("assert");
|
|
6
|
+
const cp = require("child_process");
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
|
|
9
|
+
const vscode_uri_1 = require("vscode-uri");
|
|
10
|
+
function startLanguageServer(serverModule, cwd) {
|
|
11
|
+
const childProcess = cp.fork(serverModule, ['--node-ipc', `--clientProcessId=${process.pid.toString()}`], {
|
|
12
|
+
execArgv: ['--nolazy'],
|
|
13
|
+
env: process.env,
|
|
14
|
+
cwd,
|
|
15
|
+
});
|
|
16
|
+
const connection = _.createProtocolConnection(new _.IPCMessageReader(childProcess), new _.IPCMessageWriter(childProcess));
|
|
17
|
+
const openedDocuments = new Map();
|
|
18
|
+
let untitledCounter = 0;
|
|
19
|
+
connection.listen();
|
|
20
|
+
connection.onClose((e) => console.log(e));
|
|
21
|
+
connection.onUnhandledNotification((e) => console.log(e));
|
|
22
|
+
connection.onError((e) => console.log(e));
|
|
23
|
+
connection.onDispose(() => {
|
|
24
|
+
childProcess.kill();
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
process: childProcess,
|
|
28
|
+
connection,
|
|
29
|
+
async initialize(rootUri, initializationOptions) {
|
|
30
|
+
const result = await connection.sendRequest(_.InitializeRequest.type, {
|
|
31
|
+
processId: childProcess.pid ?? null,
|
|
32
|
+
rootUri,
|
|
33
|
+
capabilities: {},
|
|
34
|
+
initializationOptions,
|
|
35
|
+
});
|
|
36
|
+
await connection.sendNotification(_.InitializedNotification.type);
|
|
37
|
+
return result;
|
|
38
|
+
},
|
|
39
|
+
async openTextDocument(fileName, languageId) {
|
|
40
|
+
const uri = vscode_uri_1.URI.file(fileName).toString();
|
|
41
|
+
if (!openedDocuments.has(uri)) {
|
|
42
|
+
const document = vscode_languageserver_textdocument_1.TextDocument.create(uri, languageId, 0, fs.readFileSync(fileName, 'utf-8'));
|
|
43
|
+
openedDocuments.set(uri, document);
|
|
44
|
+
await connection.sendNotification(_.DidOpenTextDocumentNotification.type, {
|
|
45
|
+
textDocument: {
|
|
46
|
+
uri,
|
|
47
|
+
languageId,
|
|
48
|
+
version: document.version,
|
|
49
|
+
text: document.getText(),
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return openedDocuments.get(uri);
|
|
54
|
+
},
|
|
55
|
+
async openUntitledTextDocument(content, languageId) {
|
|
56
|
+
const uri = vscode_uri_1.URI.from({ scheme: 'untitled', path: `Untitled-${untitledCounter++}` }).toString();
|
|
57
|
+
const document = vscode_languageserver_textdocument_1.TextDocument.create(uri, languageId, 0, content);
|
|
58
|
+
openedDocuments.set(uri, document);
|
|
59
|
+
await connection.sendNotification(_.DidOpenTextDocumentNotification.type, {
|
|
60
|
+
textDocument: {
|
|
61
|
+
uri,
|
|
62
|
+
languageId,
|
|
63
|
+
version: document.version,
|
|
64
|
+
text: document.getText(),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
return document;
|
|
68
|
+
},
|
|
69
|
+
closeTextDocument(uri) {
|
|
70
|
+
assert(openedDocuments.has(uri));
|
|
71
|
+
openedDocuments.delete(uri);
|
|
72
|
+
return connection.sendNotification(_.DidCloseTextDocumentNotification.type, {
|
|
73
|
+
textDocument: { uri },
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
async sendCompletionRequest(uri, position) {
|
|
77
|
+
const result = await connection.sendRequest(_.CompletionRequest.type, {
|
|
78
|
+
textDocument: { uri },
|
|
79
|
+
position,
|
|
80
|
+
});
|
|
81
|
+
// @volar/language-server only returns CompletionList
|
|
82
|
+
assert(!Array.isArray(result));
|
|
83
|
+
return result;
|
|
84
|
+
},
|
|
85
|
+
async sendCompletionResolveRequest(item) {
|
|
86
|
+
return connection.sendRequest(_.CompletionResolveRequest.type, item);
|
|
87
|
+
},
|
|
88
|
+
sendDocumentDiagnosticRequest(uri) {
|
|
89
|
+
return connection.sendRequest(_.DocumentDiagnosticRequest.type, {
|
|
90
|
+
textDocument: { uri },
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
sendHoverRequest(uri, position) {
|
|
94
|
+
return connection.sendRequest(_.HoverRequest.type, {
|
|
95
|
+
textDocument: { uri },
|
|
96
|
+
position,
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
sendDocumentFormattingRequest(uri, options) {
|
|
100
|
+
return connection.sendRequest(_.DocumentFormattingRequest.type, {
|
|
101
|
+
textDocument: { uri },
|
|
102
|
+
options,
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
sendRenameRequest(uri, position, newName) {
|
|
106
|
+
return connection.sendRequest(_.RenameRequest.type, {
|
|
107
|
+
textDocument: { uri },
|
|
108
|
+
position,
|
|
109
|
+
newName,
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
sendPrepareRenameRequest(uri, position) {
|
|
113
|
+
return connection.sendRequest(_.PrepareRenameRequest.type, {
|
|
114
|
+
textDocument: { uri },
|
|
115
|
+
position,
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
sendFoldingRangesRequest(uri) {
|
|
119
|
+
return connection.sendRequest(_.FoldingRangeRequest.type, {
|
|
120
|
+
textDocument: { uri },
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
sendDocumentSymbolRequest(uri) {
|
|
124
|
+
return connection.sendRequest(_.DocumentSymbolRequest.type, {
|
|
125
|
+
textDocument: { uri },
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
sendDocumentColorRequest(uri) {
|
|
129
|
+
return connection.sendRequest(_.DocumentColorRequest.type, {
|
|
130
|
+
textDocument: { uri },
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
sendDefinitionRequest(uri, position) {
|
|
134
|
+
return connection.sendRequest(_.DefinitionRequest.type, {
|
|
135
|
+
textDocument: { uri },
|
|
136
|
+
position,
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
sendTypeDefinitionRequest(uri, position) {
|
|
140
|
+
return connection.sendRequest(_.TypeDefinitionRequest.type, {
|
|
141
|
+
textDocument: { uri },
|
|
142
|
+
position,
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
sendReferencesRequest(uri, position, context) {
|
|
146
|
+
return connection.sendRequest(_.ReferencesRequest.type, {
|
|
147
|
+
textDocument: { uri },
|
|
148
|
+
position,
|
|
149
|
+
context,
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
sendSignatureHelpRequest(uri, position) {
|
|
153
|
+
return connection.sendRequest(_.SignatureHelpRequest.type, {
|
|
154
|
+
textDocument: { uri },
|
|
155
|
+
position,
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
sendSelectionRangesRequest(uri, positions) {
|
|
159
|
+
return connection.sendRequest(_.SelectionRangeRequest.type, {
|
|
160
|
+
textDocument: { uri },
|
|
161
|
+
positions,
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
sendCodeActionsRequest(uri, range, context) {
|
|
165
|
+
return connection.sendRequest(_.CodeActionRequest.type, {
|
|
166
|
+
textDocument: { uri },
|
|
167
|
+
range,
|
|
168
|
+
context,
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
sendCodeActionResolveRequest(codeAction) {
|
|
172
|
+
return connection.sendRequest(_.CodeActionResolveRequest.type, codeAction);
|
|
173
|
+
},
|
|
174
|
+
sendExecuteCommandRequest(command, args) {
|
|
175
|
+
return connection.sendRequest(_.ExecuteCommandRequest.type, {
|
|
176
|
+
command,
|
|
177
|
+
arguments: args,
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
sendSemanticTokensRequest(uri) {
|
|
181
|
+
return connection.sendRequest(_.SemanticTokensRequest.type, {
|
|
182
|
+
textDocument: { uri },
|
|
183
|
+
});
|
|
184
|
+
},
|
|
185
|
+
sendSemanticTokensRangeRequest(uri, range) {
|
|
186
|
+
return connection.sendRequest(_.SemanticTokensRangeRequest.type, {
|
|
187
|
+
textDocument: { uri },
|
|
188
|
+
range,
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
sendColorPresentationRequest(uri, color, range) {
|
|
192
|
+
return connection.sendRequest(_.ColorPresentationRequest.type, {
|
|
193
|
+
textDocument: { uri },
|
|
194
|
+
color,
|
|
195
|
+
range,
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
sendDocumentLinkRequest(uri) {
|
|
199
|
+
return connection.sendRequest(_.DocumentLinkRequest.type, {
|
|
200
|
+
textDocument: { uri },
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
sendDocumentLinkResolveRequest(link) {
|
|
204
|
+
return connection.sendRequest(_.DocumentLinkResolveRequest.type, link);
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
exports.startLanguageServer = startLanguageServer;
|
|
209
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@volar/test-utils",
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"**/*.js",
|
|
7
|
+
"**/*.d.ts"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/volarjs/volar.js.git",
|
|
12
|
+
"directory": "packages/test-utils"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "latest"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@volar/language-server": "2.0.0-alpha.2",
|
|
19
|
+
"vscode-languageserver-textdocument": "^1.0.11",
|
|
20
|
+
"vscode-uri": "^3.0.8"
|
|
21
|
+
}
|
|
22
|
+
}
|