Skip to content

Commit

Permalink
Also read JDT UI project prefs to configure cleanups
Browse files Browse the repository at this point in the history
Also logs unknown cleanups
  • Loading branch information
mickaelistria authored and rgrunber committed Sep 28, 2023
1 parent 26e9549 commit d022ce3
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

public class JavaLanguageServerPlugin extends Plugin {

private static final String JDT_UI_PLUGIN = "org.eclipse.jdt.ui";
public static final String JDT_UI_PLUGIN = "org.eclipse.jdt.ui";
public static final String MANUAL = "Manual";
public static final String DIRECT = "Direct";
public static final String NATIVE = "Native";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.manipulation.CleanUpContextCore;
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.Java50FixCore;

/**
Expand All @@ -29,8 +31,8 @@ public class AddDeprecatedAnnotationCleanUp implements ISimpleCleanUp {
private static final List<String> COMPILER_OPTS = Arrays.asList(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION);

@Override
public String getIdentifier() {
return "addDeprecated";
public Collection<String> getIdentifiers() {
return List.of("addDeprecated", CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.manipulation.CleanUpContextCore;
import org.eclipse.jdt.core.manipulation.ICleanUpFixCore;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.Java50FixCore;

/**
Expand All @@ -29,8 +31,8 @@ public class AddOverrideAnnotationCleanUp implements ISimpleCleanUp {
private static final List<String> COMPILER_OPTS = Arrays.asList(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION);

@Override
public String getIdentifier() {
return "addOverride";
public Collection<String> getIdentifiers() {
return List.of("addOverride", CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.manipulation.CleanUpContextCore;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextEdit;

Expand Down Expand Up @@ -55,7 +58,10 @@ public CleanUpRegistry() {

// Store in a Map so that they can be accessed by ID quickly
cleanUps = new HashMap<>();
cleanUpsList.forEach(cleanUp -> cleanUps.put(cleanUp.getIdentifier(), cleanUp));
cleanUpsList.forEach(cleanUp ->
cleanUp.getIdentifiers().forEach(id ->
cleanUps.put(id, cleanUp)
));
}

/**
Expand All @@ -72,13 +78,21 @@ public CleanUpRegistry() {
* according to the clean ups that are enabled
*/
public List<TextEdit> getEditsForAllActiveCleanUps(TextDocumentIdentifier textDocumentId, List<String> cleanUpEnabled, IProgressMonitor monitor) {

IJavaProject javaProject = JDTUtils.resolveCompilationUnit(textDocumentId.getUri()).getJavaProject();

List<ISimpleCleanUp> cleanUpsToRun = cleanUpEnabled.stream() //
.distinct() //
.map(cleanUpId -> cleanUps.get(cleanUpId)).filter(Objects::nonNull) //
.toList();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(textDocumentId.getUri());
if (unit == null) {
return List.of();
}
IJavaProject javaProject = unit.getJavaProject();

Collection<ISimpleCleanUp> cleanUpsToRun = new LinkedHashSet<>(cleanUpEnabled.size());
for (String cleanUpId : cleanUpEnabled) {
ISimpleCleanUp cleanUp = cleanUps.get(cleanUpId);
if (cleanUp != null) {
cleanUpsToRun.add(cleanUp);
} else {
JavaLanguageServerPlugin.log(Status.warning("Not found cleanup id: " + cleanUpId));
}
}
if (cleanUpsToRun.isEmpty()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static TextEdit getTextEditFromCleanUp(ISimpleCleanUp cleanUp, CleanUpCon
TextEdit jdtEdit = cleanUpChange.getEdit();
return jdtEdit;
} catch (CoreException e) {
JavaLanguageServerPlugin.logError(String.format("Failed to create text edit for clean up %s", cleanUp.getIdentifier()));
JavaLanguageServerPlugin.logError(String.format("Failed to create text edit for clean up %s", cleanUp.getIdentifiers().iterator().next()));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -25,11 +26,11 @@
public interface ISimpleCleanUp {

/**
* Returns the unique identifier for this clean up.
* Returns the possible identifiers for this clean up.
*
* @return the unique identifier for this clean up
* @return the identifiers for this clean up
*/
String getIdentifier();
Collection<String> getIdentifiers();

/**
* Returns the cleanup fix for the given source file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.PatternMatchingForInstanceofFixCore;

/**
Expand All @@ -27,8 +28,8 @@
public class InstanceofPatternMatch implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "instanceofPatternMatch";
public List<String> getIdentifiers() {
return List.of("instanceofPatternMatch", CleanUpConstants.USE_PATTERN_MATCHING_FOR_INSTANCEOF);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.InvertEqualsFixCore;

/**
Expand All @@ -28,8 +29,8 @@
public class InvertEqualsCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "invertEquals";
public List<String> getIdentifiers() {
return List.of("invertEquals", CleanUpConstants.INVERT_EQUALS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.LambdaExpressionAndMethodRefFixCore;

/**
Expand All @@ -27,8 +29,8 @@
public class LambdaExpressionAndMethodRefCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "lambdaExpression";
public Collection<String> getIdentifiers() {
return List.of("lambdaExpression", CleanUpConstants.SIMPLIFY_LAMBDA_EXPRESSION_AND_METHOD_REF);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.LambdaExpressionsFixCore;

/**
Expand All @@ -27,8 +29,8 @@
public class LambdaExpressionCleanup implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "lambdaExpressionFromAnonymousClass";
public Collection<String> getIdentifiers() {
return List.of("lambdaExpressionFromAnonymousClass", CleanUpConstants.USE_LAMBDA);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand All @@ -28,8 +29,8 @@
public class MemberAccessUsesThisCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "qualifyMembers";
public Collection<String> getIdentifiers() {
return List.of("qualifyMembers");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand All @@ -28,8 +29,8 @@
public class StaticAccessUsesClassNameCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "qualifyStaticMembers";
public Collection<String> getIdentifiers() {
return List.of("qualifyStaticMembers");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.StringConcatToTextBlockFixCore;

/**
Expand All @@ -27,8 +29,8 @@
public class StringConcatToTextBlockCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "stringConcatToTextBlock";
public Collection<String> getIdentifiers() {
return List.of("stringConcatToTextBlock", CleanUpConstants.STRINGCONCAT_TO_TEXTBLOCK);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore;

/**
Expand All @@ -27,8 +29,8 @@
public class SwitchExpressionCleanup implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "switchExpression";
public Collection<String> getIdentifiers() {
return List.of("switchExpression", CleanUpConstants.CONTROL_STATEMENTS_CONVERT_TO_SWITCH_EXPRESSIONS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.TryWithResourceFixCore;

/**
Expand All @@ -28,8 +30,8 @@
public class TryWithResourceCleanUp implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "tryWithResource";
public Collection<String> getIdentifiers() {
return List.of("tryWithResource", CleanUpConstants.TRY_WITH_RESOURCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Collection;
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.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFixCore;

/**
Expand All @@ -27,8 +29,8 @@
public class VariableDeclarationFixCleanup implements ISimpleCleanUp {

@Override
public String getIdentifier() {
return "addFinalModifier";
public Collection<String> getIdentifiers() {
return List.of("addFinalModifier", CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL);
}

@Override
Expand Down
Loading

0 comments on commit d022ce3

Please sign in to comment.