From a27517293c0ff277cd93065f754ba3f086ebff68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 16 Apr 2015 10:00:55 +0200 Subject: [PATCH 1/2] add autocompletion demo --- juci/notebook.cc | 12 ++++++------ juci/source.cc | 33 +++++++++++++++++++++++++++++++++ juci/source.h | 9 ++++++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 695591b..a6a613e 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,4 +1,5 @@ #include "notebook.h" +#include "clangmm.h" Notebook::Model::Model() { cc_extension_ = ".cc"; @@ -388,14 +389,13 @@ void Notebook::Controller::OnBufferChange() { end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); start.backward_char(); word = Buffer(text_vec_.at(page))->get_text(start, end); + std::cout << start.get_line() << std::endl; + std::cout << start.get_line_offset() << std::endl; if (word == ".") { // TODO(Forgie) Zalox,Forgie) Remove TEST - std::vector TEST; - TEST.push_back("toString()"); - TEST.push_back("toLower()"); - TEST.push_back("toUpper()"); - TEST.push_back("fuckOFF()"); - TEST.push_back("fuckOFF()"); + std::vector TEST + = text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, + start.get_line_offset()+2); GeneratePopup(TEST); } } diff --git a/juci/source.cc b/juci/source.cc index 2117b79..5946bf1 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -132,6 +132,39 @@ ReParse(const std::string &buffer) { // fired when a line in the buffer is edited void Source::Controller::OnLineEdit() { } +std::vector Source::Controller:: +GetAutoCompleteSuggestions(int line_number, + int column) { + return model().GetAutoCompleteSuggestions(view().get_buffer() + ->get_text().raw(), + line_number, + column); +} + +std::vector Source::Model:: +GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column) { + + std::vector res; + parsing.lock(); + clang::CodeCompleteResults results(&tu_, + file_path(), + buffer, + line_number, + column); + for (int i = 0; i < results.size(); i++) { + std::stringstream ss; + const vector c = results.get(i).get_chunks(); + for (auto &stringchunk : c) { + ss << stringchunk.chunk(); + } + res.emplace_back(ss.str()); + } + parsing.unlock(); + return res; +} + // sets the filepath for this mvc void Source::Model:: set_file_path(const std::string &file_path) { diff --git a/juci/source.h b/juci/source.h index 67b3d9a..c674110 100644 --- a/juci/source.h +++ b/juci/source.h @@ -95,7 +95,11 @@ namespace Source { const string& project_path() const; // gets the config member const Config& config() const; - ~Model() { } + std::vector + GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column); + ~Model() { } int ReParse(const std::string &buffer); std::vector ExtractTokens(int, int); @@ -121,6 +125,9 @@ namespace Source { Model& model(); void OnNewEmptyFile(); void OnOpenFile(const string &filename); + std::vector + GetAutoCompleteSuggestions(int line_number, + int column); Glib::RefPtr buffer(); private: From 63d69c6ca3f007c440be4e62663f05ee409098df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Thu, 16 Apr 2015 10:58:04 +0200 Subject: [PATCH 2/2] add vector as parameter --- juci/notebook.cc | 7 ++++--- juci/source.cc | 28 ++++++++++++++-------------- juci/source.h | 14 +++++++------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index a6a613e..876e619 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -393,9 +393,10 @@ void Notebook::Controller::OnBufferChange() { std::cout << start.get_line_offset() << std::endl; if (word == ".") { // TODO(Forgie) Zalox,Forgie) Remove TEST - std::vector TEST - = text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, - start.get_line_offset()+2); + std::vector TEST; + text_vec_[page]->GetAutoCompleteSuggestions(start.get_line()+1, + start.get_line_offset()+2, + &TEST); GeneratePopup(TEST); } } diff --git a/juci/source.cc b/juci/source.cc index 5946bf1..baec800 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -132,22 +132,24 @@ ReParse(const std::string &buffer) { // fired when a line in the buffer is edited void Source::Controller::OnLineEdit() { } -std::vector Source::Controller:: +void Source::Controller:: GetAutoCompleteSuggestions(int line_number, - int column) { - return model().GetAutoCompleteSuggestions(view().get_buffer() - ->get_text().raw(), - line_number, - column); + int column, + std::vector *suggestions) { + parsing.lock(); + model().GetAutoCompleteSuggestions(view().get_buffer() + ->get_text().raw(), + line_number, + column, + suggestions); + parsing.unlock(); } -std::vector Source::Model:: +void Source::Model:: GetAutoCompleteSuggestions(const std::string& buffer, int line_number, - int column) { - - std::vector res; - parsing.lock(); + int column, + std::vector *suggestions) { clang::CodeCompleteResults results(&tu_, file_path(), buffer, @@ -159,10 +161,8 @@ GetAutoCompleteSuggestions(const std::string& buffer, for (auto &stringchunk : c) { ss << stringchunk.chunk(); } - res.emplace_back(ss.str()); + suggestions->emplace_back(ss.str()); } - parsing.unlock(); - return res; } // sets the filepath for this mvc diff --git a/juci/source.h b/juci/source.h index c674110..8450807 100644 --- a/juci/source.h +++ b/juci/source.h @@ -95,10 +95,10 @@ namespace Source { const string& project_path() const; // gets the config member const Config& config() const; - std::vector - GetAutoCompleteSuggestions(const std::string& buffer, - int line_number, - int column); + void GetAutoCompleteSuggestions(const std::string& buffer, + int line_number, + int column, + std::vector *suggestions); ~Model() { } int ReParse(const std::string &buffer); std::vector ExtractTokens(int, int); @@ -125,9 +125,9 @@ namespace Source { Model& model(); void OnNewEmptyFile(); void OnOpenFile(const string &filename); - std::vector - GetAutoCompleteSuggestions(int line_number, - int column); + void GetAutoCompleteSuggestions(int line_number, + int column, + std::vector *suggestions); Glib::RefPtr buffer(); private: