Skip to content

Commit

Permalink
wip - load liquidDoc values from snippet under hover
Browse files Browse the repository at this point in the history
todo - simplify / fix the types
todo - handle files with no value
todo - optimize the way we store the liquiddoc values
  • Loading branch information
jamesmengo committed Jan 13, 2025
1 parent 6df3fa1 commit c7b2d3b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class HoverProvider {
readonly getSettingsSchemaForURI: GetThemeSettingsSchemaForURI = async () => [],
readonly getLiquidDocDefinitionsForURI: (
uri: string,
snippetName: string,
) => Promise<LiquidDocDefinitionMap> = async () => mock_data,
) {
const typeSystem = new TypeSystem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { BaseHoverProvider } from '../BaseHoverProvider';

export class RenderSnippetHoverProvider implements BaseHoverProvider {
constructor(
private getLiquidDocDefinitionsForURI: (uri: string) => Promise<LiquidDocDefinitionMap>,
private getLiquidDocDefinitionsForURI: (
uri: string,
snippetName: string,
) => Promise<LiquidDocDefinitionMap>,
) {}

async hover(
Expand All @@ -27,7 +30,10 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
}

const snippetName = currentNode.value.replace(/['"]/g, '');
const docDefinitions = await this.getLiquidDocDefinitionsForURI(params.textDocument.uri);
const docDefinitions = await this.getLiquidDocDefinitionsForURI(
params.textDocument.uri,
snippetName,
);
const snippetDefinition = docDefinitions[snippetName];

if (!snippetDefinition) {
Expand Down
55 changes: 55 additions & 0 deletions packages/theme-language-server-common/src/server/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
FileTuple,
findRoot as findConfigFileRoot,
isError,
LiquidDocDefinition,
LiquidDocDefinitionMap,
LiquidDocParameter,
makeFileExists,
makeGetDefaultSchemaTranslations,
makeGetDefaultTranslations,
Expand All @@ -12,6 +15,7 @@ import {
path,
recursiveReadDirectory,
SourceCodeType,
visit,
} from '@shopify/theme-check-common';
import {
Connection,
Expand Down Expand Up @@ -169,6 +173,56 @@ export function startServer(
return getDefaultSchemaTranslations();
};

// todo - put this stuff in theme check common
const getLiquidDocDefinitionsForURI = async (
uri: string,
snippetName: string,
): Promise<LiquidDocDefinitionMap> => {
const rootUri = await findThemeRootURI(uri);
const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`);
const snippet = documentManager.get(snippetURI);

if (!snippet || isError(snippet)) return {};

if (snippet.type !== SourceCodeType.LiquidHtml) return {};

// read the snippet AST
const ast = snippet.ast;
if (isError(ast)) return {};

const docDefinitions = visit<SourceCodeType.LiquidHtml, LiquidDocDefinition>(ast, {
LiquidRawTag: (node, _ancestors) => {
if (node.name === 'doc') {
const body = node.body;
const paramNodes = visit<SourceCodeType.LiquidHtml, LiquidDocParameter>(body, {
LiquidDocParamNode: (node) => {
return [
{
name: node.paramName.value,
description: node.paramDescription.value,
type: node.paramType.value,
},
];
},
});
return {
name: snippetName,
description: 'placeholder',
parameters: paramNodes,
};
}
},
});

const returnVal: LiquidDocDefinitionMap = {
[snippetName]: {
name: snippetName,
parameters: docDefinitions[0].parameters,
},
};
return returnVal;
};

const snippetFilter = ([uri]: FileTuple) => /\.liquid$/.test(uri) && /snippets/.test(uri);
const getSnippetNamesForURI: GetSnippetNamesForURI = async (uri: string) => {
const rootUri = await findThemeRootURI(uri);
Expand Down Expand Up @@ -249,6 +303,7 @@ export function startServer(
getMetafieldDefinitions,
getTranslationsForURI,
getThemeSettingsSchemaForURI,
getLiquidDocDefinitionsForURI,
);

const executeCommandProvider = new ExecuteCommandProvider(
Expand Down

0 comments on commit c7b2d3b

Please sign in to comment.