use of org.eclipse.jface.text.BadPositionCategoryException in project webtools.sourceediting by eclipse.
the class GenericPositionManager method addPosition.
/*
* @see org.eclipse.jface.text.IDocument#addPosition(java.lang.String,
* org.eclipse.jface.text.Position)
*/
public synchronized void addPosition(String category, Position position) throws BadLocationException, BadPositionCategoryException {
if ((0 > position.offset) || (0 > position.length) || (position.offset + position.length > getDocumentLength()))
throw new BadLocationException();
if (category == null)
throw new BadPositionCategoryException();
List list = (List) fPositions.get(category);
if (list == null)
throw new BadPositionCategoryException();
list.add(computeIndexInPositionList(list, position.offset), position);
}
use of org.eclipse.jface.text.BadPositionCategoryException in project webtools.sourceediting by eclipse.
the class GenericPositionManager method removePosition.
/*
* @see org.eclipse.jface.text.IDocument#removePosition(java.lang.String,
* org.eclipse.jface.text.Position)
*/
public synchronized void removePosition(String category, Position position) throws BadPositionCategoryException {
if (position == null)
return;
if (category == null)
throw new BadPositionCategoryException();
List c = (List) fPositions.get(category);
if (c == null)
throw new BadPositionCategoryException();
// remove based on identity not equality
int size = c.size();
for (int i = 0; i < size; i++) {
if (position == c.get(i)) {
c.remove(i);
return;
}
}
}
use of org.eclipse.jface.text.BadPositionCategoryException in project xtext-eclipse by eclipse.
the class DocumentPartitioner method computePartitioning.
/**
* {@inheritDoc}
* <p>
* May be replaced or extended by subclasses.
* </p>
*
* @since 2.2
*/
@Override
public synchronized ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
checkInitialization();
List<ITypedRegion> list = new ArrayList<ITypedRegion>();
try {
int endOffset = offset + length;
Position[] category = getPositions();
TypedPosition previous = null, current = null;
int start, end, gapOffset;
Position gap = new Position(0);
int startIndex = getFirstIndexEndingAfterOffset(category, offset);
int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
for (int i = startIndex; i < endIndex; i++) {
current = (TypedPosition) category[i];
gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
gap.setOffset(gapOffset);
gap.setLength(current.getOffset() - gapOffset);
if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
start = Math.max(offset, gapOffset);
end = Math.min(endOffset, gap.getOffset() + gap.getLength());
list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
}
if (current.overlapsWith(offset, length)) {
start = Math.max(offset, current.getOffset());
end = Math.min(endOffset, current.getOffset() + current.getLength());
list.add(new TypedRegion(start, end - start, current.getType()));
}
previous = current;
}
if (previous != null) {
gapOffset = previous.getOffset() + previous.getLength();
gap.setOffset(gapOffset);
int gapLength = fDocument.getLength() - gapOffset;
if (gapLength < 0) {
clearPositionCache();
return new TypedRegion[0];
} else {
gap.setLength(gapLength);
if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
start = Math.max(offset, gapOffset);
end = Math.min(endOffset, fDocument.getLength());
list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
}
}
}
if (list.isEmpty())
list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
} catch (BadPositionCategoryException ex) {
// Make sure we clear the cache
clearPositionCache();
} catch (RuntimeException ex) {
// Make sure we clear the cache
clearPositionCache();
throw ex;
}
TypedRegion[] result = new TypedRegion[list.size()];
list.toArray(result);
return result;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project xtext-eclipse by eclipse.
the class XbaseTemplateContext method evaluate.
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
IXtextDocument xDocument = xtextDocumentUtil.getXtextDocument(getDocument());
// Ensure clean state before evaluation starts
imports.clear();
TemplateBuffer resolvedContent = super.evaluate(template);
Position position = new Position(getCompletionOffset(), 0);
List<ReplaceRegion> rewrite = createImports(imports, xDocument);
if (rewrite.size() > 0 && !isReadOnly()) {
// Remember the completion offset before performing doc changes
// $NON-NLS-1$
final String category = "__template_position_import_section" + System.currentTimeMillis();
IPositionUpdater updater = new DefaultPositionUpdater(category);
xDocument.addPositionCategory(category);
xDocument.addPositionUpdater(updater);
xDocument.addPosition(position);
try {
replaceConverter.convertToTextEdit(rewrite).apply(xDocument);
// restore CompletionOffset
setCompletionOffset(position.getOffset());
} catch (BadLocationException e) {
throw new TemplateException(e);
} finally {
xDocument.removePosition(position);
xDocument.removePositionUpdater(updater);
try {
xDocument.removePositionCategory(category);
} catch (BadPositionCategoryException e) {
throw new TemplateException(e);
}
}
}
return resolvedContent;
}
use of org.eclipse.jface.text.BadPositionCategoryException in project xtext-eclipse by eclipse.
the class HighlightingPresenter 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 offset
* The range offset
* @param length
* The range length
* @param highlighting
* The highlighting attribute.
*/
private void addPositionFromUI(int offset, int length, TextAttribute highlighting) {
AttributedPosition position = createHighlightedPosition(offset, length, highlighting);
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
log.debug(e.getMessage(), e);
} catch (BadPositionCategoryException e) {
// Should not happen
log.debug(e.getMessage(), e);
}
}
Aggregations