forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppInfoTree.js
More file actions
390 lines (321 loc) · 12.1 KB
/
cppInfoTree.js
File metadata and controls
390 lines (321 loc) · 12.1 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require([
'codecompass/model',
'codecompass/viewHandler',
'codecompass/util'],
function (model, viewHandler, util) {
model.addService('cppservice', 'CppService', LanguageServiceClient);
function createTagLabels(tags) {
var label = '';
if (!tags)
return label;
if (tags.indexOf('static') > -1)
label += '<span class="tag tag-static" title="Static">S</span>';
if (tags.indexOf('constructor') > -1)
label += '<span class="tag tag-constructor" title="Constructor">C</span>';
if (tags.indexOf('destructor') > -1)
label += '<span class="tag tag-destructor" title="Destructor">D</span>';
if (tags.indexOf('implicit') > -1)
label += '<span class="tag tag-implicit" title="Implicit">I</span>';
if (tags.indexOf('inherited') > -1)
label += '<span class="tag tag-inherited" title="Inherited">I</span>';
if (tags.indexOf('virtual') > -1)
label += '<span class="tag tag-virtual" title="Virtual">V</span>';
if (tags.indexOf('global') > -1)
label += '<span class="tag tag-global" title="Global">G</span>';
return label;
}
function createReferenceCountLabel(label, count) {
var parsedLabel = $('<div>').append($.parseHTML(label));
parsedLabel.children('span.reference-count').remove();
parsedLabel.append('<span class="reference-count">(' + count + ')</span>');
return parsedLabel.html();
}
function createLabel(astNodeInfo) {
var labelClass = '';
if (astNodeInfo.tags.indexOf('implicit') > -1)
labelClass = 'label-implicit';
var labelValue = astNodeInfo.astNodeValue;
// Create dom node for return type of a function and place it at the end of
// signature.
if (astNodeInfo.symbolType === 'Function') {
var init = labelValue.slice(0, labelValue.indexOf('('));
var returnTypeEnd = init.lastIndexOf(' ');
//--- Constructor, destructor doesn't have return type ---//
if (returnTypeEnd !== -1) {
var funcSignature = init.slice(returnTypeEnd);
labelValue = funcSignature
+ ' : <span class="label-return-type">'
+ init.slice(0, returnTypeEnd)
+ "</span>";
}
}
var label = createTagLabels(astNodeInfo.tags)
+ '<span class="' + labelClass + '">'
+ astNodeInfo.range.range.startpos.line + ':'
+ astNodeInfo.range.range.startpos.column + ': '
+ labelValue
+ '</span>';
return label;
}
function getCssClass(astNodeInfo) {
var tags = astNodeInfo.tags;
return tags.indexOf('public') > -1 ? 'icon-visibility icon-public' :
tags.indexOf('private') > -1 ? 'icon-visibility icon-private' :
tags.indexOf('protected') > -1 ? 'icon-visibility icon-protected' :
null;
}
function groupReferencesByVisibilities(references, parentNode, nodeInfo) {
var res = [];
var visibilities = ['public', 'private', 'protected'];
visibilities.forEach(function (visibility) {
var nodes = references.filter(function (reference) {
return reference.tags.indexOf(visibility) > -1;
});
if (!nodes.length)
return;
res.push({
id : nodeInfo.id + visibility + parentNode.refType,
name : createReferenceCountLabel(visibility, nodes.length),
refType : parentNode.refType,
hasChildren : true,
cssClass : 'icon-visibility icon-' + visibility,
getChildren : function () {
var res = [];
nodes.forEach(function (reference) {
res.push({
id : visibility + reference.id,
name : createLabel(reference),
refType : parentNode.refType,
nodeInfo : reference,
hasChildren : false,
cssClass : getCssClass(reference)
});
});
return res;
}
});
});
return res;
}
function loadReferenceNodes(parentNode, nodeInfo, refTypes, scratch) {
var res = [];
var fileGroupsId = [];
scratch = scratch || {};
var references = model.cppservice.getReferences(
nodeInfo.id,
parentNode.refType);
if (parentNode.refType === refTypes['Method'] ||
parentNode.refType === refTypes['Data member'])
return groupReferencesByVisibilities(references, parentNode, nodeInfo);
references.forEach(function (reference) {
if (parentNode.refType === refTypes['Caller'] ||
parentNode.refType === refTypes['Usage']) {
//--- Group nodes by file name ---//
var fileId = reference.range.file;
if (fileGroupsId[fileId])
return;
fileGroupsId[fileId] = parentNode.refType + fileId + reference.id;
var referenceInFile = references.filter(function (reference) {
return reference.range.file === fileId;
});
var fileInfo = model.project.getFileInfo(fileId);
if (parentNode.refType === refTypes['Caller']) {
scratch.visitedNodeIDs =
(scratch.visitedNodeIDs || []).concat(nodeInfo.id);
}
res.push({
id : fileGroupsId[fileId],
name : createReferenceCountLabel(
fileInfo.name, referenceInFile.length),
refType : parentNode.refType,
hasChildren : true,
cssClass : util.getIconClass(fileInfo.path),
getChildren : function () {
var that = this;
var res = [];
referenceInFile.forEach(function (reference) {
if (parentNode.refType === refTypes['Caller']) {
var showChildren =
scratch.visitedNodeIDs.indexOf(reference.id) == -1;
res.push({
id : reference.id,
name : createLabel(reference),
nodeInfo : reference,
refType : parentNode.refType,
cssClass : 'icon icon-Method',
hasChildren : showChildren,
getChildren : showChildren
? function () {
var res = [];
//--- Recursive Node ---//
var refCount = model.cppservice.getReferenceCount(
reference.id, parentNode.refType);
if (refCount)
res.push({
id : 'Caller-' + reference.id,
name : createReferenceCountLabel(
parentNode.name, refCount),
nodeInfo : reference,
refType : parentNode.refType,
cssClass : parentNode.cssClass,
hasChildren : true,
getChildren : function () {
return loadReferenceNodes(this, reference, refTypes, scratch);
}
});
//--- Call ---//
var calls = model.cppservice.getReferences(
this.nodeInfo.id,
refTypes['This calls']);
calls.forEach(function (call) {
if (call.entityHash === nodeInfo.entityHash)
res.push({
name : createLabel(call),
refType : parentNode.refType,
nodeInfo : call,
hasChildren : false,
cssClass : getCssClass(call)
});
});
return res;
}
: undefined
});
} else if (parentNode.refType === refTypes['Usage']) {
res.push({
id : fileGroupsId[fileId] + reference.id,
name : createLabel(reference),
refType : parentNode.refType,
nodeInfo : reference,
hasChildren : false,
cssClass : getCssClass(reference)
});
}
});
return res;
}
});
} else {
res.push({
name : createLabel(reference),
refType : parentNode.refType,
nodeInfo : reference,
hasChildren : false,
cssClass : getCssClass(reference)
});
}
});
return res;
}
/**
* This function returns file references children.
* @param parentNode Reference type node in Info Tree.
*/
function loadFileReferenceNodes(parentNode) {
var res = [];
var references = model.cppservice.getFileReferences(
parentNode.nodeInfo.id,
parentNode.refType);
references.forEach(function (reference) {
res.push({
name : createLabel(reference),
refType : parentNode.refType,
nodeInfo : reference,
hasChildren : false,
cssClass : getCssClass(reference)
});
});
return res;
}
function createRootNode(elementInfo) {
var rootLabel
= '<span class="root label">'
+ (elementInfo instanceof AstNodeInfo
? elementInfo.symbolType
: 'File')
+ '</span>';
var rootValue
= '<span class="root value">'
+ (elementInfo instanceof AstNodeInfo
? elementInfo.astNodeValue
: elementInfo.name)
+ '</span>';
var label = createTagLabels(elementInfo.tags)
+ '<span class="root label">'
+ rootLabel + ': ' + rootValue
+ '</span>';
return {
id : 'root',
name : label,
cssClass : 'icon-info',
hasChildren : true,
getChildren : function () {
return that._store.query({ parent : 'root' });
}
};
}
var cppInfoTree = {
render : function (elementInfo) {
var ret = [];
ret.push(createRootNode(elementInfo));
if (elementInfo instanceof AstNodeInfo) {
//--- Properties ---//
var props = model.cppservice.getProperties(elementInfo.id);
for (var propName in props) {
var propId = propName.replace(/ /g, '-');
var label
= '<span class="label">' + propName + '</span>: '
+ '<span class="value">' + props[propName] + '</span>';
ret.push({
name : label,
parent : 'root',
nodeInfo : elementInfo,
cssClass : 'icon-' + propId,
hasChildren : false
});
}
//--- References ---//
var refTypes = model.cppservice.getReferenceTypes(elementInfo.id);
for (var refType in refTypes) {
var refCount =
model.cppservice.getReferenceCount(elementInfo.id, refTypes[refType]);
if (refCount)
ret.push({
name : createReferenceCountLabel(refType, refCount),
parent : 'root',
refType : refTypes[refType],
cssClass : 'icon-' + refType.replace(/ /g, '-'),
hasChildren : true,
getChildren : function () {
return loadReferenceNodes(this, elementInfo, refTypes);
}
});
};
} else if (elementInfo instanceof FileInfo) {
//--- File references ---//
var refTypes = model.cppservice.getFileReferenceTypes(elementInfo.id);
for (var refType in refTypes) {
var refCount = model.cppservice.getFileReferenceCount(
elementInfo.id, refTypes[refType]);
if (refCount)
ret.push({
name : createReferenceCountLabel(refType, refCount),
parent : 'root',
nodeInfo : elementInfo,
refType : refTypes[refType],
cssClass : 'icon-' + refType.replace(/ /g, '-'),
hasChildren : true,
getChildren : function () {
return loadFileReferenceNodes(this);
}
});
};
}
return ret;
}
};
viewHandler.registerModule(cppInfoTree, {
type : viewHandler.moduleType.InfoTree,
service : model.cppservice
});
});