forked from LucasBullen/LSP4J_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEclipseConTextDocumentService.java
More file actions
271 lines (244 loc) · 9.7 KB
/
EclipseConTextDocumentService.java
File metadata and controls
271 lines (244 loc) · 9.7 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package org.eclipsecon.exercise2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.DocumentHighlightParams;
import org.eclipse.lsp4j.DocumentOnTypeFormattingParams;
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.MarkedString;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureHelpParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.eclipsecon.exercise2.EclipseConDocumentModel.Route;
import org.eclipsecon.exercise2.EclipseConDocumentModel.VariableDefinition;
@SuppressWarnings("deprecation")
public class EclipseConTextDocumentService implements TextDocumentService {
private final Map<String, EclipseConDocumentModel> docs = Collections.synchronizedMap(new HashMap<>());
private final EclipseConLanguageServer eclipseConLanguageServer;
public EclipseConTextDocumentService(EclipseConLanguageServer eclipseConLanguageServer) {
this.eclipseConLanguageServer = eclipseConLanguageServer;
}
@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(
CompletionParams params) {
/* Replace this return statement with one that returns a list of CompletionItems */
return null;
}
@Override
public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem unresolved) {
return null;
}
@Override
public CompletableFuture<Hover> hover(HoverParams params) {
return CompletableFuture.supplyAsync(() -> {
EclipseConDocumentModel doc = docs.get(params.getTextDocument().getUri());
Hover res = new Hover();
res.setContents(doc.getResolvedRoutes().stream()
.filter(route -> route.line == params.getPosition().getLine())
.map(route -> route.name)
.map(EclipseConMap.INSTANCE.type::get)
.map(this::getHoverContent)
.collect(Collectors.toList()));
return res;
});
}
private Either<String, MarkedString> getHoverContent(String type) {
return Either.forLeft(type);
}
@Override
public CompletableFuture<SignatureHelp> signatureHelp(SignatureHelpParams params) {
return null;
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(DefinitionParams params) {
return CompletableFuture.supplyAsync(() -> {
EclipseConDocumentModel doc = docs.get(params.getTextDocument().getUri());
String variable = doc.getVariable(params.getPosition().getLine(), params.getPosition().getCharacter());
if (variable != null) {
int variableLine = doc.getDefintionLine(variable);
if (variableLine == -1) {
return null;
}
Location location = new Location(params.getTextDocument().getUri(), new Range(
new Position(variableLine, 0),
new Position(variableLine, variable.length())
));
return Either.forLeft(Collections.singletonList(location));
}
return null;
});
}
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
return CompletableFuture.supplyAsync(() -> {
EclipseConDocumentModel doc = docs.get(params.getTextDocument().getUri());
String variable = doc.getVariable(params.getPosition().getLine(), params.getPosition().getCharacter());
if (variable != null) {
return doc.getResolvedRoutes().stream()
.filter(route -> route.text.contains("${" + variable + "}") || route.text.startsWith(variable + "="))
.map(route -> new Location(params.getTextDocument().getUri(), new Range(
new Position(route.line, route.text.indexOf(variable)),
new Position(route.line, route.text.indexOf(variable) + variable.length())
)))
.collect(Collectors.toList());
}
String routeName = doc.getResolvedRoutes().stream()
.filter(route -> route.line == params.getPosition().getLine())
.collect(Collectors.toList())
.get(0)
.name;
return doc.getResolvedRoutes().stream()
.filter(route -> route.name.equals(routeName))
.map(route -> new Location(params.getTextDocument().getUri(), new Range(
new Position(route.line, 0),
new Position(route.line, route.text.length()))))
.collect(Collectors.toList());
});
}
@Override
public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(DocumentHighlightParams params) {
return null;
}
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(DocumentSymbolParams params) {
EclipseConDocumentModel model = docs.get(params.getTextDocument().getUri());
if(model == null)
return null;
return CompletableFuture.supplyAsync(() ->
docs.get(params.getTextDocument().getUri()).getResolvedLines().stream().map(line -> {
SymbolInformation symbol = new SymbolInformation();
symbol.setLocation(new Location(params.getTextDocument().getUri(), new Range(
new Position(line.line, line.charOffset),
new Position(line.line, line.charOffset + line.text.length()))));
if (line instanceof VariableDefinition) {
symbol.setKind(SymbolKind.Variable);
symbol.setName(((VariableDefinition) line).variableName);
} else if (line instanceof Route) {
symbol.setKind(SymbolKind.String);
symbol.setName(((Route) line).name);
}
return Either.<SymbolInformation, DocumentSymbol>forLeft(symbol);
}).collect(Collectors.toList())
);
}
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
return null;
}
@Override
public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) {
return null;
}
@Override
public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
return null;
}
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
return null;
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
return null;
}
@Override
public CompletableFuture<List<? extends TextEdit>> onTypeFormatting(DocumentOnTypeFormattingParams params) {
return null;
}
@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
return null;
}
@Override
public void didOpen(DidOpenTextDocumentParams params) {
EclipseConDocumentModel model = new EclipseConDocumentModel(params.getTextDocument().getText());
this.docs.put(params.getTextDocument().getUri(),
model);
CompletableFuture.runAsync(() ->
eclipseConLanguageServer.client.publishDiagnostics(
new PublishDiagnosticsParams(params.getTextDocument().getUri(), validate(model))
)
);
}
@Override
public void didChange(DidChangeTextDocumentParams params) {
EclipseConDocumentModel model = new EclipseConDocumentModel(params.getContentChanges().get(0).getText());
this.docs.put(params.getTextDocument().getUri(),
model);
// send notification
CompletableFuture.runAsync(() ->
eclipseConLanguageServer.client.publishDiagnostics(
new PublishDiagnosticsParams(params.getTextDocument().getUri(), validate(model))
)
);
}
private List<Diagnostic> validate(EclipseConDocumentModel model) {
List<Diagnostic> res = new ArrayList<>();
Route previousRoute = null;
for (Route route : model.getResolvedRoutes()) {
if (!EclipseConMap.INSTANCE.all.contains(route.name)) {
Diagnostic diagnostic = new Diagnostic();
diagnostic.setSeverity(DiagnosticSeverity.Error);
diagnostic.setMessage("This is not a Session");
diagnostic.setRange(new Range(
new Position(route.line, route.charOffset),
new Position(route.line, route.charOffset + route.text.length())));
res.add(diagnostic);
} else if (previousRoute != null && !EclipseConMap.INSTANCE.startsFrom(route.name, previousRoute.name)) {
Diagnostic diagnostic = new Diagnostic();
diagnostic.setSeverity(DiagnosticSeverity.Warning);
diagnostic.setMessage("'" + route.name + "' does not follow '" + previousRoute.name + "'");
diagnostic.setRange(new Range(
new Position(route.line, route.charOffset),
new Position(route.line, route.charOffset + route.text.length())));
res.add(diagnostic);
}
previousRoute = route;
}
return res;
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
this.docs.remove(params.getTextDocument().getUri());
}
@Override
public void didSave(DidSaveTextDocumentParams params) {
}
}