Search in sources :

Example 1 with ILexerTokenRegion

use of org.eclipse.xtext.ui.editor.model.ILexerTokenRegion in project xtext-eclipse by eclipse.

the class DocumentTokenSourceTest method testConcurrentModification.

@Test
public void testConcurrentModification() throws Exception {
    document.set("foo bar");
    Iterator<ILexerTokenRegion> iterator = tokenSource.getTokenInfos().iterator();
    ILexerTokenRegion region = iterator.next();
    region = iterator.next();
    document.set("barfoo");
    region = iterator.next();
    assertEquals(3, region.getLength());
}
Also used : ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) Test(org.junit.Test)

Example 2 with ILexerTokenRegion

use of org.eclipse.xtext.ui.editor.model.ILexerTokenRegion in project xtext-eclipse by eclipse.

the class PartitionTokenScannerTest method getPartitionTokenScanner.

public PartitionTokenScanner getPartitionTokenScanner(ILexerTokenRegion... tokenDescs) throws Exception {
    final List<ILexerTokenRegion> tokens = Arrays.asList(tokenDescs);
    int offset = 0;
    for (ILexerTokenRegion token : tokens) {
        ((LexerTokenRegion) token).setOffset(offset);
        offset += token.getLength();
    }
    PartitionTokenScanner tokenScanner = new PartitionTokenScanner() {

        @Override
        protected Iterable<ILexerTokenRegion> getTokens(IDocument document) {
            return tokens;
        }

        @Override
        protected boolean shouldMergePartitions(String contentType) {
            return "3".equals(contentType);
        }
    };
    tokenScanner.setMapper(new ITokenTypeToPartitionTypeMapper() {

        @Override
        public String[] getSupportedPartitionTypes() {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getPartitionType(int antlrTokenType) {
            return "" + antlrTokenType;
        }
    });
    return tokenScanner;
}
Also used : ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) PartitionTokenScanner(org.eclipse.xtext.ui.editor.model.PartitionTokenScanner) ITokenTypeToPartitionTypeMapper(org.eclipse.xtext.ui.editor.model.ITokenTypeToPartitionTypeMapper) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) IDocument(org.eclipse.jface.text.IDocument)

Example 3 with ILexerTokenRegion

use of org.eclipse.xtext.ui.editor.model.ILexerTokenRegion in project xtext-eclipse by eclipse.

the class PresentationDamager method computeInterSection.

/**
 * @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;
}
Also used : Region(org.eclipse.jface.text.Region) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IRegion(org.eclipse.jface.text.IRegion) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion)

Example 4 with ILexerTokenRegion

use of org.eclipse.xtext.ui.editor.model.ILexerTokenRegion in project statecharts by Yakindu.

the class StyleRanges method getRanges.

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;
}
Also used : DocumentTokenSource(org.eclipse.xtext.ui.editor.model.DocumentTokenSource) TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange) ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion) DocumentEvent(org.eclipse.jface.text.DocumentEvent)

Example 5 with ILexerTokenRegion

use of org.eclipse.xtext.ui.editor.model.ILexerTokenRegion in project xtext-xtend by eclipse.

the class RichStringAwareTokenScanner method nextToken.

@Override
public IToken nextToken() {
    if (currentRichTextToken != null) {
        if (currentRichTextToken.hasNext())
            return currentRichTextToken.nextToken();
        else
            currentRichTextToken = null;
    }
    if (!getIterator().hasNext())
        return Token.EOF;
    ILexerTokenRegion next = getIterator().next();
    int tokenType = next.getLexerTokenType();
    if (tokenType >= 0 && allTokenTypesAsString[tokenType] != null) {
        currentRichTextToken = createRichTextToken(allTokenTypesAsString[tokenType], next);
        return currentRichTextToken.nextToken();
    } else {
        setCurrentToken(next);
        return createToken(next);
    }
}
Also used : ILexerTokenRegion(org.eclipse.xtext.ui.editor.model.ILexerTokenRegion)

Aggregations

ILexerTokenRegion (org.eclipse.xtext.ui.editor.model.ILexerTokenRegion)9 IRegion (org.eclipse.jface.text.IRegion)3 IDocument (org.eclipse.jface.text.IDocument)2 Region (org.eclipse.jface.text.Region)2 Test (org.junit.Test)2 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 TextAttribute (org.eclipse.jface.text.TextAttribute)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 DocumentTokenSource (org.eclipse.xtext.ui.editor.model.DocumentTokenSource)1 ITokenTypeToPartitionTypeMapper (org.eclipse.xtext.ui.editor.model.ITokenTypeToPartitionTypeMapper)1 PartitionTokenScanner (org.eclipse.xtext.ui.editor.model.PartitionTokenScanner)1 XtextDocument (org.eclipse.xtext.ui.editor.model.XtextDocument)1 TokenScanner (org.eclipse.xtext.ui.editor.syntaxcoloring.TokenScanner)1