From dcc20f9ab311d8016c8a51152bb072271510721f Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Mon, 9 Sep 2024 15:34:44 -0400 Subject: [PATCH] Use parser statement recovery for String pasting where errors exist. - Add testcase Signed-off-by: Roland Grunberg --- .../internal/handlers/PasteEventHandler.java | 1 + .../handlers/PasteEventHandlerTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java index 810f194cd0..66e0f72120 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java @@ -224,6 +224,7 @@ public static DocumentPasteEdit handlePasteEvent(PasteEventParams params, IProgr ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); parser.setSource(cu); parser.setResolveBindings(false); + parser.setStatementsRecovery(true); CompilationUnit ast = (CompilationUnit) parser.createAST(monitor); DocumentPasteEdit edit = handleStringPasteEvent(params, cu, ast, monitor); if (edit == null) { diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandlerTest.java index 27ec4a68e3..5ac3bf46a1 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandlerTest.java @@ -66,6 +66,28 @@ public void testPasteIntoEmptyStringLiteral() throws CoreException { Assert.assertEquals(new DocumentPasteEdit("aaa\\n\" + //\n\t\t\t\"aaa"), actual); } + @Test + public void testPasteWithErrorOnLine() throws CoreException { + ICompilationUnit unit = fPackageTest.createCompilationUnit("A.java", // + "package test;\n" + // + "public class A {\n" + // + "public void test() {\n" + // + "\tString asdf = \"\"\n" + // + "}\n" + // + "}\n", + false, monitor); + + var params = new PasteEventParams( // + createLocation(JDTUtils.toUri(unit), 3, 16, 3, 16), // + "aaa\naaa", // + null, // + new FormattingOptions(4, false)); + + DocumentPasteEdit actual = PasteEventHandler.handlePasteEvent(params, null); + + Assert.assertEquals(new DocumentPasteEdit("aaa\\n\" + //\n\t\t\t\"aaa"), actual); + } + @Test public void testPasteWindowsNewlineInCopiedText() throws CoreException { ICompilationUnit unit = fPackageTest.createCompilationUnit("A.java", //