Java Code Examples for org.eclipse.jface.text.source.ISourceViewer#revealRange()

The following examples show how to use org.eclipse.jface.text.source.ISourceViewer#revealRange() . 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: OutlineNodeElementOpener.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
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);
				}
			});
		}
		
	}
}
 
Example 2
Source File: XtextQuickAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @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;
			}
		}
	}
}
 
Example 3
Source File: SearchMinibuffer.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: GoToMatchingBracketAction.java    From texlipse with Eclipse Public License 1.0 4 votes vote down vote up
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());
}
 
Example 5
Source File: TypeScriptEditor.java    From typescript.java with MIT License 4 votes vote down vote up
/**
 * 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);
	}
}
 
Example 6
Source File: JavaScriptLightWeightEditor.java    From typescript.java with MIT License 4 votes vote down vote up
/**
 * 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());
}
 
Example 7
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: BaseEditor.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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);
}