Skip to content

Commit 04492f2

Browse files
Add Code complete support (#62)
Fixes #3
1 parent 213c9a6 commit 04492f2

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/xinterpreter.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,21 @@ __get_cxx_version ()
248248
return kernel_res;
249249
}
250250

251-
nl::json interpreter::complete_request_impl(const std::string& /*code*/, int cursor_pos)
251+
nl::json interpreter::complete_request_impl(const std::string& code, int cursor_pos)
252252
{
253-
return xeus::create_complete_reply(
254-
nl::json::array(), /*matches*/
255-
cursor_pos, /*cursor_start*/
256-
cursor_pos /*cursor_end*/
253+
std::vector<std::string> results;
254+
255+
// split the input to have only the word in the back of the cursor
256+
std::string delims = " \t\n`!@#$^&*()=+[{]}\\|;:\'\",<>?.";
257+
std::size_t _cursor_pos = cursor_pos;
258+
auto text = split_line(code, delims, _cursor_pos);
259+
std::string to_complete = text.back().c_str();
260+
261+
Cpp::CodeComplete(results, code.c_str(), 1, _cursor_pos + 1);
262+
263+
return xeus::create_complete_reply(results /*matches*/,
264+
cursor_pos - to_complete.length() /*cursor_start*/,
265+
cursor_pos /*cursor_end*/
257266
);
258267
}
259268

src/xparser.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
#include "xparser.hpp"
1111

1212
#include <cstddef>
13+
#include <regex>
14+
#include <sstream>
1315
#include <string>
16+
#include <vector>
1417

1518
namespace xcpp
1619
{
@@ -26,4 +29,29 @@ namespace xcpp
2629
std::size_t last = str.find_last_not_of(' ');
2730
return str.substr(first, last - first + 1);
2831
}
32+
33+
std::vector<std::string>
34+
split_line(const std::string& input, const std::string& delims, std::size_t cursor_pos)
35+
{
36+
// passing -1 as the submatch index parameter performs splitting
37+
std::vector<std::string> result;
38+
std::stringstream ss;
39+
40+
ss << "[";
41+
for (auto c : delims)
42+
{
43+
ss << "\\" << c;
44+
}
45+
ss << "]";
46+
47+
std::regex re(ss.str());
48+
49+
std::copy(
50+
std::sregex_token_iterator(input.begin(), input.begin() + cursor_pos + 1, re, -1),
51+
std::sregex_token_iterator(),
52+
std::back_inserter(result)
53+
);
54+
55+
return result;
56+
}
2957
}

src/xparser.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
#include "xeus-cpp/xeus_cpp_config.hpp"
1414

1515
#include <string>
16+
#include <vector>
1617

1718
namespace xcpp
18-
{
19+
{
1920
XEUS_CPP_API
2021
std::string trim(const std::string& str);
22+
23+
XEUS_CPP_API std::vector<std::string>
24+
split_line(const std::string& input, const std::string& delims, std::size_t cursor_pos);
2125
}
2226
#endif

0 commit comments

Comments
 (0)