-
Notifications
You must be signed in to change notification settings - Fork 401
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
Show "change modifiers to final" in quick assists #2134
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
164 changes: 164 additions & 0 deletions
164
.../src/org/eclipse/jdt/ls/core/internal/handlers/GenerateFinalModifiersQuickAssistTest.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,164 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Microsoft Corporation 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: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.handlers; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.IPackageFragment; | ||
import org.eclipse.jdt.core.IPackageFragmentRoot; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.ls.core.internal.CodeActionUtil; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.CodeActionParams; | ||
import org.eclipse.lsp4j.Command; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class GenerateFinalModifiersQuickAssistTest extends AbstractCompilationUnitBasedTest { | ||
|
||
private IJavaProject fJavaProject; | ||
private IPackageFragmentRoot fRoot; | ||
private IPackageFragment fPackageP; | ||
|
||
@Override | ||
@Before | ||
public void setup() throws Exception { | ||
fJavaProject = newEmptyProject(); | ||
fRoot = fJavaProject.findPackageFragmentRoot(fJavaProject.getPath().append("src")); | ||
assertNotNull(fRoot); | ||
fPackageP = fRoot.createPackageFragment("p", true, null); | ||
server = new JDTLanguageServer(projectsManager, this.preferenceManager); | ||
} | ||
|
||
@Test | ||
public void testInsertFinalModifierForFieldDeclarationQuickAssist() throws JavaModelException { | ||
//@formatter:off | ||
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + | ||
"\r\n" + | ||
"public class A {\r\n" + | ||
" private String name = \"a\";\r\n" + | ||
" private String test;\r\n" + | ||
"}" | ||
, true, null); | ||
//@formatter:on | ||
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name"); | ||
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'name'")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "a\";"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'name'")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "test;"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'test'")); | ||
} | ||
|
||
@Test | ||
public void testInsertFinalModifierForFieldDeclarationsQuickAssist() throws JavaModelException { | ||
//@formatter:off | ||
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + | ||
"\r\n" + | ||
"public class A {\r\n" + | ||
" private String name;\r\n" + | ||
" private String name1 = \"b\";\r\n" + | ||
"}" | ||
, true, null); | ||
//@formatter:on | ||
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "private String name;\r\n private String name1 = \"b\";"); | ||
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); | ||
} | ||
|
||
@Test | ||
public void testInsertFinalModifierForParameterQuickAssist() throws JavaModelException { | ||
//@formatter:off | ||
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + | ||
"\r\n" + | ||
"public class A {\r\n" + | ||
" public String getName(String a, String b) {}\r\n" + | ||
"}" | ||
, true, null); | ||
//@formatter:on | ||
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "b"); | ||
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'b'")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "String a, String b"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); | ||
} | ||
|
||
@Test | ||
public void testInsertFinalModifierForLocalVariableSelectionQuickAssist() throws JavaModelException { | ||
//@formatter:off | ||
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + | ||
"\r\n" + | ||
"public class A {\r\n" + | ||
" public String getName(String a, String b) {\r\n" + | ||
" String c = a;\r\n" + | ||
" String d = b;\r\n" + | ||
" }\r\n" + | ||
"}" | ||
, true, null); | ||
//@formatter:on | ||
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "c"); | ||
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'c'")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "String c = a;\r\n String d = b;"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "c = a;\r\n String d = b;"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); | ||
} | ||
|
||
@Test | ||
public void testInsertFinalModifierForLocalVariableQuickAssist() throws JavaModelException { | ||
//@formatter:off | ||
ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + | ||
"\r\n" + | ||
"public class A {\r\n" + | ||
" public String getName() {\r\n" + | ||
" String c;\r\n" + | ||
" String d, e;\r\n" + | ||
" }\r\n" + | ||
"}" | ||
, true, null); | ||
//@formatter:on | ||
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "c;"); | ||
List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'c'")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "String d, e;"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Change modifiers to final")); | ||
params = CodeActionUtil.constructCodeActionParams(unit, "d"); | ||
codeActions = server.codeAction(params).join(); | ||
Assert.assertNotNull(codeActions); | ||
Assert.assertTrue(CodeActionHandlerTest.commandExists(codeActions, CodeActionHandler.COMMAND_ID_APPLY_EDIT, "Add final modifier for 'd'")); | ||
} | ||
} |
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.
Use
converingNode
from above instead ofcontext.getCoveringNode()