-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support presets blocks settings completion + hover
- Loading branch information
Showing
10 changed files
with
449 additions
and
117 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,10 @@ | ||
--- | ||
'@shopify/theme-language-server-common': minor | ||
'@shopify/theme-language-server-node': minor | ||
--- | ||
|
||
Support completion + hover for presets blocks settings under `{% schema %}` tag | ||
|
||
- Hover + Completion description for `presets.[].blocks.[].settings` will be from the referenced | ||
block's setting's label - i.e. `settings.[].label` | ||
- The label will be translated if it contains a translation key |
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
83 changes: 83 additions & 0 deletions
83
...r-common/src/json/completions/providers/PresetsBlockSettingsPropertyCompletionProvider.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,83 @@ | ||
import { JSONPath } from 'vscode-json-languageservice'; | ||
import { GetThemeBlockSchema } from '../../JSONContributions'; | ||
import { RequestContext } from '../../RequestContext'; | ||
import { JSONCompletionItem, JSONCompletionProvider } from '../JSONCompletionProvider'; | ||
import { isSectionOrBlockFile } from '../../utils'; | ||
import { deepGet, isError, SourceCodeType } from '@shopify/theme-check-common'; | ||
import { isSectionOrBlockSchema } from './BlockTypeCompletionProvider'; | ||
import { GetTranslationsForURI } from '../../../translations'; | ||
import { schemaSettingsPropertyCompletionItems } from './helpers/schemaSettings'; | ||
|
||
/** | ||
* The PresetsBlockSettingsPropertyCompletionProvider offers value completions of the | ||
* `presets.[].(recursive blocks.[]).settings` keys inside section and theme block `{% schema %}` tags. | ||
* | ||
* @example | ||
* {% schema %} | ||
* { | ||
* "presets": [ | ||
* { | ||
* "blocks": [ | ||
* { | ||
* "type": "block-type", | ||
* "settings": { | ||
* "█" | ||
* } | ||
* }, | ||
* ] | ||
* }, | ||
* ] | ||
* } | ||
* {% endschema %} | ||
*/ | ||
export class PresetsBlockSettingsPropertyCompletionProvider implements JSONCompletionProvider { | ||
constructor( | ||
private getDefaultSchemaTranslations: GetTranslationsForURI, | ||
private getThemeBlockSchema: GetThemeBlockSchema, | ||
) {} | ||
|
||
async completeProperty(context: RequestContext, path: JSONPath): Promise<JSONCompletionItem[]> { | ||
const { doc } = context; | ||
|
||
if (doc.type !== SourceCodeType.LiquidHtml) return []; | ||
if (!isSectionOrBlockFile(doc.uri) || !isPresetsBlocksSettingsPath(path)) { | ||
return []; | ||
} | ||
|
||
const schema = await doc.getSchema(); | ||
|
||
if (!schema || !isSectionOrBlockSchema(schema) || isError(schema.parsed)) { | ||
return []; | ||
} | ||
|
||
const blockType = deepGet(schema.parsed, [...path.slice(0, -1), 'type']); | ||
|
||
if (!blockType) { | ||
return []; | ||
} | ||
|
||
const blockOriginSchema = await this.getThemeBlockSchema(doc.uri, blockType); | ||
|
||
if ( | ||
!blockOriginSchema || | ||
isError(blockOriginSchema.parsed) || | ||
!isSectionOrBlockSchema(blockOriginSchema) | ||
) { | ||
return []; | ||
} | ||
|
||
if (!blockOriginSchema.parsed?.settings || !Array.isArray(blockOriginSchema.parsed?.settings)) { | ||
return []; | ||
} | ||
|
||
const translations = await this.getDefaultSchemaTranslations(doc.textDocument.uri); | ||
|
||
return schemaSettingsPropertyCompletionItems(blockOriginSchema.parsed, translations); | ||
} | ||
} | ||
|
||
// `blocks` can be nested within other `blocks` | ||
// We need to ensure the last leg of the path is { "blocks": [{ "settings": { "█" } }] } | ||
function isPresetsBlocksSettingsPath(path: JSONPath) { | ||
return path.at(0) === 'presets' && path.at(-3) === 'blocks' && path.at(-1) === 'settings'; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...ges/theme-language-server-common/src/json/completions/providers/helpers/schemaSettings.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,38 @@ | ||
import { CompletionItemKind, MarkupKind } from 'vscode-json-languageservice'; | ||
import { renderTranslation, translationValue } from '../../../../translations'; | ||
import { Translations } from '@shopify/theme-check-common'; | ||
|
||
export function schemaSettingsPropertyCompletionItems( | ||
parsedSchema: any, | ||
translations: Translations, | ||
) { | ||
return parsedSchema.settings | ||
.filter((setting: any) => setting.id) | ||
.map((setting: any) => { | ||
let docValue = ''; | ||
|
||
if (setting.label) { | ||
if (setting.label.startsWith('t:')) { | ||
const translation = translationValue(setting.label.substring(2), translations); | ||
|
||
if (translation) { | ||
docValue = renderTranslation(translation); | ||
} | ||
} else { | ||
docValue = setting.label; | ||
} | ||
} | ||
|
||
const completionText = setting.id ? `"${setting.id}"` : ''; | ||
|
||
return { | ||
kind: CompletionItemKind.Property, | ||
label: completionText, | ||
insertText: completionText, | ||
documentation: { | ||
kind: MarkupKind.Markdown, | ||
value: docValue, | ||
}, | ||
}; | ||
}); | ||
} |
Oops, something went wrong.