@parcel/lsp 2.7.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present Devon Govett
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/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@parcel/lsp",
3
+ "version": "2.7.0",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "funding": {
9
+ "type": "opencollective",
10
+ "url": "https://opencollective.com/parcel"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/parcel-bundler/parcel.git"
15
+ },
16
+ "main": "./out/LspServer.js",
17
+ "source": "src/LspServer.ts",
18
+ "scripts": {
19
+ "vscode:prepublish": "yarn run compile",
20
+ "compile": "tsc -p ./",
21
+ "watch": "tsc -watch -p ./",
22
+ "pretest": "yarn run compile && yarn run lint",
23
+ "lint": "eslint src --ext ts",
24
+ "test": "node ./out/test/runTest.js"
25
+ },
26
+ "engines": {
27
+ "node": ">= 12.0.0",
28
+ "parcel": "^2.7.0"
29
+ },
30
+ "dependencies": {
31
+ "@parcel/diagnostic": "2.5.0",
32
+ "@parcel/plugin": "2.5.0",
33
+ "@parcel/types": "2.5.0",
34
+ "@parcel/utils": "2.5.0",
35
+ "@parcel/watcher": "2.0.5",
36
+ "node-ipc": "^9.1.4",
37
+ "nullthrows": "^1.1.1",
38
+ "ps-node": "^0.1.6",
39
+ "vscode-languageclient": "^8.0.1",
40
+ "vscode-languageserver": "^8.0.1",
41
+ "vscode-languageserver-textdocument": "^1.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@types/glob": "^7.1.3",
45
+ "@types/mocha": "^8.0.4",
46
+ "@types/node": "^12.11.7",
47
+ "@types/node-ipc": "^9.1.3",
48
+ "@types/vscode": "^1.67.0",
49
+ "@typescript-eslint/eslint-plugin": "^4.14.1",
50
+ "@typescript-eslint/parser": "^4.14.1",
51
+ "eslint": "^7.19.0",
52
+ "glob": "^7.1.6",
53
+ "typescript": "^4.6.4"
54
+ },
55
+ "gitHead": "9e5d05586577e89991ccf90400f2c741dca11aa3"
56
+ }
@@ -0,0 +1,239 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import * as path from 'path';
4
+ import * as fs from 'fs';
5
+ import * as os from 'os';
6
+ import {
7
+ createConnection,
8
+ TextDocuments,
9
+ Diagnostic,
10
+ DiagnosticSeverity,
11
+ ProposedFeatures,
12
+ InitializeParams,
13
+ DidChangeConfigurationNotification,
14
+ CompletionItem,
15
+ CompletionItemKind,
16
+ TextDocumentPositionParams,
17
+ TextDocumentSyncKind,
18
+ InitializeResult,
19
+ WorkDoneProgressServerReporter,
20
+ } from 'vscode-languageserver/node';
21
+
22
+ import {
23
+ CloseAction,
24
+ ErrorAction,
25
+ LanguageClient,
26
+ LanguageClientOptions,
27
+ MessageTransports,
28
+ } from 'vscode-languageclient/node';
29
+
30
+ import * as net from 'net';
31
+ import * as invariant from 'assert';
32
+ import nullthrows from 'nullthrows';
33
+ import {IPC} from 'node-ipc';
34
+
35
+ import {TextDocument} from 'vscode-languageserver-textdocument';
36
+ import * as watcher from '@parcel/watcher';
37
+
38
+ type IPCType = InstanceType<typeof IPC>;
39
+
40
+ const connection = createConnection(ProposedFeatures.all);
41
+
42
+ // Create a simple text document manager.
43
+ const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
44
+
45
+ let hasConfigurationCapability = false;
46
+ let hasWorkspaceFolderCapability = false;
47
+ let hasDiagnosticRelatedInformationCapability = false;
48
+
49
+ connection.onInitialize((params: InitializeParams) => {
50
+ const capabilities = params.capabilities;
51
+
52
+ // Does the client support the `workspace/configuration` request?
53
+ // If not, we fall back using global settings.
54
+ hasConfigurationCapability = !!(
55
+ capabilities.workspace && !!capabilities.workspace.configuration
56
+ );
57
+ hasWorkspaceFolderCapability = !!(
58
+ capabilities.workspace && !!capabilities.workspace.workspaceFolders
59
+ );
60
+ hasDiagnosticRelatedInformationCapability = !!(
61
+ capabilities.textDocument &&
62
+ capabilities.textDocument.publishDiagnostics &&
63
+ capabilities.textDocument.publishDiagnostics.relatedInformation
64
+ );
65
+
66
+ const result: InitializeResult = {
67
+ capabilities: {
68
+ textDocumentSync: TextDocumentSyncKind.Incremental,
69
+ // Tell the client that this server supports code completion.
70
+ completionProvider: {
71
+ resolveProvider: true,
72
+ },
73
+ },
74
+ };
75
+
76
+ if (hasWorkspaceFolderCapability) {
77
+ result.capabilities.workspace = {
78
+ workspaceFolders: {
79
+ supported: true,
80
+ },
81
+ };
82
+ }
83
+ return result;
84
+ });
85
+
86
+ connection.onInitialized(() => {
87
+ if (hasConfigurationCapability) {
88
+ // Register for all configuration changes.
89
+ connection.client.register(
90
+ DidChangeConfigurationNotification.type,
91
+ undefined,
92
+ );
93
+ }
94
+ if (hasWorkspaceFolderCapability) {
95
+ connection.workspace.onDidChangeWorkspaceFolders(_event => {
96
+ connection.console.log('Workspace folder change event received.');
97
+ });
98
+ }
99
+ });
100
+
101
+ class ProgressReporter {
102
+ progressReporterPromise?: Promise<WorkDoneProgressServerReporter> | null;
103
+ lastMessage?: string;
104
+ begin() {
105
+ this.progressReporterPromise = (async () => {
106
+ let reporter = await connection.window.createWorkDoneProgress();
107
+ reporter.begin('Parcel');
108
+ return reporter;
109
+ })();
110
+ this.progressReporterPromise.then(reporter => {
111
+ if (this.lastMessage != null) {
112
+ reporter.report(this.lastMessage);
113
+ }
114
+ });
115
+ }
116
+ async done() {
117
+ if (this.progressReporterPromise == null) {
118
+ this.begin();
119
+ }
120
+ invariant(this.progressReporterPromise != null);
121
+ (await this.progressReporterPromise).done();
122
+ this.progressReporterPromise = null;
123
+ }
124
+ async report(message: string) {
125
+ if (this.progressReporterPromise == null) {
126
+ this.lastMessage = message;
127
+ this.begin();
128
+ } else {
129
+ let r = await this.progressReporterPromise;
130
+ r.report(message);
131
+ }
132
+ }
133
+ }
134
+
135
+ function createIPCClientIfPossible(
136
+ parcelLspDir: string,
137
+ filePath: string,
138
+ ): {client: IPCType; uris: Set<string>} | undefined {
139
+ let transportName: string;
140
+ try {
141
+ transportName = JSON.parse(
142
+ fs.readFileSync(filePath, {
143
+ encoding: 'utf8',
144
+ }),
145
+ ).transportName;
146
+ } catch (e) {
147
+ // TODO: Handle this
148
+ console.log(e);
149
+ return;
150
+ }
151
+
152
+ let uris: Set<string> = new Set();
153
+ let client = new IPC();
154
+ client.config.id = `parcel-lsp-${process.pid}`;
155
+ client.config.retry = 1500;
156
+ client.connectTo(transportName, function () {
157
+ client.of[transportName].on(
158
+ 'message', //any event or message type your server listens for
159
+ function (data: any) {
160
+ switch (data.type) {
161
+ case 'parcelBuildEnd':
162
+ progressReporter.done();
163
+ break;
164
+
165
+ case 'parcelFileDiagnostics':
166
+ for (let [uri, diagnostics] of data.fileDiagnostics) {
167
+ connection.sendDiagnostics({uri, diagnostics});
168
+ uris.add(uri);
169
+ }
170
+ break;
171
+
172
+ case 'parcelBuildSuccess':
173
+ progressReporter.done();
174
+ break;
175
+
176
+ case 'parcelBuildStart':
177
+ uris.clear();
178
+ progressReporter.begin();
179
+ break;
180
+
181
+ case 'parcelBuildProgress':
182
+ progressReporter.report(data.message);
183
+ break;
184
+
185
+ default:
186
+ throw new Error();
187
+ }
188
+ },
189
+ );
190
+ });
191
+
192
+ return {client, uris};
193
+ }
194
+
195
+ let progressReporter = new ProgressReporter();
196
+ let clients: Map<string, {client: IPCType; uris: Set<string>}> = new Map();
197
+ let parcelLspDir = path.join(fs.realpathSync(os.tmpdir()), 'parcel-lsp');
198
+ fs.mkdirSync(parcelLspDir, {recursive: true});
199
+ // Search for currently running Parcel processes in the parcel-lsp dir.
200
+ // Create an IPC client connection for each running process.
201
+ for (let filename of fs.readdirSync(parcelLspDir)) {
202
+ const filepath = path.join(parcelLspDir, filename);
203
+ let client = createIPCClientIfPossible(parcelLspDir, filepath);
204
+ if (client) {
205
+ clients.set(filepath, client);
206
+ }
207
+ }
208
+
209
+ // Watch for new Parcel processes in the parcel-lsp dir, and disconnect the
210
+ // client for each corresponding connection when a Parcel process ends
211
+ watcher.subscribe(parcelLspDir, async (err, events) => {
212
+ if (err) {
213
+ throw err;
214
+ }
215
+
216
+ for (let event of events) {
217
+ if (event.type === 'create') {
218
+ let client = createIPCClientIfPossible(parcelLspDir, event.path);
219
+ if (client) {
220
+ clients.set(event.path, client);
221
+ }
222
+ } else if (event.type === 'delete') {
223
+ let existing = clients.get(event.path);
224
+ if (existing) {
225
+ clients.delete(event.path);
226
+ for (let id of Object.keys(existing.client.of)) {
227
+ existing.client.disconnect(id);
228
+ }
229
+ await Promise.all(
230
+ [...existing.uris].map(uri =>
231
+ connection.sendDiagnostics({uri, diagnostics: []}),
232
+ ),
233
+ );
234
+ }
235
+ }
236
+ }
237
+ });
238
+
239
+ connection.listen();
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es6",
5
+ "outDir": "out",
6
+ "lib": ["es6"],
7
+ "sourceMap": true,
8
+ "rootDir": "src",
9
+ "strict": true /* enable all strict type-checking options */
10
+ /* Additional Checks */
11
+ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
12
+ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
13
+ // "noUnusedParameters": true, /* Report errors on unused parameters. */
14
+ },
15
+ "exclude": ["node_modules", ".vscode-test"]
16
+ }