-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathLSPIDEServices.java
More file actions
241 lines (207 loc) · 8.26 KB
/
LSPIDEServices.java
File metadata and controls
241 lines (207 loc) · 8.26 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
/*
* Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.rascalmpl.vscode.lsp;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.Logger;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.ShowDocumentParams;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.ideservices.IDEServices;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.vscode.lsp.terminal.ITerminalIDEServer.BrowseParameter;
import org.rascalmpl.vscode.lsp.terminal.ITerminalIDEServer.LanguageParameter;
import org.rascalmpl.vscode.lsp.util.Diagnostics;
import org.rascalmpl.vscode.lsp.util.DocumentChanges;
import org.rascalmpl.vscode.lsp.util.locations.Locations;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.IMap;
import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IString;
import io.usethesource.vallang.IValue;
/**
* This server forwards IDE services requests by a Rascal terminal
* directly to the LSP language client.
*/
public class LSPIDEServices implements IDEServices {
private final Logger logger;
private final IBaseLanguageClient languageClient;
private final IBaseTextDocumentService docService;
private final BaseWorkspaceService workspaceService;
private final IRascalMonitor monitor;
public LSPIDEServices(IBaseLanguageClient client, IBaseTextDocumentService docService, BaseWorkspaceService workspaceService, Logger logger, IRascalMonitor monitor) {
this.languageClient = client;
this.workspaceService = workspaceService;
this.docService = docService;
this.logger = logger;
this.monitor = monitor;
}
@Override
public void showMessage(IConstructor message) {
languageClient.showMessage(toMessageParams(message));
}
public static MessageParams toMessageParams(IConstructor message) {
var params = new MessageParams();
switch (message.getName()) {
case "error": {
params.setType(MessageType.Error);
break;
}
case "warning": {
params.setType(MessageType.Warning);
break;
}
case "info": {
params.setType(MessageType.Info);
break;
}
default: params.setType(MessageType.Log);
}
var msgText = ((IString) message.get("msg")).getValue();
if (message.has("at")) {
var at = ((ISourceLocation) message.get("at")).getURI();
params.setMessage(String.format("%s (at %s)", msgText, at));
} else {
params.setMessage(msgText);
}
return params;
}
@Override
public PrintWriter stderr() {
assert false: "this should not be used here";
return new PrintWriter(System.out);
}
@Override
public void browse(URI uri, String title, int viewColumn) {
languageClient.showContent(new BrowseParameter(uri.toString(), title, viewColumn));
}
@Override
public void edit(ISourceLocation path) {
ISourceLocation physical = Locations.toClientLocation(path);
ShowDocumentParams params = new ShowDocumentParams(physical.getURI().toASCIIString());
params.setTakeFocus(true);
if (physical.hasOffsetLength()) {
params.setSelection(Locations.toRange(physical, docService.getColumnMap(physical)));
}
languageClient.showDocument(params);
}
@Override
public ISourceLocation resolveProjectLocation(ISourceLocation input) {
for (var folder : workspaceService.workspaceFolders()) {
if (folder.getName().equals(input.getAuthority())) {
return buildProjectChildLoc(folder, input);
}
}
return input;
}
private ISourceLocation buildProjectChildLoc(WorkspaceFolder folder, ISourceLocation input) {
try {
ISourceLocation root = URIUtil.createFromURI(folder.getUri());
return URIUtil.getChildLocation(root, input.getPath());
} catch (URISyntaxException e) {
logger.catching(e);
return input;
}
}
@Override
public void registerLanguage(IConstructor language) {
languageClient.receiveRegisterLanguage(LanguageParameter.fromRascalValue(language));
}
@Override
public void unregisterLanguage(IConstructor language) {
languageClient.receiveUnregisterLanguage(LanguageParameter.fromRascalValue(language));
}
@Override
public void applyDocumentsEdits(IList edits) {
applyFileSystemEdits(edits);
}
@Override
public void applyFileSystemEdits(IList edits) {
languageClient.applyEdit(new ApplyWorkspaceEditParams(DocumentChanges.translateDocumentChanges(docService, edits)));
}
@Override
public void registerLocations(IString scheme, IString auth, IMap map) {
IDEServices.super.registerLocations(scheme, auth, map);
}
@Override
public void registerDiagnostics(IList messages) {
Map<ISourceLocation, List<Diagnostic>> translated = Diagnostics.translateMessages(messages, docService.getColumnMaps());
for (Entry<ISourceLocation, List<Diagnostic>> entry : translated.entrySet()) {
String uri = entry.getKey().getURI().toString();
languageClient.publishDiagnostics(new PublishDiagnosticsParams(uri, entry.getValue()));
}
}
@Override
public void unregisterDiagnostics(IList resources) {
for (IValue elem : resources) {
ISourceLocation loc = (ISourceLocation) elem;
languageClient.publishDiagnostics(new PublishDiagnosticsParams(loc.getURI().toString(), Collections.emptyList()));
}
}
@Override
public void jobStart(String name, int workShare, int totalWork) {
monitor.jobStart(name, workShare, totalWork);
}
@Override
public void jobStep(String name, String message, int workShare) {
monitor.jobStep(name, message, workShare);
}
@Override
public int jobEnd(String name, boolean succeeded) {
return monitor.jobEnd(name, succeeded);
}
@Override
public boolean jobIsCanceled(String name) {
return monitor.jobIsCanceled(name);
}
@Override
public void jobTodo(String name, int work) {
monitor.jobTodo(name, work);
}
@Override
public void endAllJobs() {
monitor.endAllJobs();
}
@Override
public void warning(String message, ISourceLocation src) {
monitor.warning(message, src);
}
public IRascalMonitor getMonitor() {
return monitor;
}
}