diff --git a/src/config.cc b/src/config.cc index 94f9552..19077ad 100644 --- a/src/config.cc +++ b/src/config.cc @@ -146,6 +146,8 @@ void Config::get_source() { source.style=source_json.get("style"); source.font=source_json.get("font"); + source.strip_trailing_whitespaces=source_json.get("strip_trailing_whitespaces"); + source.show_map = source_json.get("show_map"); source.map_font_size = source_json.get("map_font_size"); diff --git a/src/config.h b/src/config.h index 71c9866..4ad5f2e 100644 --- a/src/config.h +++ b/src/config.h @@ -49,6 +49,8 @@ public: std::string font; std::string spellcheck_language; + bool strip_trailing_whitespaces; + bool show_map; std::string map_font_size; diff --git a/src/files.h b/src/files.h index 954e322..fa38779 100644 --- a/src/files.h +++ b/src/files.h @@ -1,6 +1,6 @@ #include -#define JUCI_VERSION "0.9.4" +#define JUCI_VERSION "0.9.5" const std::string configjson = "{\n" @@ -29,6 +29,8 @@ const std::string configjson = " \"font\": \"Monospace\",\n" #endif #endif +" \"strip_trailing_whitespaces_comment\": \"Remove trailing whitespace characters on save, and add trailing newline if missing\",\n" +" \"strip_trailing_whitespaces\": false,\n" " \"show_map\": true,\n" " \"map_font_size\": \"1\",\n" " \"spellcheck_language_comment\": \"Use \\\"\\\" to set language from your locale settings\",\n" diff --git a/src/notebook.cc b/src/notebook.cc index 441f9e0..6096510 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -172,6 +172,30 @@ bool Notebook::save(int page, bool reparse_needed) { } auto view=get_view(page); if (view->file_path != "" && view->get_buffer()->get_modified()) { + //strip trailing whitespaces and add trailing newline if missing + if(Singleton::config->source.strip_trailing_whitespaces) { + auto buffer=view->get_buffer(); + for(int line=0;lineget_line_count();line++) { + auto iter=buffer->get_iter_at_line(line); + auto end_iter=iter; + while(!end_iter.ends_line()) + end_iter.forward_char(); + if(iter==end_iter) + continue; + iter=end_iter; + while(!iter.starts_line() && (*iter==' ' || *iter=='\t' || iter.ends_line())) + iter.backward_char(); + if(!iter.starts_line()) + iter.forward_char(); + if(iter==end_iter) + continue; + buffer->erase(iter, end_iter); + } + auto iter=buffer->end(); + if(!iter.starts_line()) + buffer->insert(buffer->end(), "\n"); + } + if(filesystem::write(view->file_path, view->get_buffer())) { if(reparse_needed) { if(auto clang_view=dynamic_cast(view)) {