下面列出了org.eclipse.jface.text.source.ISourceViewer#revealRange ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void open(IOutlineNode node, ISourceViewer textViewer) {
if (node != null) {
ITextRegion textRegion = node.getSignificantTextRegion();
if (textRegion != null && textRegion != ITextRegion.EMPTY_REGION) {
int offset = textRegion.getOffset();
int length = textRegion.getLength();
textViewer.setRangeIndication(offset, length, true);
textViewer.revealRange(offset, length);
textViewer.setSelectedRange(offset, length);
} else {
node.tryReadOnly(new IUnitOfWork.Void<EObject>() {
@Override
public void process(EObject state) throws Exception {
openEObject(state);
}
});
}
}
}
/**
* @since 2.3
*/
protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) {
if (completionProposals.isEmpty()) {
return;
}
if (!(invocationContext instanceof QuickAssistInvocationContext && ((QuickAssistInvocationContext) invocationContext).isSuppressSelection())) {
ISourceViewer sourceViewer = invocationContext.getSourceViewer();
IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
Iterator<Annotation> iterator = applicableAnnotations.iterator();
while (iterator.hasNext()) {
Position pos = annotationModel.getPosition(iterator.next());
if (pos != null) {
sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength());
sourceViewer.revealRange(pos.getOffset(), pos.getLength());
break;
}
}
}
}
protected void leave(int offset, int len, boolean isWidget) {
addToHistory();
ISourceViewer viewer = getViewer();
if (viewer != null) {
int off = (isWidget ? MarkUtils.widget2ModelOffset(getViewer(), offset) : offset);
viewer.setSelectedRange(off, len);
viewer.revealRange(off,0);
}
super.leave(true);
}
public void run(IAction action) {
if (targetEditor == null) return;
ISourceViewer sourceViewer= targetEditor.getViewer();
IDocument document= sourceViewer.getDocument();
if (document == null)
return;
ITextSelection selection = (ITextSelection) targetEditor.getSelectionProvider().getSelection();
SubStatusLineManager slm =
(SubStatusLineManager) targetEditor.getEditorSite().getActionBars().getStatusLineManager();
int selectionLength= Math.abs(selection.getLength());
if (selectionLength > 1) {
slm.setErrorMessage(TexlipsePlugin.getResourceString("gotoMatchingBracketNotSelected"));
slm.setVisible(true);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int sourceCaretOffset= selection.getOffset() + selection.getLength();
TexPairMatcher fBracketMatcher = new TexPairMatcher("{}[]()");
IRegion region= fBracketMatcher.match(document, sourceCaretOffset);
if (region == null) {
slm.setErrorMessage(TexlipsePlugin.getResourceString("gotoMatchingBracketNotFound"));
slm.setVisible(true);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int offset= region.getOffset();
int length= region.getLength();
if (length < 1) return;
int anchor = fBracketMatcher.getAnchor();
int targetOffset= (ICharacterPairMatcher.RIGHT == anchor) ? offset + 1: offset + length;
if (selection.getLength() < 0)
targetOffset -= selection.getLength();
sourceViewer.setSelectedRange(targetOffset, selection.getLength());
sourceViewer.revealRange(targetOffset, selection.getLength());
}
/**
* Highlights and moves to a corresponding element in editor
*
* @param reference
* corresponding entity in editor
* @param moveCursor
* if true, moves cursor to the reference
*/
private void setSelection(NavigationBarItem reference, boolean moveCursor) {
if (reference == null) {
return;
}
if (moveCursor) {
markInNavigationHistory();
}
ISourceViewer sourceViewer = getSourceViewer();
if (sourceViewer == null) {
return;
}
StyledText textWidget = sourceViewer.getTextWidget();
if (textWidget == null) {
return;
}
try {
Location start = reference.getSpans().get(0).getStart();
Location end = reference.getSpans().get(0).getEnd();
if (start == null || end == null)
return;
ITypeScriptFile tsFile = getTypeScriptFile();
int offset = tsFile.getPosition(start);
int length = tsFile.getPosition(end) - offset;
if (offset < 0 || length < 0 || length > sourceViewer.getDocument().getLength()) {
return;
}
textWidget.setRedraw(false);
// Uncomment that if we wish to select only variable and not the
// whole block.
// but there is a bug with this code with
// private a: string. it's the first 'a' (of private) which is
// selected and not the second.
// String documentPart = sourceViewer.getDocument().get(offset,
// length);
//
// // Try to find name because position returns for whole block
// String name = reference.getText();
// if (name != null) {
// int nameoffset = documentPart.indexOf(name);
// if (nameoffset != -1) {
// offset += nameoffset;
// length = name.length();
// }
// }
if (length > 0) {
setHighlightRange(offset, length, moveCursor);
}
if (!moveCursor) {
return;
}
if (offset > -1 && length > 0) {
sourceViewer.revealRange(offset, length);
// Selected region begins one index after offset
sourceViewer.setSelectedRange(offset, length);
markInNavigationHistory();
}
} catch (Exception e) {
} finally {
textWidget.setRedraw(true);
}
}
/**
* Jumps to the matching bracket.
*/
public void gotoMatchingBracket() {
ISourceViewer sourceViewer = getSourceViewer();
IDocument document = sourceViewer.getDocument();
if (document == null)
return;
IRegion selection = getSignedSelection(sourceViewer);
int selectionLength = Math.abs(selection.getLength());
if (selectionLength > 1) {
setStatusLineErrorMessage(JSDTTypeScriptUIMessages.GotoMatchingBracket_error_invalidSelection);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
// #26314
int sourceCaretOffset = selection.getOffset() + selection.getLength();
if (isSurroundedByBrackets(document, sourceCaretOffset))
sourceCaretOffset -= selection.getLength();
IRegion region = fBracketMatcher.match(document, sourceCaretOffset);
if (region == null) {
setStatusLineErrorMessage(JSDTTypeScriptUIMessages.GotoMatchingBracket_error_noMatchingBracket);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int offset = region.getOffset();
int length = region.getLength();
if (length < 1)
return;
int anchor = fBracketMatcher.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) {
ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
} else {
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(JSDTTypeScriptUIMessages.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());
}
/**
* Jumps to the matching bracket.
*/
public void gotoMatchingBracket() {
ISourceViewer sourceViewer= getSourceViewer();
IDocument document= sourceViewer.getDocument();
if (document == null)
return;
IRegion selection= getSignedSelection(sourceViewer);
if (fPreviousSelections == null)
initializePreviousSelectionList();
IRegion region= fBracketMatcher.match(document, selection.getOffset(), selection.getLength());
if (region == null) {
region= fBracketMatcher.findEnclosingPeerCharacters(document, selection.getOffset(), selection.getLength());
initializePreviousSelectionList();
fPreviousSelections.add(selection);
} else {
if (fPreviousSelections.size() == 2) {
if (!selection.equals(fPreviousSelections.get(1))) {
initializePreviousSelectionList();
}
} else if (fPreviousSelections.size() == 3) {
if (selection.equals(fPreviousSelections.get(2)) && !selection.equals(fPreviousSelections.get(0))) {
IRegion originalSelection= fPreviousSelections.get(0);
sourceViewer.setSelectedRange(originalSelection.getOffset(), originalSelection.getLength());
sourceViewer.revealRange(originalSelection.getOffset(), originalSelection.getLength());
initializePreviousSelectionList();
return;
}
initializePreviousSelectionList();
}
}
if (region == null) {
setStatusLineErrorMessage(JavaEditorMessages.GotoMatchingBracket_error_noMatchingBracket);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int offset= region.getOffset();
int length= region.getLength();
if (length < 1)
return;
int anchor= fBracketMatcher.getAnchor();
// http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
int targetOffset= (ICharacterPairMatcher.RIGHT == anchor) ? offset + 1 : offset + length - 1;
boolean visible= false;
if (sourceViewer instanceof ITextViewerExtension5) {
ITextViewerExtension5 extension= (ITextViewerExtension5) sourceViewer;
visible= (extension.modelOffset2WidgetOffset(targetOffset) > -1);
} else {
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(JavaEditorMessages.GotoMatchingBracket_error_bracketOutsideSelectedElement);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int adjustment= getOffsetAdjustment(document, selection.getOffset() + selection.getLength(), selection.getLength());
targetOffset+= adjustment;
int direction= (selection.getLength() == 0) ? 0 : ((selection.getLength() > 0) ? 1 : -1);
if (fPreviousSelections.size() == 1 && direction < 0) {
targetOffset++;
}
if (fPreviousSelections.size() > 0) {
fPreviousSelections.add(new Region(targetOffset, direction));
}
sourceViewer.setSelectedRange(targetOffset, direction);
sourceViewer.revealRange(targetOffset, direction);
}
/**
* implementation copied from org.eclipse.ui.externaltools.internal.ant.editor.PlantyEditor#setSelection
*/
public void setSelection(int offset, int length) {
ISourceViewer sourceViewer = getSourceViewer();
sourceViewer.setSelectedRange(offset, length);
sourceViewer.revealRange(offset, length);
}