Search in sources :

Example 1 with OrganizeImportsOperation

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);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with OrganizeImportsOperation

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();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 3 with OrganizeImportsOperation

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();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 4 with OrganizeImportsOperation

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();
    }
}
Also used : Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) TextEdit(org.eclipse.text.edits.TextEdit) IChooseImportQuery(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractMavenBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest) Test(org.junit.Test)

Example 5 with OrganizeImportsOperation

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) IProposableFix(org.eclipse.jdt.ls.core.internal.corext.fix.IProposableFix) IDocument(org.eclipse.jface.text.IDocument) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 OrganizeImportsOperation (org.eclipse.jdt.core.manipulation.OrganizeImportsOperation)6 IDocument (org.eclipse.jface.text.IDocument)6 TextEdit (org.eclipse.text.edits.TextEdit)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 IChooseImportQuery (org.eclipse.jdt.core.manipulation.OrganizeImportsOperation.IChooseImportQuery)4 AbstractMavenBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractMavenBasedTest)4 Document (org.eclipse.jface.text.Document)4 Test (org.junit.Test)4 CoreException (org.eclipse.core.runtime.CoreException)2 Path (org.eclipse.core.runtime.Path)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)1 IProposableFix (org.eclipse.jdt.ls.core.internal.corext.fix.IProposableFix)1 InnovationContext (org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)1 CUCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal)1