Search in sources :

Example 1 with ReversedList

use of org.apache.groovy.util.ReversedList in project groovy by apache.

the class SmartDocumentFilter method findTokensToRender.

private List<Token> findTokensToRender(List<Token> tokenList) {
    final Tuple2<Integer, Integer> renderRange = getRenderRange();
    if (null != renderRange) {
        long startLine = lineNumber(renderRange.getV1());
        long stopLine = lineNumber(renderRange.getV2());
        // should never happen
        if (startLine < 0 || stopLine < 0)
            return tokenList;
        return tokenList.stream().filter(e -> e.getLine() >= startLine && PositionConfigureUtils.endPosition(e).getV1() <= stopLine).collect(Collectors.toList());
    }
    List<Token> tmpLatestTokenList = filterNewlines(this.latestTokenList);
    int latestTokenListSize = tmpLatestTokenList.size();
    if (0 == latestTokenListSize) {
        return tokenList;
    }
    List<Token> tmpTokenList = filterNewlines(tokenList);
    int tmpTokenListSize = tmpTokenList.size();
    if (0 == tmpTokenListSize) {
        return tokenList;
    }
    int startTokenIndex = 0;
    int minSize = Math.min(tmpTokenListSize, latestTokenListSize);
    for (int i = 0; i < minSize; i++) {
        Token token = tmpTokenList.get(i);
        Token latestToken = tmpLatestTokenList.get(i);
        if (token.getType() == latestToken.getType() && token.getStartIndex() == latestToken.getStartIndex() && token.getStopIndex() == latestToken.getStopIndex()) {
            continue;
        }
        startTokenIndex = i;
        break;
    }
    List<Token> newTokenList = new ReversedList<>(tmpTokenList);
    List<Token> newLatestTokenList = new ReversedList<>(tmpLatestTokenList);
    int stopTokenIndex = tmpTokenListSize;
    Token lastToken = newTokenList.get(0);
    Token lastLatestToken = newLatestTokenList.get(0);
    for (int i = 0; i < minSize; i++) {
        Token token = newTokenList.get(i);
        Token latestToken = newLatestTokenList.get(i);
        if ((token.getType() == latestToken.getType()) && (token.getStartIndex() - lastToken.getStartIndex()) == (latestToken.getStartIndex() - lastLatestToken.getStartIndex()) && ((token.getStopIndex() - lastToken.getStopIndex()) == (latestToken.getStopIndex() - lastLatestToken.getStopIndex()))) {
            continue;
        }
        stopTokenIndex = tmpTokenListSize - i;
        break;
    }
    if (startTokenIndex == stopTokenIndex) {
        return tmpTokenListSize != latestTokenListSize ? tokenList : Collections.emptyList();
    } else if (startTokenIndex < stopTokenIndex) {
        return tmpTokenList.subList(startTokenIndex, stopTokenIndex);
    }
    // should never reach here. If unexpected error occurred, it's better to render all tokens
    return tokenList;
}
Also used : Color(java.awt.Color) THROWS(org.apache.groovy.parser.antlr4.GroovyLexer.THROWS) Arrays(java.util.Arrays) FloatingPointLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.FloatingPointLiteral) BooleanLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.BooleanLiteral) ABSTRACT(org.apache.groovy.parser.antlr4.GroovyLexer.ABSTRACT) STRICTFP(org.apache.groovy.parser.antlr4.GroovyLexer.STRICTFP) Token(org.antlr.v4.runtime.Token) CONST(org.apache.groovy.parser.antlr4.GroovyLexer.CONST) DEF(org.apache.groovy.parser.antlr4.GroovyLexer.DEF) RETURN(org.apache.groovy.parser.antlr4.GroovyLexer.RETURN) DEFAULT(org.apache.groovy.parser.antlr4.GroovyLexer.DEFAULT) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) EOF(org.apache.groovy.parser.antlr4.GroovyLexer.EOF) IMPLEMENTS(org.apache.groovy.parser.antlr4.GroovyLexer.IMPLEMENTS) CharStreams(org.antlr.v4.runtime.CharStreams) THROW(org.apache.groovy.parser.antlr4.GroovyLexer.THROW) CASE(org.apache.groovy.parser.antlr4.GroovyLexer.CASE) GStringEnd(org.apache.groovy.parser.antlr4.GroovyLexer.GStringEnd) NATIVE(org.apache.groovy.parser.antlr4.GroovyLexer.NATIVE) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) INSTANCEOF(org.apache.groovy.parser.antlr4.GroovyLexer.INSTANCEOF) SEALED(org.apache.groovy.parser.antlr4.GroovyLexer.SEALED) COMMA(org.apache.groovy.parser.antlr4.GroovyLexer.COMMA) AttributeSet(javax.swing.text.AttributeSet) BuiltInPrimitiveType(org.apache.groovy.parser.antlr4.GroovyLexer.BuiltInPrimitiveType) ELSE(org.apache.groovy.parser.antlr4.GroovyLexer.ELSE) NL(org.apache.groovy.parser.antlr4.GroovyLexer.NL) RECORD(org.apache.groovy.parser.antlr4.GroovyLexer.RECORD) PositionConfigureUtils(org.apache.groovy.parser.antlr4.util.PositionConfigureUtils) CONTINUE(org.apache.groovy.parser.antlr4.GroovyLexer.CONTINUE) FINAL(org.apache.groovy.parser.antlr4.GroovyLexer.FINAL) StringLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.StringLiteral) BadLocationException(javax.swing.text.BadLocationException) Collectors(java.util.stream.Collectors) GOTO(org.apache.groovy.parser.antlr4.GroovyLexer.GOTO) GStringBegin(org.apache.groovy.parser.antlr4.GroovyLexer.GStringBegin) VAR(org.apache.groovy.parser.antlr4.GroovyLexer.VAR) WHILE(org.apache.groovy.parser.antlr4.GroovyLexer.WHILE) List(java.util.List) AS(org.apache.groovy.parser.antlr4.GroovyLexer.AS) NEW(org.apache.groovy.parser.antlr4.GroovyLexer.NEW) YIELD(org.apache.groovy.parser.antlr4.GroovyLexer.YIELD) CATCH(org.apache.groovy.parser.antlr4.GroovyLexer.CATCH) SWITCH(org.apache.groovy.parser.antlr4.GroovyLexer.SWITCH) PUBLIC(org.apache.groovy.parser.antlr4.GroovyLexer.PUBLIC) PACKAGE(org.apache.groovy.parser.antlr4.GroovyLexer.PACKAGE) DefaultStyledDocument(javax.swing.text.DefaultStyledDocument) ENUM(org.apache.groovy.parser.antlr4.GroovyLexer.ENUM) FINALLY(org.apache.groovy.parser.antlr4.GroovyLexer.FINALLY) NOT_IN(org.apache.groovy.parser.antlr4.GroovyLexer.NOT_IN) IN(org.apache.groovy.parser.antlr4.GroovyLexer.IN) StyleConstants(javax.swing.text.StyleConstants) GroovyLangLexer(org.apache.groovy.parser.antlr4.GroovyLangLexer) PROTECTED(org.apache.groovy.parser.antlr4.GroovyLexer.PROTECTED) STATIC(org.apache.groovy.parser.antlr4.GroovyLexer.STATIC) FOR(org.apache.groovy.parser.antlr4.GroovyLexer.FOR) VOLATILE(org.apache.groovy.parser.antlr4.GroovyLexer.VOLATILE) IntegerLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.IntegerLiteral) UNEXPECTED_CHAR(org.apache.groovy.parser.antlr4.GroovyLexer.UNEXPECTED_CHAR) NON_SEALED(org.apache.groovy.parser.antlr4.GroovyLexer.NON_SEALED) Tuple2(groovy.lang.Tuple2) StyleContext(javax.swing.text.StyleContext) TRY(org.apache.groovy.parser.antlr4.GroovyLexer.TRY) CharStream(org.antlr.v4.runtime.CharStream) GroovySyntaxError(org.apache.groovy.parser.antlr4.GroovySyntaxError) SUPER(org.apache.groovy.parser.antlr4.GroovyLexer.SUPER) ASSERT(org.apache.groovy.parser.antlr4.GroovyLexer.ASSERT) NullLiteral(org.apache.groovy.parser.antlr4.GroovyLexer.NullLiteral) StringUtils(org.apache.groovy.parser.antlr4.util.StringUtils) GStringPart(org.apache.groovy.parser.antlr4.GroovyLexer.GStringPart) SEMI(org.apache.groovy.parser.antlr4.GroovyLexer.SEMI) SYNCHRONIZED(org.apache.groovy.parser.antlr4.GroovyLexer.SYNCHRONIZED) IF(org.apache.groovy.parser.antlr4.GroovyLexer.IF) PRIVATE(org.apache.groovy.parser.antlr4.GroovyLexer.PRIVATE) BREAK(org.apache.groovy.parser.antlr4.GroovyLexer.BREAK) TRANSIENT(org.apache.groovy.parser.antlr4.GroovyLexer.TRANSIENT) THREADSAFE(org.apache.groovy.parser.antlr4.GroovyLexer.THREADSAFE) LexerNoViableAltException(org.antlr.v4.runtime.LexerNoViableAltException) IMPORT(org.apache.groovy.parser.antlr4.GroovyLexer.IMPORT) IOException(java.io.IOException) DO(org.apache.groovy.parser.antlr4.GroovyLexer.DO) ReversedList(org.apache.groovy.util.ReversedList) THIS(org.apache.groovy.parser.antlr4.GroovyLexer.THIS) DocumentFilter(javax.swing.text.DocumentFilter) StringReader(java.io.StringReader) Style(javax.swing.text.Style) INTERFACE(org.apache.groovy.parser.antlr4.GroovyLexer.INTERFACE) TRAIT(org.apache.groovy.parser.antlr4.GroovyLexer.TRAIT) VOID(org.apache.groovy.parser.antlr4.GroovyLexer.VOID) EXTENDS(org.apache.groovy.parser.antlr4.GroovyLexer.EXTENDS) PERMITS(org.apache.groovy.parser.antlr4.GroovyLexer.PERMITS) Collections(java.util.Collections) CLASS(org.apache.groovy.parser.antlr4.GroovyLexer.CLASS) NOT_INSTANCEOF(org.apache.groovy.parser.antlr4.GroovyLexer.NOT_INSTANCEOF) Token(org.antlr.v4.runtime.Token) ReversedList(org.apache.groovy.util.ReversedList)

Aggregations

Tuple2 (groovy.lang.Tuple2)1 Color (java.awt.Color)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 AttributeSet (javax.swing.text.AttributeSet)1 BadLocationException (javax.swing.text.BadLocationException)1 DefaultStyledDocument (javax.swing.text.DefaultStyledDocument)1 DocumentFilter (javax.swing.text.DocumentFilter)1 Style (javax.swing.text.Style)1 StyleConstants (javax.swing.text.StyleConstants)1 StyleContext (javax.swing.text.StyleContext)1 CharStream (org.antlr.v4.runtime.CharStream)1 CharStreams (org.antlr.v4.runtime.CharStreams)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)1 LexerNoViableAltException (org.antlr.v4.runtime.LexerNoViableAltException)1