From 35d17357d0054aabfcee0d9585004f423bf4715a Mon Sep 17 00:00:00 2001 From: James Meng Date: Thu, 16 Jan 2025 17:25:09 -0800 Subject: [PATCH] Connect SnippetHoverProvider with server --- .../src/hover/HoverProvider.ts | 13 +++++++++++- .../src/server/startServer.ts | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/theme-language-server-common/src/hover/HoverProvider.ts b/packages/theme-language-server-common/src/hover/HoverProvider.ts index 44a62c260..9801c3637 100644 --- a/packages/theme-language-server-common/src/hover/HoverProvider.ts +++ b/packages/theme-language-server-common/src/hover/HoverProvider.ts @@ -1,4 +1,9 @@ -import { MetafieldDefinitionMap, SourceCodeType, ThemeDocset } from '@shopify/theme-check-common'; +import { + LiquidDocDefinition, + MetafieldDefinitionMap, + SourceCodeType, + ThemeDocset, +} from '@shopify/theme-check-common'; import { Hover, HoverParams } from 'vscode-languageserver'; import { TypeSystem } from '../TypeSystem'; import { DocumentManager } from '../documents'; @@ -11,6 +16,7 @@ import { LiquidObjectAttributeHoverProvider, LiquidObjectHoverProvider, LiquidTagHoverProvider, + SnippetHoverProvider, TranslationHoverProvider, } from './providers'; import { HtmlAttributeValueHoverProvider } from './providers/HtmlAttributeValueHoverProvider'; @@ -26,6 +32,10 @@ export class HoverProvider { readonly getMetafieldDefinitions: (rootUri: string) => Promise, readonly getTranslationsForURI: GetTranslationsForURI = async () => ({}), readonly getSettingsSchemaForURI: GetThemeSettingsSchemaForURI = async () => [], + readonly getLiquidDocDefinitionsForURI: ( + uri: string, + snippetName: string, + ) => Promise, ) { const typeSystem = new TypeSystem( themeDocset, @@ -41,6 +51,7 @@ export class HoverProvider { new HtmlAttributeHoverProvider(), new HtmlAttributeValueHoverProvider(), new TranslationHoverProvider(getTranslationsForURI, documentManager), + new SnippetHoverProvider(getLiquidDocDefinitionsForURI), ]; } diff --git a/packages/theme-language-server-common/src/server/startServer.ts b/packages/theme-language-server-common/src/server/startServer.ts index cbef1ddf2..867de5acd 100644 --- a/packages/theme-language-server-common/src/server/startServer.ts +++ b/packages/theme-language-server-common/src/server/startServer.ts @@ -3,10 +3,12 @@ import { FileTuple, findRoot as findConfigFileRoot, isError, + LiquidDocDefinition, makeFileExists, makeGetDefaultSchemaTranslations, makeGetDefaultTranslations, makeGetMetafieldDefinitions, + makeGetLiquidDocDefinitions, memoize, parseJSON, path, @@ -171,6 +173,23 @@ export function startServer( return getDefaultSchemaTranslations(); }; + const getLiquidDocDefinitionsForURI = async ( + uri: string, + snippetName: string, + ): Promise => { + const rootUri = await findThemeRootURI(uri); + const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`); + const snippet = documentManager.get(snippetURI); + + if (!snippet || snippet.type !== SourceCodeType.LiquidHtml || isError(snippet.ast)) { + return { name: snippetName }; + } + + const getLiquidDocDefinitions = makeGetLiquidDocDefinitions(snippet.ast, snippetName); + + return getLiquidDocDefinitions(); + }; + const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri); const getSnippetNamesForURI: GetSnippetNamesForURI = async (uri: string) => { const rootUri = await findThemeRootURI(uri); @@ -251,6 +270,7 @@ export function startServer( getMetafieldDefinitions, getTranslationsForURI, getThemeSettingsSchemaForURI, + getLiquidDocDefinitionsForURI, ); const executeCommandProvider = new ExecuteCommandProvider(