-
Notifications
You must be signed in to change notification settings - Fork 408
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
Improve code actions #3322
Merged
Merged
Improve code actions #3322
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/ChangeSignatureInfoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016-2024 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.handlers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.core.runtime.NullProgressMonitor; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.IMethod; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.IMethodBinding; | ||
import org.eclipse.jdt.core.dom.MethodDeclaration; | ||
import org.eclipse.jdt.core.manipulation.CoreASTProvider; | ||
import org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo; | ||
import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo; | ||
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore; | ||
import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor; | ||
import org.eclipse.jdt.internal.corext.util.JdtFlags; | ||
import org.eclipse.jdt.ls.core.internal.JDTUtils; | ||
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; | ||
import org.eclipse.jdt.ls.core.internal.handlers.ChangeSignatureHandler.MethodException; | ||
import org.eclipse.jdt.ls.core.internal.handlers.ChangeSignatureHandler.MethodParameter; | ||
import org.eclipse.jdt.ls.core.internal.text.correction.ChangeSignatureInfo; | ||
import org.eclipse.jdt.ls.core.internal.text.correction.CodeActionUtility; | ||
import org.eclipse.jdt.ui.text.java.IInvocationContext; | ||
import org.eclipse.lsp4j.CodeActionParams; | ||
import org.eclipse.ltk.core.refactoring.RefactoringStatus; | ||
|
||
public class ChangeSignatureInfoHandler { | ||
|
||
private static final String CANNOT_CHANGE_SIGNATURE = "Cannot change signature."; | ||
|
||
public static ChangeSignatureInfo getChangeSignatureInfo(CodeActionParams params, IProgressMonitor monitor) { | ||
if (monitor.isCanceled()) { | ||
return null; | ||
} | ||
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); | ||
if (unit == null || monitor.isCanceled()) { | ||
return null; | ||
} | ||
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); | ||
if (astRoot == null) { | ||
return null; | ||
} | ||
IInvocationContext context = CodeActionHandler.getContext(unit, astRoot, params.getRange()); | ||
ASTNode methodNode = CodeActionUtility.inferASTNode(context.getCoveringNode(), MethodDeclaration.class); | ||
if (methodNode == null) { | ||
return null; | ||
} | ||
IMethodBinding methodBinding = ((MethodDeclaration) methodNode).resolveBinding(); | ||
if (methodBinding == null) { | ||
return null; | ||
} | ||
IJavaElement element = methodBinding.getJavaElement(); | ||
if (element instanceof IMethod method) { | ||
try { | ||
ChangeSignatureProcessor processor = new ChangeSignatureProcessor(method); | ||
if (RefactoringAvailabilityTesterCore.isChangeSignatureAvailable(method)) { | ||
RefactoringStatus status = processor.checkInitialConditions(new NullProgressMonitor()); | ||
if (status.isOK()) { | ||
List<MethodParameter> parameters = new ArrayList<>(); | ||
for (ParameterInfo info : processor.getParameterInfos()) { | ||
parameters.add(new MethodParameter(info.getOldTypeName(), info.getOldName(), info.getDefaultValue() == null ? "null" : info.getDefaultValue(), info.getOldIndex())); | ||
} | ||
List<MethodException> exceptions = new ArrayList<>(); | ||
for (ExceptionInfo info : processor.getExceptionInfos()) { | ||
exceptions.add(new MethodException(info.getFullyQualifiedName(), info.getElement().getHandleIdentifier())); | ||
} | ||
return new ChangeSignatureInfo(method.getHandleIdentifier(), JdtFlags.getVisibilityString(processor.getVisibility()), processor.getReturnTypeString(), method.getElementName(), | ||
parameters.toArray(MethodParameter[]::new), exceptions.toArray(MethodException[]::new)); | ||
} else { | ||
return new ChangeSignatureInfo(CANNOT_CHANGE_SIGNATURE + status.getMessageMatchingSeverity(status.getSeverity())); | ||
} | ||
} | ||
} catch (CoreException e) { | ||
JavaLanguageServerPlugin.logException(e); | ||
} | ||
} | ||
return new ChangeSignatureInfo(CANNOT_CHANGE_SIGNATURE); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, so with this, we correctly defer these calculations to resolve right ? I noticed
ChangeCorrectionProposalCore.getChange()
only gets called fromcodeAction/resolve
. That would definitely improve things both forjavac
andecj
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to defer Inline Constant, too?
I should change
InlineVariableTest.testInlineLocalVariableWithNoReference
-eclipse.jdt.ls/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/refactoring/InlineVariableTest.java
Line 76 in f155e1f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the issue is that
checkInitialConditions
is what determines whether there are any references to inline, but by defering it tocodeAction/resolve
, you end up showing the dialog in cases where it would have been hidden. It seems like a small price to pay for improving performance though, and it's not horribly wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.
We can solve it in a separate issue. The dialog requires a client update.