@player-tools/json-language-server 0.2.2--canary.20.454
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/README.md +3 -0
- package/dist/index.cjs.js +207 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.esm.js +199 -0
- package/package.json +34 -0
- package/src/index.ts +240 -0
- package/src/utils.ts +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
# @player-tools/json-language-server
|
|
2
|
+
|
|
3
|
+
This package starts a node JSON-RPC server which maps LSP requests to the [@player-language/service](https://github.intuit.com/player/language/tree/master/packages/service). This is typically started by an IDE and used in conjunction with a client to receive the language features.
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var vscodeLanguageserver = require('vscode-languageserver');
|
|
6
|
+
var vscodeLanguageserverTextdocument = require('vscode-languageserver-textdocument');
|
|
7
|
+
var jsonLanguageService = require('@player-tools/json-language-service');
|
|
8
|
+
var fs = require('fs');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
13
|
+
|
|
14
|
+
var __async$1 = (__this, __arguments, generator) => {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
var fulfilled = (value) => {
|
|
17
|
+
try {
|
|
18
|
+
step(generator.next(value));
|
|
19
|
+
} catch (e) {
|
|
20
|
+
reject(e);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var rejected = (value) => {
|
|
24
|
+
try {
|
|
25
|
+
step(generator.throw(value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
31
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
function cancel() {
|
|
35
|
+
return new vscodeLanguageserver.ResponseError(-32800, "Request cancelled");
|
|
36
|
+
}
|
|
37
|
+
function runAndCatch(func, token, errorVal) {
|
|
38
|
+
return __async$1(this, null, function* () {
|
|
39
|
+
if (token.isCancellationRequested) {
|
|
40
|
+
return cancel();
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const result = yield func();
|
|
44
|
+
if (token.isCancellationRequested) {
|
|
45
|
+
return cancel();
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
} catch (e) {
|
|
49
|
+
return errorVal;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
var __async = (__this, __arguments, generator) => {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
var fulfilled = (value) => {
|
|
57
|
+
try {
|
|
58
|
+
step(generator.next(value));
|
|
59
|
+
} catch (e) {
|
|
60
|
+
reject(e);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var rejected = (value) => {
|
|
64
|
+
try {
|
|
65
|
+
step(generator.throw(value));
|
|
66
|
+
} catch (e) {
|
|
67
|
+
reject(e);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
71
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const dateFormat = new Intl.DateTimeFormat("en", {
|
|
75
|
+
year: "numeric",
|
|
76
|
+
month: "2-digit",
|
|
77
|
+
day: "2-digit",
|
|
78
|
+
hour12: false,
|
|
79
|
+
hour: "2-digit",
|
|
80
|
+
minute: "2-digit",
|
|
81
|
+
second: "2-digit"
|
|
82
|
+
});
|
|
83
|
+
const logFilePath = process.argv.length === 4 && fs__default["default"].existsSync(process.argv[3]) && process.argv[3];
|
|
84
|
+
const formatLog = (a) => {
|
|
85
|
+
const msg = typeof a === "string" ? a : JSON.stringify(a);
|
|
86
|
+
const date = new Date();
|
|
87
|
+
return `${dateFormat.format(date)},$${date.getMilliseconds()} | ${msg}
|
|
88
|
+
`;
|
|
89
|
+
};
|
|
90
|
+
const fileLog = logFilePath ? (a) => fs__default["default"].appendFile(logFilePath, formatLog(a), () => {
|
|
91
|
+
}) : () => {
|
|
92
|
+
};
|
|
93
|
+
const service = new jsonLanguageService.PlayerLanguageService();
|
|
94
|
+
const connection = vscodeLanguageserver.createConnection(vscodeLanguageserver.ProposedFeatures.all);
|
|
95
|
+
const documents = new vscodeLanguageserver.TextDocuments(vscodeLanguageserverTextdocument.TextDocument);
|
|
96
|
+
let hasConfigurationCapability = false;
|
|
97
|
+
process.on("unhandledRejection", (e) => {
|
|
98
|
+
console.error(e.message);
|
|
99
|
+
});
|
|
100
|
+
process.on("uncaughtException", (e) => {
|
|
101
|
+
console.error(e.message);
|
|
102
|
+
});
|
|
103
|
+
console.log = (a) => {
|
|
104
|
+
fileLog(a);
|
|
105
|
+
connection.console.log(JSON.stringify(a, null, 2));
|
|
106
|
+
};
|
|
107
|
+
console.error = (a) => {
|
|
108
|
+
fileLog(a);
|
|
109
|
+
connection.console.error(JSON.stringify(a, null, 2));
|
|
110
|
+
};
|
|
111
|
+
function validate(textDocument) {
|
|
112
|
+
return __async(this, null, function* () {
|
|
113
|
+
const diagnostics = yield service.validateTextDocument(textDocument);
|
|
114
|
+
if (diagnostics) {
|
|
115
|
+
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
connection.onInitialize((params) => {
|
|
120
|
+
const { capabilities } = params;
|
|
121
|
+
hasConfigurationCapability = Boolean(capabilities.workspace && Boolean(capabilities.workspace.configuration));
|
|
122
|
+
fileLog("Initialized Player LSP Server");
|
|
123
|
+
return {
|
|
124
|
+
capabilities: {
|
|
125
|
+
textDocumentSync: vscodeLanguageserver.TextDocumentSyncKind.Full,
|
|
126
|
+
codeActionProvider: true,
|
|
127
|
+
definitionProvider: true,
|
|
128
|
+
completionProvider: {
|
|
129
|
+
resolveProvider: false,
|
|
130
|
+
triggerCharacters: ['"', ":"]
|
|
131
|
+
},
|
|
132
|
+
documentFormattingProvider: true,
|
|
133
|
+
documentRangeFormattingProvider: true,
|
|
134
|
+
hoverProvider: true
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
connection.onCompletion((textDocumentPosition, token) => __async(undefined, null, function* () {
|
|
139
|
+
return runAndCatch(() => {
|
|
140
|
+
const document = documents.get(textDocumentPosition.textDocument.uri);
|
|
141
|
+
if (document !== void 0) {
|
|
142
|
+
return service.getCompletionsAtPosition(document, textDocumentPosition.position);
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
}, token, null);
|
|
146
|
+
}));
|
|
147
|
+
connection.onHover((hoverParams, token) => __async(undefined, null, function* () {
|
|
148
|
+
return runAndCatch(() => {
|
|
149
|
+
const document = documents.get(hoverParams.textDocument.uri);
|
|
150
|
+
if (document !== void 0) {
|
|
151
|
+
return service.getHoverInfoAtPosition(document, hoverParams.position);
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}, token, null);
|
|
155
|
+
}));
|
|
156
|
+
connection.onCompletionResolve((item, token) => runAndCatch(() => service.resolveCompletionItem(item), token, item));
|
|
157
|
+
connection.onDocumentFormatting((formattingParams, token) => runAndCatch(() => {
|
|
158
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
159
|
+
if (document !== void 0) {
|
|
160
|
+
return service.formatTextDocument(document, formattingParams.options);
|
|
161
|
+
}
|
|
162
|
+
return void 0;
|
|
163
|
+
}, token, null));
|
|
164
|
+
connection.onDocumentRangeFormatting((formattingParams, token) => runAndCatch(() => {
|
|
165
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
166
|
+
if (document !== void 0) {
|
|
167
|
+
return service.formatTextDocument(document, formattingParams.options, formattingParams.range);
|
|
168
|
+
}
|
|
169
|
+
return void 0;
|
|
170
|
+
}, token, null));
|
|
171
|
+
connection.onDefinition((definitionParams, token) => runAndCatch(() => {
|
|
172
|
+
const document = documents.get(definitionParams.textDocument.uri);
|
|
173
|
+
if (document !== void 0) {
|
|
174
|
+
return service.getDefinitionAtPosition(document, definitionParams.position);
|
|
175
|
+
}
|
|
176
|
+
return void 0;
|
|
177
|
+
}, token, null));
|
|
178
|
+
connection.onInitialized(() => {
|
|
179
|
+
if (hasConfigurationCapability) {
|
|
180
|
+
connection.client.register(vscodeLanguageserver.DidChangeConfigurationNotification.type, void 0);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
connection.onCodeAction((codeAction) => {
|
|
184
|
+
const document = documents.get(codeAction.textDocument.uri);
|
|
185
|
+
if (!document) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
return service.getCodeActionsInRange(document, codeAction.context);
|
|
189
|
+
});
|
|
190
|
+
documents.onDidClose((change) => {
|
|
191
|
+
service.onClose(change.document);
|
|
192
|
+
});
|
|
193
|
+
documents.onDidChangeContent((change) => {
|
|
194
|
+
validate(change.document);
|
|
195
|
+
});
|
|
196
|
+
connection.onNotification("player/setAssetBundles", (assetBundles) => {
|
|
197
|
+
if (Array.isArray(assetBundles)) {
|
|
198
|
+
console.log("Updating asset type bundles");
|
|
199
|
+
service.setAssetTypes(assetBundles);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
fileLog("Starting Player LSP Server");
|
|
203
|
+
documents.listen(connection);
|
|
204
|
+
connection.listen();
|
|
205
|
+
|
|
206
|
+
exports.runAndCatch = runAndCatch;
|
|
207
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CancellationToken, ResponseError } from 'vscode-languageserver';
|
|
2
|
+
|
|
3
|
+
/** Run the given function and handle being cancelled */
|
|
4
|
+
declare function runAndCatch<T>(func: () => Promise<T> | T, token: CancellationToken, errorVal: T): Promise<T | ResponseError<any>>;
|
|
5
|
+
|
|
6
|
+
export { runAndCatch };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { ResponseError, createConnection, ProposedFeatures, TextDocuments, TextDocumentSyncKind, DidChangeConfigurationNotification } from 'vscode-languageserver';
|
|
2
|
+
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
3
|
+
import { PlayerLanguageService } from '@player-tools/json-language-service';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
|
|
6
|
+
var __async$1 = (__this, __arguments, generator) => {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
var fulfilled = (value) => {
|
|
9
|
+
try {
|
|
10
|
+
step(generator.next(value));
|
|
11
|
+
} catch (e) {
|
|
12
|
+
reject(e);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var rejected = (value) => {
|
|
16
|
+
try {
|
|
17
|
+
step(generator.throw(value));
|
|
18
|
+
} catch (e) {
|
|
19
|
+
reject(e);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
23
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
function cancel() {
|
|
27
|
+
return new ResponseError(-32800, "Request cancelled");
|
|
28
|
+
}
|
|
29
|
+
function runAndCatch(func, token, errorVal) {
|
|
30
|
+
return __async$1(this, null, function* () {
|
|
31
|
+
if (token.isCancellationRequested) {
|
|
32
|
+
return cancel();
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const result = yield func();
|
|
36
|
+
if (token.isCancellationRequested) {
|
|
37
|
+
return cancel();
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
return errorVal;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
var __async = (__this, __arguments, generator) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
var fulfilled = (value) => {
|
|
49
|
+
try {
|
|
50
|
+
step(generator.next(value));
|
|
51
|
+
} catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var rejected = (value) => {
|
|
56
|
+
try {
|
|
57
|
+
step(generator.throw(value));
|
|
58
|
+
} catch (e) {
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
63
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const dateFormat = new Intl.DateTimeFormat("en", {
|
|
67
|
+
year: "numeric",
|
|
68
|
+
month: "2-digit",
|
|
69
|
+
day: "2-digit",
|
|
70
|
+
hour12: false,
|
|
71
|
+
hour: "2-digit",
|
|
72
|
+
minute: "2-digit",
|
|
73
|
+
second: "2-digit"
|
|
74
|
+
});
|
|
75
|
+
const logFilePath = process.argv.length === 4 && fs.existsSync(process.argv[3]) && process.argv[3];
|
|
76
|
+
const formatLog = (a) => {
|
|
77
|
+
const msg = typeof a === "string" ? a : JSON.stringify(a);
|
|
78
|
+
const date = new Date();
|
|
79
|
+
return `${dateFormat.format(date)},$${date.getMilliseconds()} | ${msg}
|
|
80
|
+
`;
|
|
81
|
+
};
|
|
82
|
+
const fileLog = logFilePath ? (a) => fs.appendFile(logFilePath, formatLog(a), () => {
|
|
83
|
+
}) : () => {
|
|
84
|
+
};
|
|
85
|
+
const service = new PlayerLanguageService();
|
|
86
|
+
const connection = createConnection(ProposedFeatures.all);
|
|
87
|
+
const documents = new TextDocuments(TextDocument);
|
|
88
|
+
let hasConfigurationCapability = false;
|
|
89
|
+
process.on("unhandledRejection", (e) => {
|
|
90
|
+
console.error(e.message);
|
|
91
|
+
});
|
|
92
|
+
process.on("uncaughtException", (e) => {
|
|
93
|
+
console.error(e.message);
|
|
94
|
+
});
|
|
95
|
+
console.log = (a) => {
|
|
96
|
+
fileLog(a);
|
|
97
|
+
connection.console.log(JSON.stringify(a, null, 2));
|
|
98
|
+
};
|
|
99
|
+
console.error = (a) => {
|
|
100
|
+
fileLog(a);
|
|
101
|
+
connection.console.error(JSON.stringify(a, null, 2));
|
|
102
|
+
};
|
|
103
|
+
function validate(textDocument) {
|
|
104
|
+
return __async(this, null, function* () {
|
|
105
|
+
const diagnostics = yield service.validateTextDocument(textDocument);
|
|
106
|
+
if (diagnostics) {
|
|
107
|
+
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
connection.onInitialize((params) => {
|
|
112
|
+
const { capabilities } = params;
|
|
113
|
+
hasConfigurationCapability = Boolean(capabilities.workspace && Boolean(capabilities.workspace.configuration));
|
|
114
|
+
fileLog("Initialized Player LSP Server");
|
|
115
|
+
return {
|
|
116
|
+
capabilities: {
|
|
117
|
+
textDocumentSync: TextDocumentSyncKind.Full,
|
|
118
|
+
codeActionProvider: true,
|
|
119
|
+
definitionProvider: true,
|
|
120
|
+
completionProvider: {
|
|
121
|
+
resolveProvider: false,
|
|
122
|
+
triggerCharacters: ['"', ":"]
|
|
123
|
+
},
|
|
124
|
+
documentFormattingProvider: true,
|
|
125
|
+
documentRangeFormattingProvider: true,
|
|
126
|
+
hoverProvider: true
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
connection.onCompletion((textDocumentPosition, token) => __async(undefined, null, function* () {
|
|
131
|
+
return runAndCatch(() => {
|
|
132
|
+
const document = documents.get(textDocumentPosition.textDocument.uri);
|
|
133
|
+
if (document !== void 0) {
|
|
134
|
+
return service.getCompletionsAtPosition(document, textDocumentPosition.position);
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}, token, null);
|
|
138
|
+
}));
|
|
139
|
+
connection.onHover((hoverParams, token) => __async(undefined, null, function* () {
|
|
140
|
+
return runAndCatch(() => {
|
|
141
|
+
const document = documents.get(hoverParams.textDocument.uri);
|
|
142
|
+
if (document !== void 0) {
|
|
143
|
+
return service.getHoverInfoAtPosition(document, hoverParams.position);
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}, token, null);
|
|
147
|
+
}));
|
|
148
|
+
connection.onCompletionResolve((item, token) => runAndCatch(() => service.resolveCompletionItem(item), token, item));
|
|
149
|
+
connection.onDocumentFormatting((formattingParams, token) => runAndCatch(() => {
|
|
150
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
151
|
+
if (document !== void 0) {
|
|
152
|
+
return service.formatTextDocument(document, formattingParams.options);
|
|
153
|
+
}
|
|
154
|
+
return void 0;
|
|
155
|
+
}, token, null));
|
|
156
|
+
connection.onDocumentRangeFormatting((formattingParams, token) => runAndCatch(() => {
|
|
157
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
158
|
+
if (document !== void 0) {
|
|
159
|
+
return service.formatTextDocument(document, formattingParams.options, formattingParams.range);
|
|
160
|
+
}
|
|
161
|
+
return void 0;
|
|
162
|
+
}, token, null));
|
|
163
|
+
connection.onDefinition((definitionParams, token) => runAndCatch(() => {
|
|
164
|
+
const document = documents.get(definitionParams.textDocument.uri);
|
|
165
|
+
if (document !== void 0) {
|
|
166
|
+
return service.getDefinitionAtPosition(document, definitionParams.position);
|
|
167
|
+
}
|
|
168
|
+
return void 0;
|
|
169
|
+
}, token, null));
|
|
170
|
+
connection.onInitialized(() => {
|
|
171
|
+
if (hasConfigurationCapability) {
|
|
172
|
+
connection.client.register(DidChangeConfigurationNotification.type, void 0);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
connection.onCodeAction((codeAction) => {
|
|
176
|
+
const document = documents.get(codeAction.textDocument.uri);
|
|
177
|
+
if (!document) {
|
|
178
|
+
return [];
|
|
179
|
+
}
|
|
180
|
+
return service.getCodeActionsInRange(document, codeAction.context);
|
|
181
|
+
});
|
|
182
|
+
documents.onDidClose((change) => {
|
|
183
|
+
service.onClose(change.document);
|
|
184
|
+
});
|
|
185
|
+
documents.onDidChangeContent((change) => {
|
|
186
|
+
validate(change.document);
|
|
187
|
+
});
|
|
188
|
+
connection.onNotification("player/setAssetBundles", (assetBundles) => {
|
|
189
|
+
if (Array.isArray(assetBundles)) {
|
|
190
|
+
console.log("Updating asset type bundles");
|
|
191
|
+
service.setAssetTypes(assetBundles);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
fileLog("Starting Player LSP Server");
|
|
195
|
+
documents.listen(connection);
|
|
196
|
+
connection.listen();
|
|
197
|
+
|
|
198
|
+
export { runAndCatch };
|
|
199
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-tools/json-language-server",
|
|
3
|
+
"version": "0.2.2--canary.20.454",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@player-tools/json-language-service": "0.2.2--canary.20.454",
|
|
11
|
+
"vscode-languageserver": "^6.1.1",
|
|
12
|
+
"vscode-languageserver-textdocument": "^1.0.1",
|
|
13
|
+
"@babel/runtime": "7.15.4"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.cjs.js",
|
|
16
|
+
"module": "dist/index.esm.js",
|
|
17
|
+
"typings": "dist/index.d.ts",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/player-ui/tools"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/player-ui/tools/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://player-ui.github.io",
|
|
28
|
+
"contributors": [
|
|
29
|
+
{
|
|
30
|
+
"name": "Ketan Reddy",
|
|
31
|
+
"url": "https://github.com/KetanReddy"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import type { InitializeParams } from 'vscode-languageserver';
|
|
3
|
+
import {
|
|
4
|
+
createConnection,
|
|
5
|
+
ProposedFeatures,
|
|
6
|
+
TextDocuments,
|
|
7
|
+
DidChangeConfigurationNotification,
|
|
8
|
+
TextDocumentSyncKind,
|
|
9
|
+
} from 'vscode-languageserver';
|
|
10
|
+
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
11
|
+
import { PlayerLanguageService } from '@player-tools/json-language-service';
|
|
12
|
+
import fs from 'fs';
|
|
13
|
+
import { runAndCatch } from './utils';
|
|
14
|
+
|
|
15
|
+
export * from './utils';
|
|
16
|
+
|
|
17
|
+
const dateFormat = new Intl.DateTimeFormat('en', {
|
|
18
|
+
year: 'numeric',
|
|
19
|
+
month: '2-digit',
|
|
20
|
+
day: '2-digit',
|
|
21
|
+
hour12: false,
|
|
22
|
+
hour: '2-digit',
|
|
23
|
+
minute: '2-digit',
|
|
24
|
+
second: '2-digit',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const logFilePath =
|
|
28
|
+
process.argv.length === 4 &&
|
|
29
|
+
fs.existsSync(process.argv[3]) &&
|
|
30
|
+
process.argv[3];
|
|
31
|
+
|
|
32
|
+
/** Format a log message to work in the console */
|
|
33
|
+
const formatLog = (a: unknown): string => {
|
|
34
|
+
const msg = typeof a === 'string' ? a : JSON.stringify(a);
|
|
35
|
+
const date = new Date();
|
|
36
|
+
return `${dateFormat.format(date)},$${date.getMilliseconds()} | ${msg} \n`;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const fileLog = logFilePath
|
|
40
|
+
? (a: unknown) =>
|
|
41
|
+
fs.appendFile(logFilePath, formatLog(a), () => {
|
|
42
|
+
/* do nothing */
|
|
43
|
+
})
|
|
44
|
+
: () => {
|
|
45
|
+
/* do nothing */
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const service = new PlayerLanguageService();
|
|
49
|
+
|
|
50
|
+
const connection = createConnection(ProposedFeatures.all);
|
|
51
|
+
const documents = new TextDocuments(TextDocument);
|
|
52
|
+
|
|
53
|
+
let hasConfigurationCapability = false;
|
|
54
|
+
|
|
55
|
+
process.on('unhandledRejection', (e: Error) => {
|
|
56
|
+
console.error(e.message);
|
|
57
|
+
});
|
|
58
|
+
process.on('uncaughtException', (e: Error) => {
|
|
59
|
+
console.error(e.message);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
console.log = (a: Error) => {
|
|
63
|
+
fileLog(a);
|
|
64
|
+
connection.console.log(JSON.stringify(a, null, 2));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
console.error = (a: Error) => {
|
|
68
|
+
fileLog(a);
|
|
69
|
+
connection.console.error(JSON.stringify(a, null, 2));
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** Handle validating a text-document and returning the response back to the extension */
|
|
73
|
+
async function validate(textDocument: TextDocument): Promise<void> {
|
|
74
|
+
const diagnostics = await service.validateTextDocument(textDocument);
|
|
75
|
+
|
|
76
|
+
if (diagnostics) {
|
|
77
|
+
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
connection.onInitialize((params: InitializeParams) => {
|
|
82
|
+
const { capabilities } = params;
|
|
83
|
+
|
|
84
|
+
// Does the client support the `workspace/configuration` request?
|
|
85
|
+
// If not, we will fall back using global settings
|
|
86
|
+
hasConfigurationCapability = Boolean(
|
|
87
|
+
capabilities.workspace && Boolean(capabilities.workspace.configuration)
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
fileLog('Initialized Player LSP Server');
|
|
91
|
+
return {
|
|
92
|
+
capabilities: {
|
|
93
|
+
textDocumentSync: TextDocumentSyncKind.Full,
|
|
94
|
+
codeActionProvider: true,
|
|
95
|
+
definitionProvider: true,
|
|
96
|
+
completionProvider: {
|
|
97
|
+
resolveProvider: false,
|
|
98
|
+
triggerCharacters: ['"', ':'],
|
|
99
|
+
},
|
|
100
|
+
documentFormattingProvider: true,
|
|
101
|
+
documentRangeFormattingProvider: true,
|
|
102
|
+
hoverProvider: true,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
connection.onCompletion(async (textDocumentPosition, token) => {
|
|
108
|
+
return runAndCatch(
|
|
109
|
+
() => {
|
|
110
|
+
const document = documents.get(textDocumentPosition.textDocument.uri);
|
|
111
|
+
if (document !== undefined) {
|
|
112
|
+
return service.getCompletionsAtPosition(
|
|
113
|
+
document,
|
|
114
|
+
textDocumentPosition.position
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
},
|
|
120
|
+
token,
|
|
121
|
+
null
|
|
122
|
+
);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
connection.onHover(async (hoverParams, token) => {
|
|
126
|
+
return runAndCatch(
|
|
127
|
+
() => {
|
|
128
|
+
const document = documents.get(hoverParams.textDocument.uri);
|
|
129
|
+
if (document !== undefined) {
|
|
130
|
+
return service.getHoverInfoAtPosition(document, hoverParams.position);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return null;
|
|
134
|
+
},
|
|
135
|
+
token,
|
|
136
|
+
null
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
connection.onCompletionResolve((item, token) =>
|
|
141
|
+
runAndCatch(() => service.resolveCompletionItem(item), token, item)
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
connection.onDocumentFormatting((formattingParams, token) =>
|
|
145
|
+
runAndCatch(
|
|
146
|
+
() => {
|
|
147
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
148
|
+
|
|
149
|
+
if (document !== undefined) {
|
|
150
|
+
return service.formatTextDocument(document, formattingParams.options);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return undefined;
|
|
154
|
+
},
|
|
155
|
+
token,
|
|
156
|
+
null
|
|
157
|
+
)
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
connection.onDocumentRangeFormatting((formattingParams, token) =>
|
|
161
|
+
runAndCatch(
|
|
162
|
+
() => {
|
|
163
|
+
const document = documents.get(formattingParams.textDocument.uri);
|
|
164
|
+
|
|
165
|
+
if (document !== undefined) {
|
|
166
|
+
return service.formatTextDocument(
|
|
167
|
+
document,
|
|
168
|
+
formattingParams.options,
|
|
169
|
+
formattingParams.range
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return undefined;
|
|
174
|
+
},
|
|
175
|
+
token,
|
|
176
|
+
null
|
|
177
|
+
)
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
connection.onDefinition((definitionParams, token) =>
|
|
181
|
+
runAndCatch(
|
|
182
|
+
() => {
|
|
183
|
+
const document = documents.get(definitionParams.textDocument.uri);
|
|
184
|
+
|
|
185
|
+
if (document !== undefined) {
|
|
186
|
+
return service.getDefinitionAtPosition(
|
|
187
|
+
document,
|
|
188
|
+
definitionParams.position
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return undefined;
|
|
193
|
+
},
|
|
194
|
+
token,
|
|
195
|
+
null
|
|
196
|
+
)
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
connection.onInitialized(() => {
|
|
200
|
+
if (hasConfigurationCapability) {
|
|
201
|
+
// Register for all configuration changes.
|
|
202
|
+
connection.client.register(
|
|
203
|
+
DidChangeConfigurationNotification.type,
|
|
204
|
+
undefined
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
connection.onCodeAction((codeAction) => {
|
|
210
|
+
const document = documents.get(codeAction.textDocument.uri);
|
|
211
|
+
if (!document) {
|
|
212
|
+
return [];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return service.getCodeActionsInRange(document, codeAction.context);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
documents.onDidClose((change) => {
|
|
219
|
+
service.onClose(change.document);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
documents.onDidChangeContent((change) => {
|
|
223
|
+
validate(change.document);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
connection.onNotification(
|
|
227
|
+
'player/setAssetBundles',
|
|
228
|
+
(assetBundles: Array<string>) => {
|
|
229
|
+
// Don't trust data over the wire
|
|
230
|
+
if (Array.isArray(assetBundles)) {
|
|
231
|
+
console.log('Updating asset type bundles');
|
|
232
|
+
service.setAssetTypes(assetBundles);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
fileLog('Starting Player LSP Server');
|
|
238
|
+
|
|
239
|
+
documents.listen(connection);
|
|
240
|
+
connection.listen();
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { CancellationToken } from 'vscode-languageserver';
|
|
2
|
+
import { ResponseError } from 'vscode-languageserver';
|
|
3
|
+
|
|
4
|
+
/** Get a cancellation error */
|
|
5
|
+
function cancel() {
|
|
6
|
+
return new ResponseError(
|
|
7
|
+
-32800 /* ErrorCodes.RequestCancelled */,
|
|
8
|
+
'Request cancelled'
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Run the given function and handle being cancelled */
|
|
13
|
+
export async function runAndCatch<T>(
|
|
14
|
+
func: () => Promise<T> | T,
|
|
15
|
+
token: CancellationToken,
|
|
16
|
+
errorVal: T
|
|
17
|
+
): Promise<T | ResponseError<any>> {
|
|
18
|
+
if (token.isCancellationRequested) {
|
|
19
|
+
return cancel();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const result = await func();
|
|
24
|
+
if (token.isCancellationRequested) {
|
|
25
|
+
return cancel();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return result;
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return errorVal;
|
|
31
|
+
}
|
|
32
|
+
}
|