From 7cab14c306901968c9902ce351b88912ed70f989 Mon Sep 17 00:00:00 2001 From: Alexander Penev Date: Fri, 3 May 2024 06:17:44 +0000 Subject: [PATCH 1/2] Fix --- src/xinterpreter.cpp | 19 ++++++++++++++----- src/xparser.cpp | 27 +++++++++++++++++++++++++++ src/xparser.hpp | 6 +++++- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index fdef663f..87b86897 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -248,12 +248,21 @@ __get_cxx_version () return kernel_res; } - nl::json interpreter::complete_request_impl(const std::string& /*code*/, int cursor_pos) + nl::json interpreter::complete_request_impl(const std::string& code, int cursor_pos) { - return xeus::create_complete_reply( - nl::json::array(), /*matches*/ - cursor_pos, /*cursor_start*/ - cursor_pos /*cursor_end*/ + std::vector results; + + // split the input to have only the word in the back of the cursor + std::string delims = " \t\n`!@#$^&*()=+[{]}\\|;:\'\",<>?."; + std::size_t _cursor_pos = cursor_pos; + auto text = split_line(code, delims, _cursor_pos); + std::string to_complete = text.back().c_str(); + + Cpp::CodeComplete(results, code.c_str(), 1, _cursor_pos + 1); + + return xeus::create_complete_reply(results /*matches*/, + cursor_pos - to_complete.length() /*cursor_start*/, + cursor_pos /*cursor_end*/ ); } diff --git a/src/xparser.cpp b/src/xparser.cpp index 8be51bfa..db434214 100644 --- a/src/xparser.cpp +++ b/src/xparser.cpp @@ -10,7 +10,9 @@ #include "xparser.hpp" #include +#include #include +#include namespace xcpp { @@ -26,4 +28,29 @@ namespace xcpp std::size_t last = str.find_last_not_of(' '); return str.substr(first, last - first + 1); } + + std::vector + split_line(const std::string& input, const std::string& delims, std::size_t cursor_pos) + { + // passing -1 as the submatch index parameter performs splitting + std::vector result; + std::stringstream ss; + + ss << "["; + for (auto c : delims) + { + ss << "\\" << c; + } + ss << "]"; + + std::regex re(ss.str()); + + std::copy( + std::sregex_token_iterator(input.begin(), input.begin() + cursor_pos + 1, re, -1), + std::sregex_token_iterator(), + std::back_inserter(result) + ); + + return result; + } } diff --git a/src/xparser.hpp b/src/xparser.hpp index d1bb21ce..592abee3 100644 --- a/src/xparser.hpp +++ b/src/xparser.hpp @@ -13,10 +13,14 @@ #include "xeus-cpp/xeus_cpp_config.hpp" #include +#include namespace xcpp -{ +{ XEUS_CPP_API std::string trim(const std::string& str); + + XEUS_CPP_API std::vector + split_line(const std::string& input, const std::string& delims, std::size_t cursor_pos); } #endif From b26ce6bcb2e1742ef835089a63ba274ac70182df Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Thu, 9 May 2024 19:46:19 +0300 Subject: [PATCH 2/2] Add missing include --- src/xparser.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/xparser.cpp b/src/xparser.cpp index db434214..fb81e619 100644 --- a/src/xparser.cpp +++ b/src/xparser.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include