UNPKG

@shopify/theme-language-server-common

Version:

<h1 align="center" style="position: relative;" > <br> <img src="https://github.com/Shopify/theme-check-vscode/blob/main/images/shopify_glyph.png?raw=true" alt="logo" width="141" height="160"> <br> Theme Language Server </h1>

90 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SnippetRenameHandler = void 0; const liquid_html_parser_1 = require("@shopify/liquid-html-parser"); const theme_check_common_1 = require("@shopify/theme-check-common"); const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol"); const uri_1 = require("../../utils/uri"); const visitor_1 = require("../../visitor"); /** * The SnippetRenameHandler will handle snippet renames. * * We'll change all the render and include tags that reference the old snippet * to reference the new snippet. * * {% render 'oldName' %} -> {% render 'newName' %} * * We'll do this by visiting all the liquid files in the theme and looking for * render and include tags that reference the old snippet. We'll then create a * WorkspaceEdit that changes the references to the new snippet. */ class SnippetRenameHandler { constructor(connection) { this.connection = connection; } async onDidRenameFiles(params, theme) { const isLiquidSourceCode = (file) => file.type === theme_check_common_1.SourceCodeType.LiquidHtml; const liquidSourceCodes = theme.filter(isLiquidSourceCode); const relevantRenames = params.files.filter((file) => (0, uri_1.isSnippet)(file.oldUri) && (0, uri_1.isSnippet)(file.newUri)); const promises = relevantRenames.map(async (file) => { var _a; const oldSnippetName = (0, uri_1.snippetName)(file.oldUri); const newSnippetName = (0, uri_1.snippetName)(file.newUri); const editLabel = `Rename snippet '${oldSnippetName}' to '${newSnippetName}'`; const annotationId = 'renameSnippet'; const workspaceEdit = { documentChanges: [], changeAnnotations: { [annotationId]: { label: editLabel, needsConfirmation: false, }, }, }; for (const sourceCode of liquidSourceCodes) { if (sourceCode.ast instanceof Error) continue; const textDocument = sourceCode.textDocument; const edits = (0, visitor_1.visit)(sourceCode.ast, { LiquidTag(node) { if (node.name !== liquid_html_parser_1.NamedTags.render && node.name !== liquid_html_parser_1.NamedTags.include) { return; } if (typeof node.markup === 'string') { return; } const snippet = node.markup.snippet; if (snippet.type === liquid_html_parser_1.NodeTypes.String && snippet.value === oldSnippetName) { return { newText: `${newSnippetName}`, range: vscode_languageserver_protocol_1.Range.create(textDocument.positionAt(snippet.position.start + 1), // +1 to skip the opening quote textDocument.positionAt(snippet.position.end - 1)), }; } }, }); if (edits.length === 0) continue; workspaceEdit.documentChanges.push({ textDocument: { uri: textDocument.uri, version: (_a = sourceCode.version) !== null && _a !== void 0 ? _a : null /* null means file from disk in this API */, }, annotationId, edits, }); } if (workspaceEdit.documentChanges.length === 0) { console.error('Nothing to do!'); return; } return this.connection.sendRequest(vscode_languageserver_protocol_1.ApplyWorkspaceEditRequest.type, { label: editLabel, edit: workspaceEdit, }); }); await Promise.all(promises); } } exports.SnippetRenameHandler = SnippetRenameHandler; //# sourceMappingURL=SnippetRenameHandler.js.map