ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=13730d7a75fddc0c086c6da2e86e3509c6935156

commit 13730d7a75fddc0c086c6da2e86e3509c6935156
Author: YeongJong Lee <clean...@naver.com>
Date:   Fri Jan 6 09:38:02 2017 +0000

    elm_code: Add 2 spaces auto indent after keywords
    
    Summary:
    When insert newline, check the previous line has keyword.
    If so, insert 2 spaces indent more.
    
    Test Plan:
    1. run elementry_test - Code Editor or Edi.
    2. Type some code with keywords.
    3. Type <Return>.
    4. Check that the indentation of newline is correct.
    
    Reviewers: ajwillia.ms
    
    Reviewed By: ajwillia.ms
    
    Subscribers: cedric, jpeg
    
    Differential Revision: https://phab.enlightenment.org/D4543
---
 src/lib/elementary/elm_code_indent.c        | 37 ++++++++++++++++++++++++++-
 src/tests/elementary/elm_code_test_indent.c | 39 ++++++++++++++++++++++++++++-
 2 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/src/lib/elementary/elm_code_indent.c 
b/src/lib/elementary/elm_code_indent.c
index 6709749..c76ffb6 100644
--- a/src/lib/elementary/elm_code_indent.c
+++ b/src/lib/elementary/elm_code_indent.c
@@ -2,10 +2,38 @@
 # include "elementary_config.h"
 #endif
 
+#include "regex.h"
 #include "Elementary.h"
 
 #include "elm_code_private.h"
 
+static Eina_Bool
+elm_code_line_indent_startswith_keyword(Elm_Code_Line *line)
+{
+   regex_t regex;
+   char *text;
+   Eina_Bool ret;
+   unsigned int textlen;
+
+   text = (char *)elm_code_line_text_get(line, &textlen);
+   text = strndup(text, textlen);
+
+   regcomp(&regex, "^\\s*("
+                   "((if|else\\s*if|while|for|switch)\\s*\\(.*\\)\\s*\\{?)|"
+                   "((else|do)\\s*\\{?)|"
+                   "(case\\s+.+:)|"
+                   "(default:)"
+                   ")\\s*$", REG_EXTENDED | REG_NOSUB);
+
+   ret = regexec(&regex, text, 0, NULL, 0);
+   free(text);
+
+   if (ret == 0)
+     return EINA_TRUE;
+   else
+     return EINA_FALSE;
+}
+
 EAPI char *
 elm_code_line_indent_get(Elm_Code_Line *line)
 {
@@ -22,7 +50,7 @@ elm_code_line_indent_get(Elm_Code_Line *line)
    prevtext = elm_code_line_text_get(prevline, &prevlength);
 
    ptr = (char *)prevtext;
-   buf = malloc((prevlength + 3) * sizeof(char));
+   buf = malloc((prevlength + 5) * sizeof(char));
    while (count < prevlength)
      {
         if (!_elm_code_text_char_is_whitespace(*ptr))
@@ -34,6 +62,13 @@ elm_code_line_indent_get(Elm_Code_Line *line)
 
    strncpy(buf, prevtext, count);
    buf[count] = '\0';
+
+   if (elm_code_line_indent_startswith_keyword(prevline))
+     {
+        strcpy(buf + count, "  ");
+        count += 2;
+     }
+
    if (count < prevlength)
      {
         next = *ptr;
diff --git a/src/tests/elementary/elm_code_test_indent.c 
b/src/tests/elementary/elm_code_test_indent.c
index bacc522..9a35b7c 100644
--- a/src/tests/elementary/elm_code_test_indent.c
+++ b/src/tests/elementary/elm_code_test_indent.c
@@ -75,7 +75,7 @@ START_TEST (elm_code_indent_simple_braces)
    code = elm_code_create();
    file = elm_code_file_new(code);
 
-   _indent_check(file, "if() {", "   ");
+   _indent_check(file, "if() {", "     ");
    _indent_check(file, "}", "");
 
    _indent_check(file, "  {", "     ");
@@ -125,10 +125,47 @@ START_TEST (elm_code_indent_matching_braces)
 }
 END_TEST
 
+START_TEST (elm_code_indent_startswith_keyword)
+{
+   Elm_Code_File *file;
+   Elm_Code *code;
+
+   elm_init(1, NULL);
+   code = elm_code_create();
+   file = elm_code_file_new(code);
+
+   _indent_check(file, "if ()", "  ");
+   _indent_check(file, "else", "  ");
+   _indent_check(file, "else if ()", "  ");
+   _indent_check(file, "for ()", "  ");
+   _indent_check(file, "while ()", "  ");
+   _indent_check(file, "do", "  ");
+   _indent_check(file, "do {", "     ");
+
+   _indent_check(file, "  switch ()", "    ");
+   _indent_check(file, "   case a:", "     ");
+   _indent_check(file, "   default:", "     ");
+
+   _indent_check(file, "if ();", "");
+   _indent_check(file, "  for ();", "  ");
+
+   _indent_check(file, "  iffy()", "  ");
+   _indent_check(file, "  fi()", "  ");
+   _indent_check(file, "  elihw", "  ");
+
+   _indent_check(file, "   if", "   ");
+   _indent_check(file, "   while", "   ");
+
+   elm_code_free(code);
+   elm_shutdown();
+}
+END_TEST
+
 void elm_code_test_indent(TCase *tc)
 {
    tcase_add_test(tc, elm_code_indent_whitespace_test);
    tcase_add_test(tc, elm_code_indent_comments_test);
    tcase_add_test(tc, elm_code_indent_simple_braces);
    tcase_add_test(tc, elm_code_indent_matching_braces);
+   tcase_add_test(tc, elm_code_indent_startswith_keyword);
 }

-- 


Reply via email to