ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/tools/edi.git/commit/?id=866d22c40bdc6f042831432167fffb213a1a6125

commit 866d22c40bdc6f042831432167fffb213a1a6125
Author: Andy Williams <a...@andywilliams.me>
Date:   Sun Jun 7 23:11:25 2015 +0100

    elm_code: indent lines to match whitespace above
    
    We can look at more complex scope based indenting later
---
 ChangeLog                                 |  4 ++++
 elm_code/src/lib/elm_code_text.c          | 18 ++++++++++++++++++
 elm_code/src/lib/elm_code_text.h          |  2 ++
 elm_code/src/lib/widget/elm_code_widget.c | 11 +++++++++--
 elm_code/src/tests/elm_code_test_text.c   | 19 +++++++++++++++++++
 5 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 84d1e79..7cfa99b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-06-07 ajwillia.ms (Andy Williams)
+
+       * Indent newlines to match whitespace in the previous line
+
 2015-05-31 ajwillia.ms (Andy Williams)
 
        * Add launch configuration so we can run a built binary
diff --git a/elm_code/src/lib/elm_code_text.c b/elm_code/src/lib/elm_code_text.c
index 9d42e4e..7480b4a 100644
--- a/elm_code/src/lib/elm_code_text.c
+++ b/elm_code/src/lib/elm_code_text.c
@@ -244,3 +244,21 @@ elm_code_text_newlinenpos(const char *text, unsigned int 
length, short *nllen)
    return crpos;
 }
 
+EAPI unsigned int
+elm_code_text_leading_whitespace_length(const char *text, unsigned int length)
+{
+   unsigned int count = 0;
+   char *ptr = (char *)text;
+
+   while (count < length)
+     {
+        if (!(*ptr == ' ' || *ptr == '\t'))
+          break;
+
+        count++;
+        ptr++;
+     }
+
+   return count;
+}
+
diff --git a/elm_code/src/lib/elm_code_text.h b/elm_code/src/lib/elm_code_text.h
index f547891..07e139d 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_strnpos(const char *text, unsigned int 
length, const char
 
 EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, 
short *nllen);
 
+EAPI unsigned int elm_code_text_leading_whitespace_length(const char *text, 
unsigned int length);
+
 /**
  * @}
  */
diff --git a/elm_code/src/lib/widget/elm_code_widget.c 
b/elm_code/src/lib/widget/elm_code_widget.c
index 22ba56b..bf34af4 100644
--- a/elm_code/src/lib/widget/elm_code_widget.c
+++ b/elm_code/src/lib/widget/elm_code_widget.c
@@ -975,19 +975,26 @@ _elm_code_widget_newline(Elm_Code_Widget *widget)
 {
    Elm_Code *code;
    Elm_Code_Line *line;
-   unsigned int row, col, position;
+   unsigned int row, col, position, oldlen, leading;
+   const char *oldtext;
 
    _elm_code_widget_delete_selection(widget);
    eo_do(widget,
          code = elm_obj_code_widget_code_get(),
          elm_obj_code_widget_cursor_position_get(&col, &row));
    line = elm_code_file_line_get(code->file, row);
+   oldtext = elm_code_line_text_get(line, &oldlen);
 
    position = elm_code_widget_line_text_position_for_column_get(widget, line, 
col);
    elm_code_line_split_at(line, position);
 
+   line = elm_code_file_line_get(code->file, row + 1);
+   leading = elm_code_text_leading_whitespace_length(oldtext, oldlen);
+   elm_code_line_text_insert(line, 0, oldtext, leading);
+
    eo_do(widget,
-         elm_obj_code_widget_cursor_position_set(1, row + 1),
+         elm_obj_code_widget_cursor_position_set(
+            elm_obj_code_widget_line_text_column_width_to_position(line, 
leading), row + 1),
 // TODO construct and pass a change object
          eo_event_callback_call(ELM_CODE_WIDGET_EVENT_CHANGED_USER, NULL));
 }
diff --git a/elm_code/src/tests/elm_code_test_text.c 
b/elm_code/src/tests/elm_code_test_text.c
index 85784a4..5391eb6 100644
--- a/elm_code/src/tests/elm_code_test_text.c
+++ b/elm_code/src/tests/elm_code_test_text.c
@@ -94,6 +94,24 @@ START_TEST (elm_code_text_newline_position_test)
 }
 END_TEST
 
+START_TEST (elm_code_text_leading_whitespace_test)
+{
+   const char *text;
+
+   text = "testing";
+   ck_assert_int_eq(0, elm_code_text_leading_whitespace_length(text, 
strlen(text)));
+
+   text = "  spaces";
+   ck_assert_int_eq(2, elm_code_text_leading_whitespace_length(text, 
strlen(text)));
+
+   text = "\t\ttabs";
+   ck_assert_int_eq(2, elm_code_text_leading_whitespace_length(text, 
strlen(text)));
+
+   text = " \t mix";
+   ck_assert_int_eq(3, elm_code_text_leading_whitespace_length(text, 
strlen(text)));
+}
+END_TEST
+
 void elm_code_test_text(TCase *tc)
 {
    tcase_add_test(tc, elm_code_text_get_test);
@@ -101,4 +119,5 @@ void elm_code_test_text(TCase *tc)
    tcase_add_test(tc, elm_code_text_contains_test);
    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);
 }

-- 


Reply via email to