Browse Source

Added comments to autocompletion. Try with gtkmm objects.

merge-requests/365/head
eidheim 11 years ago
parent
commit
2ec8ff15d7
  1. 40
      juci/selectiondialog.cc
  2. 7
      juci/selectiondialog.h
  3. 27
      juci/source.cc
  4. 1
      juci/source.h
  5. 26
      juci/tooltips.cc
  6. 6
      juci/tooltips.h

40
juci/selectiondialog.cc

@ -1,7 +1,5 @@
#include "selectiondialog.h" #include "selectiondialog.h"
#include <regex> #include <regex>
#include <iostream>
using namespace std;
SelectionDialog::SelectionDialog(Gtk::TextView& text_view): text_view(text_view) { SelectionDialog::SelectionDialog(Gtk::TextView& text_view): text_view(text_view) {
@ -33,6 +31,9 @@ void SelectionDialog::show() {
select(); select();
} }
}); });
list_view_text->signal_cursor_changed().connect([this](){
cursor_changed();
});
list_view_text->signal_realize().connect([this](){ list_view_text->signal_realize().connect([this](){
resize(); resize();
}); });
@ -69,24 +70,26 @@ void SelectionDialog::hide() {
void SelectionDialog::select(bool hide_window) { void SelectionDialog::select(bool hide_window) {
selected=true; selected=true;
auto selected=list_view_text->get_selected(); auto selected=list_view_text->get_selected();
std::string select; std::pair<std::string, std::string> select;
if(selected.size()>0) { if(selected.size()>0) {
select = rows.at(list_view_text->get_text(selected[0])); select = rows.at(list_view_text->get_text(selected[0]));
text_view.get_buffer()->erase(start_mark->get_iter(), text_view.get_buffer()->get_insert()->get_iter()); text_view.get_buffer()->erase(start_mark->get_iter(), text_view.get_buffer()->get_insert()->get_iter());
text_view.get_buffer()->insert(start_mark->get_iter(), select); text_view.get_buffer()->insert(start_mark->get_iter(), select.first);
} }
if(hide_window) { if(hide_window) {
if(tooltips)
tooltips->hide();
hide(); hide();
char find_char=select.back(); char find_char=select.first.back();
if(find_char==')' || find_char=='>') { if(find_char==')' || find_char=='>') {
if(find_char==')') if(find_char==')')
find_char='('; find_char='(';
else else
find_char='<'; find_char='<';
size_t pos=select.find(find_char); size_t pos=select.first.find(find_char);
if(pos!=std::string::npos) { if(pos!=std::string::npos) {
auto start_offset=start_mark->get_iter().get_offset()+pos+1; auto start_offset=start_mark->get_iter().get_offset()+pos+1;
auto end_offset=start_mark->get_iter().get_offset()+select.size()-1; auto end_offset=start_mark->get_iter().get_offset()+select.first.size()-1;
if(start_offset!=end_offset) if(start_offset!=end_offset)
text_view.get_buffer()->select_range(text_view.get_buffer()->get_iter_at_offset(start_offset), text_view.get_buffer()->get_iter_at_offset(end_offset)); text_view.get_buffer()->select_range(text_view.get_buffer()->get_iter_at_offset(start_offset), text_view.get_buffer()->get_iter_at_offset(end_offset));
} }
@ -137,6 +140,7 @@ bool SelectionDialog::on_key_press(GdkEventKey* key) {
} }
} }
select(false); select(false);
cursor_changed();
return true; return true;
} }
if(key->keyval==GDK_KEY_Up) { if(key->keyval==GDK_KEY_Up) {
@ -149,6 +153,7 @@ bool SelectionDialog::on_key_press(GdkEventKey* key) {
} }
} }
select(false); select(false);
cursor_changed();
return true; return true;
} }
if(key->keyval==GDK_KEY_Return || key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) { if(key->keyval==GDK_KEY_Return || key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) {
@ -159,6 +164,27 @@ bool SelectionDialog::on_key_press(GdkEventKey* key) {
return false; return false;
} }
void SelectionDialog::cursor_changed() {
if(tooltips)
tooltips->hide();
auto selected=list_view_text->get_selected();
if(selected.size()>0) {
auto select = rows.at(list_view_text->get_text(selected[0]));
if(select.second.size()>0) {
tooltips=std::unique_ptr<Tooltips>(new Tooltips());
auto tooltip_text=select.second;
auto get_tooltip_buffer=[this, tooltip_text]() {
auto tooltip_buffer=Gtk::TextBuffer::create(text_view.get_buffer()->get_tag_table());
//TODO: Insert newlines to tooltip_text (use 80 chars, then newline?)
tooltip_buffer->insert_at_cursor(tooltip_text);
return tooltip_buffer;
};
tooltips->emplace_back(get_tooltip_buffer, text_view, text_view.get_buffer()->create_mark(start_mark->get_iter()), text_view.get_buffer()->create_mark(text_view.get_buffer()->get_insert()->get_iter()));
tooltips->show(true);
}
}
}
void SelectionDialog::move() { void SelectionDialog::move() {
INFO("SelectionDialog set position"); INFO("SelectionDialog set position");
Gdk::Rectangle rectangle; Gdk::Rectangle rectangle;

7
juci/selectiondialog.h

@ -3,6 +3,7 @@
#include "gtkmm.h" #include "gtkmm.h"
#include "logging.h" #include "logging.h"
#include "tooltips.h"
class SelectionDialog { class SelectionDialog {
public: public:
@ -14,12 +15,13 @@ public:
bool on_key_release(GdkEventKey* key); bool on_key_release(GdkEventKey* key);
bool on_key_press(GdkEventKey* key); bool on_key_press(GdkEventKey* key);
std::map<std::string, std::string> rows; std::map<std::string, std::pair<std::string, std::string> > rows;
bool shown=false; bool shown=false;
private: private:
void resize(); void resize();
void select(bool hide_window=true); void select(bool hide_window=true);
void cursor_changed();
Gtk::Entry search_entry; Gtk::Entry search_entry;
Glib::RefPtr<Gtk::TextBuffer::Mark> start_mark; Glib::RefPtr<Gtk::TextBuffer::Mark> start_mark;
@ -30,6 +32,7 @@ private:
std::unique_ptr<Gtk::Window> window; std::unique_ptr<Gtk::Window> window;
std::unique_ptr<Gtk::ScrolledWindow> scrolled_window; std::unique_ptr<Gtk::ScrolledWindow> scrolled_window;
std::unique_ptr<Gtk::ListViewText> list_view_text; std::unique_ptr<Gtk::ListViewText> list_view_text;
std::unique_ptr<Tooltips> tooltips;
}; };
#endif // JUCI_SELECTIONDIALOG_H_ #endif // JUCI_SELECTIONDIALOG_H_

27
juci/source.cc

@ -287,12 +287,16 @@ get_autocomplete_suggestions(int line_number, int column, std::map<std::string,
line_number, line_number,
column-1); column-1);
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
const vector<clang::CompletionChunk> chunks_ = results.get(i).get_chunks(); auto result=results.get(i);
std::vector<AutoCompleteChunk> chunks; const vector<clang::CompletionChunk> chunks_ = result.get_chunks();
for (auto &chunk : chunks_) { if(chunks_.size()>0) {
chunks.emplace_back(chunk); std::vector<AutoCompleteChunk> chunks;
for (auto &chunk : chunks_) {
chunks.emplace_back(chunk);
}
suggestions.emplace_back(chunks);
suggestions.back().brief_comments=result.get_brief_comments();
} }
suggestions.emplace_back(chunks);
} }
DEBUG("Number of suggestions"); DEBUG("Number of suggestions");
DEBUG_VAR(suggestions.size()); DEBUG_VAR(suggestions.size());
@ -418,7 +422,7 @@ void Source::ClangView::update_types() {
auto get_tooltip_buffer=[this, c]() { auto get_tooltip_buffer=[this, c]() {
auto tooltip_buffer=Gtk::TextBuffer::create(get_buffer()->get_tag_table()); auto tooltip_buffer=Gtk::TextBuffer::create(get_buffer()->get_tag_table());
tooltip_buffer->insert_at_cursor("Type: "+(*clang_tokens)[c].type); tooltip_buffer->insert_at_cursor("Type: "+(*clang_tokens)[c].type);
auto brief_comment=clang_tokens->get_brief_comment(c); auto brief_comment=clang_tokens->get_brief_comments(c);
if(brief_comment!="") if(brief_comment!="")
tooltip_buffer->insert_at_cursor("\n\n"+brief_comment); tooltip_buffer->insert_at_cursor("\n\n"+brief_comment);
return tooltip_buffer; return tooltip_buffer;
@ -573,7 +577,7 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) {
std::shared_ptr<std::vector<Source::AutoCompleteData> > ac_data=std::make_shared<std::vector<Source::AutoCompleteData> >(); std::shared_ptr<std::vector<Source::AutoCompleteData> > ac_data=std::make_shared<std::vector<Source::AutoCompleteData> >();
autocomplete_done_function=[this, ac_data](){ autocomplete_done_function=[this, ac_data](){
if(!autocomplete_cancel) { if(!autocomplete_cancel) {
std::map<std::string, std::string> rows; std::map<std::string, std::pair<std::string, std::string> > rows;
for (auto &data : *ac_data) { for (auto &data : *ac_data) {
std::stringstream ss; std::stringstream ss;
std::string return_value; std::string return_value;
@ -587,11 +591,12 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) {
} }
} }
if (ss.str().length() > 0) { // if length is 0 the result is empty if (ss.str().length() > 0) { // if length is 0 the result is empty
rows[ss.str() + " --> " + return_value] = ss.str(); auto pair=std::pair<std::string, std::string>(ss.str(), data.brief_comments);
rows[ss.str() + " --> " + return_value] = pair;
} }
} }
if (rows.empty()) { if (rows.empty()) {
rows["No suggestions found..."] = ""; rows["No suggestions found..."] = std::pair<std::string, std::string>();
} }
selection_dialog.rows=std::move(rows); selection_dialog.rows=std::move(rows);
selection_dialog.show(); selection_dialog.show();
@ -614,8 +619,8 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) {
autocomplete_thread.detach(); autocomplete_thread.detach();
} }
else { else {
std::map<std::string, std::string> rows; std::map<std::string, std::pair<std::string, std::string> > rows;
rows["Autocomplete already running, try again."] = ""; rows["Autocomplete already running, try again."] = std::pair<std::string, std::string>("", "");
selection_dialog.rows=std::move(rows); selection_dialog.rows=std::move(rows);
selection_dialog.show(); selection_dialog.show();
} }

1
juci/source.h

@ -56,6 +56,7 @@ namespace Source {
explicit AutoCompleteData(const std::vector<AutoCompleteChunk> &chunks) : explicit AutoCompleteData(const std::vector<AutoCompleteChunk> &chunks) :
chunks(chunks) { } chunks(chunks) { }
std::vector<AutoCompleteChunk> chunks; std::vector<AutoCompleteChunk> chunks;
std::string brief_comments;
}; };
class View : public Gsv::View { class View : public Gsv::View {

26
juci/tooltips.cc

@ -16,8 +16,8 @@ Tooltip::~Tooltip() {
void Tooltip::update() { void Tooltip::update() {
auto iter=start_mark->get_iter(); auto iter=start_mark->get_iter();
auto end_iter=end_mark->get_iter(); auto end_iter=end_mark->get_iter();
text_view.get_iter_location(iter, activation_rectangle);
if(iter.get_offset()<end_iter.get_offset()) { if(iter.get_offset()<end_iter.get_offset()) {
text_view.get_iter_location(iter, activation_rectangle);
iter++; iter++;
while(iter!=end_iter) { while(iter!=end_iter) {
Gdk::Rectangle rectangle; Gdk::Rectangle rectangle;
@ -32,7 +32,7 @@ void Tooltip::update() {
activation_rectangle.set_y(location_window_y); activation_rectangle.set_y(location_window_y);
} }
void Tooltip::adjust() { void Tooltip::adjust(bool disregard_drawn) {
if(!window) { if(!window) {
//init window //init window
window=std::unique_ptr<Gtk::Window>(new Gtk::Window(Gtk::WindowType::WINDOW_POPUP)); window=std::unique_ptr<Gtk::Window>(new Gtk::Window(Gtk::WindowType::WINDOW_POPUP));
@ -63,13 +63,15 @@ void Tooltip::adjust() {
rectangle.set_width(tooltip_width); rectangle.set_width(tooltip_width);
rectangle.set_height(tooltip_height); rectangle.set_height(tooltip_height);
if(Tooltips::drawn_tooltips_rectangle.get_width()!=0) { if(!disregard_drawn) {
if(rectangle.intersects(Tooltips::drawn_tooltips_rectangle)) if(Tooltips::drawn_tooltips_rectangle.get_width()!=0) {
rectangle.set_y(Tooltips::drawn_tooltips_rectangle.get_y()-tooltip_height); if(rectangle.intersects(Tooltips::drawn_tooltips_rectangle))
Tooltips::drawn_tooltips_rectangle.join(rectangle); rectangle.set_y(Tooltips::drawn_tooltips_rectangle.get_y()-tooltip_height);
Tooltips::drawn_tooltips_rectangle.join(rectangle);
}
else
Tooltips::drawn_tooltips_rectangle=rectangle;
} }
else
Tooltips::drawn_tooltips_rectangle=rectangle;
window->move(rectangle.get_x(), rectangle.get_y()); window->move(rectangle.get_x(), rectangle.get_y());
} }
@ -79,11 +81,11 @@ bool Tooltip::tooltip_on_motion_notify_event(GdkEventMotion* event) {
return false; return false;
} }
void Tooltips::show(const Gdk::Rectangle& rectangle) { void Tooltips::show(const Gdk::Rectangle& rectangle, bool disregard_drawn) {
for(auto& tooltip: *this) { for(auto& tooltip: *this) {
tooltip.update(); tooltip.update();
if(rectangle.intersects(tooltip.activation_rectangle)) { if(rectangle.intersects(tooltip.activation_rectangle)) {
tooltip.adjust(); tooltip.adjust(disregard_drawn);
tooltip.window->show_all(); tooltip.window->show_all();
} }
else if(tooltip.window) else if(tooltip.window)
@ -91,10 +93,10 @@ void Tooltips::show(const Gdk::Rectangle& rectangle) {
} }
} }
void Tooltips::show() { void Tooltips::show(bool disregard_drawn) {
for(auto& tooltip: *this) { for(auto& tooltip: *this) {
tooltip.update(); tooltip.update();
tooltip.adjust(); tooltip.adjust(disregard_drawn);
tooltip.window->show_all(); tooltip.window->show_all();
} }
} }

6
juci/tooltips.h

@ -10,7 +10,7 @@ public:
~Tooltip(); ~Tooltip();
void update(); void update();
void adjust(); void adjust(bool disregard_drawn=false);
Gdk::Rectangle activation_rectangle; Gdk::Rectangle activation_rectangle;
std::unique_ptr<Gtk::Window> window; std::unique_ptr<Gtk::Window> window;
@ -28,8 +28,8 @@ private:
class Tooltips : public std::list<Tooltip> { class Tooltips : public std::list<Tooltip> {
public: public:
void init() {drawn_tooltips_rectangle=Gdk::Rectangle();} void init() {drawn_tooltips_rectangle=Gdk::Rectangle();}
void show(const Gdk::Rectangle& rectangle); void show(const Gdk::Rectangle& rectangle, bool disregard_drawn=false);
void show(); void show(bool disregard_drawn=false);
void hide(); void hide();
static Gdk::Rectangle drawn_tooltips_rectangle; static Gdk::Rectangle drawn_tooltips_rectangle;

Loading…
Cancel
Save