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 Sep 13, 2024
1 parent f592993 commit cbb11e0
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public CleanUpRegistry() {
new InstanceofPatternMatch(),
new LambdaExpressionCleanup(),
new TryWithResourceCleanUp(),
new LambdaExpressionAndMethodRefCleanUp());
new LambdaExpressionAndMethodRefCleanUp(),
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,71 @@
/*******************************************************************************
* Copyright (c) 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
* 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.Collection;
import java.util.Collections;
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.dom.CompilationUnit;
import org.eclipse.jdt.core.manipulation.OrganizeImportsOperation;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
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 Collection<String> getIdentifiers() {
return List.of("organizeImports");
}

/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.cleanup.ISimpleCleanUp#createFix(org.eclipse.jdt.core.manipulation.CleanUpContextCore)
*/
@Override
public ICleanUpFix createFix(CleanUpContext 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(new NullProgressMonitor());
return new ICleanUpFix() {
@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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,65 @@ public void test() {
assertEquals(expected, actual);
}

@Test
public void testOrganizeImportsCleanup() throws Exception {
String contents = """
package test1;
public class A {
public void test() {
List<String> a1;
Iterator<String> a2;
Map<String, String> a3;
Set<String> a4;
JarFile a5;
StringTokenizer a6;
Path a7;
URI a8;
HttpURLConnection a9;
InputStream a10;
Field a11;
Parser a12;
}
}
""";

ICompilationUnit unit = pack1.createCompilationUnit("A.java", contents, false, monitor);
String uri = unit.getUnderlyingResource().getLocationURI().toString();

String expected = """
package test1;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarFile;
public class A {
public void test() {
List<String> a1;
Iterator<String> a2;
Map<String, String> a3;
Set<String> a4;
JarFile a5;
StringTokenizer a6;
Path a7;
URI a8;
HttpURLConnection a9;
InputStream a10;
Field a11;
Parser a12;
}
}
""";
List<TextEdit> textEdits = registry.getEditsForAllActiveCleanUps(new TextDocumentIdentifier(uri), Arrays.asList("organizeImports"), monitor);
String actual = TextEditUtil.apply(unit, textEdits);
assertEquals(expected, actual);
}

}

0 comments on commit cbb11e0

Please sign in to comment.