org.eclipse.jface.text.source.ICharacterPairMatcher Java Examples

The following examples show how to use org.eclipse.jface.text.source.ICharacterPairMatcher. 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: HackDefaultCharacterPairMatcher.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
protected IRegion performMatch(IDocument doc, int caretOffset) throws BadLocationException {
	final int charOffset= caretOffset - 1;
	final char prevChar= doc.getChar(Math.max(charOffset, 0));
	if (!fPairs.contains(prevChar)) return null;
	final boolean isForward= fPairs.isStartCharacter(prevChar);
	fAnchor= isForward ? ICharacterPairMatcher.LEFT : ICharacterPairMatcher.RIGHT;
	final int searchStartPosition= isForward ? caretOffset : caretOffset - 2;
	final int adjustedOffset= isForward ? charOffset : caretOffset;
	final String partition= TextUtilities.getContentType(doc, fPartitioning, charOffset, false);
	final DocumentPartitionAccessor partDoc= new DocumentPartitionAccessor(doc, fPartitioning, partition);
	int endOffset= findMatchingPeer(partDoc, prevChar, fPairs.getMatching(prevChar),
			isForward,  isForward ? doc.getLength() : -1,
			searchStartPosition);
	if (endOffset == -1) return null;
	final int adjustedEndOffset= isForward ? endOffset + 1: endOffset;
	if (adjustedEndOffset == adjustedOffset) return null;
	return new Region(Math.min(adjustedOffset, adjustedEndOffset),
			Math.abs(adjustedEndOffset - adjustedOffset));
}
 
Example #2
Source File: XtextUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ICharacterPairMatcher bindICharacterPairMatcher() {
	return new DefaultCharacterPairMatcher(new char[] { ':', ';', '{', '}', '(', ')', '[', ']' });
}
 
Example #3
Source File: DefaultUiModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ICharacterPairMatcher bindICharacterPairMatcher() {
	return new BracePairMatcher();
}
 
Example #4
Source File: LexerTokenAndCharacterPairAwareStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected ICharacterPairMatcher getCharacterPairMatcher() {
	return characterPairMatcher;
}
 
Example #5
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 #6
Source File: PairedBracketsPainter.java    From xds-ide with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Handles a redraw request.
 *
 * @param gc the GC to draw into.
 */
private void handleDrawRequest(GC gc) {

    if (fPairPosition.isDeleted)
        return;

    int offset= fPairPosition.getOffset();
    int length= fPairPosition.getLength();
    if (length < 1)
        return;

    if (fSourceViewer instanceof ITextViewerExtension5) {
        ITextViewerExtension5 extension= (ITextViewerExtension5) fSourceViewer;
        IRegion widgetRange= extension.modelRange2WidgetRange(new Region(offset, length));
        if (widgetRange == null)
            return;

        try {
            // don't draw if the pair position is really hidden and widgetRange just
            // marks the coverage around it.
            IDocument doc= fSourceViewer.getDocument();
            int startLine= doc.getLineOfOffset(offset);
            int endLine= doc.getLineOfOffset(offset + length);
            if (extension.modelLine2WidgetLine(startLine) == -1 || extension.modelLine2WidgetLine(endLine) == -1)
                return;
        } catch (BadLocationException e) {
            return;
        }

        offset= widgetRange.getOffset();
        length= widgetRange.getLength();

    } else {
        IRegion region= fSourceViewer.getVisibleRegion();
        if (region.getOffset() > offset || region.getOffset() + region.getLength() < offset + length)
            return;
        offset -= region.getOffset();
    }
    
    boolean digraph = ((fMatchFlags & PairedBracketsMatcher.MATCH_FLAG_DIGRAPH) != 0);
    int cx = digraph ? 1 : 0;

    if (ICharacterPairMatcher.RIGHT == fAnchor)
        draw(gc, offset + length -1 - cx, offset, cx);
    else
        draw(gc, offset, offset + length -1 - cx, cx);
}
 
Example #7
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 #8
Source File: StyledTextXtextAdapter.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected ICharacterPairMatcher getCharacterPairMatcher() {
	return this.characterPairMatcher;
}
 
Example #9
Source File: CharacterPairMatcher.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private IRegion performMatch(IDocument doc, int caretOffset) throws BadLocationException
{
	int charOffset = Math.max(caretOffset - 1, 0);
	char prevChar = doc.getChar(charOffset);
	if (!fPairs.contains(prevChar))
	{
		// Now try to right of caret
		charOffset = caretOffset;
		caretOffset += 1;
		if (charOffset >= doc.getLength())
		{
			return null;
		}
		prevChar = doc.getChar(charOffset);
		if (!fPairs.contains(prevChar))
		{
			return null;
		}
	}

	ITypedRegion partition = getPartition(doc, charOffset);
	// FIXME if we're inside a string or comment, we should limit our search to just this particular partition!
	// Drop out if the char is inside a comment
	if (isComment(doc, partition))
	{
		return null;
	}

	boolean isForward = fPairs.isStartCharacter(prevChar);
	String contentType = partition.getType();
	if (fPairs.isAmbiguous(prevChar))
	{
		// If this is common start tag, look forward, if common end tag look backwards!
		if (CompositePartitionScanner.START_SWITCH_TAG.equals(contentType))
		{
			isForward = true;
		}
		else if (CompositePartitionScanner.END_SWITCH_TAG.equals(contentType))
		{
			isForward = false;
		}
		else
		{
			// Need to look at partition transition to tell if we're at end or beginning!
			String partitionAhead = TextUtilities.getContentType(doc, fPartitioning, charOffset + 1, false);
			String partitionBehind = TextUtilities.getContentType(doc, fPartitioning, charOffset - 1, false);
			if (contentType.equals(partitionBehind) && !contentType.equals(partitionAhead))
			{
				// End because we're transitioning out of a partition on this character
				isForward = false;
			}
			else if (isUnclosedPair(prevChar, doc, charOffset))
			{
				isForward = false;
			}
		}
	}
	fAnchor = isForward ? ICharacterPairMatcher.LEFT : ICharacterPairMatcher.RIGHT;
	int searchStartPosition = isForward ? charOffset + 1 : charOffset - 1;
	char endChar = fPairs.getMatching(prevChar);

	int endOffset = -1;
	if (isForward)
	{
		endOffset = searchForward(doc, searchStartPosition, prevChar, endChar, contentType);
	}
	else
	{
		endOffset = searchBackwards(doc, searchStartPosition, prevChar, endChar, contentType);
	}

	if (endOffset == -1)
	{
		return null;
	}
	final int adjustedOffset = isForward ? charOffset : caretOffset;
	final int adjustedEndOffset = isForward ? endOffset + 1 : endOffset;
	if (adjustedEndOffset == adjustedOffset)
	{
		return null;
	}
	return new Region(Math.min(adjustedOffset, adjustedEndOffset), Math.abs(adjustedEndOffset - adjustedOffset));
}
 
Example #10
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 #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);
}