Skip to content

Commit c7b2d3b

Browse files
committed
wip - load liquidDoc values from snippet under hover
todo - simplify / fix the types todo - handle files with no value todo - optimize the way we store the liquiddoc values
1 parent 6df3fa1 commit c7b2d3b

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

packages/theme-language-server-common/src/hover/HoverProvider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class HoverProvider {
4141
readonly getSettingsSchemaForURI: GetThemeSettingsSchemaForURI = async () => [],
4242
readonly getLiquidDocDefinitionsForURI: (
4343
uri: string,
44+
snippetName: string,
4445
) => Promise<LiquidDocDefinitionMap> = async () => mock_data,
4546
) {
4647
const typeSystem = new TypeSystem(

packages/theme-language-server-common/src/hover/providers/RenderSnippetHoverProvider.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { BaseHoverProvider } from '../BaseHoverProvider';
99

1010
export class RenderSnippetHoverProvider implements BaseHoverProvider {
1111
constructor(
12-
private getLiquidDocDefinitionsForURI: (uri: string) => Promise<LiquidDocDefinitionMap>,
12+
private getLiquidDocDefinitionsForURI: (
13+
uri: string,
14+
snippetName: string,
15+
) => Promise<LiquidDocDefinitionMap>,
1316
) {}
1417

1518
async hover(
@@ -27,7 +30,10 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
2730
}
2831

2932
const snippetName = currentNode.value.replace(/['"]/g, '');
30-
const docDefinitions = await this.getLiquidDocDefinitionsForURI(params.textDocument.uri);
33+
const docDefinitions = await this.getLiquidDocDefinitionsForURI(
34+
params.textDocument.uri,
35+
snippetName,
36+
);
3137
const snippetDefinition = docDefinitions[snippetName];
3238

3339
if (!snippetDefinition) {

packages/theme-language-server-common/src/server/startServer.ts

+55
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
FileTuple,
44
findRoot as findConfigFileRoot,
55
isError,
6+
LiquidDocDefinition,
7+
LiquidDocDefinitionMap,
8+
LiquidDocParameter,
69
makeFileExists,
710
makeGetDefaultSchemaTranslations,
811
makeGetDefaultTranslations,
@@ -12,6 +15,7 @@ import {
1215
path,
1316
recursiveReadDirectory,
1417
SourceCodeType,
18+
visit,
1519
} from '@shopify/theme-check-common';
1620
import {
1721
Connection,
@@ -169,6 +173,56 @@ export function startServer(
169173
return getDefaultSchemaTranslations();
170174
};
171175

176+
// todo - put this stuff in theme check common
177+
const getLiquidDocDefinitionsForURI = async (
178+
uri: string,
179+
snippetName: string,
180+
): Promise<LiquidDocDefinitionMap> => {
181+
const rootUri = await findThemeRootURI(uri);
182+
const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`);
183+
const snippet = documentManager.get(snippetURI);
184+
185+
if (!snippet || isError(snippet)) return {};
186+
187+
if (snippet.type !== SourceCodeType.LiquidHtml) return {};
188+
189+
// read the snippet AST
190+
const ast = snippet.ast;
191+
if (isError(ast)) return {};
192+
193+
const docDefinitions = visit<SourceCodeType.LiquidHtml, LiquidDocDefinition>(ast, {
194+
LiquidRawTag: (node, _ancestors) => {
195+
if (node.name === 'doc') {
196+
const body = node.body;
197+
const paramNodes = visit<SourceCodeType.LiquidHtml, LiquidDocParameter>(body, {
198+
LiquidDocParamNode: (node) => {
199+
return [
200+
{
201+
name: node.paramName.value,
202+
description: node.paramDescription.value,
203+
type: node.paramType.value,
204+
},
205+
];
206+
},
207+
});
208+
return {
209+
name: snippetName,
210+
description: 'placeholder',
211+
parameters: paramNodes,
212+
};
213+
}
214+
},
215+
});
216+
217+
const returnVal: LiquidDocDefinitionMap = {
218+
[snippetName]: {
219+
name: snippetName,
220+
parameters: docDefinitions[0].parameters,
221+
},
222+
};
223+
return returnVal;
224+
};
225+
172226
const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri);
173227
const getSnippetNamesForURI: GetSnippetNamesForURI = async (uri: string) => {
174228
const rootUri = await findThemeRootURI(uri);
@@ -249,6 +303,7 @@ export function startServer(
249303
getMetafieldDefinitions,
250304
getTranslationsForURI,
251305
getThemeSettingsSchemaForURI,
306+
getLiquidDocDefinitionsForURI,
252307
);
253308

254309
const executeCommandProvider = new ExecuteCommandProvider(

0 commit comments

Comments
 (0)