/**
* Jumps to the matching bracket.
*/
public void gotoMatchingBracket() {
final ISourceViewer sourceViewer = getSourceViewer();
final IDocument document = sourceViewer.getDocument();
if (document == null) {
return;
}
final IRegion selection = getSignedSelection(sourceViewer);
final int selectionLength = Math.abs(selection.getLength());
if (selectionLength > 1) {
setStatusLineErrorMessage(ErlangEditorMessages.GotoMatchingBracket_error_invalidSelection);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
// #26314
final int sourceCaretOffset = selection.getOffset() + selection.getLength();
// TODO fix me!
// if (isSurroundedByBrackets(document, sourceCaretOffset))
// sourceCaretOffset -= selection.getLength();
final IRegion region = getBracketMatcher().match(document, sourceCaretOffset);
if (region == null) {
setStatusLineErrorMessage(ErlangEditorMessages.GotoMatchingBracket_error_noMatchingBracket);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
final int offset = region.getOffset();
final int length = region.getLength();
if (length < 1) {
return;
}
final int anchor = getBracketMatcher().getAnchor();
// http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
int targetOffset = ICharacterPairMatcher.RIGHT == anchor ? offset + 1 : offset
+ length;
boolean visible = false;
if (sourceViewer instanceof ITextViewerExtension5) {
final ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
visible = extension.modelOffset2WidgetOffset(targetOffset) > -1;
} else {
final IRegion visibleRegion = sourceViewer.getVisibleRegion();
// http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
visible = targetOffset >= visibleRegion.getOffset()
&& targetOffset <= visibleRegion.getOffset()
+ visibleRegion.getLength();
}
if (!visible) {
setStatusLineErrorMessage(ErlangEditorMessages.GotoMatchingBracket_error_bracketOutsideSelectedElement);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
if (selection.getLength() < 0) {
targetOffset -= selection.getLength();
}
sourceViewer.setSelectedRange(targetOffset, selection.getLength());
sourceViewer.revealRange(targetOffset, selection.getLength());
}