Skip to content

Commit 7e81b7a

Browse files
committedMar 5, 2025
Fix liquid doc node hover text
1 parent 85579bc commit 7e81b7a

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed
 

‎.changeset/rare-dolls-complain.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme-language-server-common': patch
3+
---
4+
5+
Fix bug where help text appears when hovering over liquid doc nodes outside param names

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class HoverProvider {
5757
new TranslationHoverProvider(getTranslationsForURI, documentManager),
5858
new RenderSnippetHoverProvider(getSnippetDefinitionForURI),
5959
new RenderSnippetParameterHoverProvider(getSnippetDefinitionForURI),
60-
new LiquidDocTagHoverProvider(),
60+
new LiquidDocTagHoverProvider(documentManager),
6161
];
6262
}
6363

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

+4-22
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,12 @@ describe('Module: RenderSnippetParameterHoverProvider', async () => {
4848
);
4949
});
5050

51-
it('should show the param help doc when hovering over the text', async () => {
51+
it('should not show the param help doc when hovering over text outside param name', async () => {
5252
await expect(provider).to.hover(
5353
`{% doc %} @param {string} name - █your name {% enddoc %}`,
54-
formatLiquidDocTagHandle(
55-
'param',
56-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['param'].description,
57-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['param'].example,
58-
),
59-
);
60-
await expect(provider).to.hover(
61-
`{% doc %} @example my █example {% enddoc %}`,
62-
formatLiquidDocTagHandle(
63-
'example',
64-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['example'].description,
65-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['example'].example,
66-
),
67-
);
68-
await expect(provider).to.hover(
69-
`{% doc %} @description cool text█ is cool {% enddoc %}`,
70-
formatLiquidDocTagHandle(
71-
'description',
72-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['description'].description,
73-
SUPPORTED_LIQUID_DOC_TAG_HANDLES['description'].example,
74-
),
54+
null,
7555
);
56+
await expect(provider).to.hover(`{% doc %} @example my █example {% enddoc %}`, null);
57+
await expect(provider).to.hover(`{% doc %} @description cool text█ is cool {% enddoc %}`, null);
7658
});
7759
});

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

+24-21
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
11
import { NodeTypes } from '@shopify/liquid-html-parser';
22
import { LiquidHtmlNode } from '@shopify/theme-check-common';
3-
import { Hover, MarkupKind } from 'vscode-languageserver';
3+
import { Hover, HoverParams, MarkupKind } from 'vscode-languageserver';
44
import { BaseHoverProvider } from '../BaseHoverProvider';
55
import { formatLiquidDocTagHandle, SUPPORTED_LIQUID_DOC_TAG_HANDLES } from '../../utils/liquidDoc';
6+
import { DocumentManager } from '../../documents';
67

78
export class LiquidDocTagHoverProvider implements BaseHoverProvider {
8-
constructor() {}
9+
constructor(private documentManager: DocumentManager) {}
910

10-
async hover(currentNode: LiquidHtmlNode, ancestors: LiquidHtmlNode[]): Promise<Hover | null> {
11+
async hover(
12+
currentNode: LiquidHtmlNode,
13+
ancestors: LiquidHtmlNode[],
14+
params: HoverParams,
15+
): Promise<Hover | null> {
1116
const parentNode = ancestors.at(-1);
1217

13-
let docTagNode;
14-
15-
// We could be hovering on the liquidDoc tag itself
1618
if (
17-
currentNode.type === NodeTypes.LiquidDocParamNode ||
18-
currentNode.type === NodeTypes.LiquidDocDescriptionNode ||
19-
currentNode.type === NodeTypes.LiquidDocExampleNode
19+
currentNode.type !== NodeTypes.LiquidDocParamNode &&
20+
currentNode.type !== NodeTypes.LiquidDocDescriptionNode &&
21+
currentNode.type !== NodeTypes.LiquidDocExampleNode
2022
) {
21-
docTagNode = currentNode;
23+
return null;
2224
}
2325

24-
// or we could be hovering on the liquidDoc tag's text
26+
const document = this.documentManager.get(params.textDocument.uri)?.textDocument;
27+
28+
// We only want to provide hover when we are on the exact tag name
29+
// If the cursor is passed that but still within the tag node, we ignore it
30+
//
31+
// E.g.
32+
// Provide hover: @para█m name - description
33+
// Don't provide hover: @param █name - description
2534
if (
26-
(parentNode?.type === NodeTypes.LiquidDocParamNode ||
27-
parentNode?.type === NodeTypes.LiquidDocDescriptionNode ||
28-
parentNode?.type === NodeTypes.LiquidDocExampleNode) &&
29-
currentNode.type === NodeTypes.TextNode
35+
document &&
36+
document.offsetAt(params.position) > currentNode.position.start + currentNode.name.length
3037
) {
31-
docTagNode = parentNode;
32-
}
33-
34-
if (!docTagNode) {
3538
return null;
3639
}
3740

38-
const docTagData = SUPPORTED_LIQUID_DOC_TAG_HANDLES[docTagNode.name];
41+
const docTagData = SUPPORTED_LIQUID_DOC_TAG_HANDLES[currentNode.name];
3942

4043
if (!docTagData) {
4144
return null;
@@ -45,7 +48,7 @@ export class LiquidDocTagHoverProvider implements BaseHoverProvider {
4548
contents: {
4649
kind: MarkupKind.Markdown,
4750
value: formatLiquidDocTagHandle(
48-
docTagNode.name,
51+
currentNode.name,
4952
docTagData.description,
5053
docTagData.example,
5154
),

0 commit comments

Comments
 (0)
Failed to load comments.