diff --git a/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMLanguageServer.java b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMLanguageServer.java index dc95a48..111ad00 100644 --- a/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMLanguageServer.java +++ b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMLanguageServer.java @@ -1,59 +1,44 @@ package org.nittalab.dtram.languageserver; -import org.eclipse.lsp4j.*; +import org.eclipse.lsp4j.InitializeParams; +import org.eclipse.lsp4j.InitializeResult; +import org.eclipse.lsp4j.ServerCapabilities; import org.eclipse.lsp4j.services.*; import java.util.List; import java.util.concurrent.CompletableFuture; -import java.util.logging.Logger; public class DTRAMLanguageServer implements LanguageServer, LanguageClientAware { - private static final Logger logger = Logger.getLogger(DTRAMLanguageServer.class.getName()); - - private final DTRAMWorkspaceService workspaceService; + /** + * Text document service for DTRAM + */ private final DTRAMTextDocumentService textDocumentService; + /** + * The language client that the server connects to. + */ + private static LanguageClient client = null; + public DTRAMLanguageServer() { this.textDocumentService = new DTRAMTextDocumentService(); - this.workspaceService = new DTRAMWorkspaceService(); } @Override public void connect(LanguageClient client) { + setClient(client); } @Override public CompletableFuture initialize(InitializeParams params) { - ClientInfo clientInfo = params.getClientInfo(); - logger.info("Connected to: " + clientInfo.getName() + " " + clientInfo.getVersion()); - - ServerCapabilities capabilities = new ServerCapabilities(); - capabilities.setTextDocumentSync(TextDocumentSyncKind.Full); // ファイルの同期方法を指定(毎回ドキュメント全体を送信してもらう) - capabilities.setPositionEncoding(PositionEncodingKind.UTF16); // 文字列符号化方式を指定 - - /* Tells the client that code completion is available **/ - CompletionOptions completionOptions = new CompletionOptions(); - completionOptions.setTriggerCharacters(List.of(".", " ")); - capabilities.setCompletionProvider(completionOptions); - - /* Fetch all available token types from client and send them to tell the client that the server can analyze the token */ - List availableTokenTypes = params.getCapabilities().getTextDocument().getSemanticTokens().getTokenTypes(); - SemanticTokensWithRegistrationOptions semanticTokensOptions = new SemanticTokensWithRegistrationOptions(); - semanticTokensOptions.setFull(true); - semanticTokensOptions.setRange(false); - semanticTokensOptions.setLegend(new SemanticTokensLegend(availableTokenTypes, List.of())); - capabilities.setSemanticTokensProvider(semanticTokensOptions); - + ServerCapabilities capabilities = new DTRAMServerCapabilities(); InitializeResult result = new InitializeResult(capabilities); - - textDocumentService.updateSupportedTokenTypes(availableTokenTypes); - - return CompletableFuture.completedFuture(result); + textDocumentService.updateSupportedTokenTypes(List.of(DTRAMServerCapabilities.SEMANTIC_TOKEN_TYPES)); + return CompletableFuture.supplyAsync(() -> result); } @Override public CompletableFuture shutdown() { - return CompletableFuture.supplyAsync(Object::new); + return CompletableFuture.completedFuture(null); } @Override @@ -63,11 +48,22 @@ @Override public WorkspaceService getWorkspaceService() { - return workspaceService; + return null; } @Override public TextDocumentService getTextDocumentService() { return textDocumentService; } + + private static void setClient(LanguageClient client) { + DTRAMLanguageServer.client = client; + } + + public static void requestSemanticTokensRefresh() { + if (client == null) { + return; + } + client.refreshSemanticTokens(); + } } diff --git a/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMServerCapabilities.java b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMServerCapabilities.java new file mode 100644 index 0000000..ed241d2 --- /dev/null +++ b/LanguageServer/src/main/java/org/nittalab/dtram/languageserver/DTRAMServerCapabilities.java @@ -0,0 +1,34 @@ +package org.nittalab.dtram.languageserver; + +import org.eclipse.lsp4j.*; + +import java.util.List; + +public class DTRAMServerCapabilities extends ServerCapabilities { + private static final String[] COMPLETION_TRIGGER_CHARACTERS = {".", "="}; + public static final String[] SEMANTIC_TOKEN_TYPES = {SemanticTokenTypes.Type, SemanticTokenTypes.Class, SemanticTokenTypes.Parameter, SemanticTokenTypes.Variable, SemanticTokenTypes.Function, SemanticTokenTypes.Method, SemanticTokenTypes.Keyword, SemanticTokenTypes.Comment, SemanticTokenTypes.String, SemanticTokenTypes.Number, SemanticTokenTypes.Operator}; + + public DTRAMServerCapabilities() { + setTextDocumentSync(TextDocumentSyncKind.Full); + setPositionEncoding(PositionEncodingKind.UTF16); + + // Completion options + CompletionOptions completionOptions = new CompletionOptions(); + completionOptions.setTriggerCharacters(List.of(COMPLETION_TRIGGER_CHARACTERS)); + completionOptions.setResolveProvider(false); + setCompletionProvider(completionOptions); + + // Semantic tokens + SemanticTokensWithRegistrationOptions semanticOptions = new SemanticTokensWithRegistrationOptions(); + semanticOptions.setFull(true); + semanticOptions.setRange(false); + semanticOptions.setDocumentSelector(null); + semanticOptions.setLegend(new SemanticTokensLegend(List.of(SEMANTIC_TOKEN_TYPES), List.of())); + setSemanticTokensProvider(semanticOptions); + + // Workspace folder options + WorkspaceFoldersOptions workspaceOptions = new WorkspaceFoldersOptions(); + workspaceOptions.setSupported(false); + setWorkspace(new WorkspaceServerCapabilities(workspaceOptions)); + } +}