-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add completion provider for filter parameters
- Loading branch information
1 parent
03e8304
commit 32405cf
Showing
7 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
'@shopify/theme-language-server-common': patch | ||
--- | ||
|
||
Add completions support for filter parameters | ||
|
||
The following liquid statement will now offer completion suggestions for filter | ||
parameters: | ||
|
||
```liquid | ||
{{ product | image_url: width: 100, c█ }} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ge-server-common/src/completions/providers/FilterNamedParameterCompletionProvider.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, beforeEach, it, expect } from 'vitest'; | ||
import { DocumentManager } from '../../documents'; | ||
import { CompletionsProvider } from '../CompletionsProvider'; | ||
|
||
describe('Module: ObjectCompletionProvider', async () => { | ||
let provider: CompletionsProvider; | ||
|
||
beforeEach(async () => { | ||
provider = new CompletionsProvider({ | ||
documentManager: new DocumentManager(), | ||
themeDocset: { | ||
filters: async () => [ | ||
{ | ||
parameters: [ | ||
{ | ||
description: '', | ||
name: 'crop', | ||
required: false, | ||
types: ['string'], | ||
}, | ||
{ | ||
description: '', | ||
name: 'width', | ||
required: false, | ||
types: ['number'], | ||
}, | ||
], | ||
name: 'image_url', | ||
}, | ||
], | ||
objects: async () => [], | ||
tags: async () => [], | ||
systemTranslations: async () => ({}), | ||
}, | ||
}); | ||
}); | ||
|
||
it('should complete filter parameter lookups', async () => { | ||
const contexts = [ | ||
`{{ product | image_url: █`, | ||
`{{ product | image_url: width: 100, █`, | ||
`{{ product | image_url: 1, string, width: 100, █`, | ||
`{{ product | image_url: width: 100 | image_url: █`, | ||
]; | ||
await Promise.all( | ||
contexts.map((context) => expect(provider, context).to.complete(context, ['crop', 'width'])), | ||
); | ||
}); | ||
|
||
describe('when the user has already begun typing a filter parameter', () => { | ||
it('should filter options based on the text', async () => { | ||
const contexts = [ | ||
`{{ product | image_url: c█`, | ||
`{{ product | image_url: width: 100, c█`, | ||
`{{ product | image_url: 1, string, width: 100, c█`, | ||
`{{ product | image_url: width: 100 | image_url: c█`, | ||
]; | ||
await Promise.all( | ||
contexts.map((context) => expect(provider, context).to.complete(context, ['crop'])), | ||
); | ||
}); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
...anguage-server-common/src/completions/providers/FilterNamedParameterCompletionProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { NodeTypes } from '@shopify/liquid-html-parser'; | ||
import { CompletionItem, CompletionItemKind } from 'vscode-languageserver'; | ||
import { CURSOR, LiquidCompletionParams } from '../params'; | ||
import { Provider, createCompletionItem } from './common'; | ||
import { ThemeDocset } from '@shopify/theme-check-common'; | ||
|
||
export class FilterNamedParameterCompletionProvider implements Provider { | ||
constructor(private readonly themeDocset: ThemeDocset) {} | ||
|
||
async completions(params: LiquidCompletionParams): Promise<CompletionItem[]> { | ||
if (!params.completionContext) return []; | ||
|
||
const { node } = params.completionContext; | ||
if (!node || node.type !== NodeTypes.VariableLookup) { | ||
return []; | ||
} | ||
|
||
if (!node.name || node.lookups.length > 0) { | ||
// We only do top level in this one. | ||
return []; | ||
} | ||
|
||
const partial = node.name.replace(CURSOR, ''); | ||
const currentContext = params.completionContext.ancestors.at(-1); | ||
|
||
if (!currentContext || currentContext?.type !== NodeTypes.LiquidFilter) { | ||
return []; | ||
} | ||
|
||
const filters = await this.themeDocset.filters(); | ||
const foundFilter = filters.find((f) => f.name === currentContext.name); | ||
|
||
if (!foundFilter?.parameters) { | ||
return []; | ||
} | ||
|
||
const options = foundFilter.parameters | ||
.filter((p) => p.name.startsWith(partial)) | ||
.map((parameter) => { | ||
return { | ||
entry: { | ||
...parameter, | ||
}, | ||
types: parameter.types, | ||
}; | ||
}); | ||
|
||
return options.map(({ entry, types }) => | ||
createCompletionItem( | ||
entry, | ||
{ kind: CompletionItemKind.TypeParameter }, | ||
'filter', | ||
Array.isArray(types) ? types[0] : 'unknown', | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
packages/theme-language-server-common/src/completions/providers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
export { HtmlTagCompletionProvider } from './HtmlTagCompletionProvider'; | ||
export { Provider } from './common/Provider'; | ||
export { FilterCompletionProvider } from './FilterCompletionProvider'; | ||
export { FilterNamedParameterCompletionProvider } from './FilterNamedParameterCompletionProvider'; | ||
export { HtmlAttributeCompletionProvider } from './HtmlAttributeCompletionProvider'; | ||
export { HtmlAttributeValueCompletionProvider } from './HtmlAttributeValueCompletionProvider'; | ||
export { FilterCompletionProvider } from './FilterCompletionProvider'; | ||
export { HtmlTagCompletionProvider } from './HtmlTagCompletionProvider'; | ||
export { LiquidTagsCompletionProvider } from './LiquidTagsCompletionProvider'; | ||
export { ObjectAttributeCompletionProvider } from './ObjectAttributeCompletionProvider'; | ||
export { ObjectCompletionProvider } from './ObjectCompletionProvider'; | ||
export { TranslationCompletionProvider } from './TranslationCompletionProvider'; | ||
export { RenderSnippetCompletionProvider } from './RenderSnippetCompletionProvider'; | ||
export { Provider } from './common/Provider'; | ||
export { TranslationCompletionProvider } from './TranslationCompletionProvider'; |