-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathserver.ts
562 lines (507 loc) · 20.7 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import {
IPCMessageReader, IPCMessageWriter, createConnection, IConnection, TextDocuments, TextDocument,
Diagnostic, DiagnosticSeverity, InitializeResult, Position, Range, TextDocumentPositionParams, Hover, MarkedString,
Location
} from 'vscode-languageserver';
import Uri from 'vscode-uri';
import * as Url from 'url';
import * as path from 'path';
import * as fs from 'fs';
import * as jsonMap from 'json-source-map';
import * as gltfValidator from 'gltf-validator';
// Create a connection for the server. The connection uses Node's IPC as a transport
let connection: IConnection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process));
// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments = new TextDocuments();
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
interface JsonMap {
data: any,
pointers: any;
};
interface ParseResult {
jsonMap: JsonMap,
parseable: boolean
}
let documentsToHandle: Map<TextDocument, ParseResult> = new Map<TextDocument, ParseResult>();
let debounceTimer: NodeJS.Timer;
/**
* Attempt to parse a JSON document into a map of JSON pointers.
* Catch and report any parsing errors encountered.
*
* @param textDocument The document to parse
* @return A map of JSON pointers to document text locations, or `undefined`
*/
function tryGetJsonMap(textDocument: TextDocument): JsonMap {
try {
return jsonMap.parse(textDocument.getText());
} catch (ex) {
console.warn('Error parsing glTF JSON document: ' + textDocument.uri);
}
return undefined;
}
/**
* Evaluate whether a given document URI should be passed to the glTF Validator
* for further processing.
*
* @param textDocument The document to evaluate
* @return True if the URI appears to be a local `.gltf` file.
*/
function isLocalGltf(textDocument: TextDocument): boolean {
const lowerUri = textDocument.uri.toLowerCase();
return (lowerUri.startsWith('file:///') && lowerUri.endsWith('.gltf'));
}
// After the server has started the client sends an initialize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilities.
connection.onInitialize((): InitializeResult => {
return {
capabilities: {
// Tell the client that the server works in FULL text document sync mode
textDocumentSync: documents.syncKind,
// Tell the client we provide hovers
hoverProvider: true,
// Tell the client we provide definitions
definitionProvider: true,
}
}
});
// The settings interface describe the server relevant settings part
interface GltfSettings {
Validation: ValidatorSettings;
}
interface ValidatorSettings {
enable: boolean;
debounce: number;
maxIssues: number;
ignoredIssues: Array<string>;
severityOverrides: object;
}
let currentSettings: GltfSettings;
//
// This is sent on server activation, and again for every configuration change.
//
connection.onDidChangeConfiguration((change) => {
currentSettings = <GltfSettings>change.settings.glTF;
if (currentSettings.Validation.enable) {
// Schedule revalidation of all open text documents using the new settings.
documents.all().forEach(scheduleParsing);
} else {
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
documents.all().forEach(clearTextDocument);
}
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
if (currentSettings.Validation.enable) {
scheduleParsing(change.document);
}
});
// Turn off validation of closed documents.
documents.onDidClose(change => {
if (currentSettings.Validation.enable) {
unscheduleParsing(change.document);
}
// A text document was closed we clear the diagnostics
connection.sendDiagnostics({ uri: change.document.uri, diagnostics: [] });
});
/**
* Schedule a document for glTF parsing after the debounce timeout.
*
* @param textDocument The document to schedule validator for.
*/
function scheduleParsing(textDocument: TextDocument): void {
if (isLocalGltf(textDocument)) {
console.log('schedule ' + textDocument.uri);
documentsToHandle.set(textDocument, { jsonMap: null, parseable: true});
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = undefined;
}
debounceTimer = setTimeout(() => {
documentsToHandle.forEach(parseTextDocument);
}, currentSettings.Validation.debounce);
}
}
/**
* Remove a glTF document from scheduled validation, possibly because
* the document has closed or is no longer available to validate.
*
* @param textDocument The document to un-schedule
*/
function unscheduleParsing(textDocument: TextDocument): void {
console.log('un-schedule ' + textDocument.uri);
documentsToHandle.delete(textDocument);
}
/**
* Clear any previous validation output messages from a particular document,
* likely because glTF validation has been disabled.
*
* @param textDocument The document to clear
*/
function clearTextDocument(textDocument: TextDocument): void {
unscheduleParsing(textDocument);
if (isLocalGltf(textDocument)) {
console.log('disable validation ' + textDocument.uri);
const diagnostics: Diagnostic[] = [];
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
}
}
/**
* Immediately launch the glTF Validator on a .gltf JSON document.
*
* @param textDocument The document to validate
*/
function parseTextDocument(parseResult: ParseResult, textDocument: TextDocument): void {
console.log('validate ' + textDocument.uri);
const fileName = Uri.parse(textDocument.uri).fsPath;
const baseName = path.basename(fileName);
const gltfText = textDocument.getText();
const folderName = path.resolve(fileName, '..');
if (parseResult.parseable) {
if (!parseResult.jsonMap) {
parseResult.jsonMap = tryGetJsonMap(textDocument);
if (!parseResult.jsonMap) {
parseResult.parseable = false;
let diagnostics: Diagnostic[] = [getDiagnostic({
message: 'Error parsing JSON document.',
isFromLanguageServer: true
}, {data: null, pointers: null})];
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
return;
}
}
}
if ((!parseResult.jsonMap.data.asset) || (!parseResult.jsonMap.data.asset.version) || (parseResult.jsonMap.data.asset.version[0] === '1')) {
let diagnostics: Diagnostic[] = [getDiagnostic({
message: 'Validation not available for glTF 1.0 files.',
isFromLanguageServer: true,
severity: 2
}, jsonMap)];
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
parseResult.parseable = false;
return;
}
gltfValidator.validateString(gltfText, {
uri: baseName,
maxIssues: currentSettings.Validation.maxIssues,
ignoredIssues: currentSettings.Validation.ignoredIssues,
severityOverrides: currentSettings.Validation.severityOverrides,
externalResourceFunction: (uri) =>
new Promise((resolve, reject) => {
uri = path.resolve(folderName, decodeURIComponent(uri));
fs.readFile(uri, (err, data) => {
console.log("Loading external file: " + uri);
if (err) {
console.warn("Error: " + err.toString());
reject(err.toString());
return;
}
resolve(data);
});
}
),
}).then((result) => {
let diagnostics: Diagnostic[] = [];
if (result.issues && result.issues.messages) {
const messages = result.issues.messages;
const numMessages = messages.length;
for (let i = 0; i < numMessages; ++i) {
let info = messages[i];
if (info.message) {
diagnostics.push(getDiagnostic(info, parseResult.jsonMap));
}
}
if (result.issues.truncated) {
diagnostics.push(getDiagnostic({
message: 'VALIDATION ABORTED: Too many messages produced.',
isFromLanguageServer: true
}, parseResult.jsonMap));
}
}
// Send the computed diagnostics to VSCode, clearing any old messages.
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
if (diagnostics.length === 0) {
console.log('Validation passed: ' + fileName);
} else {
console.log(diagnostics.length.toFixed() + ' validation messages for ' + fileName);
}
}, (result) => {
// Validator's error
console.warn('glTF Validator failed on: ' + fileName);
console.warn(result);
let diagnostics: Diagnostic[] = [getDiagnostic({
message: 'glTF Validator error: ' + result,
isFromLanguageServer: true
}, {data: null, pointers: null})];
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
});
}
/**
* Convert an individual glTF Validator message to a Language Server Diagnostic.
*
* @param info An object containing a message from the glTF Validator's output messages
* @param map A map of JSON pointers to document text locations
* @return A `Diagnostic` to send back to the client
*/
function getDiagnostic(info: any, map: JsonMap): Diagnostic {
let range: Range = {
start: { line: 0, character: 0 },
end: { line: 0, character: Number.MAX_VALUE }
};
let severity: DiagnosticSeverity = DiagnosticSeverity.Error;
switch (info.severity) {
case 1:
severity = DiagnosticSeverity.Warning;
break;
case 2:
severity = DiagnosticSeverity.Information;
break;
case 3:
severity = DiagnosticSeverity.Hint;
break;
default:
break;
}
if (info.pointer) {
const pointerName = info.pointer;
if (map.pointers.hasOwnProperty(pointerName)) {
const pointer = map.pointers[pointerName];
const start = pointer.key || pointer.value;
range.start.line = start.line;
range.start.character = start.column;
const end = pointer.valueEnd;
range.end.line = end.line;
range.end.character = end.column;
}
}
return {
code: info.code,
severity: severity,
range,
message: info.message,
source: (info.isFromLanguageServer ? 'glTF Language Server' : 'glTF Validator')
};
}
function positionContained(selection: Position, rangeStart: Position, rangeEnd: Position): boolean {
return ((rangeStart.line === selection.line && rangeStart.character <= selection.character) || rangeStart.line < selection.line) &&
((rangeEnd.line === selection.line && rangeEnd.character >= selection.character) || rangeEnd.line > selection.line);
}
function getFromPath(glTF: any, path : string) {
const pathSplit = path.split('/');
const numPathSegments = pathSplit.length;
let result = glTF;
const firstValidIndex = 1; // Because the path has a leading slash.
for (let i = firstValidIndex; i < numPathSegments; ++i) {
result = result[pathSplit[i]];
}
return result;
}
interface PathData {
path: string,
start: Position,
end: Position,
jsonMap: JsonMap
};
function getPath(textDocumentPosition: TextDocumentPositionParams): PathData {
let hoverPos = textDocumentPosition.position;
let document = documents.get(textDocumentPosition.textDocument.uri);
let parseResult = documentsToHandle.get(document);
if (!parseResult || !parseResult.parseable || !parseResult.jsonMap) {
return null;
}
let jsonMap = parseResult.jsonMap;
let lastPath: string;
let lastStartPos: Position;
let lastEndPos: Position;
for (let path of Object.keys(jsonMap.pointers)) {
let position = jsonMap.pointers[path];
let startPos = document.positionAt(position.value.pos);
let endPos = document.positionAt(position.valueEnd.pos);
if (positionContained(hoverPos, startPos, endPos)) {
lastPath = path;
lastStartPos = startPos;
lastEndPos = endPos;
}
}
if (!lastPath) {
return null;
}
return { path: lastPath, start: lastStartPos, end: lastEndPos, jsonMap: jsonMap };
}
connection.onDefinition((textDocumentPosition: TextDocumentPositionParams): Location => {
let pathData = getPath(textDocumentPosition);
if (!pathData) {
return null;
}
let path = pathData.path;
let document = documents.get(textDocumentPosition.textDocument.uri);
const pathSplit = path.split('/');
const numPathSegments = pathSplit.length;
let result = pathData.jsonMap.data;
function makeLocation(position?: any, uri?: string) {
let range: Range;
if (position == null) {
range = Range.create(0, 0, 0, 0);
} else {
range = Range.create(document.positionAt(position.value.pos), document.positionAt(position.valueEnd.pos));
}
if (uri == null) {
uri = textDocumentPosition.textDocument.uri;
}
return Location.create(uri, range);
}
function makeDataUri(doc: TextDocumentPositionParams, path: string): string {
return 'gltf-dataUri:' + path + '#' + encodeURIComponent(Uri.parse(doc.textDocument.uri).fsPath);
}
const firstValidIndex = 1; // Because the path has a leading slash.
let inNodes: boolean = false;
let inChannels: boolean = false;
let inAccessors: boolean = false;
let inDraco: boolean = false;
let currentPath: string = '';
let currentAnimationPath: string;
for (let i = firstValidIndex; i < numPathSegments; ++i) {
let part = pathSplit[i];
currentPath += '/' + part;
result = result[part];
if (typeof result != 'object')
{
if (part === 'scene') {
return makeLocation(pathData.jsonMap.pointers['/scenes/' + result]);
}
else if (part === 'mesh') {
return makeLocation(pathData.jsonMap.pointers['/meshes/' + result]);
}
else if (part === 'skin') {
return makeLocation(pathData.jsonMap.pointers['/skins/' + result]);
}
else if (part === 'material') {
return makeLocation(pathData.jsonMap.pointers['/materials/' + result]);
}
else if (part === 'input' || part === 'output' || part === 'inverseBindMatrices') {
return makeLocation(pathData.jsonMap.pointers['/accessors/' + result]);
}
else if (part === 'indices') {
let primitivePath = currentPath.substr(0, currentPath.length - '/indices'.length);
let primitive = getFromPath(pathData.jsonMap.data, primitivePath);
if (primitive && primitive.extensions && primitive.extensions['KHR_draco_mesh_compression']) {
let indicesPath = primitivePath + '/extensions/KHR_draco_mesh_compression/attributes/indices';
let uri = makeDataUri(textDocumentPosition, indicesPath);
return makeLocation(null, uri);
} else {
return makeLocation(pathData.jsonMap.pointers['/accessors/' + result]);
}
}
else if (part === 'POSITION' || part === 'NORMAL' || part === 'TANGENT'|| part === 'TEXCOORD_0' || part === 'TEXCOORD_1' || part === 'COLOR_0' || part === 'JOINTS_0' || part === 'WEIGHTS_0') {
if (inDraco) {
let uri = makeDataUri(textDocumentPosition, currentPath);
return makeLocation(null, uri);
} else {
return makeLocation(pathData.jsonMap.pointers['/accessors/' + result]);
}
}
else if (part === 'node' || part === 'skeleton' || inNodes ) {
return makeLocation(pathData.jsonMap.pointers['/nodes/' + result]);
}
else if (part === 'bufferView') {
return makeLocation(pathData.jsonMap.pointers['/bufferViews/' + result]);
}
else if (part === 'buffer') {
return makeLocation(pathData.jsonMap.pointers['/buffers/' + result]);
}
else if (part === 'index') {
return makeLocation(pathData.jsonMap.pointers['/textures/' + result]);
}
else if (part === 'sampler') {
if (inChannels) {
return makeLocation(pathData.jsonMap.pointers[currentAnimationPath + '/samplers/' + result]);
} else {
return makeLocation(pathData.jsonMap.pointers['/samplers/' + result]);
}
}
else if (part === 'source') {
return makeLocation(pathData.jsonMap.pointers['/images/' + result]);
}
else if (part === 'camera') {
return makeLocation(pathData.jsonMap.pointers['/cameras/' + result]);
} else if (part === 'fragmentShader' || part === 'vertexShader') {
return makeLocation(pathData.jsonMap.pointers['/shaders/' + result]);
}
}
else {
if (part === 'nodes' || part === 'children' || part === 'joints') {
inNodes = true;
}
else if (part === 'channels') {
inChannels = true;
currentAnimationPath = currentPath.substring(0, currentPath.length - '/channels'.length);
}
else if (result.uri !== undefined) {
if (!result.uri.startsWith('data:') && !currentPath.startsWith('/images/')) {
return makeLocation(null, Url.resolve(textDocumentPosition.textDocument.uri, result.uri));
} else {
let uri = makeDataUri(textDocumentPosition, currentPath);
return makeLocation(null, uri);
}
} else if (part === 'accessors') {
inAccessors = true;
} else if (part === 'KHR_draco_mesh_compression') {
inDraco = true;
} else if (inAccessors && !path.includes('bufferView')) {
let uri = makeDataUri(textDocumentPosition, currentPath);
return makeLocation(null, uri);
}
}
}
return null;
});
connection.onHover((textDocumentPosition: TextDocumentPositionParams): Hover => {
let pathData = getPath(textDocumentPosition);
if (!pathData) {
return null;
}
let path = pathData.path;
if (path.startsWith('/images/')) {
let splitPath = path.split('/');
if (splitPath.length > 3) {
path = splitPath.slice(0, 3).join('/');
}
let imageData = getFromPath(pathData.jsonMap.data, path);
if (imageData && imageData.uri) {
imageData = imageData.uri;
if (!imageData.startsWith('data:')) {
imageData = Url.resolve(textDocumentPosition.textDocument.uri, imageData);
}
let contents: MarkedString[] = [`![${path}](${imageData})`];
return {
contents: contents,
range: Range.create(pathData.start, pathData.end)
};
}
}
if (path.includes('diffuseFactor') || path.includes('specularFactor') || path.includes('baseColorFactor') || path.includes('emissiveFactor'))
{
if (!Number.isNaN(parseInt(path.charAt(path.length-1)))) {
path = path.substring(0, path.length-2);
}
let colorData = getFromPath(pathData.jsonMap.data, path);
let red = Math.round(colorData[0] * 255);
let blue = Math.round(colorData[1] * 255);
let green = Math.round(colorData[2] * 255);
let hexColor = ((red < 16) ? '0' : '') + red.toString(16) + ((blue < 16) ? '0' : '') + blue.toString(16) + ((green < 16) ? '0' : '') + green.toString(16);
let contents: MarkedString[] = [`![${hexColor}](https://placehold.it/50/${hexColor}/000000?text=+)`];
return {
contents: contents,
range: Range.create(pathData.start, pathData.end)
};
}
return null;
});
// Listen on the connection
connection.listen();