-
Notifications
You must be signed in to change notification settings - Fork 45
/
SARLOutlineTreeProvider.java
299 lines (274 loc) · 10.9 KB
/
SARLOutlineTreeProvider.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
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.lang.ui.outline;
import javax.inject.Named;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtend.core.xtend.XtendClass;
import org.eclipse.xtend.core.xtend.XtendMember;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration;
import org.eclipse.xtext.common.types.JvmConstructor;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmFeature;
import org.eclipse.xtext.common.types.JvmIdentifiableElement;
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.ui.editor.outline.IOutlineNode;
import org.eclipse.xtext.ui.editor.outline.impl.DocumentRootNode;
import org.eclipse.xtext.ui.editor.outline.impl.EObjectNode;
import org.eclipse.xtext.ui.editor.outline.impl.EStructuralFeatureNode;
import org.eclipse.xtext.xbase.annotations.ui.outline.XbaseWithAnnotationsOutlineTreeProvider;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices;
import io.sarl.lang.compilation.jvmmodel.SarlJvmModelAssociations;
import io.sarl.lang.sarl.SarlAction;
import io.sarl.lang.sarl.SarlBehaviorUnit;
import io.sarl.lang.sarl.SarlCapacityUses;
import io.sarl.lang.sarl.SarlConstructor;
import io.sarl.lang.sarl.SarlField;
import io.sarl.lang.sarl.SarlRequiredCapacity;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.lang.util.Utils;
/**
* Customization of the default outline structure.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @see "https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#outline"
*/
public class SARLOutlineTreeProvider extends XbaseWithAnnotationsOutlineTreeProvider {
@Inject
private SarlJvmModelAssociations associations;
@Inject
@Named("DiagnosticDecorator")
private ILabelDecorator diagnoticDecorator;
@Inject
private CommonTypeComputationServices services;
/** Create a node for the SARL script.
*
* @param parentNode - the parent node.
* @param modelElement - the feature container for which a node should be created.
*/
protected void _createChildren(DocumentRootNode parentNode, SarlScript modelElement) {
if (!Strings.isNullOrEmpty(modelElement.getPackage())) {
// Create the node for the package declaration.
createEStructuralFeatureNode(
parentNode, modelElement,
XtendPackage.Literals.XTEND_FILE__PACKAGE,
this.imageDispatcher.invoke(getClass().getPackage()),
// Do not use the text dispatcher below for avoiding to obtain
// the filename of the script.
modelElement.getPackage(),
true);
}
// Create the nodes for the import declarations.
if (modelElement.getImportSection() != null && !modelElement.getImportSection().getImportDeclarations().isEmpty()) {
createNode(parentNode, modelElement.getImportSection());
}
// Create a node per type declaration.
for (final XtendTypeDeclaration topElement : modelElement.getXtendTypes()) {
createNode(parentNode, topElement);
}
}
/** Create a node for the given feature container.
*
* @param parentNode - the parent node.
* @param modelElement - the feature container for which a node should be created.
*/
protected void _createNode(DocumentRootNode parentNode, XtendTypeDeclaration modelElement) {
//
// The text region is set to the model element, not to the model element's name as in the
// default implementation of createStructuralFeatureNode().
// The text region computation is overridden in order to have a correct link to the editor.
//
final boolean isFeatureSet = modelElement.eIsSet(XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME);
final EStructuralFeatureNode elementNode = new EStructuralFeatureNode(
modelElement,
XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME,
parentNode,
this.imageDispatcher.invoke(modelElement),
this.textDispatcher.invoke(modelElement),
modelElement.getMembers().isEmpty() || !isFeatureSet);
final EObject primarySourceElement = this.associations.getPrimarySourceElement(modelElement);
final ICompositeNode parserNode = NodeModelUtils.getNode(
(primarySourceElement == null) ? modelElement : primarySourceElement);
elementNode.setTextRegion(parserNode.getTextRegion());
//
boolean hasConstructor = false;
if (!modelElement.getMembers().isEmpty()) {
EObjectNode capacityUseNode = null;
EObjectNode capacityRequirementNode = null;
for (final EObject feature : modelElement.getMembers()) {
if (feature instanceof SarlConstructor) {
hasConstructor = true;
createNode(elementNode, feature);
} else if (feature instanceof SarlField
|| feature instanceof SarlAction
|| feature instanceof SarlBehaviorUnit
|| feature instanceof XtendTypeDeclaration) {
createNode(elementNode, feature);
} else if (feature instanceof SarlCapacityUses) {
capacityUseNode = createCapacityUseNode(elementNode, (SarlCapacityUses) feature, capacityUseNode);
} else if (feature instanceof SarlRequiredCapacity) {
capacityRequirementNode = createRequiredCapacityNode(elementNode,
(SarlRequiredCapacity) feature, capacityRequirementNode);
}
}
}
if (!hasConstructor && modelElement instanceof XtendClass) {
createInheritedConstructors(elementNode, (XtendClass) modelElement);
}
}
private void createInheritedConstructors(EStructuralFeatureNode elementNode, XtendClass modelElement) {
final JvmTypeReference extend = modelElement.getExtends();
if (extend != null) {
final LightweightTypeReference reference = Utils.toLightweightTypeReference(extend, this.services);
if (reference != null) {
final JvmType type = reference.getType();
if (type instanceof JvmDeclaredType) {
for (final JvmConstructor constructor : ((JvmDeclaredType) type).getDeclaredConstructors()) {
createNode(elementNode, constructor);
}
}
}
}
}
private EObjectNode createCapacityUseNode(EStructuralFeatureNode elementNode, SarlCapacityUses feature,
EObjectNode oldCapacityUseNode) {
EObjectNode capacityUseNode = oldCapacityUseNode;
if (capacityUseNode == null) {
capacityUseNode = createEObjectNode(
elementNode, feature,
this.imageDispatcher.invoke(feature),
this.textDispatcher.invoke(feature),
false);
}
for (final JvmParameterizedTypeReference item : feature.getCapacities()) {
createEObjectNode(
capacityUseNode, item,
this.imageDispatcher.invoke(item),
this.textDispatcher.invoke(item),
true);
}
return capacityUseNode;
}
private EObjectNode createRequiredCapacityNode(EStructuralFeatureNode elementNode, SarlRequiredCapacity feature,
EObjectNode oldCapacityRequirementNode) {
EObjectNode capacityRequirementNode = oldCapacityRequirementNode;
if (capacityRequirementNode == null) {
capacityRequirementNode = createEObjectNode(
elementNode, feature,
this.imageDispatcher.invoke(feature),
this.textDispatcher.invoke(feature),
false);
}
for (final JvmParameterizedTypeReference item : feature.getCapacities()) {
createEObjectNode(
capacityRequirementNode, item,
this.imageDispatcher.invoke(item),
this.textDispatcher.invoke(item),
true);
}
return capacityRequirementNode;
}
/** Replies if the type declaration element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(XtendTypeDeclaration modelElement) {
return modelElement.getMembers().isEmpty();
}
/** Replies if the member element is a leaf in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(XtendMember modelElement) {
return true;
}
/** Replies if the JVM elements are leafs in the outline.
*
* @param modelElement - the model element.
* @return <code>true</code> if it is a leaf, <code>false</code> otherwise.
*/
@SuppressWarnings("static-method")
protected boolean _isLeaf(JvmIdentifiableElement modelElement) {
return true;
}
@Override
protected EObjectNode createEObjectNode(
IOutlineNode parentNode,
EObject modelElement, Image image, Object text,
boolean isLeaf) {
final SARLEObjectNode objectNode = new SARLEObjectNode(modelElement, parentNode, image, text, isLeaf);
configureNode(parentNode, modelElement, objectNode);
return objectNode;
}
private void configureNode(IOutlineNode parentNode, EObject modelElement, SARLEObjectNode objectNode) {
final EObject primarySourceElement = this.associations.getPrimarySourceElement(modelElement);
final ICompositeNode parserNode = NodeModelUtils.getNode(
(primarySourceElement == null) ? modelElement : primarySourceElement);
if (parserNode != null) {
objectNode.setTextRegion(parserNode.getTextRegion());
}
if (isLocalElement(parentNode, modelElement)) {
objectNode.setShortTextRegion(this.locationInFileProvider.getSignificantTextRegion(modelElement));
}
objectNode.setStatic(isStatic(modelElement));
}
private static boolean isStatic(EObject element) {
if (element instanceof JvmFeature) {
return ((JvmFeature) element).isStatic();
}
if (element instanceof JvmDeclaredType) {
return ((JvmDeclaredType) element).isStatic();
}
if (element instanceof XtendMember) {
try {
return ((XtendMember) element).isStatic();
} catch (Exception exception) {
// Some XtendMember does not support
}
}
return false;
}
/** Get the image for the Xtend members.
*
* @param modelElement the member.
* @return the image.
*/
protected Image _image(XtendMember modelElement) {
final Image img = super._image(modelElement);
return this.diagnoticDecorator.decorateImage(img, modelElement);
}
}