ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/tools/edi.git/commit/?id=23b3d3ffba9d2b45698225d6a7803224819c98c7

commit 23b3d3ffba9d2b45698225d6a7803224819c98c7
Author: Andy Williams <a...@andywilliams.me>
Date:   Fri Aug 28 00:15:55 2015 +0100

    [elm_code] Trim empty lines on save
    
    When writing out a document set line length to 0 if it
    only contains whitespace
---
 ChangeLog                               |  4 ++++
 elm_code/src/lib/elm_code_file.c        |  5 +++++
 elm_code/src/lib/elm_code_text.c        | 10 ++++++++++
 elm_code/src/lib/elm_code_text.h        |  2 ++
 elm_code/src/tests/elm_code_test_text.c | 16 ++++++++++++++++
 5 files changed, 37 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 6631679..bd14aeb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-08-27 ajwillia.ms (Andy Williams)
+
+       * Trim lines that are purely whitespace during save
+
 2015-07-17 ajwillia.ms (Andy Williams)
 
        * Create new files in the selected directory rather than project root
diff --git a/elm_code/src/lib/elm_code_file.c b/elm_code/src/lib/elm_code_file.c
index 0b164bb..8832fe0 100644
--- a/elm_code/src/lib/elm_code_file.c
+++ b/elm_code/src/lib/elm_code_file.c
@@ -179,6 +179,11 @@ EAPI void elm_code_file_save(Elm_Code_File *file)
    EINA_LIST_FOREACH(file->lines, item, line_item)
      {
         content = elm_code_line_text_get(line_item, &length);
+        if (elm_code_text_is_whitespace(content, length))
+          {
+             length = 0;
+             elm_code_line_text_set(line_item, "", 0);
+          }
         fwrite(content, sizeof(char), length, out);
         fwrite(crchars, sizeof(char), crlength, out);
      }
diff --git a/elm_code/src/lib/elm_code_text.c b/elm_code/src/lib/elm_code_text.c
index 9e7a7c0..83216c6 100644
--- a/elm_code/src/lib/elm_code_text.c
+++ b/elm_code/src/lib/elm_code_text.c
@@ -256,3 +256,13 @@ elm_code_text_leading_whitespace_length(const char *text, 
unsigned int length)
    return count;
 }
 
+EAPI unsigned int
+elm_code_text_is_whitespace(const char *text, unsigned int length)
+{
+   unsigned int leading;
+
+   leading = elm_code_text_leading_whitespace_length(text, length);
+
+   return leading == length;
+}
+
diff --git a/elm_code/src/lib/elm_code_text.h b/elm_code/src/lib/elm_code_text.h
index 2370599..66bfbff 100644
--- a/elm_code/src/lib/elm_code_text.h
+++ b/elm_code/src/lib/elm_code_text.h
@@ -54,6 +54,8 @@ EAPI int elm_code_text_newlinenpos(const char *text, unsigned 
int length, short
 
 EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, 
unsigned int length);
 
+EAPI unsigned int elm_code_text_is_whitespace(const char *text, unsigned int 
length);
+
 /**
  * @}
  */
diff --git a/elm_code/src/tests/elm_code_test_text.c 
b/elm_code/src/tests/elm_code_test_text.c
index 5391eb6..9644ba2 100644
--- a/elm_code/src/tests/elm_code_test_text.c
+++ b/elm_code/src/tests/elm_code_test_text.c
@@ -112,6 +112,21 @@ START_TEST (elm_code_text_leading_whitespace_test)
 }
 END_TEST
 
+START_TEST (elm_code_text_is_whitespace_test)
+{
+   const char *text;
+
+   text = " ";
+   ck_assert_int_eq(1, elm_code_text_is_whitespace(text, strlen(text)));
+
+   text = " \t\t ";
+   ck_assert_int_eq(1, elm_code_text_is_whitespace(text, strlen(text)));
+
+   text = " . ";
+   ck_assert_int_eq(0, elm_code_text_is_whitespace(text, strlen(text)));
+}
+END_TEST
+
 void elm_code_test_text(TCase *tc)
 {
    tcase_add_test(tc, elm_code_text_get_test);
@@ -120,4 +135,5 @@ void elm_code_test_text(TCase *tc)
    tcase_add_test(tc, elm_code_text_strpos_test);
    tcase_add_test(tc, elm_code_text_newline_position_test);
    tcase_add_test(tc, elm_code_text_leading_whitespace_test);
+   tcase_add_test(tc, elm_code_text_is_whitespace_test);
 }

-- 


Reply via email to