下面列出了org.eclipse.jface.text.ITextViewer#getSelectedRange ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
int offset) {
CompletionContext context = computeContext(viewer, offset);
List<ICompletionProposal> completionProposals = new ArrayList<ICompletionProposal>();
doComputeCompletionProposals(context, completionProposals);
if (completionProposals.isEmpty() /*&& !isAstUpToDate()*/) {
IModuleSymbol moduleSymbol = context.getModuleSymbol();
if (moduleSymbol == null) {
EmptyCompletionProposal emptyCompletionProposal = new EmptyCompletionProposal(Messages.XdsOutlinePage_Loading,
LOADING_IMAGE, viewer.getSelectedRange().x);
return new ICompletionProposal[]{emptyCompletionProposal, emptyCompletionProposal};
}
else {
return null;
}
}
return completionProposals.toArray(new ICompletionProposal[0]);
}
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
initIfNeeded();
IDocument document = viewer.getDocument();
if (fTextViewer == null) {
fTextViewer = viewer;
}
// don't eat if not in preferences, XOR with modifier key 1 (Ctrl)
// but: if there is a selection, replace it!
Point selection = viewer.getSelectedRange();
fToggleEating = (stateMask & SWT.MOD1) != 0;
int newLength = selection.x + selection.y - getReplacementOffset();
if ((insertCompletion() ^ fToggleEating) && newLength >= 0) {
setReplacementLength(newLength);
}
apply(document, trigger, offset);
fToggleEating = false;
}
public void doubleClicked(ITextViewer part)
{
int pos = part.getSelectedRange().x;
if (pos < 0)
return;
fText = part;
if (fCtrlDown)
{
if (!selectComment(pos))
{
selectWord(pos);
}
}
else
{
selectWord(pos);
}
}
public void doubleClicked( ITextViewer part){
int pos = part.getSelectedRange().x;
if (pos < 0)
return;
fText = part;
if (!selectComment(pos)) {
selectWord(pos);
}
}
public void doubleClicked(ITextViewer part) {
int pos = part.getSelectedRange().x;
if (pos < 0)
return;
fText = part;
if (!selectComment(pos)) {
selectWord(pos);
}
}
/**
* Copy of {@link org.eclipse.jface.text.source.MatchingCharacterPainter#getSignedSelection()}
*/
public static final IRegion getSignedSelection(ITextViewer sourceViewer) {
Point viewerSelection= sourceViewer.getSelectedRange();
StyledText text= sourceViewer.getTextWidget();
Point selection= text.getSelectionRange();
if (text.getCaretOffset() == selection.x) {
viewerSelection.x= viewerSelection.x + viewerSelection.y;
viewerSelection.y= -viewerSelection.y;
}
return new Region(viewerSelection.x, viewerSelection.y);
}
@Override
public IRegion getHoverRegion(final ITextViewer textViewer, final int offset) {
final Point selection = textViewer.getSelectedRange();
if (selection.x <= offset && offset < selection.x + selection.y)
return new Region(selection.x, selection.y);
return new Region(offset, 0);
}
public IContextInformation[] computeContextInformation(ITextViewer viewer,
int offset) {
// FIXME -- for testing
// Retrieve selected range
Point selectedRange = viewer.getSelectedRange();
if (selectedRange.y > 0) {
if (styleManager == null) {
styleManager = TexStyleCompletionManager.getInstance();
}
return styleManager.getStyleContext();
}
return new ContextInformation[0];
}
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
final IDocument document = viewer.getDocument();
// get the selection range
final Point selectedRange = viewer.getSelectedRange();
final List<ICompletionProposal> propList = new ArrayList<ICompletionProposal>();
try {
// The zero-based row index of the caret.
final int caretRowIndex = document.getLineOfOffset(offset);
// The zero-based column index of the caret.
final int carretColumnIndex = offset - document.getLineOffset(caretRowIndex);
if (selectedRange.y > 0) {
// the range is non-empty
final String text = document.get(selectedRange.x, selectedRange.y);
computeWordProposals(text, offset, carretColumnIndex, propList);
} else {
// the range is empty, no selection in the editor
// get the region
final IRegion wordRegion = DocumentHelper.getRegionExpandedBackwards(document, offset,
DocumentHelper.getDefaultWordDetector());
final String word = document.get(wordRegion.getOffset(), wordRegion.getLength());
computeWordProposals(word, offset, carretColumnIndex, propList);
}
} catch (final BadLocationException ignore) {
}
return propList.toArray(new ICompletionProposal[propList.size()]);
}
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
// Retrieve selected range
final Point selectedRange = viewer.getSelectedRange();
if (selectedRange.y > 0) {
// Text is selected. Create a context information array.
final IContextInformation[] contextInfos = new ContextInformation[ITLAReserveredWords.ALL_WORDS_ARRAY.length];
// Create one context information item for each style
for (int i = 0; i < ITLAReserveredWords.ALL_WORDS_ARRAY.length; i++) {
contextInfos[i] = new ContextInformation(null, ITLAReserveredWords.ALL_WORDS_ARRAY[i] + " Style");
}
return contextInfos;
}
return new ContextInformation[0];
}
public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
// Retrieve selected range
final Point selectedRange = viewer.getSelectedRange();
if (selectedRange.y > 0) {
// Text is selected. Create a context information array.
final IContextInformation[] contextInfos = new ContextInformation[ITLAReserveredWords.ALL_WORDS_ARRAY.length];
// Create one context information item for each style
for (int i = 0; i < ITLAReserveredWords.ALL_WORDS_ARRAY.length; i++) {
contextInfos[i] = new ContextInformation(null, ITLAReserveredWords.ALL_WORDS_ARRAY[i] + " Style");
}
return contextInfos;
}
return new ContextInformation[0];
}
private static IRegion getSelectedLinesRegion(ITextViewer textWidget) throws BadLocationException
{
Point selectionRange = textWidget.getSelectedRange();
int startLine = textWidget.getDocument().getLineOfOffset(selectionRange.x);
int endLine = textWidget.getDocument().getLineOfOffset(selectionRange.x + selectionRange.y);
int startOffset = textWidget.getDocument().getLineOffset(startLine);
IRegion endRegion = textWidget.getDocument().getLineInformation(endLine);
int endOffset = endRegion.getOffset() + endRegion.getLength();
return new Region(startOffset, endOffset - startOffset);
}
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
Point selection= viewer.getSelectedRange();
boolean smartToggle= (stateMask & SWT.CTRL) != 0;
if (!(insertCompletion() ^ smartToggle) && selection.y > 0)
fReplacementLengthComputed= false;
super.apply(viewer, trigger, stateMask, offset);
}
@Override
public void selected(ITextViewer viewer, boolean smartToggle) {
Point selection= viewer.getSelectedRange();
if (!(insertCompletion() ^ smartToggle) && selection.y > 0)
fReplacementLengthComputed= false;
super.selected(viewer, smartToggle);
}
@Override
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
IDocument document = viewer.getDocument();
if (!(getContextType() instanceof JavaStatementPostfixContextType))
return;
Point selection = viewer.getSelectedRange();
String selectedText = null;
if (selection.y != 0) {
return;
}
JavaStatementPostfixContext context = ((JavaStatementPostfixContextType) getContextType()).createContext(document, completionPosition, selection.y, compilationUnit, currentNode, parentNode);
context.setVariable("selection", selectedText); //$NON-NLS-1$
int start = context.getStart();
int end = context.getEnd();
IRegion region = new Region(start, end - start);
Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates(getContextType().getId());
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template)) {
getProposals().add(new PostfixTemplateProposal(template, context, region, getImage()));
}
}
}
public IRegion getHoverRegion( ITextViewer textViewer, int offset){
Point selection = textViewer.getSelectedRange();
if (selection.x <= offset && offset < selection.x + selection.y)
return new Region(selection.x, selection.y);
return new Region(offset, 0);
}
protected static FilterInputProvider getInputProvider(ITextViewer textWidget, CommandElement command,
InputType inputType) throws BadLocationException
{
Point selectionRange = textWidget.getSelectedRange();
switch (inputType)
{
// TODO Move this logic into the enum itself
case UNDEFINED:
case NONE:
return CommandExecutionUtils.EOF;
case SELECTION:
if (selectionRange.y == 0)
return null;
IRegion selectedRegion = getSelectedRegion(textWidget);
return new CommandExecutionUtils.StringInputProvider(textWidget.getDocument().get(
selectedRegion.getOffset(), selectedRegion.getLength()));
case SELECTED_LINES:
if (selectionRange.y == 0)
return null;
IRegion region = getSelectedLinesRegion(textWidget);
return new CommandExecutionUtils.StringInputProvider(textWidget.getDocument().get(region.getOffset(),
region.getLength()));
case DOCUMENT:
return new CommandExecutionUtils.StringInputProvider(textWidget.getDocument().get());
case LEFT_CHAR:
if (getCaretOffset(textWidget) < 1)
return null;
return new CommandExecutionUtils.StringInputProvider(textWidget.getDocument().get(
getCaretOffset(textWidget) - 1, 1));
case RIGHT_CHAR:
if (getCaretOffset(textWidget) < getEndOffset(textWidget))
return new CommandExecutionUtils.StringInputProvider(textWidget.getDocument().get(
getCaretOffset(textWidget), 1));
return null;
case CLIPBOARD:
String contents = getClipboardContents();
if (contents == null || contents.trim().length() == 0)
return null;
return new CommandExecutionUtils.StringInputProvider(contents);
case LINE:
return new CommandExecutionUtils.StringInputProvider(getCurrentLineText(textWidget));
case WORD:
String currentWord = findWord(textWidget);
if (currentWord == null || currentWord.trim().length() == 0)
return null;
return new CommandExecutionUtils.StringInputProvider(currentWord);
case INPUT_FROM_CONSOLE:
return new CommandExecutionUtils.EclipseConsoleInputProvider(CommandExecutionUtils.DEFAULT_CONSOLE_NAME);
case INPUT_FROM_FILE:
return new CommandExecutionUtils.FileInputProvider(command.getInputPath());
}
return null;
}
private static IRegion getSelectedRegion(ITextViewer textWidget)
{
return new Region(textWidget.getSelectedRange().x, textWidget.getSelectedRange().y);
}
public static SourceRange getSelectedRange(ITextViewer viewer) {
Point selectedRange = viewer.getSelectedRange();
return new SourceRange(selectedRange.x, selectedRange.y);
}
private int getCaretOffset() {
ITextViewer viewer= fViewer;
Point point= viewer.getSelectedRange();
return point.x;
}