Search in sources :

Example 1 with HighlightedPosition

use of org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition in project webtools.sourceediting by eclipse.

the class SemanticHighlightingPresenter method addPositionFromUI.

/**
 * Add a position with the given range and highlighting unconditionally, only from UI thread.
 * The position will also be registered on the document. The text presentation is not invalidated.
 *
 * @param uiPosition the highlighted position to add from the UI
 */
private void addPositionFromUI(HighlightedPosition uiPosition) {
    Position position = createHighlightedPosition(uiPosition, uiPosition.getHighlighting(), uiPosition.isReadOnly());
    synchronized (fPositionLock) {
        insertPosition(position);
    }
    IDocument document = fSourceViewer.getDocument();
    if (document == null)
        return;
    String positionCategory = getPositionCategory();
    try {
        document.addPosition(positionCategory, position);
    } catch (BadLocationException e) {
        // Should not happen
        Logger.logException(e);
    } catch (BadPositionCategoryException e) {
        // Should not happen
        Logger.logException(e);
    }
}
Also used : HighlightedPosition(org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition) Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with HighlightedPosition

use of org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition in project webtools.sourceediting by eclipse.

the class SemanticHighlightingReconciler method addPosition.

private void addPosition(Position position, HighlightingStyle highlighting, boolean isReadOnly) {
    boolean isExisting = false;
    // TODO: use binary search
    for (int i = 0, n = fRemovedPositions.size(); i < n; i++) {
        HighlightedPosition highlightedPosition = (HighlightedPosition) fRemovedPositions.get(i);
        if (highlightedPosition == null)
            continue;
        if (highlightedPosition.isEqual(position, highlighting)) {
            isExisting = true;
            fRemovedPositions.set(i, null);
            fNOfRemovedPositions--;
            break;
        }
    }
    if (!isExisting) {
        fAddedPositions.add(fJobPresenter.createHighlightedPosition(position, highlighting, isReadOnly));
    }
}
Also used : HighlightedPosition(org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition)

Example 3 with HighlightedPosition

use of org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition in project webtools.sourceediting by eclipse.

the class SemanticHighlightingPresenter method updatePresentation.

/**
 * Invalidate the presentation of the positions based on the given added positions and the existing deleted positions.
 * Also unregisters the deleted positions from the document and patches the positions of this presenter.
 * <p>
 * NOTE: Indirectly called from background thread by UI runnable.
 * </p>
 * @param textPresentation the text presentation or <code>null</code>, if the presentation should computed in the UI thread
 * @param addedPositions the added positions
 * @param removedPositions the removed positions
 */
public void updatePresentation(TextPresentation textPresentation, HighlightedPosition[] addedPositions, HighlightedPosition[] removedPositions) {
    if (fSourceViewer == null)
        return;
    // TODO: reuse removed positions
    if (isCanceled())
        return;
    IDocument document = fSourceViewer.getDocument();
    if (document == null)
        return;
    String positionCategory = getPositionCategory();
    List removedPositionsList = Arrays.asList(removedPositions);
    try {
        synchronized (fPositionLock) {
            List oldPositions = fPositions;
            int newSize = Math.max(fPositions.size() + addedPositions.length - removedPositions.length, 10);
            /*
				 * The following loop is a kind of merge sort: it merges two List<Position>, each
				 * sorted by position.offset, into one new list. The first of the two is the
				 * previous list of positions (oldPositions), from which any deleted positions get
				 * removed on the fly. The second of two is the list of added positions. The result
				 * is stored in newPositions.
				 */
            List newPositions = new ArrayList(newSize);
            Position position = null;
            Position addedPosition = null;
            for (int i = 0, j = 0, n = oldPositions.size(), m = addedPositions.length; i < n || position != null || j < m || addedPosition != null; ) {
                // a) find the next non-deleted Position from the old list
                while (position == null && i < n) {
                    position = (Position) oldPositions.get(i++);
                    if (position.isDeleted() || contain(removedPositionsList, position)) {
                        document.removePosition(positionCategory, position);
                        position = null;
                    }
                }
                // b) find the next Position from the added list
                if (addedPosition == null && j < m) {
                    addedPosition = addedPositions[j++];
                    document.addPosition(positionCategory, addedPosition);
                }
                // c) merge: add the next of position/addedPosition with the lower offset
                if (position != null) {
                    if (addedPosition != null)
                        if (position.getOffset() <= addedPosition.getOffset()) {
                            newPositions.add(position);
                            position = null;
                        } else {
                            newPositions.add(addedPosition);
                            addedPosition = null;
                        }
                    else {
                        newPositions.add(position);
                        position = null;
                    }
                } else if (addedPosition != null) {
                    newPositions.add(addedPosition);
                    addedPosition = null;
                }
            }
            fPositions = newPositions;
            Collections.sort(fPositions, new Comparator() {

                public int compare(Object arg0, Object arg1) {
                    Position p1 = (Position) arg0;
                    Position p2 = (Position) arg1;
                    return p1.offset - p2.offset;
                }
            });
        }
    } catch (BadPositionCategoryException e) {
        // Should not happen
        Logger.logException(e);
    } catch (BadLocationException e) {
        // Should not happen
        Logger.logException(e);
    }
    if (textPresentation != null)
        fSourceViewer.changeTextPresentation(textPresentation, false);
    else
        fSourceViewer.invalidateTextPresentation();
}
Also used : HighlightedPosition(org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ArrayList(java.util.ArrayList) List(java.util.List) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Comparator(java.util.Comparator)

Example 4 with HighlightedPosition

use of org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition in project webtools.sourceediting by eclipse.

the class SemanticHighlightingPresenter method applyTextPresentation.

/*
	 * @see org.eclipse.jface.text.ITextPresentationListener#applyTextPresentation(org.eclipse.jface.text.TextPresentation)
	 */
public void applyTextPresentation(TextPresentation textPresentation) {
    IRegion region = textPresentation.getExtent();
    int minStart = Integer.MAX_VALUE;
    int maxEnd = Integer.MIN_VALUE;
    int i = computeIndexAtOffset(fPositions, region.getOffset()), n = computeIndexAtOffset(fPositions, region.getOffset() + region.getLength());
    if (n - i > 2) {
        List ranges = new ArrayList(n - i);
        for (; i < n; i++) {
            HighlightedPosition position = (HighlightedPosition) fPositions.get(i);
            if (!position.isDeleted()) {
                if (!position.isReadOnly())
                    ranges.add(position.createStyleRange());
                else {
                    int offset = position.getOffset();
                    minStart = Math.min(minStart, offset);
                    maxEnd = Math.max(maxEnd, offset + position.getLength());
                }
            }
        }
        StyleRange[] array = new StyleRange[ranges.size()];
        array = (StyleRange[]) ranges.toArray(array);
        textPresentation.replaceStyleRanges(array);
    } else {
        for (; i < n; i++) {
            HighlightedPosition position = (HighlightedPosition) fPositions.get(i);
            if (!position.isDeleted()) {
                if (!position.isReadOnly())
                    textPresentation.replaceStyleRange(position.createStyleRange());
                else {
                    int offset = position.getOffset();
                    minStart = Math.min(minStart, offset);
                    maxEnd = Math.max(maxEnd, offset + position.getLength());
                }
            }
        }
    }
    if (minStart < maxEnd) {
        IStructuredDocument document = (IStructuredDocument) fSourceViewer.getDocument();
        if (document.containsReadOnly(minStart, maxEnd)) {
            Iterator nonDefaultStyleRangeIterator = textPresentation.getNonDefaultStyleRangeIterator();
            while (nonDefaultStyleRangeIterator.hasNext()) {
                StyleRange styleRange = (StyleRange) nonDefaultStyleRangeIterator.next();
                if (document.containsReadOnly(styleRange.start, styleRange.length)) {
                    adjustForeground(styleRange);
                }
            }
        }
    }
}
Also used : HighlightedPosition(org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition) StyleRange(org.eclipse.swt.custom.StyleRange) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

HighlightedPosition (org.eclipse.wst.sse.ui.internal.style.SemanticHighlightingManager.HighlightedPosition)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)2 IDocument (org.eclipse.jface.text.IDocument)2 Position (org.eclipse.jface.text.Position)2 Comparator (java.util.Comparator)1 Iterator (java.util.Iterator)1 IRegion (org.eclipse.jface.text.IRegion)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)1