@shopify/theme-language-server-common
Version:
<h1 align="center" style="position: relative;" > <br> <img src="https://github.com/Shopify/theme-check-vscode/blob/main/images/shopify_glyph.png?raw=true" alt="logo" width="141" height="160"> <br> Theme Language Server </h1>
52 lines (43 loc) • 1.79 kB
text/typescript
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),
},
};
});
}
}