-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create cleanup actions for some existing quick assists.
- Add final modifier where possible - Convert switch statement to switch expression - Use pattern matching for instanceof checks - Convert anonymous function to lambda expression - Add test cases, and set CleanUpsTest compliance to 19 - Due to LSP limitations (no overlapping text edits), we now return the final state of the document after all cleanups are applied as a text edit Signed-off-by: Roland Grunberg <[email protected]>
- Loading branch information
Showing
7 changed files
with
496 additions
and
16 deletions.
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
57 changes: 57 additions & 0 deletions
57
...ipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/InstanceofPatternMatch.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,57 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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 | ||
* http://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.cleanup; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.manipulation.CleanUpContextCore; | ||
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; | ||
import org.eclipse.jdt.internal.corext.fix.PatternMatchingForInstanceofFixCore; | ||
|
||
/** | ||
* Represents a cleanup that uses pattern matching for 'instanceof' cast checks | ||
*/ | ||
public class InstanceofPatternMatch implements ISimpleCleanUp { | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getIdentifier() | ||
*/ | ||
@Override | ||
public String getIdentifier() { | ||
return "instanceofPatternMatch"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#createFix(org.eclipse.jdt.core.manipulation.CleanUpContextCore) | ||
*/ | ||
@Override | ||
public ICleanUpFixCore createFix(CleanUpContextCore context) throws CoreException { | ||
CompilationUnit unit = context.getAST(); | ||
if (unit == null) { | ||
return null; | ||
} | ||
return PatternMatchingForInstanceofFixCore.createCleanUp(unit); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getRequiredCompilerMarkers() | ||
*/ | ||
@Override | ||
public List<String> getRequiredCompilerMarkers() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...pse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/LambdaExpressionCleanup.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,54 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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 | ||
* http://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.cleanup; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.manipulation.CleanUpContextCore; | ||
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; | ||
import org.eclipse.jdt.internal.corext.fix.LambdaExpressionsFixCore; | ||
|
||
public class LambdaExpressionCleanup implements ISimpleCleanUp { | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getIdentifier() | ||
*/ | ||
@Override | ||
public String getIdentifier() { | ||
return "lambdaExpression"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#createFix(org.eclipse.jdt.core.manipulation.CleanUpContextCore) | ||
*/ | ||
@Override | ||
public ICleanUpFixCore createFix(CleanUpContextCore context) throws CoreException { | ||
CompilationUnit unit = context.getAST(); | ||
if (unit == null) { | ||
return null; | ||
} | ||
return LambdaExpressionsFixCore.createCleanUp(unit, true, false); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getRequiredCompilerMarkers() | ||
*/ | ||
@Override | ||
public List<String> getRequiredCompilerMarkers() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...pse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/SwitchExpressionCleanup.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,57 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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 | ||
* http://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.cleanup; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.manipulation.CleanUpContextCore; | ||
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; | ||
import org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore; | ||
|
||
/** | ||
* Represents a cleanup that converts a switch statement to a switch expression | ||
*/ | ||
public class SwitchExpressionCleanup implements ISimpleCleanUp { | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getIdentifier() | ||
*/ | ||
@Override | ||
public String getIdentifier() { | ||
return "switchExpression"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#createFix(org.eclipse.jdt.core.manipulation.CleanUpContextCore) | ||
*/ | ||
@Override | ||
public ICleanUpFixCore createFix(CleanUpContextCore context) throws CoreException { | ||
CompilationUnit unit = context.getAST(); | ||
if (unit == null) { | ||
return null; | ||
} | ||
return SwitchExpressionsFixCore.createCleanUp(unit); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getRequiredCompilerMarkers() | ||
*/ | ||
@Override | ||
public List<String> getRequiredCompilerMarkers() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...t.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/VariableDeclarationFixCleanup.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,57 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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 | ||
* http://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.cleanup; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.manipulation.CleanUpContextCore; | ||
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore; | ||
import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore; | ||
|
||
/** | ||
* Represents a cleanup that adds the 'final' modifier where possible | ||
*/ | ||
public class VariableDeclarationFixCleanup implements ISimpleCleanUp { | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getIdentifier() | ||
*/ | ||
@Override | ||
public String getIdentifier() { | ||
return "addFinalModifier"; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#createFix(org.eclipse.jdt.core.manipulation.CleanUpContextCore) | ||
*/ | ||
@Override | ||
public ICleanUpFixCore createFix(CleanUpContextCore context) throws CoreException { | ||
CompilationUnit unit = context.getAST(); | ||
if (unit == null) { | ||
return null; | ||
} | ||
return VariableDeclarationFixCore.createCleanUp(unit, true, true, true); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getRequiredCompilerMarkers() | ||
*/ | ||
@Override | ||
public List<String> getRequiredCompilerMarkers() { | ||
return Collections.emptyList(); | ||
} | ||
|
||
} |
Oops, something went wrong.