diff --git a/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMTextDocumentService.java b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMTextDocumentService.java index 995164b..adfcb25 100644 --- a/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMTextDocumentService.java +++ b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMTextDocumentService.java @@ -10,12 +10,11 @@ import org.nittalab.dtram.languageserver.utils.SemanticAnalyzer; import org.nittalab.dtram.languageserver.utils.Tokenizer; -import java.io.BufferedReader; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -26,12 +25,20 @@ */ private List supportedTokenTypes = new ArrayList<>(); + /** + * Current source code on the editor. + */ + private String sourceText = null; + + @Override public void didOpen(DidOpenTextDocumentParams params) { + sourceText = params.getTextDocument().getText(); } @Override public void didChange(DidChangeTextDocumentParams params) { + sourceText = params.getContentChanges().getFirst().getText(); } @Override @@ -40,6 +47,7 @@ @Override public void didSave(DidSaveTextDocumentParams params) { + sourceText = params.getText(); } @Override @@ -57,58 +65,55 @@ @Override public CompletableFuture semanticTokensFull(SemanticTokensParams params) { - return CompletableFutures.computeAsync(checker -> { - /* We don't have to do unnecessary jobs */ - if (checker.isCanceled()) { - return new SemanticTokens(); - } - - /* Analyze and get semantic tokens */ - SemanticTokens semanticTokens = new SemanticTokens(); - - Path filePath = Paths.get(URI.create(params.getTextDocument().getUri())); - try (BufferedReader br = Files.newBufferedReader(filePath)) { - List tokens = SemanticAnalyzer.analyze(Tokenizer.tokenize(br)); - - List tokenData = new ArrayList<>(); - int prevLine = 0; - int columnOffset = 0; - for (Token token : tokens) { - if (token instanceof SemanticToken semanticToken) { - int tokenType = supportedTokenTypes.indexOf(semanticToken.getSemanticType()); - - /* Skip the token if the token type is not supported by the client. */ - if (tokenType == -1) { - continue; - } - - /* Prepare data of the semantic token */ - int tokenLine = semanticToken.getStartPos().getLine() - 1; - int tokenColumn = semanticToken.getStartPos().getColumn() - 1; - if (tokenLine == prevLine) { - tokenLine -= prevLine; - tokenColumn -= columnOffset; - } else { - tokenLine -= prevLine; - prevLine = semanticToken.getStartPos().getLine() - 1; - } - tokenData.add(tokenLine); // line - tokenData.add(tokenColumn); // deltaStartChar - tokenData.add(semanticToken.getText().length()); // length - tokenData.add(tokenType); // tokenType - tokenData.add(0); // tokenModifiers - - columnOffset = semanticToken.getStartPos().getColumn() - 1; - } - } - semanticTokens.setData(tokenData); - } catch (IOException e) { + return CompletableFuture.supplyAsync(() -> { + try { + Path path = Path.of(new URI(params.getTextDocument().getUri())); + sourceText = Files.readString(path); + // Send results to the client + return updateSemanticTokens(sourceText, supportedTokenTypes); + } catch (IOException | URISyntaxException e) { throw new RuntimeException(e); } - return semanticTokens; // Send result to the client }); } + private SemanticTokens updateSemanticTokens(String sourceText, List tokenTypes) throws IOException { + List tokenData = new ArrayList<>(); + + List tokens = SemanticAnalyzer.analyze(Tokenizer.tokenize(sourceText)); + int prevLine = 0; + int columnOffset = 0; + for (Token token : tokens) { + if (token instanceof SemanticToken semanticToken) { + int tokenType = tokenTypes.indexOf(semanticToken.getSemanticType()); + + /* Skip the token if the token type is not supported by the client. */ + if (tokenType == -1) { + continue; + } + + /* Prepare data of the semantic token */ + int tokenLine = semanticToken.getStartPos().getLine() - 1; + int tokenColumn = semanticToken.getStartPos().getColumn() - 1; + if (tokenLine == prevLine) { + tokenLine -= prevLine; + tokenColumn -= columnOffset; + } else { + tokenLine -= prevLine; + prevLine = semanticToken.getStartPos().getLine() - 1; + } + tokenData.add(tokenLine); // line + tokenData.add(tokenColumn); // deltaStartChar + tokenData.add(semanticToken.getText().length()); // length + tokenData.add(tokenType); // tokenType + tokenData.add(0); // tokenModifiers + + columnOffset = semanticToken.getStartPos().getColumn() - 1; + } + } + return new SemanticTokens(tokenData); + } + /** * Updates the supported token types by the client. *