Browse Source

Added option to strip trailing whitespaces on save, and add trailing newline if missing.

merge-requests/365/head
eidheim 10 years ago
parent
commit
17732a3e82
  1. 2
      src/config.cc
  2. 2
      src/config.h
  3. 4
      src/files.h
  4. 24
      src/notebook.cc

2
src/config.cc

@ -146,6 +146,8 @@ void Config::get_source() {
source.style=source_json.get<std::string>("style"); source.style=source_json.get<std::string>("style");
source.font=source_json.get<std::string>("font"); source.font=source_json.get<std::string>("font");
source.strip_trailing_whitespaces=source_json.get<bool>("strip_trailing_whitespaces");
source.show_map = source_json.get<bool>("show_map"); source.show_map = source_json.get<bool>("show_map");
source.map_font_size = source_json.get<std::string>("map_font_size"); source.map_font_size = source_json.get<std::string>("map_font_size");

2
src/config.h

@ -49,6 +49,8 @@ public:
std::string font; std::string font;
std::string spellcheck_language; std::string spellcheck_language;
bool strip_trailing_whitespaces;
bool show_map; bool show_map;
std::string map_font_size; std::string map_font_size;

4
src/files.h

@ -1,6 +1,6 @@
#include <string> #include <string>
#define JUCI_VERSION "0.9.4" #define JUCI_VERSION "0.9.5"
const std::string configjson = const std::string configjson =
"{\n" "{\n"
@ -29,6 +29,8 @@ const std::string configjson =
" \"font\": \"Monospace\",\n" " \"font\": \"Monospace\",\n"
#endif #endif
#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" " \"show_map\": true,\n"
" \"map_font_size\": \"1\",\n" " \"map_font_size\": \"1\",\n"
" \"spellcheck_language_comment\": \"Use \\\"\\\" to set language from your locale settings\",\n" " \"spellcheck_language_comment\": \"Use \\\"\\\" to set language from your locale settings\",\n"

24
src/notebook.cc

@ -172,6 +172,30 @@ bool Notebook::save(int page, bool reparse_needed) {
} }
auto view=get_view(page); auto view=get_view(page);
if (view->file_path != "" && view->get_buffer()->get_modified()) { 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;line<buffer->get_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(filesystem::write(view->file_path, view->get_buffer())) {
if(reparse_needed) { if(reparse_needed) {
if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) { if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) {

Loading…
Cancel
Save