Browse Source

Added and made use of ScopeGuard class

merge-requests/389/head
eidheim 7 years ago
parent
commit
804b56a2c9
  1. 1
      src/CMakeLists.txt
  2. 38
      src/source.cc
  3. 29
      src/source_base.cc
  4. 6
      src/utility.cc
  5. 8
      src/utility.h

1
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)

38
src/source.cc

@ -7,6 +7,7 @@
#include "menu.h"
#include "selection_dialog.h"
#include "terminal.h"
#include "utility.h"
#include <boost/property_tree/json_parser.hpp>
#include <boost/spirit/home/qi/char.hpp>
#include <boost/spirit/home/qi/operator.hpp>
@ -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;

29
src/source_base.cc

@ -3,6 +3,7 @@
#include "git.h"
#include "info.h"
#include "terminal.h"
#include "utility.h"
#include <fstream>
#include <gtksourceview/gtksource.h>
@ -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();

6
src/utility.cc

@ -0,0 +1,6 @@
#include "utility.h"
ScopeGuard::~ScopeGuard() {
if(on_exit)
on_exit();
}

8
src/utility.h

@ -0,0 +1,8 @@
#pragma once
#include <functional>
class ScopeGuard {
public:
std::function<void()> on_exit;
~ScopeGuard();
};
Loading…
Cancel
Save