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 a500e91dd4..78cbec65a0 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 @@ -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<>(); 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..ea9f33cfed --- /dev/null +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/OrganizeImportsCleanup.java @@ -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 getRequiredCompilerMarkers() { + return Collections.emptyList(); + } + +}