use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class AnnotationModel method modifyAnnotationPosition.
/**
* Modifies the associated position of the given annotation to the given
* position. If the annotation is not yet managed by this annotation model,
* the annotation is added. When the position is <code>null</code>, the
* annotation is removed from the model.
* <p>
* If requested, all annotation model change listeners will be informed
* about the change.
*
* @param annotation the annotation whose associated position should be
* modified
* @param position the position to whose values the associated position
* should be changed
* @param fireModelChanged indicates whether to notify all model listeners
* @since 3.0
*/
protected void modifyAnnotationPosition(Annotation annotation, Position position, boolean fireModelChanged) {
if (position == null) {
removeAnnotation(annotation, fireModelChanged);
} else {
Position p = fAnnotations.get(annotation);
if (p != null) {
if (position.getOffset() != p.getOffset() || position.getLength() != p.getLength()) {
fDocument.removePosition(p);
p.setOffset(position.getOffset());
p.setLength(position.getLength());
try {
fDocument.addPosition(p);
} catch (BadLocationException e) {
// ignore invalid position
}
}
synchronized (getLockObject()) {
getAnnotationModelEvent().annotationChanged(annotation);
}
if (fireModelChanged)
fireModelChanged();
} else {
try {
addAnnotation(annotation, position, fireModelChanged);
} catch (BadLocationException x) {
// ignore invalid position
}
}
}
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class GotoLineTest method goToLine.
private void goToLine(int line, int expectedResult) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
try {
IEditorPart part = IDE.openEditor(page, fFile);
if (part instanceof ITextEditor) {
ITextEditor editor = (ITextEditor) part;
IAction action = editor.getAction(ITextEditorActionConstants.GOTO_LINE);
Accessor accessor = new Accessor(action, GotoLineAction.class);
accessor.invoke("gotoLine", new Class[] { int.class }, new Integer[] { Integer.valueOf(line) });
Control control = part.getAdapter(Control.class);
if (control instanceof StyledText) {
int caretLine = -1;
StyledText styledText = (StyledText) control;
int caret = styledText.getCaretOffset();
try {
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
caretLine = document.getLineOfOffset(caret);
} catch (BadLocationException e1) {
fail();
}
assertEquals(expectedResult, caretLine);
} else
fail();
} else
fail();
} catch (PartInitException e) {
fail();
}
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class EditorAccessHighlighter method addHighlights.
@Override
public void addHighlights(Match[] matches) {
Map<IAnnotationModel, HashMap<Annotation, Position>> mapsByAnnotationModel = new HashMap<>();
for (Match match : matches) {
int offset = match.getOffset();
int length = match.getLength();
if (offset >= 0 && length >= 0) {
try {
Position position = createPosition(match);
if (position != null) {
Map<Annotation, Position> map = getMap(mapsByAnnotationModel, match);
if (map != null) {
Annotation annotation = match.isFiltered() ? new Annotation(SearchPlugin.FILTERED_SEARCH_ANNOTATION_TYPE, true, null) : new Annotation(SearchPlugin.SEARCH_ANNOTATION_TYPE, true, null);
fMatchesToAnnotations.put(match, annotation);
map.put(annotation, position);
}
}
} catch (BadLocationException e) {
SearchPlugin.log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, SearchMessages.EditorAccessHighlighter_error_badLocation, e));
}
}
}
for (Entry<IAnnotationModel, HashMap<Annotation, Position>> entry : mapsByAnnotationModel.entrySet()) {
addAnnotations(entry.getKey(), entry.getValue());
}
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class PositionTracker method dirtyStateChanged.
@Override
public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {
if (isDirty)
return;
final int[] trackCount = new int[1];
doForExistingMatchesIn(buffer, new IFileBufferMatchOperation() {
@Override
public void run(ITextFileBuffer textBuffer, Match match) {
trackCount[0]++;
Position pos = fMatchesToPositions.get(match);
if (pos != null) {
if (pos.isDeleted()) {
AbstractTextSearchResult result = fMatchesToSearchResults.get(match);
// might be that the containing element has been removed.
if (result != null) {
result.removeMatch(match);
}
untrackPosition(textBuffer, match);
} else {
if (match.getBaseUnit() == Match.UNIT_LINE) {
try {
pos = convertToLinePosition(pos, textBuffer.getDocument());
} catch (BadLocationException e) {
SearchPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, SearchPlugin.getID(), 0, e.getLocalizedMessage(), e));
}
}
match.setOffset(pos.getOffset());
match.setLength(pos.getLength());
}
}
}
});
}
use of org.eclipse.jface.text.BadLocationException in project eclipse.platform.text by eclipse.
the class PositionTracker method trackPosition.
private void trackPosition(AbstractTextSearchResult result, ITextFileBuffer fb, Match match) {
int offset = match.getOffset();
int length = match.getLength();
if (offset < 0 || length < 0)
return;
try {
IDocument doc = fb.getDocument();
Position position = new Position(offset, length);
if (match.getBaseUnit() == Match.UNIT_LINE) {
position = convertToCharacterPosition(position, doc);
}
doc.addPosition(position);
fMatchesToSearchResults.put(match, result);
fMatchesToPositions.put(match, position);
addFileBufferMapping(fb, match);
} catch (BadLocationException e) {
// the match is outside the document
result.removeMatch(match);
}
}
Aggregations