Browse Source

Yet another cursor history cleanup

pipelines/235045657
eidheim 6 years ago
parent
commit
706c94301d
  1. 85
      src/notebook.cpp

85
src/notebook.cpp

@ -327,27 +327,62 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position
}); });
// Cursor history // Cursor history
auto update_cursor_locations = [this, view]() { auto add_cursor_location = [this, view](const Gtk::TextIter &iter) {
auto iter = view->get_buffer()->get_insert()->get_iter();
bool moved = false;
if(current_cursor_location != cursor_locations.end()) { if(current_cursor_location != cursor_locations.end()) {
if(current_cursor_location->view == view && abs(current_cursor_location->mark->get_iter().get_line() - iter.get_line()) <= 2) { // Remove history newer than current (create new history branch)
current_cursor_location->view->get_buffer()->move_mark(current_cursor_location->mark, iter); // Move current cursor for(auto it = std::next(current_cursor_location); it != cursor_locations.end();) {
moved = true; it->view->get_buffer()->delete_mark(it->mark);
it = cursor_locations.erase(it);
}
}
current_cursor_location = cursor_locations.emplace(cursor_locations.end(), view, view->get_buffer()->create_mark(iter, false));
if(cursor_locations.size() > 10) {
auto it = cursor_locations.begin();
it->view->get_buffer()->delete_mark(it->mark);
cursor_locations.erase(it);
}
};
view->get_buffer()->signal_mark_set().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::RefPtr<Gtk::TextBuffer::Mark> &mark) {
if(mark->get_name() == "insert") {
if(disable_next_update_cursor_locations) {
disable_next_update_cursor_locations = false;
return;
} }
if(!moved) { auto iter = mark->get_iter();
// Remove history newer than current (create new history branch) if(current_cursor_location != cursor_locations.end()) {
for(auto it = std::next(current_cursor_location); it != cursor_locations.end();) { if(current_cursor_location->view == view && abs(current_cursor_location->mark->get_iter().get_line() - iter.get_line()) <= 2) {
it->view->get_buffer()->delete_mark(it->mark); current_cursor_location->view->get_buffer()->move_mark(current_cursor_location->mark, iter); // Move current cursor
it = cursor_locations.erase(it);
// Combine cursor histories adjacent and similar to current cursor
auto it = std::next(current_cursor_location);
if(it != cursor_locations.end()) {
if(it->view == current_cursor_location->view && abs(it->mark->get_iter().get_line() - current_cursor_location->mark->get_iter().get_line()) <= 2) {
it->view->get_buffer()->delete_mark(it->mark);
cursor_locations.erase(it);
}
}
if(current_cursor_location != cursor_locations.begin()) {
it = std::prev(current_cursor_location);
if(it->view == current_cursor_location->view && abs(it->mark->get_iter().get_line() - current_cursor_location->mark->get_iter().get_line()) <= 2) {
it->view->get_buffer()->delete_mark(it->mark);
cursor_locations.erase(it);
}
}
return;
} }
} }
add_cursor_location(iter);
} }
});
if(!moved) view->get_buffer()->signal_insert().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::ustring & /*text*/, int /*bytes*/) {
current_cursor_location = cursor_locations.emplace(cursor_locations.end(), view, view->get_buffer()->create_mark(iter, false)); if(current_cursor_location == cursor_locations.end() || current_cursor_location->view != view)
add_cursor_location(view->get_buffer()->get_insert()->get_iter());
});
view->get_buffer()->signal_erase().connect([this, view, add_cursor_location](const Gtk::TextBuffer::iterator & /*start*/, const Gtk::TextBuffer::iterator & /*end*/) {
if(current_cursor_location == cursor_locations.end() || current_cursor_location->view != view)
add_cursor_location(view->get_buffer()->get_insert()->get_iter());
// Combine adjacent cursor histories that are similar // Combine adjacent cursor histories that are similar
if(cursor_locations.size() > 1) { if(cursor_locations.size() > 1) {
@ -371,26 +406,6 @@ bool Notebook::open(const boost::filesystem::path &file_path_, Position position
} }
} }
} }
if(!moved) {
if(cursor_locations.size() > 10) {
auto it = cursor_locations.begin();
it->view->get_buffer()->delete_mark(it->mark);
cursor_locations.erase(it);
}
}
};
view->get_buffer()->signal_mark_set().connect([this, update_cursor_locations](const Gtk::TextBuffer::iterator & /*iter*/, const Glib::RefPtr<Gtk::TextBuffer::Mark> &mark) {
if(mark->get_name() == "insert") {
if(disable_next_update_cursor_locations) {
disable_next_update_cursor_locations = false;
return;
}
update_cursor_locations();
}
});
view->get_buffer()->signal_changed().connect([update_cursor_locations] {
update_cursor_locations();
}); });
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG

Loading…
Cancel
Save