fish-lsp
Version:
LSP implementation for fish/fish-shell
80 lines (79 loc) • 2.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRenames = getRenames;
const references_1 = require("./references");
const analyze_1 = require("./analyze");
const logger_1 = require("./logger");
function getRenames(doc, position, newText) {
const symbol = analyze_1.analyzer.getDefinition(doc, position);
if (!symbol || !newText)
return [];
if (!canRenameWithNewText(analyze_1.analyzer, doc, position, newText))
return [];
newText = fixNewText(symbol, position, newText);
const locs = (0, references_1.getReferences)(doc, position);
return locs.map(loc => {
const locationText = analyze_1.analyzer.getTextAtLocation(loc);
let replaceText = newText || locationText;
if (locationText.startsWith('_flag_') && symbol.fishKind === 'ARGPARSE') {
loc.range.start.character += '_flag_'.length;
if (newText?.includes('-')) {
replaceText = newText.replace(/-/g, '_');
}
}
if (locationText.includes('=') && symbol.fishKind === 'ARGPARSE') {
loc.range.end.character = loc.range.start.character + locationText.indexOf('=');
}
return {
uri: loc.uri,
range: loc.range,
type: symbol.fishKind,
newText: replaceText,
};
});
}
function fixNewText(symbol, position, newText) {
if (symbol.fishKind === 'ARGPARSE' && !symbol.containsPosition(position) && newText?.startsWith('_flag_')) {
return newText.replace(/^_flag_/g, '').replace(/_/g, '-');
}
if (symbol.fishKind === 'ARGPARSE' && !symbol.containsPosition(position) && newText?.startsWith('-')) {
return newText.replace(/^-{1,2}/, '');
}
return newText;
}
function canRenameWithNewText(analyzer, doc, position, newText) {
const isShort = (str) => {
if (str.startsWith('--'))
return false;
if (str.startsWith('-'))
return true;
return false;
};
const isLong = (str) => {
if (str.startsWith('--'))
return true;
if (str.startsWith('-'))
return false;
return false;
};
const isEqualFlags = (str1, str2) => {
if (isShort(str1) && !isShort(str2)) {
return false;
}
if (isLong(str1) && !isLong(str2)) {
return false;
}
return true;
};
const isFlag = (str) => {
return str.startsWith('-');
};
const oldText = analyzer.wordAtPoint(doc.uri, position.line, position.character);
logger_1.logger.log({
oldText,
newText,
});
if (oldText && isFlag(oldText) && !isEqualFlags(oldText, newText))
return false;
return true;
}