Skip to content

Commit 3b0a42f

Browse files
committed
Added caching and optional callback parameters to clang_tool
1 parent 52bf73f commit 3b0a42f

1 file changed

Lines changed: 47 additions & 7 deletions

File tree

cpp_server.js

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,73 @@ module.exports = function (vfs, options, register) {
2525
};
2626

2727
var clang_obj = null;
28+
var clang_cache = {}; // caches ast and diagnosis for all files
2829

2930
register(null, {
3031
// Should be called when the server is first invoked, do not call multiple times
31-
load: function() {
32+
load: function(cb) {
3233
myLog("[cpp_server load]");
3334
clang_obj = new clang_tool.object;
35+
36+
if (cb)
37+
cb(false);
3438
},
3539

3640
// Sets compiler arguments
37-
setArgs: function(args) {
41+
setArgs: function(args, cb) {
3842
myLog("[cpp_server setArgs]", args);
3943
clang_obj.setArgs(args);
44+
45+
if (cb)
46+
cb(false);
4047
},
4148

4249
// Adds / updates a file on the index
43-
indexTouch: function(file) {
50+
indexTouch: function(file, cb) {
4451
myLog("[cpp_server indexTouch]", file);
4552
clang_obj.indexTouch(file);
53+
54+
// cache diagnosis and ast for the file in the cache
55+
clang_cache[file] = {
56+
diag: clang_obj.fileDiagnose(file),
57+
ast: clang_obj.fileAst(file)
58+
};
59+
60+
if (cb)
61+
cb(false);
4662
},
4763

4864
// Add unsaved contents to index
49-
indexTouchUnsaved: function(file, content) {
65+
indexTouchUnsaved: function(file, content, cb) {
5066
myLog("[cpp_server indexTouchUnsaved]", file);
5167
clang_obj.indexTouchUnsaved(file, content);
68+
69+
if (cb)
70+
cb(false);
5271
},
5372

5473
// Returns memory usage for each file on the index
5574
indexStatus: function(cb) {
56-
myLog("[cpp_server indexStatus]");
75+
myLog("[cpp_server indexStatus]", clang_obj);
5776
cb(false, clang_obj.indexStatus());
5877
},
5978

6079
// Clears the index [for a specifc file]
61-
indexClear: function(file) {
80+
indexClear: function(file, cb) {
6281
if (typeof(file) != "undefined") {
6382
myLog("[cpp_server indexClear]", file);
6483
clang_obj.indexClear(file);
84+
85+
if (typeof(clang_cache[file]) != "undefined")
86+
delete clang_cache[file];
6587
} else {
6688
myLog("[cpp_server indexClear]");
6789
clang_obj.indexClear();
90+
clang_cache = {};
6891
}
92+
93+
if (cb)
94+
cb(false);
6995
},
7096

7197
// Generates file outline
@@ -77,7 +103,21 @@ module.exports = function (vfs, options, register) {
77103
// Returns diagnostic information
78104
fileDiagnose: function(file, cb) {
79105
myLog("[cpp_server fileDiagnose]", file);
80-
cb(false, clang_obj.fileDiagnose(file));
106+
107+
if (typeof(clang_cache[file]) != "undefined")
108+
cb(false, clang_cache[file].diag);
109+
else
110+
cb(false, []);
111+
},
112+
113+
// Returns ast
114+
fileAst: function(file, cb) {
115+
myLog("[cpp_server fileAst]", file);
116+
117+
if (typeof(clang_cache[file]) != "undefined")
118+
cb(false, clang_cache[file].ast);
119+
else
120+
cb(false, {});
81121
},
82122

83123
// Returns potential code completion candidates at a specific location

0 commit comments

Comments
 (0)