Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG ( and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at /
 package org.eclipse.xtext.ui.editor.doubleClicking;
import  org.eclipse.jface.text.BadLocationException;
import  org.eclipse.jface.text.IDocument;
import  org.eclipse.jface.text.IRegion;
import  org.eclipse.jface.text.Region;
import  org.eclipse.jface.text.source.ICharacterPairMatcher;
This implementation uses the tokens of an Xtext document to identify words in the document. Thereby it recognizes keywords that consist only of characters that are no letters, e.g. += or ?=. Furthermore it will select the contents of a brace pair if the opening or closing peer is clicked. Tokens with a length of one character have a lower priority in case a token boundary is selected.

Sebastian Zarnekow - Initial contribution and API
	private ICharacterPairMatcher characterPairMatcher;
	protected IRegion findWord(IDocument documentint offset) {
		if (document instanceof XtextDocument) {
			Iterator<ILexerTokenRegiontokenIterator = ((XtextDocumentdocument).getTokens().iterator();
			ILexerTokenRegion leadingToken = null;
			ILexerTokenRegion trailingToken = null;
			while(tokenIterator.hasNext()) {
				ILexerTokenRegion token =;
				if (token.getOffset() <= offset && token.getOffset() + token.getLength() >= offset) {
					if (leadingToken != null)
						trailingToken = token;
						leadingToken = token;
				if (token.getOffset() > offset)
			if (leadingToken != null) {
				try {
					if (leadingToken.getLength() > 1 && (trailingToken == null || !Character.isLetter(document.getChar(trailingToken.getOffset())))) {
						return new Region(leadingToken.getOffset(), leadingToken.getLength());
else if (trailingToken != null) {
						return new Region(trailingToken.getOffset(), trailingToken.getLength());
catch(BadLocationException ignore) {}
		return super.findWord(documentoffset);
	protected IRegion findExtendedDoubleClickSelection(IDocument documentint offset) {
		IRegion match.match(documentoffset);
		if (match != null && match.getLength() >= 2)
			return new Region(match.getOffset() + 1, match.getLength() - 2);
		return findWord(documentoffset);
	protected ICharacterPairMatcher getCharacterPairMatcher() {
New to GrepCode? Check out our FAQ X