[mod-commands] Rely on document text, rather than PSI text when building the final command

For some reason, they might be different. Probably due to incorrect PSI at the beginning of the action. In any case, document should be the source of truth, as the ModCommand will be applied to the document, rather than PSI
Fixes IDEA-359782 try postfix completion generates trailing garbage


(cherry picked from commit 292399a28d347f9b4fa22730002299d5c5b68167)

IJ-CR-147269

GitOrigin-RevId: 2ccd437d3c51c0ded16be634650ed8dfc59770b4
This commit is contained in:
Tagir Valeev
2024-10-18 17:43:24 +02:00
committed by intellij-monorepo-bot
parent 83fc84993c
commit 1d58cede1d
5 changed files with 36 additions and 4 deletions

View File

@@ -0,0 +1,12 @@
class Hello {
public static void main(String[] args) {
new Thread(() -> {
int a = 1;
int b = 2;
int c = 3;
}).start();.try<caret>
int d = 4;
}
}

View File

@@ -0,0 +1,16 @@
class Hello {
public static void main(String[] args) {
try {
new Thread(() -> {
int a = 1;
int b = 2;
int c = 3;
}).start();<caret>
} catch (Exception e) {
throw new RuntimeException(e);
}
int d = 4;
}
}

View File

@@ -59,4 +59,8 @@ public class TryPostfixTemplateTest extends PostfixTemplateTestCase {
public void testConstructorStatement() {
doTest();
}
public void testAfterLambda() {
doTest();
}
}

View File

@@ -23,7 +23,7 @@ public record ModUpdateFileText(@NotNull VirtualFile file, @NotNull String oldTe
for (int i = 0; i < updatedRanges.size(); i++) {
Fragment prev = updatedRanges.get(i);
if (prev.offset() + prev.newLength > newText.length()) {
throw new IllegalArgumentException("Range out of bounds: " + prev);
throw new IllegalArgumentException("Range out of bounds: " + prev + "; newText.length()=" + newText.length());
}
if (i < updatedRanges.size() - 1) {
Fragment next = updatedRanges.get(i + 1);

View File

@@ -143,7 +143,7 @@ final class PsiUpdateImpl {
}
myManager.commitDocument(myDocument);
unblock();
String newText = myTargetFile.getText();
String newText = myTargetFile.getFileDocument().getText();
return myOrigText.equals(newText) ? new ModNothing() :
new ModUpdateFileText(origVirtualFile, myOrigText, newText, myFragments);
}
@@ -303,7 +303,7 @@ final class PsiUpdateImpl {
return tracker;
});
}
@Override
public @NotNull PsiFile getOriginalFile(@NotNull PsiFile copyFile) throws IllegalArgumentException {
Map.Entry<PsiFile, FileTracker> entry = ContainerUtil.find(myChangedFiles.entrySet(), e -> e.getValue().myCopyFile == copyFile);
@@ -644,7 +644,7 @@ final class PsiUpdateImpl {
private @NotNull ModCommand getNavigateCommand() {
if (!myPositionUpdated || myRenameSymbol != null) return nop();
int length = myTracker.myTargetFile.getTextLength();
int length = myTracker.myTargetFile.getFileDocument().getTextLength();
int start = -1, end = -1, caret = -1;
if (mySelection.getEndOffset() <= length) {
start = mySelection.getStartOffset();