use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class PositionTracker method getCurrentPosition.
public Position getCurrentPosition(Match match) {
Position pos = fMatchesToPositions.get(match);
if (pos == null)
return pos;
AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
if (match.getBaseUnit() == Match.UNIT_LINE && result != null) {
ITextFileBuffer fb = getTrackedFileBuffer(result, match.getElement());
if (fb != null) {
IDocument doc = fb.getDocument();
try {
pos = convertToLinePosition(pos, doc);
} catch (BadLocationException e) {
}
}
}
return pos;
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class RetrieverAction method expandSelection.
private ITextSelection expandSelection(ITextSelection sel, IDocument document, String stopChars) {
int offset = sel.getOffset();
int length = sel.getLength();
// left or right.
if (length == 0) {
// try right
char chr = 0;
char chl = 0;
try {
chr = document.getChar(offset);
} catch (BadLocationException e2) {
}
try {
chl = document.getChar(offset - 1);
} catch (BadLocationException e2) {
}
if (isPartOfIdentifier(chr)) {
length = 1;
} else if (isPartOfIdentifier(chl)) {
offset--;
length = 1;
} else if (stopChars != null && stopChars.indexOf(chr) == -1) {
length = 1;
} else if (stopChars != null && stopChars.indexOf(chl) == -1) {
offset--;
length = 1;
} else {
return sel;
}
}
int a = offset + length - 1;
int z = a;
// move z one behind last character.
try {
char ch = document.getChar(z);
while (isValidChar(stopChars, ch)) {
ch = document.getChar(++z);
}
} catch (BadLocationException e2) {
}
// move a one before the first character
try {
char ch = document.getChar(a);
while (isValidChar(stopChars, ch)) {
ch = document.getChar(--a);
}
} catch (BadLocationException e2) {
}
if (a == z) {
offset = a;
length = 0;
} else {
offset = a + 1;
length = z - a - 1;
}
return new TextSelection(document, offset, length);
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class OverviewRuler method doPaint.
/**
* Draws this overview ruler.
*
* @param gc the GC to draw into
*/
private void doPaint(GC gc) {
Rectangle r = new Rectangle(0, 0, 0, 0);
int yy, hh = ANNOTATION_HEIGHT;
IDocument document = fTextViewer.getDocument();
StyledText textWidget = fTextViewer.getTextWidget();
ITextViewerExtension5 extension = null;
IRegion visible = null;
if (fTextViewer instanceof ITextViewerExtension5)
extension = (ITextViewerExtension5) fTextViewer;
else
// legacy support
visible = fTextViewer.getVisibleRegion();
WidgetInfos infos = null;
for (Object annotationType : fAnnotationsSortedByLayer) {
if (skip(annotationType))
continue;
int[] style = new int[] { FilterIterator.PERSISTENT, FilterIterator.TEMPORARY };
for (int element : style) {
boolean areColorsComputed = false;
Color fill = null;
Color stroke = null;
Iterator<Annotation> e = new FilterIterator(annotationType, element, fCachedAnnotations.iterator());
while (e.hasNext()) {
Annotation a = e.next();
Position p = fModel.getPosition(a);
if (p == null)
continue;
if (visible != null && !p.overlapsWith(visible.getOffset(), visible.getLength()))
continue;
int annotationOffset = p.getOffset();
int annotationLength = p.getLength();
IRegion widgetRegion = null;
if (visible != null) {
annotationOffset = Math.max(p.getOffset(), visible.getOffset());
int annotationEnd = Math.min(p.getOffset() + p.getLength(), visible.getOffset() + visible.getLength());
annotationLength = annotationEnd - annotationOffset;
} else {
widgetRegion = extension.modelRange2WidgetRange(new Region(annotationOffset, annotationLength));
if (widgetRegion == null)
continue;
}
if (infos == null) {
infos = new WidgetInfos(textWidget, fCanvas);
r.x = INSET;
r.width = infos.bounds.width - (2 * INSET);
}
try {
int startOffset = visible != null ? annotationOffset - visible.getOffset() : widgetRegion.getOffset();
int startLine = textWidget.getLineAtOffset(startOffset);
yy = computeY(startLine, infos);
if (ANNOTATION_HEIGHT_SCALABLE) {
int numberOfLines = document.getNumberOfLines(annotationOffset, annotationLength);
// don't count empty trailing line
IRegion lastLine = document.getLineInformationOfOffset(annotationOffset + annotationLength);
if (lastLine.getOffset() == annotationOffset + annotationLength) {
numberOfLines--;
}
if (numberOfLines > 1) {
int yy2 = computeY(startLine + numberOfLines - 1, infos);
hh = Math.max(yy2 - yy, ANNOTATION_HEIGHT);
} else {
hh = ANNOTATION_HEIGHT;
}
}
fAnnotationHeight = hh;
if (!areColorsComputed) {
stroke = getStrokeColor(annotationType, element == FilterIterator.TEMPORARY);
fill = fUseSaturatedColors ? stroke : getFillColor(annotationType, element == FilterIterator.TEMPORARY);
areColorsComputed = true;
}
if (fill != null) {
gc.setBackground(fill);
gc.fillRectangle(INSET, yy, infos.bounds.width - (2 * INSET), hh);
}
if (stroke != null) {
gc.setForeground(stroke);
r.y = yy;
if (yy + hh == infos.bounds.height)
r.y--;
r.height = hh;
// NOTE: 0 means width is 1 but with optimized performance
gc.setLineWidth(0);
gc.drawRectangle(r);
}
} catch (BadLocationException x) {
}
}
}
}
if (DEBUG_DRAW) {
// draw debugging guides (boundaries):
if (infos == null)
infos = new WidgetInfos(textWidget, fCanvas);
gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_DARK_MAGENTA));
yy = infos.thumbHeight / 2;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
yy = infos.bounds.height - infos.thumbHeight / 2;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLUE));
yy = 0;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
yy = infos.bounds.height - 1;
gc.drawLine(0, yy, infos.bounds.x / 2, yy);
}
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class OverviewRuler method getAnnotationPosition.
/**
* Returns the position of the first annotation found in the given line range.
*
* @param lineNumbers the line range
* @return the position of the first found annotation
*/
private Position getAnnotationPosition(int[] lineNumbers) {
if (lineNumbers[0] == -1)
return null;
Position found = null;
try {
IDocument d = fTextViewer.getDocument();
IRegion line = d.getLineInformation(lineNumbers[0]);
int start = line.getOffset();
line = d.getLineInformation(lineNumbers[lineNumbers.length - 1]);
int end = line.getOffset() + line.getLength();
for (int i = fAnnotationsSortedByLayer.size() - 1; i >= 0; i--) {
Object annotationType = fAnnotationsSortedByLayer.get(i);
Iterator<Annotation> e = new FilterIterator(annotationType, FilterIterator.PERSISTENT | FilterIterator.TEMPORARY);
while (e.hasNext() && found == null) {
Annotation a = e.next();
if (a.isMarkedDeleted())
continue;
if (skip(a.getType()))
continue;
Position p = fModel.getPosition(a);
if (p == null)
continue;
int posOffset = p.getOffset();
int posEnd = posOffset + p.getLength();
IRegion region = d.getLineInformationOfOffset(posEnd);
// trailing empty lines don't count
if (posEnd > posOffset && region.getOffset() == posEnd) {
posEnd--;
region = d.getLineInformationOfOffset(posEnd);
}
if (posOffset <= end && posEnd >= start)
found = p;
}
}
} catch (BadLocationException x) {
}
return found;
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class TemplateCompletionProcessor method extractPrefix.
/**
* Heuristically extracts the prefix used for determining template relevance
* from the viewer's document. The default implementation returns the String from
* offset backwards that forms a java identifier.
*
* @param viewer the viewer
* @param offset offset into document
* @return the prefix to consider
* @see #getRelevance(Template, String)
*/
protected String extractPrefix(ITextViewer viewer, int offset) {
int i = offset;
IDocument document = viewer.getDocument();
if (i > document.getLength())
// $NON-NLS-1$
return "";
try {
while (i > 0) {
char ch = document.getChar(i - 1);
if (!Character.isJavaIdentifierPart(ch))
break;
i--;
}
return document.get(i, offset - i);
} catch (BadLocationException e) {
// $NON-NLS-1$
return "";
}
}
Aggregations