Search in sources :

Example 1 with OrganizeImportResult

use of org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult in project che by eclipse.

the class CodeAssist method createOrganizeImportOperation.

private OrganizeImportResult createOrganizeImportOperation(ICompilationUnit compilationUnit, List<String> chosen) throws CoreException {
    CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(compilationUnit.getJavaProject());
    OrganizeImportsOperation operation = new OrganizeImportsOperation(compilationUnit, null, settings.importIgnoreLowercase, !compilationUnit.isWorkingCopy(), true, chosen, null);
    NullProgressMonitor monitor = new NullProgressMonitor();
    TextEdit edit = operation.createTextEdit(monitor);
    OrganizeImportResult result = DtoFactory.newDto(OrganizeImportResult.class);
    TypeNameMatch[][] choices = operation.getChoices();
    //or all conflicts were resolved (!chosen.isEmpty())
    if ((chosen != null && !chosen.isEmpty()) || choices == null || choices.length == 0) {
        IBuffer buffer = compilationUnit.getBuffer();
        IDocument document = new Document(buffer.getContents());
        DocumentChangeListener documentChangeListener = new DocumentChangeListener(document);
        try {
            edit.apply(document);
        } catch (BadLocationException e) {
            LOG.debug("Applying Organize import text edits goes wrong:", e);
        }
        result.setChanges(documentChangeListener.getChanges());
        return result;
    }
    result.setConflicts(createListOfDTOMatches(choices));
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) OrganizeImportsOperation(org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) OrganizeImportResult(org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult) TextEdit(org.eclipse.text.edits.TextEdit) DocumentChangeListener(org.eclipse.jdt.internal.corext.format.DocumentChangeListener) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IBuffer(org.eclipse.jdt.core.IBuffer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with OrganizeImportResult

use of org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult in project che by eclipse.

the class CodeAssist method applyChosenImports.

/**
     * Applies chosen imports after resolving conflicts.
     *
     * @param project
     *        current java project
     * @param fqn
     *         fully qualified name of the java file
     * @param  chosen
     *          list of chosen imports as result of resolving conflicts which needed to add to all imports.
     */
public List<Change> applyChosenImports(IJavaProject project, String fqn, List<String> chosen) throws CoreException, BadLocationException {
    ICompilationUnit compilationUnit = prepareCompilationUnit(project, fqn);
    OrganizeImportResult result = createOrganizeImportOperation(compilationUnit, chosen);
    return result.getChanges();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) OrganizeImportResult(org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult)

Aggregations

OrganizeImportResult (org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)1 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)1 DocumentChangeListener (org.eclipse.jdt.internal.corext.format.DocumentChangeListener)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 IDocument (org.eclipse.jface.text.IDocument)1 TextEdit (org.eclipse.text.edits.TextEdit)1