Skip to content

Commit 99c1728

Browse files
committed
Update RenderSnippetHoverProvider to display optional parameters with default values
- Modify hover tooltip to show default values for optional parameters
1 parent 771f5a4 commit 99c1728

File tree

2 files changed

+135
-63
lines changed

2 files changed

+135
-63
lines changed

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

+120-58
Original file line numberDiff line numberDiff line change
@@ -8,70 +8,59 @@ import '../../../../theme-check-common/src/test/test-setup';
88
describe('Module: RenderSnippetHoverProvider', async () => {
99
let provider: HoverProvider;
1010
let getSnippetDefinition: GetSnippetDefinitionForURI;
11-
const mockSnippetDefinition: SnippetDefinition = {
12-
name: 'product-card',
13-
liquidDoc: {
14-
parameters: [
15-
{
16-
name: 'title',
17-
description: 'The title of the product',
18-
type: 'string',
19-
required: true,
20-
},
21-
{
22-
name: 'border-radius',
23-
description: 'The border radius in px',
24-
type: 'number',
25-
required: false,
26-
},
27-
{
28-
name: 'no-type',
29-
description: 'This parameter has no type',
30-
type: null,
31-
required: true,
32-
},
33-
{
34-
name: 'no-description',
35-
description: null,
36-
type: 'string',
37-
required: true,
38-
},
39-
{
40-
name: 'no-type-or-description',
41-
description: null,
42-
type: null,
43-
required: true,
44-
},
45-
],
46-
},
47-
};
4811

49-
const createProvider = (getSnippetDefinition: GetSnippetDefinitionForURI) => {
50-
return new HoverProvider(
51-
new DocumentManager(),
52-
{
53-
filters: async () => [],
54-
objects: async () => [],
55-
tags: async () => [],
56-
systemTranslations: async () => ({}),
12+
describe('required parameters', () => {
13+
const mockSnippetDefinition: SnippetDefinition = {
14+
name: 'product-card',
15+
liquidDoc: {
16+
parameters: [
17+
{
18+
name: 'title',
19+
description: 'The title of the product',
20+
type: 'string',
21+
required: true,
22+
defaultValue: null,
23+
},
24+
{
25+
name: 'border_radius',
26+
description: 'The border radius in px',
27+
type: 'number',
28+
required: false,
29+
defaultValue: null,
30+
},
31+
{
32+
name: 'no_type',
33+
description: 'This parameter has no type',
34+
type: null,
35+
required: true,
36+
defaultValue: null,
37+
},
38+
{
39+
name: 'no_description',
40+
description: null,
41+
type: 'string',
42+
required: true,
43+
defaultValue: null,
44+
},
45+
{
46+
name: 'no_type_or_description',
47+
description: null,
48+
type: null,
49+
required: true,
50+
defaultValue: null,
51+
},
52+
],
5753
},
58-
async (_rootUri: string) => ({} as MetafieldDefinitionMap),
59-
async () => ({}),
60-
async () => [],
61-
getSnippetDefinition,
62-
);
63-
};
54+
};
6455

65-
beforeEach(async () => {
66-
getSnippetDefinition = async () => mockSnippetDefinition;
67-
provider = createProvider(getSnippetDefinition);
68-
});
56+
beforeEach(async () => {
57+
provider = createProvider(async () => mockSnippetDefinition);
58+
});
6959

70-
describe('hover', () => {
7160
it('should return snippet definition with all parameters', async () => {
7261
await expect(provider).to.hover(
7362
`{% render 'product-car█d' %}`,
74-
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `[border-radius]`: number - The border radius in px\n- `no-type` - This parameter has no type\n- `no-description`: string\n- `no-type-or-description`',
63+
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `[border_radius]`: number - The border radius in px\n- `no_type` - This parameter has no type\n- `no_description`: string\n- `no_type_or_description`',
7564
);
7665
});
7766

@@ -95,8 +84,81 @@ describe('Module: RenderSnippetHoverProvider', async () => {
9584
it('should return snippet definition with all parameters', async () => {
9685
await expect(provider).to.hover(
9786
`{% render 'product-car█d' %}`,
98-
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `[border-radius]`: number - The border radius in px\n- `no-type` - This parameter has no type\n- `no-description`: string\n- `no-type-or-description`',
87+
'### product-card\n\n**Parameters:**\n- `title`: string - The title of the product\n- `[border_radius]`: number - The border radius in px\n- `no_type` - This parameter has no type\n- `no_description`: string\n- `no_type_or_description`',
88+
);
89+
});
90+
});
91+
92+
describe('optional parameters', () => {
93+
beforeEach(async () => {
94+
getSnippetDefinition = async () => mockSnippetDefinitionWithOptionalParameters;
95+
provider = createProvider(getSnippetDefinition);
96+
});
97+
98+
const mockSnippetDefinitionWithOptionalParameters: SnippetDefinition = {
99+
name: 'product-card',
100+
liquidDoc: {
101+
parameters: [
102+
{
103+
name: 'with_default_value',
104+
description: null,
105+
type: null,
106+
required: false,
107+
defaultValue: 'default value',
108+
},
109+
{
110+
name: 'with_default_value_and_type',
111+
description: null,
112+
type: 'String',
113+
required: false,
114+
defaultValue: 'default value',
115+
},
116+
{
117+
name: 'with_default_value_and_description',
118+
description: 'The title of the product',
119+
type: null,
120+
required: false,
121+
defaultValue: 'default value',
122+
},
123+
{
124+
name: 'with_default_value_and_type_and_description',
125+
description: 'The title of the product',
126+
type: 'String',
127+
required: false,
128+
defaultValue: 'default value',
129+
},
130+
{
131+
name: 'no_default_value',
132+
description: 'The title of the product',
133+
type: 'String',
134+
required: false,
135+
defaultValue: null,
136+
},
137+
],
138+
},
139+
};
140+
141+
it('should return snippet definition with all parameters', async () => {
142+
await expect(provider).to.hover(
143+
`{% render 'product-car█d' %}`,
144+
'### product-card\n\n**Parameters:**\n- `[with_default_value=default value]`\n- `[with_default_value_and_type=default value]`: String\n- `[with_default_value_and_description=default value]` - The title of the product\n- `[with_default_value_and_type_and_description=default value]`: String - The title of the product\n- `[no_default_value]`: String - The title of the product',
99145
);
100146
});
101147
});
102148
});
149+
150+
const createProvider = (getSnippetDefinition: GetSnippetDefinitionForURI) => {
151+
return new HoverProvider(
152+
new DocumentManager(),
153+
{
154+
filters: async () => [],
155+
objects: async () => [],
156+
tags: async () => [],
157+
systemTranslations: async () => ({}),
158+
},
159+
async (_rootUri: string) => ({} as MetafieldDefinitionMap),
160+
async () => ({}),
161+
async () => [],
162+
getSnippetDefinition,
163+
);
164+
};

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,21 @@ export class RenderSnippetHoverProvider implements BaseHoverProvider {
6464

6565
private buildParameters(parameters: LiquidDocParameter[]) {
6666
return parameters
67-
.map(({ name, type, description, required }: LiquidDocParameter) => {
68-
const nameStr = required ? `\`${name}\`` : `\`[${name}]\``;
69-
const typeStr = type ? `: ${type}` : '';
70-
const descStr = description ? ` - ${description}` : '';
71-
return `- ${nameStr}${typeStr}${descStr}`;
67+
.map(({ name, type, description, required, defaultValue }) => {
68+
let paramName: string;
69+
70+
if (required) {
71+
paramName = `\`${name}\``;
72+
} else {
73+
paramName = `\`[${name}`;
74+
if (defaultValue) paramName += `=${defaultValue}`;
75+
paramName += ']`';
76+
}
77+
78+
const typePart = type ? `: ${type}` : '';
79+
const descPart = description ? ` - ${description}` : '';
80+
81+
return `- ${paramName}${typePart}${descPart}`;
7282
})
7383
.join('\n');
7484
}

0 commit comments

Comments
 (0)