use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class OrganizeImportsCommand method organizeImportsInCompilationUnit.
public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
try {
InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", unit, IProposalRelevance.ORGANIZE_IMPORTS) {
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
CompilationUnit astRoot = context.getASTRoot();
OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
editRoot.addChild(op.createTextEdit(null));
}
};
addWorkspaceEdit(unit, proposal, rootEdit);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem organize imports ", e);
}
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class ImportOrganizeTest method testStaticImports1.
@Test
public void testStaticImports1() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package pack1;\n");
buf.append("\n");
buf.append("import static java.lang.System.out;\n");
buf.append("\n");
buf.append("public class C {\n");
buf.append(" public int foo() {\n");
buf.append(" out.print(File.separator);\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
String[] order = new String[] { "java", "pack", "#java" };
IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
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());
buf = new StringBuilder();
buf.append("package pack1;\n");
buf.append("\n");
buf.append("import java.io.File;\n");
buf.append("\n");
buf.append("import static java.lang.System.out;\n");
buf.append("\n");
buf.append("public class C {\n");
buf.append(" public int foo() {\n");
buf.append(" out.print(File.separator);\n");
buf.append(" }\n");
buf.append("}\n");
assertTrue(cu.getSource().equals(buf.toString()));
} finally {
cu.discardWorkingCopy();
}
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class ImportOrganizeTest method testStaticImports2.
@Test
public void testStaticImports2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package pack1;\n");
buf.append("\n");
buf.append("import static java.io.File.*;\n");
buf.append("\n");
buf.append("public class C {\n");
buf.append(" public String foo() {\n");
buf.append(" return pathSeparator + separator + File.separator;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("C.java", buf.toString(), false, null);
String[] order = new String[] { "#java.io.File", "java", "pack" };
IChooseImportQuery query = createQuery("C", new String[] {}, new int[] {});
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());
buf = new StringBuilder();
buf.append("package pack1;\n");
buf.append("\n");
buf.append("import static java.io.File.pathSeparator;\n");
buf.append("import static java.io.File.separator;\n");
buf.append("\n");
buf.append("import java.io.File;\n");
buf.append("\n");
buf.append("public class C {\n");
buf.append(" public String foo() {\n");
buf.append(" return pathSeparator + separator + File.separator;\n");
buf.append(" }\n");
buf.append("}\n");
assertTrue(cu.getSource().equals(buf.toString()));
} finally {
cu.discardWorkingCopy();
}
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class ImportOrganizeTest method test1.
@Test
public void test1() throws Exception {
requireJUnitSources();
ICompilationUnit cu = (ICompilationUnit) javaProject.findElement(new Path("junit/runner/BaseTestRunner.java"));
assertNotNull("BaseTestRunner.java", 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[0];
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[] { "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.text.NumberFormat", "java.util.Properties", "junit.framework.AssertionFailedError", "junit.framework.Test", "junit.framework.TestListener", "junit.framework.TestSuite" });
// @formatter:on
} finally {
cu.discardWorkingCopy();
}
}
use of org.eclipse.jdt.core.manipulation.OrganizeImportsOperation in project eclipse.jdt.ls by eclipse.
the class ReorgCorrectionsSubProcessor method removeImportStatementProposals.
public static void removeImportStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
IProposableFix fix = UnusedCodeFix.createRemoveUnusedImportFix(context.getASTRoot(), problem);
if (fix != null) {
try {
CompilationUnitChange change = fix.createChange(null);
CUCorrectionProposal proposal = new CUCorrectionProposal(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.REMOVE_UNUSED_IMPORT);
proposals.add(proposal);
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
}
final ICompilationUnit cu = context.getCompilationUnit();
String name = CorrectionMessages.ReorgCorrectionsSubProcessor_organizeimports_description;
CUCorrectionProposal proposal = new CUCorrectionProposal(name, cu, IProposalRelevance.ORGANIZE_IMPORTS) {
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
CompilationUnit astRoot = context.getASTRoot();
OrganizeImportsOperation op = new OrganizeImportsOperation(cu, astRoot, true, false, true, null);
editRoot.addChild(op.createTextEdit(null));
}
};
proposals.add(proposal);
}
Aggregations