-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
content_for
block type completion + document link
- Loading branch information
Showing
8 changed files
with
116 additions
and
3 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,13 @@ | ||
--- | ||
'@shopify/theme-language-server-common': minor | ||
--- | ||
|
||
Support `content_for` block type completion + document link | ||
|
||
- The following code will offer completion suggestions based on public blocks | ||
within the blocks folder. | ||
``` | ||
{% content_for "block", type: "█", id: "" %} | ||
``` | ||
- You can navigate to a block file by clicking through the `type` parameter value | ||
within the `content_for "block"` tag. |
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
39 changes: 39 additions & 0 deletions
39
...age-server-common/src/completions/providers/ContentForBlockTypeCompletionProvider.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,39 @@ | ||
import { MetafieldDefinitionMap } from '@shopify/theme-check-common'; | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
import { InsertTextFormat } from 'vscode-json-languageservice'; | ||
import { DocumentManager } from '../../documents'; | ||
import { CompletionsProvider } from '../CompletionsProvider'; | ||
|
||
describe('Module: ContentForBlockTypeCompletionProvider', async () => { | ||
let provider: CompletionsProvider; | ||
|
||
beforeEach(async () => { | ||
provider = new CompletionsProvider({ | ||
documentManager: new DocumentManager(), | ||
themeDocset: { | ||
filters: async () => [], | ||
objects: async () => [], | ||
tags: async () => [], | ||
systemTranslations: async () => ({}), | ||
}, | ||
getMetafieldDefinitions: async (_rootUri: string) => ({} as MetafieldDefinitionMap), | ||
getThemeBlockNames: async (_rootUri: string, _includePrivate: boolean) => [ | ||
'block-1', | ||
'block-2', | ||
], | ||
}); | ||
}); | ||
|
||
it('should complete content_for "block" type parameter ', async () => { | ||
const expected = ['block-1', 'block-2'].sort(); | ||
await expect(provider).to.complete('{% content_for "block", type: "█" %}', expected); | ||
}); | ||
|
||
it('should not complete content_for "blocks" type parameter', async () => { | ||
await expect(provider).to.complete('{% content_for "blocks", type: "█" %}', []); | ||
}); | ||
|
||
it('should not complete content_for "block" id parameter', async () => { | ||
await expect(provider).to.complete('{% content_for "block", type: "", id: "█" %}', []); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
...language-server-common/src/completions/providers/ContentForBlockTypeCompletionProvider.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,42 @@ | ||
import { NodeTypes } from '@shopify/liquid-html-parser'; | ||
import { CompletionItem, CompletionItemKind } from 'vscode-languageserver'; | ||
import { LiquidCompletionParams } from '../params'; | ||
import { Provider } from './common'; | ||
|
||
export class ContentForBlockTypeCompletionProvider implements Provider { | ||
constructor( | ||
private readonly getThemeBlockNames: ( | ||
rootUri: string, | ||
includePrivate: boolean, | ||
) => Promise<string[]>, | ||
) {} | ||
|
||
async completions(params: LiquidCompletionParams): Promise<CompletionItem[]> { | ||
if (!params.completionContext) return []; | ||
|
||
const { document } = params; | ||
const doc = document.textDocument; | ||
const { node, ancestors } = params.completionContext; | ||
const parentNode = ancestors.at(-1); | ||
const grandParentNode = ancestors.at(-2); | ||
|
||
if ( | ||
!node || | ||
!parentNode || | ||
!grandParentNode || | ||
node.type !== NodeTypes.String || | ||
parentNode.type !== NodeTypes.NamedArgument || | ||
parentNode.name !== 'type' || | ||
grandParentNode.type !== NodeTypes.ContentForMarkup || | ||
grandParentNode.contentForType.value !== 'block' | ||
) { | ||
return []; | ||
} | ||
|
||
return (await this.getThemeBlockNames(doc.uri, false)).map((blockName) => ({ | ||
label: blockName, | ||
kind: CompletionItemKind.Keyword, | ||
insertText: blockName, | ||
})); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
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
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
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