mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 13:45:34 -05:00
Fixup line and offset of rename location of refactor (#19265)
* Fixup line and offset of rename location of refactor * Fixes * Handle "\r" only documents * Update api baselines * Fix error if an edit comes *after* the rename location * Add bounds check * Simpler implementation: get new text, then calculate line starts the usual way
This commit is contained in:
@@ -1514,16 +1514,18 @@ namespace ts.server {
|
||||
}
|
||||
|
||||
if (simplifiedResult) {
|
||||
const file = result.renameFilename;
|
||||
let location: protocol.Location | undefined;
|
||||
if (file !== undefined && result.renameLocation !== undefined) {
|
||||
const renameScriptInfo = project.getScriptInfoForNormalizedPath(toNormalizedPath(file));
|
||||
location = renameScriptInfo.positionToLineOffset(result.renameLocation);
|
||||
const { renameFilename, renameLocation, edits } = result;
|
||||
let mappedRenameLocation: protocol.Location | undefined;
|
||||
if (renameFilename !== undefined && renameLocation !== undefined) {
|
||||
const renameScriptInfo = project.getScriptInfoForNormalizedPath(toNormalizedPath(renameFilename));
|
||||
const snapshot = renameScriptInfo.getSnapshot();
|
||||
const oldText = snapshot.getText(0, snapshot.getLength());
|
||||
mappedRenameLocation = getLocationInNewDocument(oldText, renameFilename, renameLocation, edits);
|
||||
}
|
||||
return {
|
||||
renameLocation: location,
|
||||
renameFilename: file,
|
||||
edits: result.edits.map(change => this.mapTextChangesToCodeEdits(project, change))
|
||||
renameLocation: mappedRenameLocation,
|
||||
renameFilename,
|
||||
edits: edits.map(change => this.mapTextChangesToCodeEdits(project, change))
|
||||
};
|
||||
}
|
||||
else {
|
||||
@@ -2040,4 +2042,26 @@ namespace ts.server {
|
||||
response?: {};
|
||||
responseRequired?: boolean;
|
||||
}
|
||||
|
||||
/* @internal */ // Exported only for tests
|
||||
export function getLocationInNewDocument(oldText: string, renameFilename: string, renameLocation: number, edits: ReadonlyArray<FileTextChanges>): protocol.Location {
|
||||
const newText = applyEdits(oldText, renameFilename, edits);
|
||||
const { line, character } = computeLineAndCharacterOfPosition(computeLineStarts(newText), renameLocation);
|
||||
return { line: line + 1, offset: character + 1 };
|
||||
}
|
||||
|
||||
function applyEdits(text: string, textFilename: string, edits: ReadonlyArray<FileTextChanges>): string {
|
||||
for (const { fileName, textChanges } of edits) {
|
||||
if (fileName !== textFilename) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let i = textChanges.length - 1; i >= 0; i--) {
|
||||
const { newText, span: { start, length } } = textChanges[i];
|
||||
text = text.slice(0, start) + newText + text.slice(start + length);
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user