-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathSchemaTranslationCompletionProvider.ts
51 lines (43 loc) · 1.75 KB
/
SchemaTranslationCompletionProvider.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
import { deepGet } from '@shopify/theme-check-common';
import { CompletionItemKind, JSONPath } from 'vscode-json-languageservice';
import {
GetTranslationsForURI,
renderTranslation,
translationOptions,
} from '../../../translations';
import { isLiquidRequestContext, RequestContext } from '../../RequestContext';
import { isSectionOrBlockFile } from '../../utils';
import { JSONCompletionItem, JSONCompletionProvider } from '../JSONCompletionProvider';
export class SchemaTranslationsCompletionProvider implements JSONCompletionProvider {
constructor(private getDefaultSchemaTranslations: GetTranslationsForURI) {}
async completeValue(context: RequestContext, path: JSONPath): Promise<JSONCompletionItem[]> {
if (!isSectionOrBlockFile(context.doc.uri) || !isLiquidRequestContext(context)) {
return [];
}
const { doc, parsed } = context;
const label = deepGet(parsed, path);
if (!label || typeof label !== 'string' || !label.startsWith('t:')) {
return [];
}
const partial = /^t:(.*)/.exec(label)?.[1];
if (partial === undefined) return [];
const translations = await this.getDefaultSchemaTranslations(doc.uri);
// We'll let the frontend do the filtering. But we'll only include shopify
// translations if the shopify prefix is present
const options = translationOptions(translations);
return options.map((option): JSONCompletionItem => {
const tLabel = `t:${option.path.join('.')}`;
return {
label: tLabel,
kind: CompletionItemKind.Value,
filterText: `"${tLabel}"`,
insertText: `"${tLabel}"`,
insertTextFormat: 1,
documentation: {
kind: 'markdown',
value: renderTranslation(option.translation),
},
};
});
}
}