@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.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssetRenameHandler = 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 AssetRenameHandler will handle asset renames.
*
* We'll change all the `| asset_url` that reference the old asset:
* {{ 'oldName.js' | asset_url }} -> {{ 'newName.js' | asset_url }}
*
* We'll do that for `.(css|js).liquid` files as well
*
* We'll do this by visiting all the liquid files in the theme and looking for
* string | asset_url Variable nodes that reference the old asset. We'll then create a
* WorkspaceEdit that changes the references to the new asset.
*/
class AssetRenameHandler {
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.isAsset)(file.oldUri) && (0, uri_1.isAsset)(file.newUri));
const promises = relevantRenames.map(async (file) => {
var _a;
const oldAssetName = (0, uri_1.assetName)(file.oldUri);
const newAssetName = (0, uri_1.assetName)(file.newUri);
const editLabel = `Rename asset '${oldAssetName}' to '${newAssetName}'`;
const annotationId = 'renameAsset';
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, {
LiquidVariable(node) {
if (node.filters.length === 0)
return;
if (node.expression.type !== liquid_html_parser_1.NodeTypes.String)
return;
if (node.filters[0].name !== 'asset_url')
return;
const assetName = node.expression.value;
if (assetName !== oldAssetName)
return;
return {
newText: newAssetName,
range: vscode_languageserver_protocol_1.Range.create(textDocument.positionAt(node.expression.position.start + 1), // +1 to skip the opening quote
textDocument.positionAt(node.expression.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.AssetRenameHandler = AssetRenameHandler;
//# sourceMappingURL=AssetRenameHandler.js.map
;