Search in sources :

Example 21 with SemanticTokens

use of org.eclipse.lsp4j.SemanticTokens in project magik-tools by StevenLooman.

the class SemanticTokenProviderTest method testMethodAndSlot.

@Test
void testMethodAndSlot() {
    final String code = "_method aaa.bbb .slot _endmethod";
    final SemanticTokens semanticTokens = this.getSemanticTokens(code);
    assertThat(semanticTokens.getData()).containsExactly(0, 0, "_method".length(), SemanticToken.Type.KEYWORD.getTokenType(), 0, 0, 8, "aaa".length(), SemanticToken.Type.CLASS.getTokenType(), 0, 0, 4, "bbb".length(), SemanticToken.Type.METHOD.getTokenType(), 0, 0, 5, "slot".length(), SemanticToken.Type.PROPERTY.getTokenType(), 0, 0, 5, "_endmethod".length(), SemanticToken.Type.KEYWORD.getTokenType(), 0);
}
Also used : SemanticTokens(org.eclipse.lsp4j.SemanticTokens) Test(org.junit.jupiter.api.Test)

Example 22 with SemanticTokens

use of org.eclipse.lsp4j.SemanticTokens in project magik-tools by StevenLooman.

the class SemanticTokenProviderTest method testString.

@Test
void testString() {
    final String code = "\"string\"";
    final SemanticTokens semanticTokens = this.getSemanticTokens(code);
    assertThat(semanticTokens.getData()).containsExactly(0, 0, "\"string\"".length(), SemanticToken.Type.STRING.getTokenType(), 0);
}
Also used : SemanticTokens(org.eclipse.lsp4j.SemanticTokens) Test(org.junit.jupiter.api.Test)

Example 23 with SemanticTokens

use of org.eclipse.lsp4j.SemanticTokens in project magik-tools by StevenLooman.

the class SemanticTokenProviderTest method testProcedureInvocation.

@Test
void testProcedureInvocation() {
    final String code = "a()";
    final SemanticTokens semanticTokens = this.getSemanticTokens(code);
    assertThat(semanticTokens.getData()).containsExactly(0, 0, "a".length(), SemanticToken.Type.FUNCTION.getTokenType(), // TODO: Why no varGlobalModifier?
    0);
}
Also used : SemanticTokens(org.eclipse.lsp4j.SemanticTokens) Test(org.junit.jupiter.api.Test)

Example 24 with SemanticTokens

use of org.eclipse.lsp4j.SemanticTokens in project magik-tools by StevenLooman.

the class SemanticTokenProviderTest method testMethodSyntaxError.

@Test
void testMethodSyntaxError() {
    final String code = "_method a.b\n_a\n_endmethod";
    final SemanticTokens semanticTokens = this.getSemanticTokens(code);
    assertThat(semanticTokens.getData()).containsExactly(0, 0, "_method".length(), SemanticToken.Type.KEYWORD.getTokenType(), 0, 2, 0, "_endmethod".length(), SemanticToken.Type.KEYWORD.getTokenType(), 0);
}
Also used : SemanticTokens(org.eclipse.lsp4j.SemanticTokens) Test(org.junit.jupiter.api.Test)

Example 25 with SemanticTokens

use of org.eclipse.lsp4j.SemanticTokens in project redmatch by aehrc.

the class SemanticTokeniser method tokenise.

public static SemanticTokens tokenise(String text) {
    // First we need to get the tokens from the lexer
    List<SemanticToken> tokens = new ArrayList<>();
    final Lexer lexer = new RedmatchLexer(CharStreams.fromString(text));
    for (Token tok : lexer.getAllTokens()) {
        // account for 0-index differences
        int line = tok.getLine() - 1;
        int startChar = tok.getCharPositionInLine();
        int length = tok.getText().length();
        int tokenType = mapTokenType(tok.getType());
        if (tokenType != -1) {
            tokens.add(new SemanticToken(line, startChar, length, tokenType));
        }
    }
    // { deltaLine: 2, deltaStartChar: 5, length: 3, tokenType: 0, tokenModifiers: 3 },
    if (!tokens.isEmpty()) {
        tokens.get(0).deltaLine = tokens.get(0).line;
        tokens.get(0).deltaStartChar = tokens.get(0).startChar;
    }
    for (int i = 1; i < tokens.size(); i++) {
        SemanticToken prevToken = tokens.get(i - 1);
        SemanticToken token = tokens.get(i);
        token.deltaLine = token.line - prevToken.line;
        // Now we need to "deltify" the current token
        if (prevToken.line == token.line) {
            // They are on the same line, so we need to make the start token relative
            token.deltaStartChar = token.startChar - prevToken.startChar;
        } else {
            // There was a change of line so the first token delta start char is the same as the start char
            token.deltaStartChar = token.startChar;
        }
    }
    log.info("Encoding " + tokens.size() + " semantic tokens");
    // Finally, we transform them into the format expected by the language server protocol
    List<Integer> encodedTokens = new ArrayList<>();
    for (SemanticToken token : tokens) {
        encodedTokens.add(token.deltaLine);
        encodedTokens.add(token.deltaStartChar);
        encodedTokens.add(token.length);
        encodedTokens.add(token.tokenType);
        // No modifiers for Redmatch
        encodedTokens.add(0);
    }
    return new SemanticTokens(encodedTokens);
}
Also used : RedmatchLexer(au.csiro.redmatch.grammar.RedmatchLexer) Lexer(org.antlr.v4.runtime.Lexer) RedmatchLexer(au.csiro.redmatch.grammar.RedmatchLexer) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) SemanticTokens(org.eclipse.lsp4j.SemanticTokens)

Aggregations

SemanticTokens (org.eclipse.lsp4j.SemanticTokens)27 Test (org.junit.jupiter.api.Test)21 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 SemanticTokensParams (org.eclipse.lsp4j.SemanticTokensParams)2 SemanticTokensWithRegistrationOptions (org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 RedmatchLexer (au.csiro.redmatch.grammar.RedmatchLexer)1 JsonArray (com.google.gson.JsonArray)1 AstNode (com.sonar.sslr.api.AstNode)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1