Search in sources :

Example 1 with MarkupPartition

use of org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner.MarkupPartition in project mylyn.docs by eclipse.

the class MarkupTokenScanner method setRange.

public void setRange(IDocument document, int offset, int length) {
    IDocumentPartitioner partitioner = document.getDocumentPartitioner();
    List<Token> tokens = null;
    if (partitioner instanceof FastMarkupPartitioner) {
        FastMarkupPartitioner fastMarkupPartitioner = (FastMarkupPartitioner) partitioner;
        ITypedRegion[] partitioning = partitioner.computePartitioning(offset, length);
        if (partitioning != null) {
            tokens = new ArrayList<>();
            ITypedRegion[] partitions = ((FastMarkupPartitioner) partitioner).getScanner().computePartitions(document, offset, length);
            int lastEnd = offset;
            Token defaultToken;
            {
                StyleRange styleRange = styleManager.createStyleRange(defaultState, 0, 1);
                TextAttribute textAttribute = createTextAttribute(styleRange);
                defaultToken = new Token(defaultState, textAttribute, offset, length);
            }
            if (partitions != null) {
                for (ITypedRegion region : partitions) {
                    if (region.getOffset() >= (offset + length)) {
                        break;
                    }
                    if ((region.getOffset() + region.getLength()) < offset) {
                        continue;
                    }
                    if (region instanceof MarkupPartition) {
                        MarkupPartition partition = (MarkupPartition) region;
                        if (lastEnd < partition.getOffset()) {
                            Token blockBridgeToken = new Token(defaultToken.fontState, defaultToken.getData(), lastEnd, partition.getOffset() - lastEnd);
                            addToken(tokens, blockBridgeToken);
                        }
                        // a token that spans the whole block
                        Token blockToken = createToken(partition);
                        if (blockToken == null) {
                            blockToken = defaultToken;
                        }
                        if (!partition.getBlock().isSpansComputed()) {
                            fastMarkupPartitioner.reparse(document, partition.getBlock());
                        }
                        List<Span> spans = partition.getSpans();
                        if (spans != null) {
                            for (Span span : spans) {
                                if (span.getOffset() < lastEnd) {
                                    continue;
                                }
                                Token spanToken = createToken(blockToken.getFontState(), span);
                                if (spanToken != null) {
                                    int blockTokenStartOffset = lastEnd < offset ? offset : lastEnd;
                                    if (blockTokenStartOffset < spanToken.getOffset()) {
                                        int blockTokenLength = spanToken.getOffset() - blockTokenStartOffset;
                                        final Token blockBridgeToken = new Token(blockToken.fontState, blockToken.getData(), blockTokenStartOffset, blockTokenLength);
                                        addToken(tokens, blockBridgeToken);
                                    }
                                    Token[] spanTokens = null;
                                    if (!span.getChildren().isEmpty()) {
                                        spanTokens = splitSpan(spanToken, span, defaultToken);
                                    }
                                    if (spanTokens != null) {
                                        for (Token spanSplitToken : spanTokens) {
                                            addToken(tokens, spanSplitToken);
                                        }
                                    } else {
                                        addToken(tokens, spanToken);
                                    }
                                    lastEnd = spanToken.offset + spanToken.length;
                                    if (lastEnd > partition.getOffset() + partition.getLength()) {
                                        throw new IllegalStateException();
                                    }
                                }
                            }
                        }
                        final int partitionEnd = partition.getOffset() + partition.getLength();
                        if (lastEnd < partitionEnd) {
                            final int realLastEnd = Math.max(lastEnd, partition.getOffset());
                            int diff = (partitionEnd) - realLastEnd;
                            if (diff > 0) {
                                int blockTokenStartOffset = realLastEnd;
                                int blockTokenLength = diff;
                                final Token blockBridgeToken = new Token(blockToken.fontState, blockToken.getData(), blockTokenStartOffset, blockTokenLength);
                                addToken(tokens, blockBridgeToken);
                                lastEnd = blockTokenStartOffset + blockTokenLength;
                                if (lastEnd > partition.getOffset() + partition.getLength()) {
                                    throw new IllegalStateException();
                                }
                            }
                        }
                    }
                }
            }
            if (lastEnd < (offset + length)) {
                addToken(tokens, new Token(defaultToken.fontState, defaultToken.getData(), lastEnd, length - (lastEnd - offset)));
            }
        }
    }
    currentToken = null;
    if (tokens == null || tokens.isEmpty()) {
        tokenIt = null;
    } else {
        Iterator<Token> it = tokens.iterator();
        while (it.hasNext()) {
            Token next = it.next();
            if (next.getOffset() < offset) {
                it.remove();
            } else if (next.getOffset() + next.getLength() > (offset + length)) {
                it.remove();
            }
        }
        tokenIt = tokens.iterator();
    }
}
Also used : TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange) IToken(org.eclipse.jface.text.rules.IToken) MarkupPartition(org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner.MarkupPartition) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Aggregations

IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 TextAttribute (org.eclipse.jface.text.TextAttribute)1 IToken (org.eclipse.jface.text.rules.IToken)1 MarkupPartition (org.eclipse.mylyn.internal.wikitext.ui.editor.syntax.FastMarkupPartitioner.MarkupPartition)1 StyleRange (org.eclipse.swt.custom.StyleRange)1