Search in sources :

Example 6 with LinearRange

use of org.eclipse.che.ide.api.editor.text.LinearRange in project che by eclipse.

the class JavaQuickAssistProcessor method computeQuickAssistProposals.

@Override
public void computeQuickAssistProposals(final QuickAssistInvocationContext quickAssistContext, final CodeAssistCallback callback) {
    final TextEditor textEditor = quickAssistContext.getTextEditor();
    final Document document = textEditor.getDocument();
    LinearRange tempRange;
    tempRange = textEditor.getSelectedLinearRange();
    final LinearRange range = tempRange;
    final boolean goToClosest = (range.getLength() == 0);
    final QueryAnnotationsEvent.AnnotationFilter filter = new QueryAnnotationsEvent.AnnotationFilter() {

        @Override
        public boolean accept(final Annotation annotation) {
            if (!(annotation instanceof JavaAnnotation)) {
                return false;
            } else {
                JavaAnnotation javaAnnotation = (JavaAnnotation) annotation;
                return (!javaAnnotation.isMarkedDeleted());
            }
        }
    };
    final QueryAnnotationsEvent.QueryCallback queryCallback = new QueryAnnotationsEvent.QueryCallback() {

        @Override
        public void respond(final Map<Annotation, Position> annotations) {
            List<Problem> problems = new ArrayList<>();
            /*final Map<Annotation, Position> problems =*/
            int offset = collectQuickFixableAnnotations(range, document, annotations, goToClosest, problems);
            if (offset != range.getStartOffset()) {
                TextEditor presenter = ((TextEditor) textEditor);
                presenter.getCursorModel().setCursorPosition(offset);
            }
            setupProposals(callback, textEditor, offset, problems);
        }
    };
    final QueryAnnotationsEvent event = new QueryAnnotationsEvent.Builder().withFilter(filter).withCallback(queryCallback).build();
    document.getDocumentHandle().getDocEventBus().fireEvent(event);
}
Also used : LinearRange(org.eclipse.che.ide.api.editor.text.LinearRange) ArrayList(java.util.ArrayList) Document(org.eclipse.che.ide.api.editor.document.Document) QueryAnnotationsEvent(org.eclipse.che.ide.api.editor.annotation.QueryAnnotationsEvent) Annotation(org.eclipse.che.ide.api.editor.text.annotation.Annotation) TextEditor(org.eclipse.che.ide.api.editor.texteditor.TextEditor) Problem(org.eclipse.che.ide.ext.java.shared.dto.Problem) Map(java.util.Map)

Example 7 with LinearRange

use of org.eclipse.che.ide.api.editor.text.LinearRange in project che by eclipse.

the class JavaQuickAssistProcessor method collectQuickFixableAnnotations.

private int collectQuickFixableAnnotations(final LinearRange lineRange, Document document, final Map<Annotation, Position> annotations, final boolean goToClosest, List<Problem> resultingProblems) {
    int invocationLocation = lineRange.getStartOffset();
    if (goToClosest) {
        LinearRange line = document.getLinearRangeForLine(document.getPositionFromIndex(lineRange.getStartOffset()).getLine());
        int rangeStart = line.getStartOffset();
        int rangeEnd = rangeStart + line.getLength();
        ArrayList<Position> allPositions = new ArrayList<>();
        List<JavaAnnotation> allAnnotations = new ArrayList<>();
        int bestOffset = Integer.MAX_VALUE;
        for (Annotation problem : annotations.keySet()) {
            if (problem instanceof JavaAnnotation) {
                JavaAnnotation ann = ((JavaAnnotation) problem);
                Position pos = annotations.get(problem);
                if (pos != null && isInside(pos.offset, rangeStart, rangeEnd)) {
                    // inside our range?
                    allAnnotations.add(ann);
                    allPositions.add(pos);
                    bestOffset = processAnnotation(problem, pos, invocationLocation, bestOffset);
                }
            }
        }
        if (bestOffset == Integer.MAX_VALUE) {
            return invocationLocation;
        }
        for (int i = 0; i < allPositions.size(); i++) {
            Position pos = allPositions.get(i);
            if (isInside(bestOffset, pos.offset, pos.offset + pos.length)) {
                resultingProblems.add(createProblem(allAnnotations.get(i), pos));
            }
        }
        return bestOffset;
    } else {
        for (Annotation problem : annotations.keySet()) {
            Position pos = annotations.get(problem);
            if (pos != null && isInside(invocationLocation, pos.offset, pos.offset + pos.length)) {
                resultingProblems.add(createProblem((JavaAnnotation) problem, pos));
            }
        }
        return invocationLocation;
    }
}
Also used : LinearRange(org.eclipse.che.ide.api.editor.text.LinearRange) Position(org.eclipse.che.ide.api.editor.text.Position) ArrayList(java.util.ArrayList) Annotation(org.eclipse.che.ide.api.editor.text.annotation.Annotation)

Example 8 with LinearRange

use of org.eclipse.che.ide.api.editor.text.LinearRange in project che by eclipse.

the class MinimapAnnotationRenderer method removeAnnotationItem.

private void removeAnnotationItem(final AnnotationModelEvent event, final Annotation annotation, final Map<Integer, List<Annotation>> toRestore) {
    final Position position = event.getPositionOfRemovedAnnotation(annotation);
    final TextPosition textPosition = this.document.getPositionFromIndex(position.getOffset());
    final int line = textPosition.getLine();
    // remove all marks on the line
    this.minimap.removeMarks(line, line);
    // restore marks that are not removed
    final LinearRange rangeForLine = this.document.getLinearRangeForLine(line);
    final AnnotationModel model = event.getAnnotationModel();
    final Iterator<Annotation> it = model.getAnnotationIterator(rangeForLine.getStartOffset(), rangeForLine.getLength(), false, true);
    while (it.hasNext()) {
        final Annotation current = it.next();
        List<Annotation> lineAnnotations = toRestore.get(line);
        if (!current.equals(annotation)) {
            if (lineAnnotations == null) {
                lineAnnotations = new ArrayList<>();
                toRestore.put(line, lineAnnotations);
            }
            lineAnnotations.add(current);
        } else {
            if (lineAnnotations != null) {
                lineAnnotations.removeAll(Collections.singletonList(current));
                if (lineAnnotations.isEmpty()) {
                    toRestore.remove(line);
                }
            }
        }
    }
}
Also used : LinearRange(org.eclipse.che.ide.api.editor.text.LinearRange) TextPosition(org.eclipse.che.ide.api.editor.text.TextPosition) Position(org.eclipse.che.ide.api.editor.text.Position) TextPosition(org.eclipse.che.ide.api.editor.text.TextPosition) Annotation(org.eclipse.che.ide.api.editor.text.annotation.Annotation)

Aggregations

LinearRange (org.eclipse.che.ide.api.editor.text.LinearRange)8 Annotation (org.eclipse.che.ide.api.editor.text.annotation.Annotation)4 Position (org.eclipse.che.ide.api.editor.text.Position)3 ArrayList (java.util.ArrayList)2 TextPosition (org.eclipse.che.ide.api.editor.text.TextPosition)2 HandlesUndoRedo (org.eclipse.che.ide.api.editor.texteditor.HandlesUndoRedo)2 UndoableEditor (org.eclipse.che.ide.api.editor.texteditor.UndoableEditor)2 Element (elemental.dom.Element)1 Node (elemental.dom.Node)1 CustomEvent (elemental.events.CustomEvent)1 Event (elemental.events.Event)1 EventListener (elemental.events.EventListener)1 SpanElement (elemental.html.SpanElement)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 QueryAnnotationsEvent (org.eclipse.che.ide.api.editor.annotation.QueryAnnotationsEvent)1 Completion (org.eclipse.che.ide.api.editor.codeassist.Completion)1 CompletionProposal (org.eclipse.che.ide.api.editor.codeassist.CompletionProposal)1 Document (org.eclipse.che.ide.api.editor.document.Document)1