From 804b56a2c9d3ed403a6528cd3d870da79feb6dc5 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 12 Aug 2018 07:48:42 +0200 Subject: [PATCH] Added and made use of ScopeGuard class --- src/CMakeLists.txt | 1 + src/source.cc | 38 ++++++++++++++++++++++---------------- src/source_base.cc | 29 ++++++++++------------------- src/utility.cc | 6 ++++++ src/utility.h | 8 ++++++++ 5 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 src/utility.cc create mode 100644 src/utility.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4c9b738..059bfd7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,7 @@ set(JUCI_SHARED_FILES source_spellcheck.cc terminal.cc usages_clang.cc + utility.cc ) if(LIBLLDB_FOUND) list(APPEND JUCI_SHARED_FILES debug_lldb.cc) diff --git a/src/source.cc b/src/source.cc index af4ad8b..332df68 100644 --- a/src/source.cc +++ b/src/source.cc @@ -7,6 +7,7 @@ #include "menu.h" #include "selection_dialog.h" #include "terminal.h" +#include "utility.h" #include #include #include @@ -1331,13 +1332,10 @@ bool Source::View::is_possible_argument() { } bool Source::View::on_key_press_event(GdkEventKey *key) { - class Guard { - public: - bool &value; - Guard(bool &value_) : value(value_) { value = true; } - ~Guard() { value = false; } - }; - Guard guard{enable_multiple_cursors}; + enable_multiple_cursors = true; + ScopeGuard guard{[this] { + enable_multiple_cursors = false; + }}; if(SelectionDialog::get() && SelectionDialog::get()->is_visible()) { if(SelectionDialog::get()->on_key_press(key)) @@ -1717,8 +1715,19 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey *key) { cleanup_whitespace_characters(iter); iter = get_buffer()->get_insert()->get_iter(); - auto condition_iter = iter; - condition_iter.backward_char(); + auto previous_iter = iter; + previous_iter.backward_char(); + // Remove matching bracket highlights that get extended when inserting text in between the brackets + ScopeGuard guard; + if((*previous_iter == '{' && *iter == '}') || (*previous_iter == '(' && *iter == ')') || + (*previous_iter == '[' && *iter == ']') || (*previous_iter == '<' && *iter == '>')) { + get_source_buffer()->set_highlight_matching_brackets(false); + guard.on_exit = [this] { + get_source_buffer()->set_highlight_matching_brackets(true); + }; + } + + auto condition_iter = previous_iter; condition_iter = find_non_whitespace_code_iter_backward(condition_iter); auto start_iter = get_start_of_expression(condition_iter); auto tabs_end_iter = get_tabs_end_iter(start_iter); @@ -2171,13 +2180,10 @@ bool Source::View::on_key_press_event_smart_brackets(GdkEventKey *key) { } bool Source::View::on_key_press_event_smart_inserts(GdkEventKey *key) { - class Guard { - public: - bool &value; - Guard(bool &value_) : value(value_) { value = true; } - ~Guard() { value = false; } - }; - Guard guard{keep_argument_marks}; + keep_argument_marks = true; + ScopeGuard guard{[this] { + keep_argument_marks = false; + }}; if(get_buffer()->get_has_selection()) { bool perform_insertion = false; diff --git a/src/source_base.cc b/src/source_base.cc index 3d4d082..a3e048d 100644 --- a/src/source_base.cc +++ b/src/source_base.cc @@ -3,6 +3,7 @@ #include "git.h" #include "info.h" #include "terminal.h" +#include "utility.h" #include #include @@ -76,18 +77,11 @@ bool Source::BaseView::load(bool not_undoable_action) { disable_spellcheck = true; if(not_undoable_action) get_source_buffer()->begin_not_undoable_action(); - - class Guard { - public: - Source::BaseView *view; - bool not_undoable_action; - ~Guard() { - if(not_undoable_action) - view->get_source_buffer()->end_not_undoable_action(); - view->disable_spellcheck = false; - } - }; - Guard guard{this, not_undoable_action}; + ScopeGuard guard{[this, not_undoable_action] { + if(not_undoable_action) + get_source_buffer()->end_not_undoable_action(); + disable_spellcheck = false; + }}; if(language) { std::ifstream input(file_path.string(), std::ofstream::binary); @@ -668,13 +662,10 @@ void Source::BaseView::cleanup_whitespace_characters(const Gtk::TextIter &iter) } void Source::BaseView::paste() { - class Guard { - public: - bool &value; - Guard(bool &value_) : value(value_) { value = true; } - ~Guard() { value = false; } - }; - Guard guard{enable_multiple_cursors}; + enable_multiple_cursors = true; + ScopeGuard guard{[this] { + enable_multiple_cursors = false; + }}; std::string text = Gtk::Clipboard::get()->wait_for_text(); diff --git a/src/utility.cc b/src/utility.cc new file mode 100644 index 0000000..370ed3c --- /dev/null +++ b/src/utility.cc @@ -0,0 +1,6 @@ +#include "utility.h" + +ScopeGuard::~ScopeGuard() { + if(on_exit) + on_exit(); +} diff --git a/src/utility.h b/src/utility.h new file mode 100644 index 0000000..3f0e1a0 --- /dev/null +++ b/src/utility.h @@ -0,0 +1,8 @@ +#pragma once +#include + +class ScopeGuard { +public: + std::function on_exit; + ~ScopeGuard(); +};