1- import { NodeTypes } from '@shopify/liquid-html-parser' ;
1+ import { LiquidHtmlNode , LiquidTag , NodeTypes } from '@shopify/liquid-html-parser' ;
22import { CompletionItem , CompletionItemKind } from 'vscode-languageserver' ;
33import { LiquidCompletionParams } from '../params' ;
44import { Provider } from './common' ;
5+ import { SourceCodeType , visit } from '@shopify/theme-check-common' ;
56
67export type GetSnippetNamesForURI = ( uri : string ) => Promise < string [ ] > ;
78
@@ -14,21 +15,13 @@ export class RenderSnippetCompletionProvider implements Provider {
1415 const { node, ancestors } = params . completionContext ;
1516 const parentNode = ancestors . at ( - 1 ) ;
1617
17- if (
18- ! node ||
19- ! parentNode ||
20- node . type !== NodeTypes . String ||
21- parentNode . type !== NodeTypes . RenderMarkup
22- ) {
18+ if ( ! node || ! parentNode || parentNode . type !== NodeTypes . RenderMarkup ) {
2319 return [ ] ;
2420 }
2521
26- const options = await this . getSnippetNamesForURI ( params . textDocument . uri ) ;
27- const partial = node . value ;
28-
29- return options
30- . filter ( ( option ) => option . startsWith ( partial ) )
31- . map (
22+ if ( node . type === NodeTypes . String ) {
23+ const fileSnippets = await this . getSnippetNamesForURI ( params . textDocument . uri ) ;
24+ const fileCompletionItems = fileSnippets . map (
3225 ( option : string ) : CompletionItem => ( {
3326 label : option ,
3427 kind : CompletionItemKind . Snippet ,
@@ -38,5 +31,32 @@ export class RenderSnippetCompletionProvider implements Provider {
3831 } ,
3932 } ) ,
4033 ) ;
34+ return fileCompletionItems ;
35+ } else if ( node . type === NodeTypes . VariableLookup ) {
36+ const inlineSnippets = getInlineSnippetsNames ( params . completionContext . partialAst ) ;
37+ const inlineCompletionItems = inlineSnippets . map (
38+ ( option : string ) : CompletionItem => ( {
39+ label : option ,
40+ kind : CompletionItemKind . Snippet ,
41+ documentation : {
42+ kind : 'markdown' ,
43+ value : `Inline snippet (defined in this file)` ,
44+ } ,
45+ } ) ,
46+ ) ;
47+ return inlineCompletionItems ;
48+ } else {
49+ return [ ] ;
50+ }
4151 }
4252}
53+
54+ function getInlineSnippetsNames ( ast : LiquidHtmlNode ) : string [ ] {
55+ return visit < SourceCodeType . LiquidHtml , string > ( ast , {
56+ LiquidTag ( node : LiquidTag ) {
57+ if ( node . name === 'snippet' && typeof node . markup !== 'string' && node . markup . name ) {
58+ return node . markup . name ;
59+ }
60+ } ,
61+ } ) ;
62+ }
0 commit comments