use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class OrganizeImportsHandler method organizeImports.
public static TextEdit organizeImports(ICompilationUnit unit, Function<ImportSelection[], ImportCandidate[]> chooseImports, IProgressMonitor monitor) {
if (unit == null) {
return null;
}
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return null;
}
OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, (TypeNameMatch[][] openChoices, ISourceRange[] ranges) -> {
List<ImportSelection> selections = new ArrayList<>();
for (int i = 0; i < openChoices.length; i++) {
ImportCandidate[] candidates = Stream.of(openChoices[i]).map((choice) -> new ImportCandidate(choice)).toArray(ImportCandidate[]::new);
Range range = null;
try {
range = JDTUtils.toRange(unit, ranges[i].getOffset(), ranges[i].getLength());
} catch (JavaModelException e) {
range = JDTUtils.newRange();
}
// TODO Sort the ambiguous candidates based on a relevance score.
selections.add(new ImportSelection(candidates, range));
}
ImportCandidate[] chosens = chooseImports.apply(selections.toArray(new ImportSelection[0]));
if (chosens == null) {
return null;
}
Map<String, TypeNameMatch> typeMaps = new HashMap<>();
Stream.of(openChoices).flatMap(x -> Arrays.stream(x)).forEach(x -> {
typeMaps.put(x.getFullyQualifiedName() + "@" + x.hashCode(), x);
});
return Stream.of(chosens).filter(chosen -> chosen != null && typeMaps.containsKey(chosen.id)).map(chosen -> typeMaps.get(chosen.id)).toArray(TypeNameMatch[]::new);
});
try {
JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
TextEdit edit = op.createTextEdit(null);
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=283287
// and https://github.com/redhat-developer/vscode-java/issues/1472
TextEdit staticEdit = wrapStaticImports(edit, astRoot, unit);
if (staticEdit.getChildrenSize() == 0) {
return null;
}
return staticEdit;
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Failed to resolve organize imports source action", e);
}
return null;
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getOrganizeImportsProposal.
private TextEdit getOrganizeImportsProposal(IInvocationContext context, IProgressMonitor monitor) {
ICompilationUnit unit = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
try {
TextEdit edit = op.createTextEdit(monitor);
TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit);
if (staticEdit.getChildrenSize() > 0) {
return staticEdit;
}
return edit;
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Resolve organize imports source action", e);
}
return null;
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class ImportOrganizeTest method test1WithOrder.
@Test
public void test1WithOrder() throws Exception {
requireJUnitSources();
ICompilationUnit cu = (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
assertNotNull("BaseTestRunner.java is missing", cu);
IPackageFragmentRoot root = (IPackageFragmentRoot) cu.getParent().getParent();
IPackageFragment pack = root.createPackageFragment("mytest", true, null);
ICompilationUnit colidingCU = pack.getCompilationUnit("TestListener.java");
colidingCU.createType("public abstract class TestListener {\n}\n", null, true, null);
String[] order = new String[] { "junit", "java.text", "java.io", "java" };
IChooseImportQuery query = createQuery("BaseTestRunner", new String[] { "junit.framework.TestListener" }, new int[] { 2 });
OrganizeImportsOperation op = createOperation(cu, order, false, true, true, query);
TextEdit edit = op.createTextEdit(new NullProgressMonitor());
IDocument document = new Document(cu.getSource());
edit.apply(document);
try {
cu.becomeWorkingCopy(new NullProgressMonitor());
cu.getBuffer().setContents(document.get());
cu.reconcile(ICompilationUnit.NO_AST, true, null, new NullProgressMonitor());
// @formatter:off
assertImports(cu, new String[] { "junit.framework.AssertionFailedError", "junit.framework.Test", "junit.framework.TestListener", "junit.framework.TestSuite", "java.text.NumberFormat", "java.io.BufferedReader", "java.io.File", "java.io.FileInputStream", "java.io.FileOutputStream", "java.io.IOException", "java.io.InputStream", "java.io.PrintWriter", "java.io.StringReader", "java.io.StringWriter", "java.lang.reflect.InvocationTargetException", "java.lang.reflect.Method", "java.lang.reflect.Modifier", "java.util.Properties" });
// @formatter:on
} finally {
cu.discardWorkingCopy();
}
}
Aggregations