@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>
104 lines • 5.07 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiquidVariableRenameProvider = void 0;
const liquid_html_parser_1 = require("@shopify/liquid-html-parser");
const vscode_languageserver_1 = require("vscode-languageserver");
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
const visitor_1 = require("../../visitor");
class LiquidVariableRenameProvider {
constructor(documentManager) {
this.documentManager = documentManager;
}
async prepare(node, ancestors, params) {
const document = this.documentManager.get(params.textDocument.uri);
const textDocument = document === null || document === void 0 ? void 0 : document.textDocument;
if (!textDocument || !node || !ancestors)
return null;
if (!supportedTags(node))
return null;
const oldName = variableName(node);
const offsetOfVariableNameEnd = node.position.start + oldName.length;
// The cursor could be past the end of the variable name
if (textDocument.offsetAt(params.position) > offsetOfVariableNameEnd)
return null;
return {
range: vscode_languageserver_1.Range.create(textDocument.positionAt(node.position.start), textDocument.positionAt(offsetOfVariableNameEnd)),
placeholder: oldName,
};
}
async rename(node, ancestors, params) {
const document = this.documentManager.get(params.textDocument.uri);
const textDocument = document === null || document === void 0 ? void 0 : document.textDocument;
if (!textDocument || !node || !ancestors)
return null;
if (document.ast instanceof Error)
return null;
if (!supportedTags(node))
return null;
const oldName = variableName(node);
const scope = variableNameBlockScope(oldName, ancestors);
const replaceRange = textReplaceRange(oldName, textDocument, scope);
const ranges = (0, visitor_1.visit)(document.ast, {
VariableLookup: replaceRange,
AssignMarkup: replaceRange,
ForMarkup: replaceRange,
});
const textDocumentEdit = vscode_languageserver_protocol_1.TextDocumentEdit.create({ uri: textDocument.uri, version: textDocument.version }, ranges.map((range) => vscode_languageserver_protocol_1.TextEdit.replace(range, params.newName)));
return {
documentChanges: [textDocumentEdit],
};
}
}
exports.LiquidVariableRenameProvider = LiquidVariableRenameProvider;
function supportedTags(node) {
return (node.type === liquid_html_parser_1.NodeTypes.AssignMarkup ||
node.type === liquid_html_parser_1.NodeTypes.VariableLookup ||
node.type === liquid_html_parser_1.NodeTypes.ForMarkup);
}
function variableName(node) {
var _a, _b;
switch (node.type) {
case liquid_html_parser_1.NodeTypes.VariableLookup:
case liquid_html_parser_1.NodeTypes.AssignMarkup:
return (_a = node.name) !== null && _a !== void 0 ? _a : '';
case liquid_html_parser_1.NodeTypes.ForMarkup:
return (_b = node.variableName) !== null && _b !== void 0 ? _b : '';
default:
return '';
}
}
/*
* Find the scope where the variable name is used. Looks at defined in `tablerow` and `for` tags.
*/
function variableNameBlockScope(variableName, ancestors) {
let scopedAncestor;
for (let i = ancestors.length - 1; i >= 0; i--) {
const ancestor = ancestors[i];
if (ancestor.type === liquid_html_parser_1.NodeTypes.LiquidTag &&
(ancestor.name === liquid_html_parser_1.NamedTags.tablerow || ancestor.name === liquid_html_parser_1.NamedTags.for) &&
typeof ancestor.markup !== 'string' &&
ancestor.markup.variableName === variableName) {
scopedAncestor = ancestor;
break;
}
}
if (!scopedAncestor || !scopedAncestor.blockEndPosition)
return;
return {
start: scopedAncestor.blockStartPosition.start,
end: scopedAncestor.blockEndPosition.end,
};
}
function textReplaceRange(oldName, textDocument, selectedVariableScope) {
return (node, ancestors) => {
if (variableName(node) !== oldName)
return;
const ancestorScope = variableNameBlockScope(oldName, ancestors);
if ((ancestorScope === null || ancestorScope === void 0 ? void 0 : ancestorScope.start) !== (selectedVariableScope === null || selectedVariableScope === void 0 ? void 0 : selectedVariableScope.start) ||
(ancestorScope === null || ancestorScope === void 0 ? void 0 : ancestorScope.end) !== (selectedVariableScope === null || selectedVariableScope === void 0 ? void 0 : selectedVariableScope.end)) {
return;
}
return vscode_languageserver_1.Range.create(textDocument.positionAt(node.position.start), textDocument.positionAt(node.position.start + oldName.length));
};
}
//# sourceMappingURL=LiquidVariableRenameProvider.js.map
;