Browse Source

Reparsing now when buffer has not changed in 1 sec. Some cleanup as well.

merge-requests/365/head
eidheim 11 years ago
parent
commit
e368e9653b
  1. 7
      juci/selectiondialog.cc
  2. 79
      juci/source.cc
  3. 16
      juci/source.h

7
juci/selectiondialog.cc

@ -11,7 +11,7 @@ void SelectionDialog::show() {
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));
scrolled_window=std::unique_ptr<Gtk::ScrolledWindow>(new Gtk::ScrolledWindow()); scrolled_window=std::unique_ptr<Gtk::ScrolledWindow>(new Gtk::ScrolledWindow());
list_view_text=std::unique_ptr<Gtk::ListViewText>(new Gtk::ListViewText(1, false, Gtk::SelectionMode::SELECTION_SINGLE)); list_view_text=std::unique_ptr<Gtk::ListViewText>(new Gtk::ListViewText(1, false, Gtk::SelectionMode::SELECTION_BROWSE));
window->set_default_size(0, 0); window->set_default_size(0, 0);
window->property_decorated()=false; window->property_decorated()=false;
@ -22,7 +22,7 @@ void SelectionDialog::show() {
list_view_text->set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); list_view_text->set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL);
list_view_text->set_activate_on_single_click(true); list_view_text->set_activate_on_single_click(true);
list_view_text->set_search_entry(search_entry); list_view_text->set_search_entry(search_entry);
list_view_text->set_hover_selection(true); list_view_text->set_hover_selection(false);
list_view_text->set_rules_hint(true); list_view_text->set_rules_hint(true);
//list_view_text->set_fixed_height_mode(true); //TODO: This is buggy on OS X, remember to post an issue on GTK+ 3 //list_view_text->set_fixed_height_mode(true); //TODO: This is buggy on OS X, remember to post an issue on GTK+ 3
@ -108,7 +108,6 @@ bool SelectionDialog::on_key_release(GdkEventKey* key) {
auto text=text_view.get_buffer()->get_text(start_mark->get_iter(), text_view.get_buffer()->get_insert()->get_iter()); auto text=text_view.get_buffer()->get_text(start_mark->get_iter(), text_view.get_buffer()->get_insert()->get_iter());
if(text.size()>0) { if(text.size()>0) {
search_entry.set_text(text); search_entry.set_text(text);
list_view_text->set_search_entry(search_entry);
} }
} }
@ -173,7 +172,7 @@ void SelectionDialog::cursor_changed() {
if(select.second.size()>0) { if(select.second.size()>0) {
tooltips=std::unique_ptr<Tooltips>(new Tooltips()); tooltips=std::unique_ptr<Tooltips>(new Tooltips());
auto tooltip_text=select.second; auto tooltip_text=select.second;
auto get_tooltip_buffer=[this, tooltip_text]() { auto get_tooltip_buffer=[this, tooltip_text]() {
auto tooltip_buffer=Gtk::TextBuffer::create(text_view.get_buffer()->get_tag_table()); auto tooltip_buffer=Gtk::TextBuffer::create(text_view.get_buffer()->get_tag_table());
//TODO: Insert newlines to tooltip_text (use 80 chars, then newline?) //TODO: Insert newlines to tooltip_text (use 80 chars, then newline?)
tooltip_buffer->insert_at_cursor(tooltip_text); tooltip_buffer->insert_at_cursor(tooltip_text);

79
juci/source.cc

@ -170,7 +170,7 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
start_offset, start_offset,
end_offset, end_offset,
&ClangView::clang_index); &ClangView::clang_index);
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); //TODO: replace get_source_buffer()->get_text().size() update_syntax();
//GTK-calls must happen in main thread, so the parse_thread //GTK-calls must happen in main thread, so the parse_thread
//sends signals to the main thread that it is to call the following functions: //sends signals to the main thread that it is to call the following functions:
@ -188,10 +188,10 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
if(parse_thread_mapped) { if(parse_thread_mapped) {
if(parsing_mutex.try_lock()) { if(parsing_mutex.try_lock()) {
INFO("Updating syntax"); INFO("Updating syntax");
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size())); update_syntax();
update_diagnostics(); update_diagnostics();
update_types(); update_types();
clang_updated=true; clang_readable=true;
parsing_mutex.unlock(); parsing_mutex.unlock();
INFO("Syntax updated"); INFO("Syntax updated");
} }
@ -221,17 +221,20 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
} }
} }
}); });
autocomplete_done.connect([this](){
if(autocomplete_done_function)
autocomplete_done_function();
});
get_source_buffer()->signal_changed().connect([this]() { get_source_buffer()->signal_changed().connect([this]() {
autocomplete_cancel=true; cancel_show_autocomplete=true;
parse_thread_mapped=false; parse_thread_mapped=false;
clang_updated=false; delayed_reparse_connection.disconnect();
parse_thread_go=true; if(!selection_dialog.shown) {
delayed_reparse_connection=Glib::signal_timeout().connect([this]() {
clang_readable=false;
parse_thread_go=true;
return false;
}, 1000);
}
type_tooltips.hide();
diagnostic_tooltips.hide();
}); });
signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false);
@ -263,6 +266,10 @@ init_syntax_highlighting(const std::map<std::string, std::string>
file_path, file_path,
arguments, arguments,
buffers)); buffers));
clang::SourceLocation start(clang_tu.get(), file_path, 0);
clang::SourceLocation end(clang_tu.get(), file_path, buffers.find(file_path)->second.size()-1);
clang::SourceRange range(&start, &end);
clang_tokens=std::unique_ptr<clang::Tokens>(new clang::Tokens(clang_tu.get(), &range));
} }
std::map<std::string, std::string> Source::ClangView:: std::map<std::string, std::string> Source::ClangView::
@ -274,7 +281,12 @@ get_buffer_map() const {
int Source::ClangView:: int Source::ClangView::
reparse(const std::map<std::string, std::string> &buffer) { reparse(const std::map<std::string, std::string> &buffer) {
return clang_tu->ReparseTranslationUnit(file_path, buffer); int status = clang_tu->ReparseTranslationUnit(file_path, buffer);
clang::SourceLocation start(clang_tu.get(), file_path, 0);
clang::SourceLocation end(clang_tu.get(), file_path, parse_thread_buffer_map.find(file_path)->second.size()-1);
clang::SourceRange range(&start, &end);
clang_tokens=std::unique_ptr<clang::Tokens>(new clang::Tokens(clang_tu.get(), &range));
return status;
} }
std::vector<Source::AutoCompleteData> Source::ClangView:: std::vector<Source::AutoCompleteData> Source::ClangView::
@ -320,13 +332,8 @@ get_compilation_commands() {
return arguments; return arguments;
} }
std::vector<Source::Range> Source::ClangView:: void Source::ClangView::update_syntax() {
extract_tokens(int start_offset, int end_offset) {
std::vector<Source::Range> ranges; std::vector<Source::Range> ranges;
clang::SourceLocation start(clang_tu.get(), file_path, start_offset);
clang::SourceLocation end(clang_tu.get(), file_path, end_offset);
clang::SourceRange range(&start, &end);
clang_tokens=std::unique_ptr<clang::Tokens>(new clang::Tokens(clang_tu.get(), &range));
for (auto &token : *clang_tokens) { for (auto &token : *clang_tokens) {
switch (token.kind()) { switch (token.kind()) {
case 0: highlight_cursor(&token, &ranges); break; // PunctuationToken case 0: highlight_cursor(&token, &ranges); break; // PunctuationToken
@ -336,10 +343,6 @@ extract_tokens(int start_offset, int end_offset) {
case 4: highlight_token(&token, &ranges, 705); break; // CommentToken case 4: highlight_token(&token, &ranges, 705); break; // CommentToken
} }
} }
return ranges;
}
void Source::ClangView::update_syntax(const std::vector<Source::Range> &ranges) {
if (ranges.empty() || ranges.size() == 0) { if (ranges.empty() || ranges.size() == 0) {
return; return;
} }
@ -424,7 +427,7 @@ void Source::ClangView::update_types() {
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_comments(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;
}; };
@ -435,8 +438,8 @@ void Source::ClangView::update_types() {
} }
bool Source::ClangView::clangview_on_motion_notify_event(GdkEventMotion* event) { bool Source::ClangView::clangview_on_motion_notify_event(GdkEventMotion* event) {
on_mark_set_timeout_connection.disconnect(); delayed_tooltips_connection.disconnect();
if(clang_updated) { if(clang_readable) {
Gdk::Rectangle rectangle(event->x, event->y, 1, 1); Gdk::Rectangle rectangle(event->x, event->y, 1, 1);
diagnostic_tooltips.init(); diagnostic_tooltips.init();
type_tooltips.show(rectangle); type_tooltips.show(rectangle);
@ -452,16 +455,16 @@ bool Source::ClangView::clangview_on_motion_notify_event(GdkEventMotion* event)
void Source::ClangView::clangview_on_mark_set(const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark) { void Source::ClangView::clangview_on_mark_set(const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark) {
if(get_buffer()->get_has_selection() && mark->get_name()=="selection_bound") if(get_buffer()->get_has_selection() && mark->get_name()=="selection_bound")
on_mark_set_timeout_connection.disconnect(); delayed_tooltips_connection.disconnect();
if(mark->get_name()=="insert") { if(mark->get_name()=="insert") {
autocomplete_cancel=true; cancel_show_autocomplete=true;
if(selection_dialog.shown) { if(selection_dialog.shown) {
selection_dialog.hide(); selection_dialog.hide();
} }
on_mark_set_timeout_connection.disconnect(); delayed_tooltips_connection.disconnect();
on_mark_set_timeout_connection=Glib::signal_timeout().connect([this]() { delayed_tooltips_connection=Glib::signal_timeout().connect([this]() {
if(clang_updated) { if(clang_readable) {
Gdk::Rectangle rectangle; Gdk::Rectangle rectangle;
get_iter_location(get_buffer()->get_insert()->get_iter(), rectangle); get_iter_location(get_buffer()->get_insert()->get_iter(), rectangle);
int location_window_x, location_window_y; int location_window_x, location_window_y;
@ -481,7 +484,7 @@ void Source::ClangView::clangview_on_mark_set(const Gtk::TextBuffer::iterator& i
} }
bool Source::ClangView::clangview_on_focus_out_event(GdkEventFocus* event) { bool Source::ClangView::clangview_on_focus_out_event(GdkEventFocus* event) {
on_mark_set_timeout_connection.disconnect(); delayed_tooltips_connection.disconnect();
type_tooltips.hide(); type_tooltips.hide();
diagnostic_tooltips.hide(); diagnostic_tooltips.hide();
return false; return false;
@ -491,7 +494,7 @@ bool Source::ClangView::clangview_on_scroll_event(GdkEventScroll* event) {
if(selection_dialog.shown) if(selection_dialog.shown)
selection_dialog.move(); selection_dialog.move();
on_mark_set_timeout_connection.disconnect(); delayed_tooltips_connection.disconnect();
type_tooltips.hide(); type_tooltips.hide();
diagnostic_tooltips.hide(); diagnostic_tooltips.hide();
return false; return false;
@ -570,13 +573,15 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) {
} else { } else {
return false; return false;
} }
delayed_reparse_connection.disconnect();
if(!autocomplete_running) { if(!autocomplete_running) {
autocomplete_running=true; autocomplete_running=true;
autocomplete_cancel=false; cancel_show_autocomplete=false;
INFO("Source::ClangView::on_key_release getting autocompletions"); INFO("Source::ClangView::on_key_release getting autocompletions");
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_connection.disconnect();
if(!autocomplete_cancel) { autocomplete_done_connection=autocomplete_done.connect([this, ac_data](){
if(!cancel_show_autocomplete) {
std::map<std::string, std::pair<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;
@ -602,7 +607,7 @@ bool Source::ClangView::on_key_release(GdkEventKey* key) {
selection_dialog.show(); selection_dialog.show();
} }
autocomplete_running=false; autocomplete_running=false;
}; });
std::shared_ptr<std::map<std::string, std::string> > buffer_map=std::make_shared<std::map<std::string, std::string> >(); std::shared_ptr<std::map<std::string, std::string> > buffer_map=std::make_shared<std::map<std::string, std::string> >();
(*buffer_map)[file_path]=get_buffer()->get_text(get_buffer()->begin(), get_buffer()->get_insert()->get_iter()); (*buffer_map)[file_path]=get_buffer()->get_text(get_buffer()->begin(), get_buffer()->get_insert()->get_iter());

16
juci/source.h

@ -96,16 +96,16 @@ namespace Source {
clang::Index *index); clang::Index *index);
std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column, std::map<std::string, std::string>& buffer_map); std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column, std::map<std::string, std::string>& buffer_map);
SelectionDialog selection_dialog; SelectionDialog selection_dialog;
int reparse(const std::map<std::string, std::string> &buffers); int reparse(const std::map<std::string, std::string> &buffers);
std::vector<Range> extract_tokens(int, int); void update_syntax();
void update_syntax(const std::vector<Range> &locations);
void update_diagnostics(); void update_diagnostics();
void update_types(); void update_types();
Tooltips diagnostic_tooltips; Tooltips diagnostic_tooltips;
Tooltips type_tooltips; Tooltips type_tooltips;
bool clangview_on_motion_notify_event(GdkEventMotion* event); bool clangview_on_motion_notify_event(GdkEventMotion* event);
void clangview_on_mark_set(const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark); void clangview_on_mark_set(const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark);
sigc::connection on_mark_set_timeout_connection; sigc::connection delayed_tooltips_connection;
bool clangview_on_focus_out_event(GdkEventFocus* event); bool clangview_on_focus_out_event(GdkEventFocus* event);
bool clangview_on_scroll_event(GdkEventScroll* event); bool clangview_on_scroll_event(GdkEventScroll* event);
@ -115,7 +115,7 @@ namespace Source {
std::unique_ptr<clang::TranslationUnit> clang_tu; std::unique_ptr<clang::TranslationUnit> clang_tu;
std::unique_ptr<clang::Tokens> clang_tokens; std::unique_ptr<clang::Tokens> clang_tokens;
bool clang_updated=false; bool clang_readable=false;
void highlight_token(clang::Token *token, void highlight_token(clang::Token *token,
std::vector<Range> *source_ranges, std::vector<Range> *source_ranges,
int token_kind); int token_kind);
@ -125,12 +125,14 @@ namespace Source {
bool on_key_press(GdkEventKey* key); bool on_key_press(GdkEventKey* key);
bool on_key_release(GdkEventKey* key); bool on_key_release(GdkEventKey* key);
Terminal::Controller& terminal; Terminal::Controller& terminal;
std::shared_ptr<Terminal::InProgress> parsing_in_progress;
Glib::Dispatcher autocomplete_done; Glib::Dispatcher autocomplete_done;
std::function<void()> autocomplete_done_function; sigc::connection autocomplete_done_connection;
sigc::connection delayed_reparse_connection;
std::shared_ptr<Terminal::InProgress> parsing_in_progress;
bool autocomplete_running=false; bool autocomplete_running=false;
bool autocomplete_cancel=false; bool cancel_show_autocomplete=false;
Glib::Dispatcher parse_done; Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start; Glib::Dispatcher parse_start;
std::thread parse_thread; std::thread parse_thread;

Loading…
Cancel
Save