Java Code Examples for org.eclipse.jface.text.ITextViewer#setSelectedRange()

The following examples show how to use org.eclipse.jface.text.ITextViewer#setSelectedRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnippetsCompletionProcessor.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
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);
	}
}
 
Example 2
Source File: PyDoubleClickStrategy.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @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);
    }
}
 
Example 3
Source File: SelectNextOccurrenceHandler.java    From eclipse-multicursor with Eclipse Public License 1.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: TextViewerMoveLinesAction.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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
}
 
Example 5
Source File: TextViewerJoinLinesAction.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@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
	}

}
 
Example 6
Source File: ConfigurableCompletionProposal.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void updateSelection(ITextViewer viewer) {
	rememberedSelection = viewer.getSelectedRange();
	int offset = rememberedSelection.x;
	int length= getReplaceContextLength() - (offset - getReplacementOffset());
	
	viewer.setSelectedRange(offset, length);
}
 
Example 7
Source File: JavaMoveLinesAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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
}
 
Example 8
Source File: PyMoveLineAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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
    }
}
 
Example 9
Source File: ConfigurableCompletionProposal.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
private void restoreSelection(ITextViewer viewer) {
	if (rememberedSelection != null)
		viewer.setSelectedRange(rememberedSelection.x, rememberedSelection.y);
}
 
Example 10
Source File: PyDoubleClickStrategy.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
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) {
        }
    }
 
Example 11
Source File: GotoMatchingBracketManager.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
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);
}