Java Code Examples for org.eclipse.xtext.ui.editor.model.ILexerTokenRegion#getOffset()

The following examples show how to use org.eclipse.xtext.ui.editor.model.ILexerTokenRegion#getOffset() . 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: TemplateAwareTokenScanner.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new template token for the given token id. A template token is in fact a list of tokens for the
 * colorer, e.g a single terminal on the grammar level may yield various tokens for the syntax coloring.
 */
protected TemplateToken createTemplateToken(int id, ILexerTokenRegion token) {
	int tokenOffset = token.getOffset();
	int tokenLength = token.getLength();
	TemplateToken result = new TemplateToken();
	switch (id) {
	case RULE_TEMPLATE_HEAD:
		result.delimiter = false; // ranges defined by 'offsets' and 'lengths' do *not* start with a delimiter
		result.offsets = new int[] { tokenOffset, tokenOffset + tokenLength - 2 };
		result.lengths = new int[] { tokenLength - 2, 2 };
		break;
	case RULE_TEMPLATE_MIDDLE:
		if (tokenLength == 2) {
			result.delimiter = true; // ranges defined by 'offsets' and 'lengths' start with a delimiter
			result.offsets = new int[] { tokenOffset };
			result.lengths = new int[] { tokenLength };
		} else {
			result.delimiter = false; // ranges defined by 'offsets' and 'lengths' do *not* start with a delimiter
			result.offsets = new int[] { tokenOffset, tokenOffset + tokenLength - 2 };
			result.lengths = new int[] { tokenLength - 2, 2 };
		}
	}
	return result;
}
 
Example 2
Source File: StyleRanges.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
public List<StyleRange> getRanges(String expression) {
	final List<StyleRange> ranges = Lists.newArrayList();
	DocumentEvent event = new DocumentEvent();
	event.fDocument = new DummyDocument(expression);
	DocumentTokenSource tokenSource = tokenSourceProvider.get();
	tokenSource.updateStructure(event);
	Iterator<ILexerTokenRegion> iterator = tokenSource.getTokenInfos().iterator();
	while (iterator.hasNext()) {
		ILexerTokenRegion next = iterator.next();
		TextAttribute attribute = attributeProvider.getAttribute(tokenTypeMapper.getId(next.getLexerTokenType()));
		StyleRange range = new StyleRange(next.getOffset(), next.getLength(), attribute.getForeground(),
				attribute.getBackground());
		range.font = attribute.getFont();
		range.fontStyle = attribute.getStyle();
		ranges.add(range);
	}
	return merge(ranges);
}
 
Example 3
Source File: LexerTokenAndCharacterPairAwareStrategy.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected IRegion findWord(IDocument document, int offset) {
	Iterable<ILexerTokenRegion> tokenIterable = tokenSourceAccess.getTokens(document, true);
	if (tokenIterable == null) {
		return super.findWord(document, offset);
	}
	Iterator<ILexerTokenRegion> tokenIterator = tokenIterable.iterator();
	ILexerTokenRegion leadingToken = null;
	ILexerTokenRegion trailingToken = null;
	while(tokenIterator.hasNext()) {
		ILexerTokenRegion token = tokenIterator.next();
		if (token.getOffset() <= offset && token.getOffset() + token.getLength() >= offset) {
			if (leadingToken != null)
				trailingToken = token;
			else
				leadingToken = token;
		}
		if (token.getOffset() > offset)
			break;
	}
	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(document, offset);
}
 
Example 4
Source File: PresentationDamager.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private IRegion doComputeIntersection(ITypedRegion partition, DocumentEvent e, IDocument document) {
	Iterable<ILexerTokenRegion> tokensInPartition = Iterables.filter(tokenSourceAccess.getTokens(document, false), Regions.overlaps(partition.getOffset(), partition.getLength()));
	Iterator<ILexerTokenRegion> tokens = Iterables.filter(tokensInPartition, Regions.overlaps(e.getOffset(), e.getLength())).iterator();
	if (tokens.hasNext()) {
		ILexerTokenRegion first = tokens.next();
		ILexerTokenRegion last = first;
		while(tokens.hasNext())
			last = tokens.next();
		return new Region(first.getOffset(), last.getOffset()+last.getLength() -first.getOffset());
	}
	// this shouldn't happen, but just in case return the whole partition
	return partition;
}
 
Example 5
Source File: RichStringAwareTokenScanner.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected RichTextToken createRichTextToken(String id, ILexerTokenRegion token) {
	int tokenOffset = token.getOffset();
	int tokenLength = token.getLength();
	RichTextToken result = new RichTextToken();
	if ("RULE_RICH_TEXT_START".equals(id)) {
		result.delimiter = false;
		result.offsets = new int[] { tokenOffset, tokenOffset + tokenLength - 1 };
		result.lengths = new int[] { tokenLength - 1, 1 };
	} else if ("RULE_RICH_TEXT_END".equals(id)) {
		result.delimiter = true;
		result.offsets = new int[] { tokenOffset, tokenOffset + 1 };
		result.lengths = new int[] { 1, tokenLength - 1 };
	} else if ("RULE_RICH_TEXT_INBETWEEN".equals(id)) {
		result.delimiter = true;
		if (tokenLength == 2) {
			result.offsets = new int[] { tokenOffset };
			result.lengths = new int[] { tokenLength };
		} else {
			result.offsets = new int[] { tokenOffset, tokenOffset + 1, tokenOffset + tokenLength - 1 };
			result.lengths = new int[] { 1, tokenLength - 2, 1 };
		}
	} else if ("RULE_COMMENT_RICH_TEXT_END".equals(id)) {
		result.delimiter = true;
		result.offsets = new int[] { tokenOffset, tokenOffset + 2 };
		result.lengths = new int[] { 2, tokenLength - 2 };
	} else if ("RULE_COMMENT_RICH_TEXT_INBETWEEN".equals(id)) {
		result.delimiter = true;
		result.offsets = new int[] { tokenOffset, tokenOffset + 2, tokenOffset + tokenLength - 1 };
		result.lengths = new int[] { 2, tokenLength - 3, 1 };
	} else 
		throw new IllegalArgumentException(id);
	return result;
}