Search in sources :

Example 71 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-xtend by eclipse.

the class DerivedSourceView method selectAndReveal.

@Override
protected void selectAndReveal(IWorkbenchPartSelection workbenchPartSelection) {
    if (selectedSource != null) {
        IAnnotationModel annotationModel = getSourceViewer().getAnnotationModel();
        TextRegion localRegion = mapTextRegion(workbenchPartSelection);
        IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
        if (trace != null) {
            Iterable<? extends ILocationInEclipseResource> allAssociatedLocations = trace.getAllAssociatedLocations(localRegion, selectedSource);
            ILocationInResource firstLocationInResource = Iterables.getFirst(allAssociatedLocations, null);
            if (firstLocationInResource != null) {
                ITextRegion textRegion = firstLocationInResource.getTextRegion();
                if (textRegion != null) {
                    openEditorAction.setSelectedRegion(textRegion);
                    getSourceViewer().revealRange(textRegion.getOffset(), textRegion.getLength());
                }
            }
            for (ILocationInResource locationInResource : allAssociatedLocations) {
                ITextRegion textRegion = locationInResource.getTextRegion();
                if (textRegion != null) {
                    annotationModel.addAnnotation(new Annotation(SEARCH_ANNOTATION_TYPE, true, null), new Position(textRegion.getOffset(), textRegion.getLength()));
                }
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Position(org.eclipse.jface.text.Position) ITextRegion(org.eclipse.xtext.util.ITextRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) Annotation(org.eclipse.jface.text.source.Annotation)

Example 72 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse-pmd by acanda.

the class JavaQuickFix method fixMarkersInFile.

/**
 * Fixes all provided markers in a file.
 *
 * @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
 *            by this quick fix.
 */
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
    monitor.subTask(file.getFullPath().toOSString());
    final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
    if (!optionalCompilationUnit.isPresent()) {
        return;
    }
    final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
    ITextFileBufferManager bufferManager = null;
    final IPath path = compilationUnit.getPath();
    try {
        bufferManager = FileBuffers.getTextFileBufferManager();
        bufferManager.connect(path, LocationKind.IFILE, null);
        final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
        final IDocument document = textFileBuffer.getDocument();
        final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
        final ASTParser astParser = ASTParser.newParser(AST.JLS4);
        astParser.setKind(ASTParser.K_COMPILATION_UNIT);
        astParser.setResolveBindings(needsTypeResolution());
        astParser.setSource(compilationUnit);
        final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
        final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
        parserMonitor.done();
        startFixingMarkers(ast);
        final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
        for (final IMarker marker : markers) {
            try {
                final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
                // if the annotation is null it means that is was deleted by a previous quick fix
                if (annotation != null) {
                    final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
                    if (node.isPresent()) {
                        final boolean isSuccessful = fixMarker(node.get(), document, options);
                        if (isSuccessful) {
                            marker.delete();
                        }
                    }
                }
            } finally {
                monitor.worked(100);
            }
        }
        finishFixingMarkers(ast, document, options);
        // commit changes to underlying file if it is not opened in an editor
        if (!isEditorOpen(file)) {
            final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
            textFileBuffer.commit(commitMonitor, false);
            commitMonitor.done();
        } else {
            monitor.worked(100);
        }
    } catch (CoreException | MalformedTreeException | BadLocationException e) {
    // TODO: log error
    // PMDPlugin.getDefault().error("Error processing quickfix", e);
    } finally {
        if (bufferManager != null) {
            try {
                bufferManager.disconnect(path, LocationKind.IFILE, null);
            } catch (final CoreException e) {
            // TODO: log error
            // PMDPlugin.getDefault().error("Error processing quickfix", e);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) AST(org.eclipse.jdt.core.dom.AST) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IMarker(org.eclipse.core.resources.IMarker) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 73 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.

the class EditorTracker method markLine.

private void markLine(final ITextEditor editor, final LineResult lr) {
    final IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
    log.info("mark line " + lr.getLineNum());
    try {
        final IRegion reg = doc.getLineInformation(lr.getLineNum() - 1);
        final int length = reg.getLength();
        final int offset = reg.getOffset();
        final Position pos = new Position(offset, length);
        Annotation annotation;
        if (lr.called()) {
            annotation = CoverageAnnotationFactory.create(CoverageTypes.FULL_COVERAGE);
        } else {
            annotation = CoverageAnnotationFactory.create(CoverageTypes.NO_COVERAGE);
        }
        final Annotation lastAnn = coverage.getAnnotation(editor.getTitle(), lr);
        log.info(lastAnn);
        if (lastAnn == null) {
            annMod.addAnnotation(annotation, pos);
            coverage.addAnnotation(editor.getTitle(), lr, annotation);
        } else if (annMod.getPosition(lastAnn) == null) {
            annMod.addAnnotation(lastAnn, pos);
        } else if (lastAnn.getType().equals(CoverageTypes.NO_COVERAGE) && annotation.getType().equals(CoverageTypes.FULL_COVERAGE)) {
            annMod.removeAnnotation(lastAnn);
            annMod.addAnnotation(annotation, pos);
            coverage.addAnnotation(editor.getTitle(), lr, annotation);
        }
    } catch (final BadLocationException e) {
        log.error(e);
        ErlLogger.error(e);
    }
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 74 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.

the class EditorTracker method clearAnnotations.

public void clearAnnotations(final IWorkbenchPart part) {
    if (part != null) {
        log.info(part.getTitle());
    }
    if (part instanceof ITextEditor) {
        final ITextEditor editor = (ITextEditor) part;
        final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
        @SuppressWarnings("rawtypes") final Iterator it = annMod.getAnnotationIterator();
        while (it.hasNext()) {
            final Annotation annotation = (Annotation) it.next();
            if (annotation.getType().equals(CoverageTypes.FULL_COVERAGE) || annotation.getType().equals(CoverageTypes.NO_COVERAGE)) {
                annMod.removeAnnotation(annotation);
            }
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Iterator(java.util.Iterator) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 75 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.

the class EditorTracker method removeAnnotationsFragment.

/**
 * Removes coverage annotations from a fragment of file
 *
 * @param fileName
 * @param start
 * @param end
 */
public void removeAnnotationsFragment(final String fileName, final int start, final int end) {
    final IEditorPart currentEditor = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (currentEditor.getTitle().equals(fileName) && currentEditor instanceof ITextEditor) {
        final ITextEditor editor = (ITextEditor) currentEditor;
        final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
        final Set<LineResult> list = coverage.getLineSet(editor.getTitle());
        for (final LineResult lr : list) {
            if (lr.getLineNum() < start || end != -1 && lr.getLineNum() > end) {
                continue;
            }
            log.info(lr.getLineNum());
            if (coverage.containsAnnotation(editor.getTitle(), lr)) {
                final Annotation ann = coverage.getAnnotation(editor.getTitle(), lr);
                annMod.removeAnnotation(ann);
                coverage.removeAnnotation(editor.getTitle(), lr);
            }
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) LineResult(org.erlide.cover.views.model.LineResult) IEditorPart(org.eclipse.ui.IEditorPart) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10