Search in sources :

Example 1 with AbstractInlinedAnnotation

use of org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation in project eclipse.platform.text by eclipse.

the class CodeMiningManager method renderCodeMinings.

/**
 * Render the codemining grouped by line position.
 *
 * @param groups code minings grouped by lines position
 * @param viewer the viewer
 * @param monitor the progress monitor
 */
private void renderCodeMinings(Map<Position, List<ICodeMining>> groups, ISourceViewer viewer, IProgressMonitor monitor) {
    // check if request was canceled.
    monitor.isCanceled();
    IDocument document = viewer != null ? viewer.getDocument() : null;
    if (document == null) {
        // done.
        return;
    }
    Set<ICodeMiningAnnotation> annotationsToRedraw = new HashSet<>();
    Set<AbstractInlinedAnnotation> currentAnnotations = new HashSet<>();
    // Loop for grouped code minings
    groups.entrySet().stream().forEach(g -> {
        // check if request was canceled.
        monitor.isCanceled();
        Position pos = new Position(g.getKey().offset, g.getKey().length);
        List<ICodeMining> minings = g.getValue();
        boolean inLineHeader = minings.size() > 0 ? (minings.get(0) instanceof LineHeaderCodeMining) : true;
        // Try to find existing annotation
        AbstractInlinedAnnotation ann = fInlinedAnnotationSupport.findExistingAnnotation(pos);
        if (ann == null) {
            // The annotation doesn't exists, create it.
            ann = inLineHeader ? new CodeMiningLineHeaderAnnotation(pos, viewer) : new CodeMiningLineContentAnnotation(pos, viewer);
        } else if (ann instanceof ICodeMiningAnnotation && ((ICodeMiningAnnotation) ann).isInVisibleLines()) {
            // annotation is in visible lines
            annotationsToRedraw.add((ICodeMiningAnnotation) ann);
        }
        ((ICodeMiningAnnotation) ann).update(minings, monitor);
        currentAnnotations.add(ann);
    });
    // check if request was canceled.
    monitor.isCanceled();
    fInlinedAnnotationSupport.updateAnnotations(currentAnnotations);
    // redraw the existing codemining annotations since their content can change
    annotationsToRedraw.stream().forEach(ann -> ann.redraw());
}
Also used : ICodeMining(org.eclipse.jface.text.codemining.ICodeMining) Position(org.eclipse.jface.text.Position) LineHeaderCodeMining(org.eclipse.jface.text.codemining.LineHeaderCodeMining) AbstractInlinedAnnotation(org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation) IDocument(org.eclipse.jface.text.IDocument) HashSet(java.util.HashSet)

Example 2 with AbstractInlinedAnnotation

use of org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation in project eclipse.platform.text by eclipse.

the class InlinedAnnotationDemo method getInlinedAnnotation.

/**
 * Returns the inlined annotations list to display in the given viewer.
 *
 * @param viewer
 *            the viewer
 * @param support
 *            the inlined annotation suppor.
 * @return the inlined annotations list to display in the given viewer.
 */
private static Set<AbstractInlinedAnnotation> getInlinedAnnotation(ISourceViewer viewer, InlinedAnnotationSupport support) {
    IDocument document = viewer.getDocument();
    Set<AbstractInlinedAnnotation> annotations = new HashSet<>();
    int lineCount = document.getNumberOfLines();
    for (int i = 0; i < lineCount; i++) {
        String line = getLineText(document, i).trim();
        int index = line.indexOf("color:");
        if (index == 0) {
            String rgb = line.substring(index + "color:".length(), line.length()).trim();
            try {
                String status = "OK!";
                Color color = parse(rgb, viewer.getTextWidget().getDisplay());
                if (color != null) {
                } else {
                    status = "ERROR!";
                }
                // Status color annotation
                Position pos = Positions.of(i, document, true);
                ColorStatusAnnotation statusAnnotation = support.findExistingAnnotation(pos);
                if (statusAnnotation == null) {
                    statusAnnotation = new ColorStatusAnnotation(pos, viewer);
                }
                statusAnnotation.setStatus(status);
                annotations.add(statusAnnotation);
                // Color annotation
                if (color != null) {
                    Position colorPos = new Position(pos.offset + index + "color:".length(), 1);
                    ColorAnnotation colorAnnotation = support.findExistingAnnotation(colorPos);
                    if (colorAnnotation == null) {
                        colorAnnotation = new ColorAnnotation(colorPos, viewer);
                    }
                    colorAnnotation.setColor(color);
                    annotations.add(colorAnnotation);
                }
                // rgb parameter names annotations
                int rgbIndex = line.indexOf("rgb");
                if (rgbIndex != -1) {
                    rgbIndex = rgbIndex + "rgb".length();
                    int startOffset = pos.offset + rgbIndex;
                    String rgbContent = line.substring(rgbIndex, line.length());
                    int startIndex = addRGBParamNameAnnotation("red:", rgbContent, 0, startOffset, viewer, support, annotations);
                    if (startIndex != -1) {
                        startIndex = addRGBParamNameAnnotation("green:", rgbContent, startIndex, startOffset, viewer, support, annotations);
                        if (startIndex != -1) {
                            startIndex = addRGBParamNameAnnotation("blue:", rgbContent, startIndex, startOffset, viewer, support, annotations);
                        }
                    }
                }
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }
    return annotations;
}
Also used : AbstractInlinedAnnotation(org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation) Position(org.eclipse.jface.text.Position) Color(org.eclipse.swt.graphics.Color) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)2 IDocument (org.eclipse.jface.text.IDocument)2 Position (org.eclipse.jface.text.Position)2 AbstractInlinedAnnotation (org.eclipse.jface.text.source.inlined.AbstractInlinedAnnotation)2 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ICodeMining (org.eclipse.jface.text.codemining.ICodeMining)1 LineHeaderCodeMining (org.eclipse.jface.text.codemining.LineHeaderCodeMining)1 Color (org.eclipse.swt.graphics.Color)1