diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java index aad72c7679..e605de886c 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java @@ -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<>(); diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/OrganizeImportsCleanup.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/OrganizeImportsCleanup.java new file mode 100644 index 0000000000..3981d1ee61 --- /dev/null +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/OrganizeImportsCleanup.java @@ -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 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 getRequiredCompilerMarkers() { + return Collections.emptyList(); + } + +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpsTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpsTest.java index 42c786eb64..696d185d47 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpsTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpsTest.java @@ -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 a1; + Iterator a2; + Map a3; + Set 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 a1; + Iterator a2; + Map a3; + Set a4; + JarFile a5; + StringTokenizer a6; + Path a7; + URI a8; + HttpURLConnection a9; + InputStream a10; + Field a11; + Parser a12; + } + } + """; + List textEdits = registry.getEditsForAllActiveCleanUps(new TextDocumentIdentifier(uri), Arrays.asList("organizeImports"), monitor); + String actual = TextEditUtil.apply(unit, textEdits); + assertEquals(expected, actual); + } + }