下面列出了org.eclipse.jface.text.ITextViewer#setSelectedRange ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void insertAsTemplate(ITextViewer textViewer, final IRegion region, String templateText,
CommandElement commandElement)
{
SnippetsCompletionProcessor snippetsCompletionProcessor = new SnippetsCompletionProcessor();
Template template = new SnippetTemplate(commandElement, templateText);
TemplateContext context = snippetsCompletionProcessor.createContext(textViewer, region);
SnippetTemplateProposal completionProposal = (SnippetTemplateProposal) snippetsCompletionProcessor
.createProposal(template, context, region, 0);
completionProposal.setTemplateProposals(new ICompletionProposal[] { completionProposal });
completionProposal.apply(textViewer, '0', SWT.NONE, region.getOffset());
Point selection = completionProposal.getSelection(textViewer.getDocument());
if (selection != null)
{
textViewer.setSelectedRange(selection.x, selection.y);
textViewer.revealRange(selection.x, selection.y);
}
}
/**
* @see ITextDoubleClickStrategy#doubleClicked
*/
@Override
public void doubleClicked(ITextViewer textViewer) {
int offset = textViewer.getSelectedRange().x;
if (offset < 0) {
return;
}
IDocument document = textViewer.getDocument();
IRegion region = fPairMatcher.match(document, offset);
if (region != null && region.getLength() >= 2) {
textViewer.setSelectedRange(region.getOffset() + 1, region.getLength() - 2);
} else {
selectWord(textViewer, document, offset);
}
}
private void startLinkedEdit(List<IRegion> selections, ITextViewer viewer, Point originalSelection)
throws BadLocationException {
final LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup();
for (IRegion selection : selections) {
linkedPositionGroup.addPosition(new LinkedPosition(viewer.getDocument(), selection.getOffset(), selection
.getLength()));
}
LinkedModeModel model = new LinkedModeModel();
model.addGroup(linkedPositionGroup);
model.forceInstall();
//FIXME can add a listener here to listen for the end of linked mode
//model.addLinkingListener(null);
LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
ui.setExitPolicy(new DeleteBlockingExitPolicy(viewer.getDocument()));
ui.enter();
// by default the text being edited is selected so restore original selection
viewer.setSelectedRange(originalSelection.x, originalSelection.y);
}
/**
* Performs similar to AbstractTextEditor.selectAndReveal, but does not update
* the viewers highlight area.
*
* @param viewer the viewer that we want to select on
* @param offset the offset of the selection
* @param length the length of the selection
*/
private void selectAndReveal(ITextViewer viewer, int offset, int length) {
// invert selection to avoid jumping to the end of the selection in st.showSelection()
viewer.setSelectedRange(offset + length, -length);
//viewer.revealRange(offset, length); // will trigger jumping
StyledText st= viewer.getTextWidget();
if (st != null)
st.showSelection(); // only minimal scrolling
}
@Override
public void run() {
ITextViewer viewer= getTextViewer();
if (viewer == null)
return;
if (!canModifyViewer())
return;
IDocument document= viewer.getDocument();
if (document == null)
return;
ITextSelection selection= getSelection(viewer);
if (selection == null)
return;
int startLine= selection.getStartLine();
int endLine= selection.getEndLine();
try {
int caretOffset= joinLines(document, startLine, endLine);
if (caretOffset > -1) {
StyledText widget= viewer.getTextWidget();
widget.setRedraw(false);
adjustHighlightRange(viewer, caretOffset, 0);
viewer.revealRange(caretOffset, 0);
viewer.setSelectedRange(caretOffset, 0);
widget.setRedraw(true);
}
} catch (BadLocationException e) {
// should not happen
}
}
private void updateSelection(ITextViewer viewer) {
rememberedSelection = viewer.getSelectedRange();
int offset = rememberedSelection.x;
int length= getReplaceContextLength() - (offset - getReplacementOffset());
viewer.setSelectedRange(offset, length);
}
/**
* Performs similar to AbstractTextEditor.selectAndReveal, but does not update
* the viewers highlight area.
*
* @param viewer the viewer that we want to select on
* @param offset the offset of the selection
* @param length the length of the selection
*/
private void selectAndReveal(ITextViewer viewer, int offset, int length) {
// invert selection to avoid jumping to the end of the selection in st.showSelection()
viewer.setSelectedRange(offset + length, -length);
//viewer.revealRange(offset, length); // will trigger jumping
StyledText st= viewer.getTextWidget();
if (st != null)
st.showSelection(); // only minimal scrolling
}
/**
* Performs similar to AbstractTextEditor.selectAndReveal, but does not update
* the viewers highlight area.
*
* @param viewer the viewer that we want to select on
* @param offset the offset of the selection
* @param length the length of the selection
*/
private void selectAndReveal(ITextViewer viewer, int offset, int length) {
if (viewer == null) {
return; // in tests
}
// invert selection to avoid jumping to the end of the selection in st.showSelection()
viewer.setSelectedRange(offset + length, -length);
//viewer.revealRange(offset, length); // will trigger jumping
StyledText st = viewer.getTextWidget();
if (st != null) {
st.showSelection(); // only minimal scrolling
}
}
private void restoreSelection(ITextViewer viewer) {
if (rememberedSelection != null)
viewer.setSelectedRange(rememberedSelection.x, rememberedSelection.y);
}
protected void selectWord(ITextViewer textViewer, IDocument document, final int anchor) {
try {
int offset = anchor;
char c;
while (offset >= 0) {
c = document.getChar(offset);
if (!Character.isJavaIdentifierPart(c)) {
break;
}
--offset;
}
int start = offset;
offset = anchor;
final int length = document.getLength();
while (offset < length) {
c = document.getChar(offset);
if (!Character.isJavaIdentifierPart(c)) {
break;
}
++offset;
}
int end = offset;
if (start == end) {
//Nothing to select... let's check if we can select whitespaces
offset = anchor;
while (offset >= 0) {
c = document.getChar(offset);
if (c != ' ' && c != '\t') {
break;
}
--offset;
}
start = offset;
offset = anchor;
while (offset < length) {
c = document.getChar(offset);
if (c != ' ' && c != '\t') {
break;
}
++offset;
}
end = offset;
}
if (start == end) {
textViewer.setSelectedRange(start, 0);
} else {
textViewer.setSelectedRange(start + 1, end - start - 1);
}
} catch (BadLocationException x) {
}
}
public void gotoMatchingBracket() {
ITextViewer sourceViewer = langEditor.getSourceViewer_();
IDocument document= sourceViewer.getDocument();
if (document == null)
return;
IRegion selection= EditorUtils.getSignedSelection(sourceViewer);
if (fPreviousSelections == null)
initializePreviousSelectionList();
IRegion region= getBracketMatcher().match(document, selection.getOffset(), selection.getLength());
if (region == null) {
region= getBracketMatcher().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) {
langEditor.setStatusLineErrorMessage(LangEditorMessages.GotoMatchingBracket_error_noMatchingBracket);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int offset= region.getOffset();
int length= region.getLength();
if (length < 1)
return;
int anchor= getBracketMatcher().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) {
langEditor.setStatusLineErrorMessage(LangEditorMessages.GotoMatchingBracket_error_bracketOutsideSelectedElement);
sourceViewer.getTextWidget().getDisplay().beep();
return;
}
int adjustment= getBracketMatcher().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);
}