From 279d1d8f655a01ab0ec2b08dc7639157bbe6c4dc Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sun, 28 Apr 2024 07:38:27 -0300 Subject: [PATCH] Fix issue computing replace offset text edit. --- .../AddTokenAndImportStatement.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/AddTokenAndImportStatement.java b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/AddTokenAndImportStatement.java index 18b7864c52..c4295a81e1 100644 --- a/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/AddTokenAndImportStatement.java +++ b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/AddTokenAndImportStatement.java @@ -53,14 +53,19 @@ public ComputedInfo(String realImportRep, int fReplacementOffset, int fLen, Stri public List replaceEdit = new ArrayList(2); public void replace(int offset, int length, String text) { - replaceEdit.add(new ReplaceEdit(offset, length, text)); + try { + replaceEdit.add(new ReplaceEdit(offset, length, text)); + } catch (Exception e) { + Log.log("Error trying to create ReplaceEdit with offset: " + offset + " len: " + length + " text: " + + text, e); + } } } private IDocument document; private char trigger; - private int offset; + private final int offset; private boolean addLocalImport; private boolean addLocalImportsOnTopOfMethod; private boolean groupImports; @@ -234,12 +239,12 @@ public void createTextEdit(ComputedInfo computedInfo) { lastImportLineBuf.deleteLast(); } - offset = TextSelectionUtils.getAbsoluteCursorOffset(document, lastImportLineNum, + int replaceOffset = TextSelectionUtils.getAbsoluteCursorOffset(document, lastImportLineNum, lastImportLineBuf.length()); - offset -= lastImportLineBuf.length() - lastImportLineBuf.rightTrim().length(); + replaceOffset -= lastImportLineBuf.length() - lastImportLineBuf.rightTrim().length(); while (lastImportLineBuf.endsWith(',')) { - offset--; + replaceOffset--; lastImportLineBuf.deleteLast(); } @@ -254,12 +259,12 @@ public void createTextEdit(ComputedInfo computedInfo) { + realImportHandleInfo.getImportedStr().get(0); } computedInfo.importLen = strToAdd.length(); - computedInfo.replace(offset, 0, strToAdd); + computedInfo.replace(replaceOffset, 0, strToAdd); return; } else { //regular addition (it won't pass the number of columns expected). computedInfo.importLen = strToAdd.length(); - computedInfo.replace(offset, 0, strToAdd); + computedInfo.replace(replaceOffset, 0, strToAdd); return; } }