Skip to content

Commit

Permalink
Add Organize Imports as a clean up.
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Mar 24, 2023
1 parent 1181a0e commit 2497722
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public CleanUpRegistry() {
cleanUpsList.add(new InstanceofPatternMatch());
cleanUpsList.add(new LambdaExpressionCleanup());
cleanUpsList.add(new TryWithResourceCleanUp());
cleanUpsList.add(new OrganizeImportsCleanup());

// Store in a Map so that they can be accessed by ID quickly
cleanUps = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* Copyright (c) 2023 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.core.runtime.IProgressMonitor;
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.core.manipulation.OrganizeImportsOperation;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.text.edits.TextEdit;

public class OrganizeImportsCleanup implements ISimpleCleanUp {

/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getIdentifier()
*/
@Override
public String getIdentifier() {
return "organizeImports";
}

/* (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;
}
OrganizeImportsOperation op = new OrganizeImportsOperation(context.getCompilationUnit(), unit,
false, false, true,
null,
false);
TextEdit te = op.createTextEdit(null);
return new ICleanUpFixCore() {
@Override
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
CompilationUnitChange change = new CompilationUnitChange("", context.getCompilationUnit());
change.setEdit(te);
return change;
}
};
}

/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#getRequiredCompilerMarkers()
*/
@Override
public List<String> getRequiredCompilerMarkers() {
return Collections.emptyList();
}

}

0 comments on commit 2497722

Please sign in to comment.