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);
}
}
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));
}
}
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();
}
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);
}
}
}
}
}
Aggregations