-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstartServer.ts
551 lines (489 loc) · 18.4 KB
/
startServer.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
import {
AugmentedThemeDocset,
FileTuple,
findRoot as findConfigFileRoot,
isError,
makeFileExists,
makeGetDefaultSchemaTranslations,
makeGetDefaultTranslations,
makeGetMetafieldDefinitions,
memoize,
parseJSON,
path,
recursiveReadDirectory,
SourceCodeType,
} from '@shopify/theme-check-common';
import {
Connection,
FileOperationRegistrationOptions,
InitializeResult,
ShowDocumentRequest,
TextDocumentSyncKind,
} from 'vscode-languageserver';
import { ClientCapabilities } from '../ClientCapabilities';
import { CodeActionKinds, CodeActionsProvider } from '../codeActions';
import { Commands, ExecuteCommandProvider } from '../commands';
import { CompletionsProvider } from '../completions';
import { GetSnippetNamesForURI } from '../completions/providers/RenderSnippetCompletionProvider';
import { DiagnosticsManager, makeRunChecks } from '../diagnostics';
import { DocumentHighlightsProvider } from '../documentHighlights/DocumentHighlightsProvider';
import { DocumentLinksProvider } from '../documentLinks';
import { DocumentManager } from '../documents';
import { OnTypeFormattingProvider } from '../formatting';
import { HoverProvider } from '../hover';
import { JSONLanguageService } from '../json/JSONLanguageService';
import { LinkedEditingRangesProvider } from '../linkedEditingRanges/LinkedEditingRangesProvider';
import { RenameProvider } from '../rename/RenameProvider';
import { RenameHandler } from '../renamed/RenameHandler';
import { GetTranslationsForURI } from '../translations';
import { Dependencies } from '../types';
import { debounce } from '../utils';
import { snippetName } from '../utils/uri';
import { VERSION } from '../version';
import { CachedFileSystem } from './CachedFileSystem';
import { Configuration } from './Configuration';
import { ReferencesProvider } from '../references/ReferencesProvider';
const defaultLogger = () => {};
/**
* The `git:` VFS does not support the `fs.readDirectory` call and makes most things break.
* `git` URIs are the ones you'd encounter when doing a git diff in VS Code. They're not
* real files, they're just a way to represent changes in a git repository. As such, I don't
* think we want to sync those in our document manager or try to offer document links, etc.
*
* A middleware would be nice but it'd be a bit of a pain to implement.
*/
const hasUnsupportedDocument = (params: any) => {
return (
'textDocument' in params &&
'uri' in params.textDocument &&
typeof params.textDocument.uri === 'string' &&
params.textDocument.uri.startsWith('git:')
);
};
/**
* This code runs in node and the browser, it can't talk to the file system
* or make requests. Stuff like that should be injected.
*
* In browser, theme-check-js wants these things:
* - fileExists(path)
* - defaultTranslations
*
* Which means we gotta provide 'em from here too!
*/
export function startServer(
connection: Connection,
{
fs: injectedFs,
loadConfig,
log = defaultLogger,
jsonValidationSet,
themeDocset: remoteThemeDocset,
}: Dependencies,
) {
const fs = new CachedFileSystem(injectedFs);
const fileExists = makeFileExists(fs);
const clientCapabilities = new ClientCapabilities();
const configuration = new Configuration(connection, clientCapabilities);
const documentManager: DocumentManager = new DocumentManager(
fs,
connection,
clientCapabilities,
getModeForURI,
isValidSchema,
);
const diagnosticsManager = new DiagnosticsManager(connection);
const documentLinksProvider = new DocumentLinksProvider(documentManager, findThemeRootURI);
const codeActionsProvider = new CodeActionsProvider(documentManager, diagnosticsManager);
const onTypeFormattingProvider = new OnTypeFormattingProvider(
documentManager,
async function setCursorPosition(textDocument, position) {
if (!clientCapabilities.hasShowDocumentSupport) return;
connection.sendRequest(ShowDocumentRequest.type, {
uri: textDocument.uri,
takeFocus: true,
selection: {
start: position,
end: position,
},
});
},
);
const linkedEditingRangesProvider = new LinkedEditingRangesProvider(documentManager);
const documentHighlightProvider = new DocumentHighlightsProvider(documentManager);
const renameProvider = new RenameProvider(documentManager);
const renameHandler = new RenameHandler(
connection,
clientCapabilities,
documentManager,
findThemeRootURI,
);
const referencesProvider = new ReferencesProvider(
documentManager,
getLiquidFiles,
);
async function findThemeRootURI(uri: string) {
const rootUri = await findConfigFileRoot(uri, fileExists);
const config = await loadConfig(rootUri, fs);
return config.rootUri;
}
const getMetafieldDefinitionsForRootUri = memoize(
makeGetMetafieldDefinitions(fs),
(rootUri: string) => rootUri,
);
const getMetafieldDefinitions = async (uri: string) => {
const rootUri = await findThemeRootURI(uri);
return getMetafieldDefinitionsForRootUri(rootUri);
};
// These are augmented here so that the caching is maintained over different runs.
const themeDocset = new AugmentedThemeDocset(remoteThemeDocset);
const runChecks = debounce(
makeRunChecks(documentManager, diagnosticsManager, {
fs,
loadConfig,
themeDocset,
jsonValidationSet,
getMetafieldDefinitions,
}),
100,
);
const getTranslationsForURI: GetTranslationsForURI = async (uri) => {
const rootURI = await findThemeRootURI(uri);
const theme = documentManager.theme(rootURI);
const getDefaultTranslations = makeGetDefaultTranslations(fs, theme, rootURI);
const [defaultTranslations, shopifyTranslations] = await Promise.all([
getDefaultTranslations(),
themeDocset.systemTranslations(),
]);
return { ...shopifyTranslations, ...defaultTranslations };
};
const getSchemaTranslationsForURI: GetTranslationsForURI = async (uri) => {
const rootURI = await findThemeRootURI(uri);
const theme = documentManager.theme(rootURI);
const getDefaultSchemaTranslations = makeGetDefaultSchemaTranslations(fs, theme, rootURI);
return getDefaultSchemaTranslations();
};
const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri);
const getSnippetNamesForURI: GetSnippetNamesForURI = async (uri: string) => {
const rootUri = await findThemeRootURI(uri);
const snippetUris = await recursiveReadDirectory(fs, rootUri, snippetFilter);
return snippetUris.map(snippetName);
};
const getThemeSettingsSchemaForURI = async (uri: string) => {
try {
const rootUri = await findThemeRootURI(uri);
const settingsSchemaUri = path.join(rootUri, 'config', 'settings_schema.json');
const contents = await fs.readFile(settingsSchemaUri);
const json = parseJSON(contents);
if (isError(json) || !Array.isArray(json)) {
throw new Error('Settings JSON file not in correct format');
}
return json;
} catch (error) {
console.error(error);
return [];
}
};
async function getModeForURI(uri: string) {
const rootUri = await findConfigFileRoot(uri, fileExists);
const config = await loadConfig(rootUri, fs);
return config.context;
}
async function getThemeBlockNames(uri: string) {
const rootUri = await findThemeRootURI(uri);
const blocks = await fs.readDirectory(path.join(rootUri, 'blocks'));
return blocks.map(([uri]) => path.basename(uri, '.liquid'));
}
// TODO: Fix this; seems very costly
async function getLiquidFiles(uri: string) {
const rootUri = await findThemeRootURI(uri);
const blockFiles = await fs.readDirectory(path.join(rootUri, 'blocks'));
const sectionFiles = await fs.readDirectory(path.join(rootUri, 'sections'));
const snippetFiles = await fs.readDirectory(path.join(rootUri, 'snippets'));
const sources = [];
for(let [uri, _] of [...blockFiles, ...sectionFiles, ...snippetFiles]) {
const doc = documentManager.get(uri);
if (!doc || doc.type !== SourceCodeType.LiquidHtml) {
continue;
}
sources.push(doc);
}
return sources;
}
// Defined as a function to solve a circular dependency (doc manager & json
// lang service both need each other)
async function isValidSchema(uri: string, jsonString: string) {
return jsonLanguageService.isValidSchema(uri, jsonString);
}
const jsonLanguageService = new JSONLanguageService(
documentManager,
jsonValidationSet,
getSchemaTranslationsForURI,
getModeForURI,
getThemeBlockNames,
);
const completionsProvider = new CompletionsProvider({
documentManager,
themeDocset,
getTranslationsForURI,
getSnippetNamesForURI,
getThemeSettingsSchemaForURI,
log,
getMetafieldDefinitions,
});
const hoverProvider = new HoverProvider(
documentManager,
themeDocset,
getMetafieldDefinitions,
getTranslationsForURI,
getThemeSettingsSchemaForURI,
);
const executeCommandProvider = new ExecuteCommandProvider(
documentManager,
diagnosticsManager,
clientCapabilities,
runChecks,
connection,
);
connection.onInitialize((params) => {
clientCapabilities.setup(params.capabilities, params.initializationOptions);
jsonLanguageService.setup(params.capabilities);
configuration.setup();
const fileOperationRegistrationOptions: FileOperationRegistrationOptions = {
filters: [
{
pattern: {
glob: '**/*.{liquid,json}',
},
},
{
pattern: {
glob: '**/assets/*',
},
},
],
};
const result: InitializeResult = {
capabilities: {
textDocumentSync: {
change: TextDocumentSyncKind.Full,
save: true,
openClose: true,
},
codeActionProvider: {
codeActionKinds: [...CodeActionKinds],
},
completionProvider: {
triggerCharacters: ['.', '{{ ', '{% ', '<', '/', '[', '"', "'", ':'],
},
documentOnTypeFormattingProvider: {
firstTriggerCharacter: ' ',
moreTriggerCharacter: ['{', '%', '-', '>'],
},
documentLinkProvider: {
resolveProvider: false,
workDoneProgress: false,
},
referencesProvider: true,
documentHighlightProvider: true,
linkedEditingRangeProvider: true,
renameProvider: {
prepareProvider: true,
},
executeCommandProvider: {
commands: [...Commands],
},
hoverProvider: {
workDoneProgress: false,
},
workspace: {
fileOperations: {
didRename: fileOperationRegistrationOptions,
didCreate: fileOperationRegistrationOptions,
didDelete: fileOperationRegistrationOptions,
},
},
},
serverInfo: {
name: 'theme-language-server',
version: VERSION,
},
};
return result;
});
connection.onInitialized(() => {
log(`[SERVER] Let's roll!`);
configuration.fetchConfiguration();
configuration.registerDidChangeCapability();
configuration.registerDidChangeWatchedFilesNotification({
watchers: [
{
globPattern: '**/.shopify/*',
},
],
});
});
connection.onDidChangeConfiguration((_params) => {
configuration.clearCache();
});
connection.onDidOpenTextDocument(async (params) => {
if (hasUnsupportedDocument(params)) return;
const { uri, text, version } = params.textDocument;
documentManager.open(uri, text, version);
if (await configuration.shouldCheckOnOpen()) {
runChecks([uri]);
}
// The objective at the time of writing this is to make {Asset,Snippet}Rename
// fast when you eventually need it.
//
// I'm choosing the textDocument/didOpen notification as a hook because
// I'm not sure we have a better solution than this. Yes we have the
// initialize request with the workspace folders, but you might have opened
// an app folder. The root of a theme app extension would probably be
// at ${workspaceRoot}/extensions/${appExtensionName}. It'd be hard to
// figure out from the initialize request params.
//
// If we open a file that we know is liquid, then we can kind of guarantee
// we'll find a theme root and we'll preload that.
if (await configuration.shouldPreloadOnBoot()) {
const rootUri = await findThemeRootURI(uri);
documentManager.preload(rootUri);
}
});
connection.onDidChangeTextDocument(async (params) => {
if (hasUnsupportedDocument(params)) return;
const { uri, version } = params.textDocument;
documentManager.change(uri, params.contentChanges[0].text, version);
if (await configuration.shouldCheckOnChange()) {
runChecks([uri]);
} else {
// The diagnostics may be stale! Clear em!
diagnosticsManager.clear(params.textDocument.uri);
}
});
connection.onDidSaveTextDocument(async (params) => {
if (hasUnsupportedDocument(params)) return;
const { uri } = params.textDocument;
fs.readFile.invalidate(uri);
fs.stat.invalidate(uri);
if (await configuration.shouldCheckOnSave()) {
runChecks([uri]);
}
});
connection.onDidCloseTextDocument((params) => {
if (hasUnsupportedDocument(params)) return;
const { uri } = params.textDocument;
documentManager.close(uri);
diagnosticsManager.clear(uri);
});
connection.onDocumentLinks(async (params) => {
if (hasUnsupportedDocument(params)) return [];
return documentLinksProvider.documentLinks(params.textDocument.uri);
});
connection.onCodeAction(async (params) => {
return codeActionsProvider.codeActions(params);
});
connection.onExecuteCommand(async (params) => {
await executeCommandProvider.execute(params);
});
connection.onCompletion(async (params) => {
if (hasUnsupportedDocument(params)) return [];
return (
(await jsonLanguageService.completions(params)) ??
(await completionsProvider.completions(params))
);
});
connection.onHover(async (params) => {
if (hasUnsupportedDocument(params)) return null;
return (await jsonLanguageService.hover(params)) ?? (await hoverProvider.hover(params));
});
connection.onDocumentOnTypeFormatting(async (params) => {
if (hasUnsupportedDocument(params)) return null;
return onTypeFormattingProvider.onTypeFormatting(params);
});
connection.onDocumentHighlight(async (params) => {
if (hasUnsupportedDocument(params)) return [];
return documentHighlightProvider.documentHighlights(params);
});
connection.onPrepareRename(async (params) => {
if (hasUnsupportedDocument(params)) return null;
return renameProvider.prepare(params);
});
connection.onRenameRequest(async (params) => {
if (hasUnsupportedDocument(params)) return null;
return renameProvider.rename(params);
});
connection.languages.onLinkedEditingRange(async (params) => {
if (hasUnsupportedDocument(params)) return null;
return linkedEditingRangesProvider.linkedEditingRanges(params);
});
connection.onReferences(async (params) => {
if (hasUnsupportedDocument(params)) return [];
return referencesProvider.references(params);
});
connection.onDidChangeWatchedFiles(async (params) => {
if (params.changes.length === 0) return;
for (const change of params.changes) {
fs.readDirectory.invalidate(path.dirname(change.uri));
fs.readFile.invalidate(change.uri);
fs.stat.invalidate(change.uri);
if (change.uri.endsWith('metafields.json')) {
const rootUri = await findThemeRootURI(change.uri);
getMetafieldDefinitionsForRootUri.invalidate(rootUri);
}
}
});
connection.workspace.onDidCreateFiles((params) => {
const triggerUris = params.files.map((fileCreate) => fileCreate.uri);
for (const { uri } of params.files) {
// When a file is created, to make sure preload isn't invalidated, we add
// an empty string to the document manager for the new URI.
if (!documentManager.has(uri)) documentManager.open(uri, '', undefined);
fs.readDirectory.invalidate(path.dirname(uri)); // Since there's a new file there
fs.readFile.invalidate(uri); // Since this is no longer an error
fs.stat.invalidate(uri); // Since this is no longer an error
}
// MissingAssets/MissingSnippet should be rerun when a new file exists
// since the file creation might invalidate the error.
runChecks.force(triggerUris);
});
connection.workspace.onDidRenameFiles(async (params) => {
const triggerUris = params.files.map((fileRename) => fileRename.newUri);
// Behold the cache invalidation monster
for (const { oldUri, newUri } of params.files) {
// When a file is renamed, we paste the content of the old file into the
// new file in the document manager. We don't need to invalidate preload
// because that's the only thing that changed.
documentManager.rename(oldUri, newUri);
// When a file is renamed, readDirectory to the parent folder is invalidated.
fs.readDirectory.invalidate(path.dirname(oldUri));
fs.readDirectory.invalidate(path.dirname(newUri));
// When a file is renamed, readFile and stat for both the old and new URIs are invalidated.
fs.readFile.invalidate(oldUri);
fs.readFile.invalidate(newUri);
fs.stat.invalidate(oldUri);
fs.stat.invalidate(newUri);
}
// We should complete refactors before running theme check
await renameHandler.onDidRenameFiles(params);
// MissingAssets/MissingSnippet should be rerun when a file is renamed
// since the file rename might invalidate an error.
runChecks.force(triggerUris);
});
connection.workspace.onDidDeleteFiles((params) => {
const triggerUris = params.files.map((fileDelete) => fileDelete.uri);
for (const { uri } of params.files) {
// When a file is deleted, we remove it from the document manager.
// Don't need to invalidate preload because that operation covers it.
documentManager.delete(uri);
// Since the file is gone, we invalidate the parent directory's readDirectory.
fs.readDirectory.invalidate(path.dirname(uri));
// Since the file is gone, we invalidate the readFile and stat for the URI.
fs.readFile.invalidate(uri);
fs.stat.invalidate(uri);
}
// MissingAssets/MissingSnippet should be rerun when a file is deleted
// since the file rename might cause an error.
runChecks.force(triggerUris);
});
connection.listen();
}