Skip to content

Commit

Permalink
Use parser statement recovery for String pasting where errors exist.
Browse files Browse the repository at this point in the history
- Add testcase

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Sep 10, 2024
1 parent a703baf commit dcc20f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", //
Expand Down

0 comments on commit dcc20f9

Please sign in to comment.