Skip to content

Commit 48d8364

Browse files
committed
Added outline generation from ast to worker
1 parent 3b0a42f commit 48d8364

2 files changed

Lines changed: 109 additions & 81 deletions

File tree

cpp.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ define(function(require, exports, module) {
5757
}
5858

5959
clang_tool = plugin;
60-
clang_tool.load();
61-
clang_tool.setArgs(settings.get("project/c_cpp/@compilerArguments").split("\n"));
60+
clang_tool.load(function (err) {
61+
clang_tool.setArgs(settings.get("project/c_cpp/@compilerArguments").split("\n"));
62+
});
6263
});
6364
}
6465

6566
c9.on("connect", loadClangTool);
6667
c9.on("disconnect", function() {
6768
if (clang_tool) {
68-
clang_tool.indexClear();
69-
clang_tool = null;
69+
clang_tool.indexClear(function() {
70+
clang_tool = null;
71+
});
7072
}
7173
});
7274

@@ -99,7 +101,8 @@ define(function(require, exports, module) {
99101
return;
100102

101103
// add / update on index
102-
clang_tool.indexTouch(basedir+ev.path);
104+
if (clang_tool)
105+
clang_tool.indexTouch(basedir+ev.path);
103106
});
104107

105108
// Automatically free memory of closed objects
@@ -125,12 +128,11 @@ define(function(require, exports, module) {
125128
var value = tabManager.focussedTab.document.value;
126129
var path = basedir+tabManager.focussedTab.path;
127130

128-
// add temporary data to index
129-
clang_tool.indexTouchUnsaved(path, value);
130-
131-
// do the code completion
132-
clang_tool.cursorCandidatesAt(path, event.data.pos.row+1, event.data.pos.column+1, function(err, res) {
133-
worker.emit("_completionResult", {data: {id: event.data.id, results: res}});
131+
// add temporary data to index and do code completion
132+
clang_tool.indexTouchUnsaved(path, value, function () {
133+
clang_tool.cursorCandidatesAt(path, event.data.pos.row+1, event.data.pos.column+1, function(err, res) {
134+
worker.emit("_completionResult", {data: {id: event.data.id, results: res}});
135+
});
134136
});
135137
});
136138

@@ -145,7 +147,6 @@ define(function(require, exports, module) {
145147
});
146148

147149
worker.emit("_diagnoseResult", {data: {id: event.data.id, results: res, path: path}});
148-
//worker.emit("_diagnoseResult", {data: {id: event.data.id, results: [], path: path}});
149150
});
150151
});
151152

@@ -157,8 +158,8 @@ define(function(require, exports, module) {
157158
return;
158159

159160
// generate outline
160-
clang_tool.fileOutline(path, function(err, res) {
161-
worker.emit("_outlineResult", {data: {id: event.data.id, results: res}});
161+
clang_tool.fileAst(path, function(err, res) {
162+
worker.emit("_outlineResult", {data: {id: event.data.id, ast: res.children}});
162163
});
163164
});
164165
});
@@ -187,7 +188,7 @@ define(function(require, exports, module) {
187188
settings.setDefaults("project/c_cpp", [
188189
["compilerArguments", "-I/usr/include\n-I/usr/local/include"]
189190
]);
190-
}, plugin);
191+
}, plugin);
191192

192193
// Listen to updates
193194
settings.on("project/c_cpp/@compilerArguments", function(value) {
@@ -197,7 +198,6 @@ define(function(require, exports, module) {
197198

198199
// Add css
199200
ui.insertCss(require("text!./icons.css"), false, plugin);
200-
console.log("Added css", options.staticPrefix, require("text!./icons.css"));
201201
});
202202

203203
// Make sure the plugin is unloaded correctly so that cached translation units get purged

cpp_worker.js

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ define(function(require, exports, module) {
1313

1414
// different completion types
1515
var completion_type = {
16-
namespace_t: 0,
17-
class_t: 1,
18-
attribute_t: 2,
19-
method_t: 3,
20-
parameter_t: 4,
21-
struct_t: 5,
22-
function_t: 6,
23-
enum_t: 7,
24-
enum_static_t: 8,
25-
union_t: 9,
26-
typedef_t: 10,
27-
variable_t: 11,
28-
macro_t: 12,
29-
unkown_t: 13,
16+
namespace_t: 0,
17+
class_t: 1,
18+
attribute_t: 2,
19+
method_t: 3,
20+
parameter_t: 4,
21+
struct_t: 5,
22+
function_t: 6,
23+
enum_t: 7,
24+
enum_static_t: 8,
25+
union_t: 9,
26+
typedef_t: 10,
27+
variable_t: 11,
28+
macro_t: 12,
29+
include_t: 13,
30+
unkown_t: 14
3031
};
3132

3233
// caches last results
@@ -51,7 +52,7 @@ define(function(require, exports, module) {
5152

5253
// check if we handle the language
5354
completer.handlesLanguage = function(language) {
54-
return language === "c_cpp";
55+
return (language === "c_cpp");
5556
};
5657

5758
// do an initial parse to speed things up in the future
@@ -226,64 +227,91 @@ define(function(require, exports, module) {
226227
// create a unique numeric id to identify correct callback relationships
227228
var cId = ++uId;
228229

229-
completer.sender.on("_outlineResult", function invoTmp(ev) {
230-
if (ev.data.id != cId)
231-
return;
232-
233-
// unregister this cb
234-
completer.sender.off("_outlineResult", invoTmp);
235-
var items = [];
236-
237-
// @todo: Add location once available in clang_tool
238-
_.forEach(ev.data.results.includes, function(include) {
239-
items.push({
240-
icon: "c_cpp_include",
241-
name: "<"+include+">",
242-
pos: { sl: 1, sc: 1 },
243-
displayPos: { sl: 1, sc: 1 }
244-
});
245-
});
230+
// returns function arguments for an ast node
231+
var astParam = function(ast) {
232+
var params = [];
246233

247-
_.forEach(ev.data.results.functions, function(func) {
248-
items.push({
249-
icon: "method2",
250-
name: func.name + "(" + func.params.join(", ") + ")",
251-
pos: { sl: 1, sc: 1 },
252-
displayPos: { sl: 1, sc: 1 }
253-
});
234+
_.forEach(ast, function(ele) {
235+
if (ele.cursor == completion_type.parameter_t) {
236+
params.push(ele.type+" "+ele.name);
237+
}
254238
});
255239

256-
_.forEach(ev.data.results.classes, function(c) {
257-
var itemssub = [];
240+
return "("+params.join(", ")+")";
241+
}
258242

259-
_.forEach(c.functions, function(func) {
260-
itemssub.push({
261-
icon: "method2",
262-
name: func.name + "(" + func.params.join(", ") + ")",
263-
pos: { sl: 1, sc: 1 },
264-
displayPos: { sl: 1, sc: 1 }
265-
});
266-
});
243+
// recursive ast parser function
244+
// @todo: icon for class / union / struct enum
245+
// @todo: take access specifier into account (green = public, blue = protected, red = private)
246+
// @todo: handle enum elements
247+
var parseAst = function (ast, item) {
248+
_.forEach(ast, function (ele) {
249+
switch (ele.cursor) {
250+
// includes, no subs
251+
case completion_type.include_t:
252+
item.items.push({
253+
icon: "c_cpp_include",
254+
name: "<"+ele.name+">",
255+
pos: { sl: ele.loc_row, sc: ele.loc_col },
256+
displayPos: { ele.loc_row, sc: ele.loc_col }
257+
});
258+
break;
267259

268-
_.forEach(c.attributes, function(attr) {
269-
itemssub.push({
270-
icon: "property",
271-
name: attr,
272-
pos: { sl: 1, sc: 1 },
273-
displayPos: { sl: 1, sc: 1 }
274-
});
275-
});
260+
// classes, subs maybe other classes, attributes or functions
261+
case completion_type.class_t:
262+
case completion_type.union_t:
263+
case completion_type.struct_t:
264+
case completion_type.enum_t: {
265+
var it = {
266+
icon: "c_cpp_class",
267+
name: ele.name,
268+
pos: { ele.loc_row, sc: ele.loc_col },
269+
displayPos: { ele.loc_row, sc: ele.loc_col },
270+
items: []
271+
};
272+
273+
parseAst(ele.children, it);
274+
item.items.push(it);
275+
} break;
276+
277+
// attributes, no subs
278+
case completion_type.enum_static_t:
279+
case completion_type.attribute_t:
280+
item.items.push({
281+
icon: "property",
282+
name: ele.name,
283+
pos: { ele.loc_row, sc: ele.loc_col },
284+
displayPos: { ele.loc_row, sc: ele.loc_col }
285+
});
286+
break;
276287

277-
items.push({
278-
icon: "c_cpp_class",
279-
name: c.name,
280-
pos: { sl: 1, sc: 1 },
281-
displayPos: { sl: 1, sc: 1 },
282-
items: itemssub
283-
});
288+
// functions and methods
289+
case completion_type.function_t:
290+
case completion_type.method_t: {
291+
var it = {
292+
icon: "method2",
293+
name: ele.name + astParam(ele.children),
294+
pos: { sl: 1, sc: 1 },
295+
displayPos: { sl: 1, sc: 1 },
296+
items: []
297+
};
298+
299+
parseAst(ele.children, it);
300+
item.items.push(it);
301+
} break;
302+
}
284303
});
304+
};
305+
306+
completer.sender.on("_outlineResult", function invoTmp(ev) {
307+
if (ev.data.id != cId)
308+
return;
309+
310+
completer.sender.off("_outlineResult", invoTmp);
311+
var data = {items:[]};
312+
parseAst(ev.data.ast, data);
285313

286-
return callback({ items: items});
314+
return callback(data);
287315
});
288316

289317
// send the data to the server

0 commit comments

Comments
 (0)