Search in sources :

Example 1 with DocumentChangeListener

use of org.eclipse.jdt.internal.corext.format.DocumentChangeListener 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)

Aggregations

OrganizeImportResult (org.eclipse.che.ide.ext.java.shared.dto.OrganizeImportResult)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IBuffer (org.eclipse.jdt.core.IBuffer)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