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
This commit is contained in:
YeongJong Lee 2017-01-06 09:38:02 +00:00 committed by Andy Williams
parent bb6c324e73
commit 13730d7a75
2 changed files with 74 additions and 2 deletions

View File

@ -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;

View File

@ -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);
}