-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoLanguageServer.java
212 lines (179 loc) · 7.05 KB
/
AcceleoLanguageServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ls;
import java.time.Instant;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocument;
import org.eclipse.acceleo.aql.ls.services.textdocument.AcceleoTextDocumentService;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoWorkspace;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoWorkspaceService;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.TextDocumentSyncOptions;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.LanguageServer;
/**
* Acceleo <a href="https://microsoft.github.io/language-server-protocol/">LSP</a> server.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AcceleoLanguageServer implements LanguageServer, LanguageClientAware {
/**
* The text-document-related service.
*/
private final AcceleoTextDocumentService textDocumentService;
/**
* The workspace-related service.
*/
private final AcceleoWorkspaceService workspaceService;
/**
* The root {@link IAcceleoLanguageServerContext} of this server.
*/
private final IAcceleoLanguageServerContext acceleoLanguageServerContext;
/**
* The {@link Instant} at which this server was created.
*/
private final Instant creationTimestamp;
/**
* The current language client.
*/
private LanguageClient languageClient;
/**
* Creates a new {@link AcceleoLanguageServer}.
*
* @param acceleoLanguageServerContext
* the (non-{@code null}) root {@link IAcceleoLanguageServerContext} for this server.
*/
public AcceleoLanguageServer(IAcceleoLanguageServerContext acceleoLanguageServerContext) {
this.creationTimestamp = Instant.now();
this.acceleoLanguageServerContext = Objects.requireNonNull(acceleoLanguageServerContext);
this.workspaceService = new AcceleoWorkspaceService(this);
this.textDocumentService = new AcceleoTextDocumentService(this);
}
/**
* Provides a {@link String} label to identify this language server.
*
* @return the label for this language server.
*/
public String getLabel() {
return this.getClass().getSimpleName() + "@" + this.hashCode() + "_" + this.creationTimestamp;
}
/**
* Connects the {@link AcceleoWorkspace}.
*
* @return the {@link AcceleoWorkspace}.
*/
public AcceleoWorkspace connectWorkspace() {
final AcceleoWorkspace workspace = this.acceleoLanguageServerContext.getWorkspace();
workspace.setOwner(this);
return workspace;
}
/**
* Publishes all {@link AcceleoTextDocument} validation result in the workspace of this server.
*/
private void publishAll() {
if (this.getWorkspace() != null) {
this.getWorkspace().getAllTextDocuments().forEach(textDocument -> textDocumentService
.publishValidationResults(textDocument));
}
}
@Override
public void connect(LanguageClient newLanguageClient) {
this.languageClient = newLanguageClient;
this.textDocumentService.connect(newLanguageClient);
this.workspaceService.connect(newLanguageClient);
// The client just changed, re-validate all documents so they publish the validation results to the
// client.
this.publishAll();
newLanguageClient.logMessage(new MessageParams(MessageType.Info,
"Connected to the Acceleo Language Server, relying on Acceleo Workspace " + this
.getWorkspace().getName()));
}
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
final ServerCapabilities capabilities = new ServerCapabilities();
capabilities.setTextDocumentSync(createTextDocumentSyncOptions());
final CompletionOptions completionProvider = new CompletionOptions();
completionProvider.setResolveProvider(true);
capabilities.setCompletionProvider(completionProvider);
// Note that LSP4E seems to ignore the declaration provider, cf. {@link
// OpenDeclarationHyperlinkDetector}, so we also need to enable the definition provider.
// capabilities.setDeclarationProvider(true);
capabilities.setDefinitionProvider(true);
// FIXME: Maybe at some point we will need to set hierarchicalDocumentSymbolSupport to true?
capabilities.setDocumentSymbolProvider(true);
capabilities.setReferencesProvider(true);
capabilities.setRenameProvider(new RenameOptions(true));
capabilities.setCodeActionProvider(true);
final InitializeResult res = new InitializeResult(capabilities);
return CompletableFuture.completedFuture(res);
}
/**
* Creates the {@link TextDocumentSyncOptions} capability for this server.
*
* @return the {@link TextDocumentSyncOptions} for this server.
*/
private TextDocumentSyncOptions createTextDocumentSyncOptions() {
TextDocumentSyncOptions textDocumentSyncOptions = new TextDocumentSyncOptions();
textDocumentSyncOptions.setChange(TextDocumentSyncKind.Full);
textDocumentSyncOptions.setOpenClose(true);
textDocumentSyncOptions.setSave(new SaveOptions(true));
textDocumentSyncOptions.setWillSave(true);
textDocumentSyncOptions.setWillSaveWaitUntil(true);
return textDocumentSyncOptions;
}
@Override
public CompletableFuture<Object> shutdown() {
final AcceleoWorkspace workspace = this.acceleoLanguageServerContext.getWorkspace();
workspace.setOwner(null);
if (this.languageClient != null) {
this.languageClient.logMessage(new MessageParams(MessageType.Log,
"Acceleo Language Server is shutting down"));
this.languageClient = null;
this.textDocumentService.disconnect();
this.workspaceService.disconnect();
}
return CompletableFuture.completedFuture(null);
}
@Override
public void exit() {
if (this.languageClient != null) {
// FIXME removed log on editor closing, fails in DEV mode
// this.languageClient.logMessage(new MessageParams(MessageType.Log,
// "Acceleo Language Server is exiting"));
}
}
@Override
public AcceleoTextDocumentService getTextDocumentService() {
return this.textDocumentService;
}
@Override
public AcceleoWorkspaceService getWorkspaceService() {
return this.workspaceService;
}
/**
* Provides the {@link AcceleoWorkspace}.
*
* @return the current (non-{@code null}) {@link AcceleoWorkspace}.
*/
public AcceleoWorkspace getWorkspace() {
return this.getWorkspaceService().getWorkspace();
}
}