From 56a655f09676beb5509d9f33c6589e3cd99a7aad Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 6 Sep 2021 20:17:59 +0200 Subject: [PATCH] Changed preference item source.cleanup_whitespace_characters into two seperate items: add_missing_newline_at_end_of_file_on_save and remove_trailing_whitespace_characters_on_save --- CMakeLists.txt | 2 +- LICENSE | 1 - src/config.cpp | 7 ++++--- src/config.hpp | 3 ++- src/source.cpp | 17 ++++++++++++----- src/source_base.cpp | 13 +++++++------ src/source_base.hpp | 5 +++-- tests/source_test.cpp | 14 +++++++++----- 8 files changed, 38 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e289ca2..ff46984 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.1) project(juci) -set(JUCI_VERSION "1.7.1") +set(JUCI_VERSION "1.7.1.1") set(CPACK_PACKAGE_NAME "jucipp") set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim ") diff --git a/LICENSE b/LICENSE index f4a9222..d0322c9 100644 --- a/LICENSE +++ b/LICENSE @@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/src/config.cpp b/src/config.cpp index 2a60103..8c4a7af 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -123,7 +123,8 @@ void Config::read(const JSON &cfg) { auto source_json = cfg.object("source"); source.style = source_json.string("style"); source.font = source_json.string("font"); - source.cleanup_whitespace_characters = source_json.boolean("cleanup_whitespace_characters", JSON::ParseOptions::accept_string); + source.add_missing_newline_at_end_of_file_on_save = source_json.boolean("add_missing_newline_at_end_of_file_on_save", JSON::ParseOptions::accept_string); + source.remove_trailing_whitespace_characters_on_save = source_json.boolean("remove_trailing_whitespace_characters_on_save", JSON::ParseOptions::accept_string); source.show_whitespace_characters = source_json.string("show_whitespace_characters"); source.format_style_on_save = source_json.boolean("format_style_on_save", JSON::ParseOptions::accept_string); source.format_style_on_save_if_style_file_found = source_json.boolean("format_style_on_save_if_style_file_found", JSON::ParseOptions::accept_string); @@ -251,8 +252,8 @@ std::string Config::default_config() { #endif #endif R"RAW( - "cleanup_whitespace_characters_comment": "Remove trailing whitespace characters on save, and add trailing newline if missing", - "cleanup_whitespace_characters": false, + "add_missing_newline_at_end_of_file_on_save": true, + "remove_trailing_whitespace_characters_on_save": false, "show_whitespace_characters_comment": "Determines what kind of whitespaces should be drawn. Use comma-separated list of: space, tab, newline, nbsp, leading, text, trailing or all", "show_whitespace_characters": "", "format_style_on_save_comment": "Performs style format on save if supported on language in buffer", diff --git a/src/config.hpp b/src/config.hpp index 28d1abc..a3b6a6f 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -68,7 +68,8 @@ public: std::string font; std::string spellcheck_language; - bool cleanup_whitespace_characters; + bool add_missing_newline_at_end_of_file_on_save; + bool remove_trailing_whitespace_characters_on_save; std::string show_whitespace_characters; bool format_style_on_save; diff --git a/src/source.cpp b/src/source.cpp index 4dd4897..b82f8ff 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -389,8 +389,13 @@ Gsv::DrawSpacesFlags Source::View::parse_show_whitespace_characters(const std::s bool Source::View::save() { if(file_path.empty() || !get_buffer()->get_modified()) return false; - if(Config::get().source.cleanup_whitespace_characters) - cleanup_whitespace_characters(); + + get_buffer()->begin_user_action(); + + if(Config::get().source.remove_trailing_whitespace_characters_on_save) + remove_trailing_whitespace_characters(); + if(Config::get().source.add_missing_newline_at_end_of_file_on_save) + add_missing_newline_at_end_of_file(); if(format_style && file_path.filename() != "package.json") { if(Config::get().source.format_style_on_save) @@ -400,6 +405,8 @@ bool Source::View::save() { hide_tooltips(); } + get_buffer()->end_user_action(); + try { auto io_channel = Glib::IOChannel::create_from_file(file_path.string(), "w"); auto start_iter = get_buffer()->begin(); @@ -2677,7 +2684,7 @@ bool Source::View::on_key_press_event_basic(GdkEventKey *event) { // Indent as in current or next line if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && !get_buffer()->get_has_selection() && !iter.starts_line()) { - cleanup_whitespace_characters(iter); + remove_whitespace_characters(iter); iter = get_buffer()->get_insert()->get_iter(); auto condition_iter = iter; @@ -2882,7 +2889,7 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey *event) { // Add * at start of line in comment blocks if(event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) { if(!iter.starts_line() && (!string_tag || (!iter.has_tag(string_tag) && !iter.ends_tag(string_tag)))) { - cleanup_whitespace_characters(iter); + remove_whitespace_characters(iter); iter = get_buffer()->get_insert()->get_iter(); auto start_iter = get_tabs_end_iter(iter.get_line()); @@ -2911,7 +2918,7 @@ bool Source::View::on_key_press_event_bracket_language(GdkEventKey *event) { // Indent depending on if/else/etc and brackets if((event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) && !iter.starts_line()) { - cleanup_whitespace_characters(iter); + remove_whitespace_characters(iter); iter = get_buffer()->get_insert()->get_iter(); auto previous_iter = iter; diff --git a/src/source_base.cpp b/src/source_base.cpp index 565b580..3869bcf 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -893,9 +893,8 @@ std::string Source::BaseView::get_token(const Glib::ustring &string, size_t pos) return string.substr(start, end - start); } -void Source::BaseView::cleanup_whitespace_characters() { +void Source::BaseView::remove_trailing_whitespace_characters() { auto buffer = get_buffer(); - buffer->begin_user_action(); for(int line = 0; line < buffer->get_line_count(); line++) { auto iter = buffer->get_iter_at_line(line); auto end_iter = get_iter_at_line_end(line); @@ -910,13 +909,15 @@ void Source::BaseView::cleanup_whitespace_characters() { continue; buffer->erase(iter, end_iter); } - auto iter = buffer->end(); +} + +void Source::BaseView::add_missing_newline_at_end_of_file() { + auto iter = get_buffer()->end(); if(!iter.starts_line()) - buffer->insert(buffer->end(), "\n"); - buffer->end_user_action(); + get_buffer()->insert(iter, "\n"); } -void Source::BaseView::cleanup_whitespace_characters(const Gtk::TextIter &iter) { +void Source::BaseView::remove_whitespace_characters(const Gtk::TextIter &iter) { auto start_blank_iter = iter; auto end_blank_iter = iter; while((*end_blank_iter == ' ' || *end_blank_iter == '\t') && diff --git a/src/source_base.hpp b/src/source_base.hpp index 2cb482c..87399b6 100644 --- a/src/source_base.hpp +++ b/src/source_base.hpp @@ -177,8 +177,9 @@ namespace Source { std::pair get_token_iters(Gtk::TextIter iter); std::string get_token(const Gtk::TextIter &iter); std::string get_token(const Glib::ustring &string, size_t pos = 0); - void cleanup_whitespace_characters(); - void cleanup_whitespace_characters(const Gtk::TextIter &iter); + void remove_trailing_whitespace_characters(); + void add_missing_newline_at_end_of_file(); + void remove_whitespace_characters(const Gtk::TextIter &iter); bool enable_multiple_cursors = false; diff --git a/tests/source_test.cpp b/tests/source_test.cpp index 1cec392..c7b372e 100644 --- a/tests/source_test.cpp +++ b/tests/source_test.cpp @@ -3,13 +3,13 @@ #include "source.hpp" #include -std::string hello_world = R"(#include +std::string hello_world_with_trailing_whitespaces = R"(#include int main() { std::cout << "hello world\n"; })"; -std::string hello_world_cleaned = R"(#include +std::string hello_world = R"(#include int main() { std::cout << "hello world\n"; @@ -30,14 +30,18 @@ int main() { { Source::View view(source_file, Glib::RefPtr()); - view.get_buffer()->set_text(hello_world); + view.get_buffer()->set_text(hello_world_with_trailing_whitespaces); g_assert(view.save()); } Source::View view(source_file, Glib::RefPtr()); + g_assert(view.get_buffer()->get_text() == hello_world_with_trailing_whitespaces); + view.remove_trailing_whitespace_characters(); + view.add_missing_newline_at_end_of_file(); + g_assert(view.get_buffer()->get_text() == hello_world); + view.remove_trailing_whitespace_characters(); + view.add_missing_newline_at_end_of_file(); g_assert(view.get_buffer()->get_text() == hello_world); - view.cleanup_whitespace_characters(); - g_assert(view.get_buffer()->get_text() == hello_world_cleaned); g_assert(boost::filesystem::remove(source_file)); g_assert(!boost::filesystem::exists(source_file));