Skip to content

Commit 3afb318

Browse files
committed
Refactor Liquid documentation types and update references to improve clarity
- Renamed `GetLiquidDocDefinitionsForURI` to `GetSnippetDefinitionForURI` - Introduced `SnippetDefinition` type to replace `LiquidDocDefinition`, enhancing type specificity.
1 parent e298ecb commit 3afb318

File tree

6 files changed

+76
-68
lines changed

6 files changed

+76
-68
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { HtmlAttributeValueHoverProvider } from './providers/HtmlAttributeValueHoverProvider';
1818
import { findCurrentNode } from '@shopify/theme-check-common';
1919
import { GetThemeSettingsSchemaForURI } from '../settings';
20-
import { GetLiquidDocDefinitionsForURI } from '../liquidDoc';
20+
import { GetSnippetDefinitionForURI } from '../liquidDoc';
2121

2222
export class HoverProvider {
2323
private providers: BaseHoverProvider[] = [];
@@ -28,7 +28,7 @@ export class HoverProvider {
2828
readonly getMetafieldDefinitions: (rootUri: string) => Promise<MetafieldDefinitionMap>,
2929
readonly getTranslationsForURI: GetTranslationsForURI = async () => ({}),
3030
readonly getSettingsSchemaForURI: GetThemeSettingsSchemaForURI = async () => [],
31-
readonly getLiquidDocDefinitionsForURI: GetLiquidDocDefinitionsForURI = async (
31+
readonly getSnippetDefinitionForURI: GetSnippetDefinitionForURI = async (
3232
_uri,
3333
snippetName,
3434
) => ({
@@ -49,7 +49,7 @@ export class HoverProvider {
4949
new HtmlAttributeHoverProvider(),
5050
new HtmlAttributeValueHoverProvider(),
5151
new TranslationHoverProvider(getTranslationsForURI, documentManager),
52-
new RenderSnippetHoverProvider(getLiquidDocDefinitionsForURI),
52+
new RenderSnippetHoverProvider(getSnippetDefinitionForURI),
5353
];
5454
}
5555

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { describe, beforeEach, it, expect } from 'vitest';
22
import { DocumentManager } from '../../documents';
33
import { HoverProvider } from '../HoverProvider';
44
import { MetafieldDefinitionMap } from '@shopify/theme-check-common';
5-
import { LiquidDocDefinition } from '../../liquidDoc';
5+
import { GetSnippetDefinitionForURI, SnippetDefinition } from '../../liquidDoc';
66

77
describe('Module: RenderSnippetHoverProvider', async () => {
88
let provider: HoverProvider;
9-
let getLiquidDoc: () => Promise<LiquidDocDefinition>;
10-
const mockDefinitions = {
11-
'product-card': {
12-
name: 'product-card',
9+
let getSnippetDefinition: GetSnippetDefinitionForURI;
10+
const mockSnippetDefinition: SnippetDefinition = {
11+
name: 'product-card',
12+
liquidDoc: {
1313
parameters: [
1414
{
1515
name: 'title',
@@ -23,12 +23,9 @@ describe('Module: RenderSnippetHoverProvider', async () => {
2323
},
2424
],
2525
},
26-
'empty-snippet': {
27-
name: 'empty-snippet',
28-
},
2926
};
3027

31-
const createProvider = (getLiquidDoc: () => Promise<LiquidDocDefinition>) => {
28+
const createProvider = (getLiquidDoc: GetSnippetDefinitionForURI) => {
3229
return new HoverProvider(
3330
new DocumentManager(),
3431
{
@@ -45,21 +42,21 @@ describe('Module: RenderSnippetHoverProvider', async () => {
4542
};
4643

4744
beforeEach(async () => {
48-
getLiquidDoc = async () => mockDefinitions['product-card'];
49-
provider = createProvider(getLiquidDoc);
45+
getSnippetDefinition = async () => mockSnippetDefinition;
46+
provider = createProvider(getSnippetDefinition);
5047
});
5148

5249
describe('hover', () => {
53-
it('should return snippet documentation with all parameters', async () => {
50+
it('should return snippet definition with all parameters', async () => {
5451
await expect(provider).to.hover(
5552
`{% render 'product-car█d' %}`,
5653
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `border-radius`: number - The border radius in px',
5754
);
5855
});
5956

6057
it('should return an H3 with snippet name if no LiquidDocDefinition found', async () => {
61-
getLiquidDoc = async () => undefined as any;
62-
provider = createProvider(getLiquidDoc);
58+
getSnippetDefinition = async () => ({ name: 'unknown-snippet' });
59+
provider = createProvider(getSnippetDefinition);
6360
await expect(provider).to.hover(`{% render 'unknown-sni█ppet' %}`, '### unknown-snippet');
6461
});
6562

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

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { NodeTypes } from '@shopify/liquid-html-parser';
22
import { LiquidHtmlNode } from '@shopify/theme-check-common';
33
import { Hover, HoverParams } from 'vscode-languageserver';
44
import { BaseHoverProvider } from '../BaseHoverProvider';
5-
import { LiquidDocDefinition, LiquidDocParameter } from '../../liquidDoc';
5+
import { SnippetDefinition, LiquidDocParameter } from '../../liquidDoc';
66

7+
/**
8+
* @param x {string} asdf
9+
*/
710
export class RenderSnippetHoverProvider implements BaseHoverProvider {
811
constructor(
9-
private getLiquidDocDefinitionsForURI: (
12+
private getSnippetDefinitionForURI: (
1013
uri: string,
1114
snippetName: string,
12-
) => Promise<LiquidDocDefinition>,
15+
) => Promise<SnippetDefinition>,
1316
) {}
1417

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

2932
const snippetName = currentNode.value.replace(/['"]/g, '');
30-
const liquidDocDefinition = await this.getLiquidDocDefinitionsForURI(
33+
const snippetDefinition = await this.getSnippetDefinitionForURI(
3134
params.textDocument.uri,
3235
snippetName,
3336
);
3437

35-
if (!liquidDocDefinition) {
38+
if (!snippetDefinition.liquidDoc) {
3639
return {
3740
contents: {
3841
kind: 'markdown',
39-
value: `### ${snippetName}`,
42+
value: `### ${snippetDefinition.name}`,
4043
},
4144
};
4245
}
4346

44-
const parameterDocs = liquidDocDefinition.parameters
47+
const liquidDoc = snippetDefinition.liquidDoc;
48+
49+
const parameters = liquidDoc.parameters
4550
?.map(
4651
(param: LiquidDocParameter) =>
4752
`- \`${param.name}\`${param.type ? `: ${param.type}` : ''} - ${param.description || ''}`,
4853
)
4954
.join('\n');
5055

51-
const parts = [`### ${liquidDocDefinition.name}`];
52-
if (parameterDocs) {
53-
parts.push('', '**Parameters:**', parameterDocs);
56+
const parts = [`### ${snippetDefinition.name}`];
57+
if (parameters) {
58+
parts.push('', '**Parameters:**', parameters);
5459
}
5560

5661
return {

packages/theme-language-server-common/src/liquidDoc.spec.ts

+27-23
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ describe('Unit: makeGetLiquidDocDefinitions', () => {
2020
const result = getSnippetDefinition(ast, 'product-card');
2121
expect(result).to.deep.equal({
2222
name: 'product-card',
23-
parameters: [],
23+
liquidDoc: {
24+
parameters: [],
25+
},
2426
});
2527
});
2628

@@ -37,28 +39,30 @@ describe('Unit: makeGetLiquidDocDefinitions', () => {
3739
const result = getSnippetDefinition(ast, 'product-card');
3840
expect(result).to.deep.equal({
3941
name: 'product-card',
40-
parameters: [
41-
{
42-
name: 'firstParam',
43-
description: 'The first param',
44-
type: 'String',
45-
},
46-
{
47-
name: 'secondParam',
48-
description: 'The second param',
49-
type: 'Number',
50-
},
51-
{
52-
name: 'paramWithNoType',
53-
description: 'param with no type',
54-
type: null,
55-
},
56-
{
57-
name: 'paramWithOnlyName',
58-
description: '',
59-
type: null,
60-
},
61-
],
42+
liquidDoc: {
43+
parameters: [
44+
{
45+
name: 'firstParam',
46+
description: 'The first param',
47+
type: 'String',
48+
},
49+
{
50+
name: 'secondParam',
51+
description: 'The second param',
52+
type: 'Number',
53+
},
54+
{
55+
name: 'paramWithNoType',
56+
description: 'param with no type',
57+
type: null,
58+
},
59+
{
60+
name: 'paramWithOnlyName',
61+
description: '',
62+
type: null,
63+
},
64+
],
65+
},
6266
});
6367
});
6468
});

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@ import { LiquidHtmlNode } from '@shopify/theme-check-common';
44

55
import { LiquidDocParamNode } from '@shopify/liquid-html-parser';
66

7-
export type GetLiquidDocDefinitionsForURI = (
7+
export type GetSnippetDefinitionForURI = (
88
uri: string,
99
snippetName: string,
10-
) => Promise<LiquidDocDefinition>;
10+
) => Promise<SnippetDefinition>;
1111

1212
export type LiquidDocParameter = {
1313
name: string;
1414
description: string | null;
1515
type: string | null;
1616
};
1717

18-
export type LiquidDocDefinition = {
18+
export type SnippetDefinition = {
1919
name: string;
20+
liquidDoc?: LiquidDocDefinition;
21+
};
22+
23+
type LiquidDocDefinition = {
2024
parameters?: LiquidDocParameter[];
2125
};
2226

23-
export function getSnippetDefinition(snippet: LiquidHtmlNode, snippetName: string) {
27+
export function getSnippetDefinition(
28+
snippet: LiquidHtmlNode,
29+
snippetName: string,
30+
): SnippetDefinition {
2431
const liquidDocAnnotations: LiquidDocParameter[] = visit<
2532
SourceCodeType.LiquidHtml,
2633
LiquidDocParameter
@@ -36,6 +43,8 @@ export function getSnippetDefinition(snippet: LiquidHtmlNode, snippetName: strin
3643

3744
return {
3845
name: snippetName,
39-
parameters: liquidDocAnnotations,
46+
liquidDoc: {
47+
parameters: liquidDocAnnotations,
48+
},
4049
};
4150
}

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

+6-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
path,
1313
recursiveReadDirectory,
1414
SourceCodeType,
15-
visit,
1615
} from '@shopify/theme-check-common';
1716
import {
1817
Connection,
@@ -30,7 +29,7 @@ import { GetSnippetNamesForURI } from '../completions/providers/RenderSnippetCom
3029
import { DiagnosticsManager, makeRunChecks } from '../diagnostics';
3130
import { DocumentHighlightsProvider } from '../documentHighlights/DocumentHighlightsProvider';
3231
import { DocumentLinksProvider } from '../documentLinks';
33-
import { AugmentedLiquidSourceCode, DocumentManager } from '../documents';
32+
import { DocumentManager } from '../documents';
3433
import { OnTypeFormattingProvider } from '../formatting';
3534
import { HoverProvider } from '../hover';
3635
import { JSONLanguageService } from '../json/JSONLanguageService';
@@ -44,8 +43,8 @@ import { snippetName } from '../utils/uri';
4443
import { VERSION } from '../version';
4544
import { CachedFileSystem } from './CachedFileSystem';
4645
import { Configuration } from './Configuration';
47-
import { LiquidDocParamNode, LiquidHtmlNode } from '@shopify/liquid-html-parser';
48-
import { LiquidDocDefinition, LiquidDocParameter } from '../liquidDoc';
46+
import { LiquidHtmlNode } from '@shopify/liquid-html-parser';
47+
import { getSnippetDefinition, SnippetDefinition } from '../liquidDoc';
4948

5049
const defaultLogger = () => {};
5150

@@ -174,10 +173,10 @@ export function startServer(
174173
return getDefaultSchemaTranslations();
175174
};
176175

177-
const getLiquidDocDefinitionsForURI = async (
176+
const getSnippetDefinitionsForURI = async (
178177
uri: string,
179178
snippetName: string,
180-
): Promise<LiquidDocDefinition> => {
179+
): Promise<SnippetDefinition> => {
181180
const rootUri = await findThemeRootURI(uri);
182181
const snippetURI = path.join(rootUri, 'snippets', `${snippetName}.liquid`);
183182
const snippet = documentManager.get(snippetURI);
@@ -270,7 +269,7 @@ export function startServer(
270269
getMetafieldDefinitions,
271270
getTranslationsForURI,
272271
getThemeSettingsSchemaForURI,
273-
getLiquidDocDefinitionsForURI,
272+
getSnippetDefinitionsForURI,
274273
);
275274

276275
const executeCommandProvider = new ExecuteCommandProvider(
@@ -583,9 +582,3 @@ export function startServer(
583582

584583
connection.listen();
585584
}
586-
function getSnippetDefinition(
587-
ast: LiquidHtmlNode,
588-
snippetName: string,
589-
): LiquidDocDefinition | PromiseLike<LiquidDocDefinition> {
590-
throw new Error('Function not implemented.');
591-
}

0 commit comments

Comments
 (0)