Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rename compilation unit support. #864

Merged
merged 6 commits into from
Nov 28, 2018
Merged

Add rename compilation unit support. #864

merged 6 commits into from
Nov 28, 2018

Conversation

yaohaizh
Copy link
Contributor

@yaohaizh yaohaizh commented Nov 22, 2018

@fbricon For resource rename support, I'll enable it in two steps: Firstly support compilation unit for most scenarios. Would like to roll out this feature ASAP.

For rename package, it need LSP support from these two issue:

@snjeza snjeza self-requested a review November 22, 2018 23:48
assertEquals(JDTUtils.toURI(cu), resourceChange.getCurrent());
Either<TextDocumentEdit, ResourceOperation> change = resourceChanges.get(2);
RenameFile resourceChange = (RenameFile) change.getRight();
assertEquals(JDTUtils.toURI(cu), resourceChange.getOldUri());
assertEquals(JDTUtils.toURI(cu).replace("E", "Newname"), resourceChange.getNewUri());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals(JDTUtils.toURI(cu).replace("E.java", "Newname.java"), resourceChange.getNewUri());

If the workspace name contains "E", the test will fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snjeza Add test cases for failure situation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @snjeza, the regexp in replace() call needs to be more specific

create.run(monitor);
if (check.getStatus().getSeverity() >= RefactoringStatus.ERROR) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (check.getStatus().hasFatalError()) {

otherwise, a rename refactoring operation will fail if there is an invalid rename participant.

That's why the following four tests fail in Eclipse - testRenamePackage, testRenameType, testRenameOverrideMethodSimple and testRenameField.

The testRenameTypeWithResourceChanges test still fails in Eclipse, if the workspace name contains "E".

Could you try to run RenameHandlerTest in Eclipse using the workspace '${workspace_loc}/../junit-workspacE' name ?

@@ -262,7 +260,42 @@ public void testRenameTypeWhithResourceChanges() throws JavaModelException, BadL
assertEquals(expected, TextEditUtil.apply(builder.toString(), testChanges));
}

@Test
@Test(expected = ResponseErrorException.class)
public void testRenameTypeWithrErrors() throws JavaModelException, BadLocationException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testRenameTypeWithErrors()

@yaohaizh
Copy link
Contributor Author

@snjeza Updated the PR

@fbricon
Copy link
Contributor

fbricon commented Nov 26, 2018

This now works much better! One problem I found though: if, from a file, you try to rename a package to something that already exists (effectively moving the type from one package to another), then the server complains the package already exists. I think it should just move the file to the new package if it already exists. Although this might go beyond the scope of the current PR. It's probably ok to handle that separately.

Copy link
Contributor

@snjeza snjeza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testRenameTypeWithResourceChanges test still fails, if the workspace name contains "E".

Could you try the following:

cd eclipse.jdt.ls
cd ..
mv eclipse.jdt.ls Eclipse.jdt.ls
cd Eclipse.jdt.ls
mvn clean verify

You will get the following:

Failed tests: 
  RenameHandlerTest.testRenameTypeWithResourceChanges:243 expected:<.../home/snjeza/projects/[Newname]clipse.jdt.ls/org.ec...> but was:<.../home/snpe/projects/[E]clipse.jdt.ls/org.ec...>

@fbricon
Copy link
Contributor

fbricon commented Nov 26, 2018

ok so I was able to rename the package in package example; but not package ex.am.ple (Rename this element is not supported yet)

context.setASTRoot(ast);
ASTNode node = context.getCoveredNode();
// Rename package is not fully support yet.
if (!(node instanceof PackageDeclaration) && !(node instanceof QualifiedName && node.getParent() instanceof PackageDeclaration)) {
Copy link
Contributor

@fbricon fbricon Nov 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when renaming package example;, example is found to be a SimpleName instead of a QualifiedName, so it goes through this condition

JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName() + " in prepareRename", e);
}
}
throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidRequest, "Rename this element is not supported yet.", null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this element is not supported.

@snjeza
Copy link
Contributor

snjeza commented Nov 27, 2018

test this please

@yaohaizh
Copy link
Contributor Author

@snjeza @fbricon Updated the PR

JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName() + " in prepareRename", e);
}
}
throw new ResponseErrorException(new ResponseError(ResponseErrorCode.InvalidRequest, "Rename this element is not supported.", null));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Rename/Renaming

}

} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName() + " in prepareRename", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem computing occurrences for

InnovationContext context = new InnovationContext(unit, loc.getOffset(), loc.getLength());
context.setASTRoot(ast);
ASTNode node = context.getCoveredNode();
// Rename package is not fully support yet.
Copy link
Contributor

@fbricon fbricon Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming package is not fully supported yet.

@yaohaizh
Copy link
Contributor Author

test this please

@yaohaizh
Copy link
Contributor Author

@fbricon Updated the PR

@fbricon
Copy link
Contributor

fbricon commented Nov 27, 2018

@snjeza can you please check the last changes I pushed to this PR?

@fbricon
Copy link
Contributor

fbricon commented Nov 27, 2018

if you try to rename Exception in public class MyException extends Exception, the rename query returns an internal error.

[Error - 6:36:21 PM] Request textDocument/rename failed.
  Message: Internal error.
  Code: -32603 
java.util.concurrent.CompletionException: java.lang.NullPointerException
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:604)
	at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
	at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.NullPointerException
	at org.eclipse.jdt.internal.corext.refactoring.Checks.checkIfCuBroken(Checks.java:559)
	at org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameTypeProcessor.checkInitialConditions(RenameTypeProcessor.java:475)
	at org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring.checkInitialConditions(ProcessorBasedRefactoring.java:206)
	at org.eclipse.ltk.core.refactoring.Refactoring.checkAllConditions(Refactoring.java:161)
	at org.eclipse.ltk.core.refactoring.CheckConditionsOperation.run(CheckConditionsOperation.java:82)
	at org.eclipse.ltk.core.refactoring.CreateChangeOperation.run(CreateChangeOperation.java:122)
	at org.eclipse.jdt.ls.core.internal.handlers.RenameHandler.rename(RenameHandler.java:89)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$21(JDTLanguageServer.java:666)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$26(JDTLanguageServer.java:793)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
	... 6 more

@fbricon
Copy link
Contributor

fbricon commented Nov 28, 2018

@snjeza please check the new fix

@fbricon
Copy link
Contributor

fbricon commented Nov 28, 2018

new fix is incomplete. Renaming java.lang.Exception is still allowed.

@fbricon
Copy link
Contributor

fbricon commented Nov 28, 2018

new fix handles FQCN renames

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants