@Test public void keyword() { Token token1 = new TokenImpl(range(1, 1, 1, 3), "ab", Token.Type.KEYWORD); Token token2 = new TokenImpl(range(1, 4, 1, 6), "cd", Token.Type.KEYWORD); Token token3 = new TokenImpl(range(1, 6, 1, 7), "{", Token.Type.OTHER); Token token4 = new TokenImpl(range(1, 7, 1, 8), "ef", Token.Type.OTHER); TreeMetaDataProvider provider = new TreeMetaDataProvider(emptyList(), Arrays.asList(token1, token2, token3, token4)); assertThat(provider.keyword(range(1, 3, 1, 7))).isEqualTo(token2); assertThat(provider.keyword(range(1, 3, 1, 8))).isEqualTo(token2); assertThatThrownBy(() -> provider.keyword(range(1, 3, 1, 4))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Cannot find single keyword in TextRange[1, 3, 1, 4]"); assertThatThrownBy(() -> provider.keyword(range(1, 1, 1, 7))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Cannot find single keyword in TextRange[1, 1, 1, 7]"); }
public Token keyword(TextRange textRange) { List<Token> keywordsInRange = getElementsInRange(sortedTokens, textRange).stream() .filter(t -> t.type() == Token.Type.KEYWORD) .collect(Collectors.toList()); if (keywordsInRange.size() != 1) { throw new IllegalArgumentException("Cannot find single keyword in " + textRange); } return keywordsInRange.get(0); }
TreeMetaDataProvider { public Token keyword(TextRange textRange) { List<Token> keywordsInRange = getElementsInRange(sortedTokens, textRange).stream() .filter(t -> t.type() == Token.Type.KEYWORD) .collect(Collectors.toList()); if (keywordsInRange.size() != 1) { throw new IllegalArgumentException("Cannot find single keyword in " + textRange); } return keywordsInRange.get(0); } }
TreeMetaDataProvider { public Token keyword(TextRange textRange) { List<Token> keywordsInRange = getElementsInRange(sortedTokens, textRange).stream() .filter(t -> t.type() == Token.Type.KEYWORD) .collect(Collectors.toList()); if (keywordsInRange.size() != 1) { throw new IllegalArgumentException("Cannot find single keyword in " + textRange); } return keywordsInRange.get(0); } TreeMetaDataProvider(List<Comment> comments, List<Token> tokens); }
TreeMetaDataProvider { public Token keyword(TextRange textRange) { List<Token> keywordsInRange = getElementsInRange(sortedTokens, textRange).stream() .filter(t -> t.type() == Token.Type.KEYWORD) .collect(Collectors.toList()); if (keywordsInRange.size() != 1) { throw new IllegalArgumentException("Cannot find single keyword in " + textRange); } return keywordsInRange.get(0); } TreeMetaDataProvider(List<Comment> comments, List<Token> tokens); List<Comment> allComments(); List<Token> allTokens(); int indexOfFirstToken(TextRange textRange); Optional<Token> firstToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange, String expectedTokenValue); Optional<Token> previousToken(TextRange textRange, Predicate<Token> expectedConditionToMatch); void updateTokenType(Token token, Token.Type newType); Token keyword(TextRange textRange); TreeMetaData metaData(TextRange textRange); }
TreeMetaDataProvider { public Token keyword(TextRange textRange) { List<Token> keywordsInRange = getElementsInRange(sortedTokens, textRange).stream() .filter(t -> t.type() == Token.Type.KEYWORD) .collect(Collectors.toList()); if (keywordsInRange.size() != 1) { throw new IllegalArgumentException("Cannot find single keyword in " + textRange); } return keywordsInRange.get(0); } TreeMetaDataProvider(List<Comment> comments, List<Token> tokens); List<Comment> allComments(); List<Token> allTokens(); int indexOfFirstToken(TextRange textRange); Optional<Token> firstToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange, String expectedTokenValue); Optional<Token> previousToken(TextRange textRange, Predicate<Token> expectedConditionToMatch); void updateTokenType(Token token, Token.Type newType); Token keyword(TextRange textRange); TreeMetaData metaData(TextRange textRange); static final Comparator<HasTextRange> COMPARATOR; }