import { CompletionItem, CompletionItemKind, InsertTextFormat } from 'vscode-languageserver-types'; import { HTMLDataVSCode, HTMLProvideAttrs, HTMLTagAttributes } from '../'; import * as specification from '../data'; import { schema } from './schema'; import { html5 } from './states'; import { Tokens } from '../../utils/enums'; import { descriptive, documentation } from '../../utils/signature'; export function HTMLCompletions () { const data = Object.create(null); /* -------------------------------------------- */ /* TAG COMPLETIONS */ /* -------------------------------------------- */ data.tags = Object.entries(specification.tags).map(([ label, spec ]) => ({ label, documentation: documentation(spec.description, spec.reference), data: { void: spec.void, attributes: HTMLTagAttrs(spec?.attributes) } })); /* -------------------------------------------- */ /* GLOBAL ATTRIBUTE COMPLETIONS */ /* -------------------------------------------- */ data.attributes = Object.entries(specification.attributes).map(([ label, { description, reference, value = false } ]) => ({ label, documentation: documentation(description, reference), data: { value } })); return data; } /* -------------------------------------------- */ /* FUNCTIONS */ /* -------------------------------------------- */ /** * Accepts custom data as per the vscode spec * for HTML. */ export function HTMLCustomData (data: HTMLDataVSCode) { // Re-write to align with Liquify spec schema html5.data.variation = Object.assign(html5.data.variation, schema(data, specification)); html5.data.completions = HTMLCompletions(); } export function HTMLTagComplete () { html5.attribute = undefined; html5.value = undefined; return html5.data.completions.tags; } export function HTMLAttrsComplete (tag: string): HTMLProvideAttrs { if (html5.attribute || html5.attribute?.length) return html5.attribute; return HTMLTagAttrs(html5.data.variation.tags[tag].attributes).concat(html5.data.completions.attributes); } export function HTMLValueComplete (token: string = html5.value) { if (!html5.value || !html5.data.variation.values?.[token]) return false; return html5.data.variation.values[token].map(value => ({ ...value, data: { token: Tokens.HTMLValue } })); } export function HTMLTagResolve (item: CompletionItem) { html5.attribute = item.data.attributes; item.kind = CompletionItemKind.Property; item.insertTextFormat = InsertTextFormat.Snippet; item.insertText = item.data.void ? `${item.label} $1 $0` : `${item.label} $1> $0 ({ label: name, documentation: descriptive(description), data: { token: Tokens.HTMLAttribute, value } })); } /* -------------------------------------------- */ /* PRIVATES */ /* -------------------------------------------- */