-
Notifications
You must be signed in to change notification settings - Fork 316
/
DocumentSymbolService.java
336 lines (300 loc) · 11.6 KB
/
DocumentSymbolService.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Copyright (c) 2016, 2022 TypeFox GmbH (http://www.typefox.io) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.ide.server.symbol;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.SymbolKind;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.xtext.findReferences.IReferenceFinder;
import org.eclipse.xtext.findReferences.ReferenceAcceptor;
import org.eclipse.xtext.findReferences.TargetURICollector;
import org.eclipse.xtext.findReferences.TargetURIs;
import org.eclipse.xtext.ide.server.Document;
import org.eclipse.xtext.ide.server.DocumentExtensions;
import org.eclipse.xtext.ide.util.CancelIndicatorProgressMonitor;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.EObjectAtOffsetHelper;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.service.OperationCanceledManager;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import com.google.common.graph.Traverser;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
/**
* @author kosyakov - Initial contribution and API
*
* Contributors:
* Rubén Porras Campo (Avaloq Evolution AG)
* - Do not return definitions for elements without identifiers.
* - Use the language specific DocumentExtentions depending on the URI of the particular symbol.
*
* @since 2.11
*/
@Singleton
public class DocumentSymbolService implements IDocumentSymbolService {
@Inject
private EObjectAtOffsetHelper eObjectAtOffsetHelper;
@Inject
private IQualifiedNameProvider qualifiedNameProvider;
@Inject
private IReferenceFinder referenceFinder;
@Inject
private TargetURICollector targetURICollector;
@Inject
private Provider<TargetURIs> targetURIProvider;
@Inject
private OperationCanceledManager operationCanceledManager;
@Inject
private IResourceServiceProvider.Registry resourceServiceProviderRegistry;
@Inject
private HierarchicalDocumentSymbolService hierarchicalDocumentSymbolService;
/**
* @since 2.21
*/
public List<? extends Location> getDefinitions(Document document, XtextResource resource, DefinitionParams params,
IReferenceFinder.IResourceAccess resourceAccess, CancelIndicator cancelIndicator) {
int offset = document.getOffSet(params.getPosition());
return getDefinitions(resource, offset, resourceAccess, cancelIndicator);
}
public List<? extends Location> getDefinitions(XtextResource resource, int offset,
IReferenceFinder.IResourceAccess resourceAccess, CancelIndicator cancelIndicator) {
EObject element = eObjectAtOffsetHelper.getElementWithNameAt(resource, offset);
if (element == null) {
return Collections.emptyList();
}
List<Location> locations = new ArrayList<>();
TargetURIs targetURIs = collectTargetURIs(element);
for (URI targetURI : targetURIs) {
operationCanceledManager.checkCanceled(cancelIndicator);
doRead(resourceAccess, targetURI, (EObject obj) -> {
Location location = getDocumentExtensions(targetURI).newLocation(obj);
if (location != null) {
locations.add(location);
}
});
}
return locations;
}
public List<? extends Location> getReferences(Document document, XtextResource resource, ReferenceParams params,
IReferenceFinder.IResourceAccess resourceAccess, IResourceDescriptions indexData,
CancelIndicator cancelIndicator) {
int offset = document.getOffSet(params.getPosition());
List<? extends Location> definitions = Collections.emptyList();
if (params.getContext().isIncludeDeclaration()) {
definitions = getDefinitions(resource, offset, resourceAccess, cancelIndicator);
}
List<? extends Location> references = getReferences(resource, offset, resourceAccess, indexData,
cancelIndicator);
List<Location> result = new ArrayList<>();
result.addAll(definitions);
result.addAll(references);
return result;
}
public List<? extends Location> getReferences(XtextResource resource, int offset,
IReferenceFinder.IResourceAccess resourceAccess, IResourceDescriptions indexData,
CancelIndicator cancelIndicator) {
EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
if (element == null) {
return Collections.emptyList();
}
List<Location> locations = new ArrayList<>();
TargetURIs targetURIs = collectTargetURIs(element);
referenceFinder.findAllReferences(targetURIs, resourceAccess, indexData,
new ReferenceAcceptor(resourceServiceProviderRegistry, (IReferenceDescription reference) -> {
URI sourceEObjectUri = reference.getSourceEObjectUri();
doRead(resourceAccess, sourceEObjectUri, (EObject obj) -> {
try {
Location location = getDocumentExtensions(sourceEObjectUri).newLocation(obj,
reference.getEReference(), reference.getIndexInList());
if (location != null) {
locations.add(location);
}
} catch (Exception e) {
Logger.getLogger(DocumentSymbolService.class).error("exception finding references", e);
}
});
}), new CancelIndicatorProgressMonitor(cancelIndicator));
return locations;
}
protected TargetURIs collectTargetURIs(EObject targetObject) {
TargetURIs targetURIs = targetURIProvider.get();
targetURICollector.add(targetObject, targetURIs);
return targetURIs;
}
@Override
public List<DocumentSymbol> getSymbols(Document document, XtextResource resource,
DocumentSymbolParams params, CancelIndicator cancelIndicator) {
return getSymbols(resource, cancelIndicator);
}
public List<DocumentSymbol> getSymbols(XtextResource resource, CancelIndicator cancelIndicator) {
List<DocumentSymbol> infos = new ArrayList<>();
List<DocumentSymbol> rootSymbols = hierarchicalDocumentSymbolService.getSymbols(resource, cancelIndicator);
for (DocumentSymbol rootSymbol : rootSymbols) {
Iterable<DocumentSymbol> symbols = Traverser.forTree(DocumentSymbol::getChildren)
.depthFirstPreOrder(rootSymbol);
for (DocumentSymbol s : symbols) {
infos.add(s);
}
}
return infos;
}
protected EObject getContainer(EObject obj) {
return obj.eContainer();
}
/**
* @since 2.16
*/
protected WorkspaceSymbol createSymbol(String uri, DocumentSymbol symbol,
Function1<? super DocumentSymbol, ? extends String> containerNameProvider) {
WorkspaceSymbol workspaceSymbol = new WorkspaceSymbol();
workspaceSymbol.setName(symbol.getName());
workspaceSymbol.setKind(symbol.getKind());
workspaceSymbol.setTags(symbol.getTags());
Location location = new Location();
location.setUri(uri);
location.setRange(symbol.getSelectionRange());
workspaceSymbol.setLocation(Either.forLeft(location));
workspaceSymbol.setContainerName(containerNameProvider.apply(symbol));
return workspaceSymbol;
}
protected WorkspaceSymbol createSymbol(EObject object) {
String name = getSymbolName(object);
if (name == null) {
return null;
}
SymbolKind kind = getSymbolKind(object);
if (kind == null) {
return null;
}
Location location = getSymbolLocation(object);
if (location == null) {
return null;
}
WorkspaceSymbol symbol = new WorkspaceSymbol();
symbol.setName(name);
symbol.setKind(kind);
symbol.setLocation(Either.forLeft(location));
return symbol;
}
protected String getSymbolName(EObject object) {
return getSymbolName(qualifiedNameProvider.getFullyQualifiedName(object));
}
protected SymbolKind getSymbolKind(EObject object) {
return getSymbolKind(object.eClass());
}
protected Location getSymbolLocation(EObject object) {
return getDocumentExtensions(object.eResource().getURI()).newLocation(object);
}
public List<? extends WorkspaceSymbol> getSymbols(IResourceDescription resourceDescription, String query,
IReferenceFinder.IResourceAccess resourceAccess, CancelIndicator cancelIndicator) {
List<WorkspaceSymbol> symbols = new LinkedList<>();
for (IEObjectDescription description : resourceDescription.getExportedObjects()) {
operationCanceledManager.checkCanceled(cancelIndicator);
if (filter(description, query)) {
createSymbol(description, resourceAccess, (WorkspaceSymbol symbol) -> {
symbols.add(symbol);
});
}
}
return symbols;
}
protected boolean filter(IEObjectDescription description, String query) {
return description.getQualifiedName().toLowerCase().toString().contains(query.toLowerCase());
}
protected void createSymbol(IEObjectDescription description, IReferenceFinder.IResourceAccess resourceAccess,
Procedure1<? super WorkspaceSymbol> acceptor) {
String name = getSymbolName(description);
if (name == null) {
return;
}
SymbolKind kind = getSymbolKind(description);
if (kind == null) {
return;
}
getSymbolLocation(description, resourceAccess, (Location location) -> {
WorkspaceSymbol symbol = new WorkspaceSymbol(name, kind, Either.forLeft(location));
acceptor.apply(symbol);
});
}
protected WorkspaceSymbol createSymbol(IEObjectDescription description) {
String symbolName = getSymbolName(description);
if (symbolName == null) {
return null;
}
SymbolKind symbolKind = getSymbolKind(description);
if (symbolKind == null) {
return null;
}
WorkspaceSymbol symbol = new WorkspaceSymbol();
symbol.setName(symbolName);
symbol.setKind(symbolKind);
return symbol;
}
protected String getSymbolName(IEObjectDescription description) {
return getSymbolName(description.getQualifiedName());
}
protected SymbolKind getSymbolKind(IEObjectDescription description) {
return getSymbolKind(description.getEClass());
}
protected void getSymbolLocation(IEObjectDescription description, IReferenceFinder.IResourceAccess resourceAccess,
Procedure1<? super Location> acceptor) {
doRead(resourceAccess, description.getEObjectURI(), (EObject obj) -> {
Location location = getSymbolLocation(obj);
if (location != null) {
acceptor.apply(location);
}
});
}
protected String getSymbolName(QualifiedName qualifiedName) {
if (qualifiedName != null) {
return qualifiedName.toString();
}
return null;
}
protected SymbolKind getSymbolKind(EClass type) {
// TODO implement meaningful
return SymbolKind.Property;
}
protected void doRead(IReferenceFinder.IResourceAccess resourceAccess, URI objectURI,
Procedure1<? super EObject> acceptor) {
resourceAccess.readOnly(objectURI, (ResourceSet resourceSet) -> {
EObject object = resourceSet.getEObject(objectURI, true);
if (object != null) {
acceptor.apply(object);
}
return null;
});
}
private DocumentExtensions getDocumentExtensions(final URI targetURI) {
return resourceServiceProviderRegistry.getResourceServiceProvider(targetURI.trimFragment())
.get(DocumentExtensions.class);
}
}