/** * 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; }
@Override public IToken nextToken() { if (currentTemplateTextToken != null) { if (currentTemplateTextToken.hasNext()) return currentTemplateTextToken.nextToken(); else currentTemplateTextToken = null; } if (!getIterator().hasNext()) return Token.EOF; ILexerTokenRegion next = getIterator().next(); int tokenType = next.getLexerTokenType(); switch (tokenType) { case RULE_TEMPLATE_HEAD: case RULE_TEMPLATE_MIDDLE: { currentTemplateTextToken = createTemplateToken(tokenType, next); return currentTemplateTextToken.nextToken(); } default: setCurrentToken(next); return createToken(next); } }
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 ranges; }
@Override protected IRegion findWord(IDocument document, int offset) { if (document instanceof XtextDocument) { Iterator<ILexerTokenRegion> tokenIterator = ((XtextDocument) document).getTokens().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); }
/** * @return the common region of the given partition and the changed region in the DocumentEvent based on the underlying tokens. */ protected IRegion computeInterSection(ITypedRegion partition, DocumentEvent e, XtextDocument document) { Iterable<ILexerTokenRegion> tokensInPartition = Iterables.filter(document.getTokens(),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; }
public ILexerTokenRegion next() { if (!computedHasNext) { // compute hasNext on demand to make sure the internal state is valid hasNext(); } if (!hasNext) { throw new NoSuchElementException("You should check for #hasNext prior to calling #next"); } ILexerTokenRegion result = current; current = null; computedHasNext = false; hasNext = false; return result; }
public void setRange(IDocument document, final int offset, final int length) { this.regionOffset = offset; overlapFilter = Regions.overlaps(offset, length); Iterable<ILexerTokenRegion> newIterable = getTokens(document); if (delegateIterable != null) { if (!delegateIterable.equals(newIterable)) { current = null; } } if (current == null) { delegate = newIterable.iterator(); delegateIterable = newIterable; computedHasNext = false; hasNext = false; } else { if (current.getOffset() <= offset && current.getOffset() + current.getLength() > offset) { // offset is inside of current - return current as next computedHasNext = true; hasNext = true; } else { // restart - use a new delegate computedHasNext = false; hasNext = false; delegate = newIterable.iterator(); delegateIterable = newIterable; } } }
protected Iterable<ILexerTokenRegion> getTokens(IDocument document) { XtextDocument doc = (XtextDocument) document; return doc.getTokens(); }
protected IToken createToken(ILexerTokenRegion currentToken) { String id = tokenIdMapper.getId(currentToken.getLexerTokenType()); Token token = new Token(getAttribute(id)); return token; }
protected Iterator<ILexerTokenRegion> getIterator() { return reentrantIterator; }
protected ILexerTokenRegion getCurrentToken() { return currentToken; }
protected void setCurrentToken(ILexerTokenRegion currentToken) { this.currentToken = currentToken; }