# 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(®ex, "^\\s*("
+ "((if|else\\s*if|while|for|switch)\\s*\\(.*\\)\\s*\\{?)|"
+ "((else|do)\\s*\\{?)|"
+ "(case\\s+.+:)|"
+ "(default:)"
+ ")\\s*$", REG_EXTENDED | REG_NOSUB);
+
+ ret = regexec(®ex, 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)
{
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))
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;
code = elm_code_create();
file = elm_code_file_new(code);
- _indent_check(file, "if() {", " ");
+ _indent_check(file, "if() {", " ");
_indent_check(file, "}", "");
_indent_check(file, " {", " ");
}
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);
}