summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2018-03-24 10:39:32 +0000
committerAndy Williams <andy@andywilliams.me>2018-03-24 10:39:32 +0000
commit228ddd733ce58af7db54c518f969509eb22434fd (patch)
treea4af12f62db1d40e553c88fde00c972792bdecfe
parent73f68ae183c3ae835418b87c079ceb033b7768e6 (diff)
downloadefl-228ddd733ce58af7db54c518f969509eb22434fd.tar.gz
elm_code: Support indentation styles that are purely tab based
read: Allow non-EFL style indentation. This is off by default but is switched on if you turn 'tabs insert spaces' off
-rw-r--r--src/bin/elementary/test.c2
-rw-r--r--src/bin/elementary/test_code.c50
-rw-r--r--src/lib/elementary/elm_code.c1
-rw-r--r--src/lib/elementary/elm_code_common.h1
-rw-r--r--src/lib/elementary/elm_code_indent.c25
-rw-r--r--src/lib/elementary/elm_code_widget.c2
-rw-r--r--src/tests/elementary/elm_code_test_indent.c61
7 files changed, 131 insertions, 11 deletions
diff --git a/src/bin/elementary/test.c b/src/bin/elementary/test.c
index ddfa3a70b4..27f7a13bc1 100644
--- a/src/bin/elementary/test.c
+++ b/src/bin/elementary/test.c
@@ -303,6 +303,7 @@ void test_colorclass(void *data, Evas_Object *obj, void *event_info);
void test_code_welcome(void *data, Evas_Object *obj, void *event_info);
void test_code_editor(void *data, Evas_Object *obj, void *event_info);
void test_code_syntax(void *data, Evas_Object *obj, void *event_info);
+void test_code_syntax_tabbed(void *data, Evas_Object *obj, void *event_info);
void test_code_mirror(void *data, Evas_Object *obj, void *event_info);
void test_code_log(void *data, Evas_Object *obj, void *event_info);
void test_code_diff(void *data, Evas_Object *obj, void *event_info);
@@ -749,6 +750,7 @@ add_tests:
ADD_TEST(NULL, "Advanced Entries", "Code Entry Markup", test_code_welcome);
ADD_TEST(NULL, "Advanced Entries", "Code Editor", test_code_editor);
ADD_TEST(NULL, "Advanced Entries", "Code Syntax", test_code_syntax);
+ ADD_TEST(NULL, "Advanced Entries", "Code Syntax (Tabbed)", test_code_syntax_tabbed);
ADD_TEST(NULL, "Advanced Entries", "Mirrored Editor", test_code_mirror);
ADD_TEST(NULL, "Advanced Entries", "Logger", test_code_log);
ADD_TEST(NULL, "Advanced Entries", "Diff Comparison", test_code_diff);
diff --git a/src/bin/elementary/test_code.c b/src/bin/elementary/test_code.c
index 30290dba65..f8f94196e7 100644
--- a/src/bin/elementary/test_code.c
+++ b/src/bin/elementary/test_code.c
@@ -152,6 +152,40 @@ _elm_code_test_syntax_setup(Evas_Object *parent)
}
static Evas_Object *
+_elm_code_test_syntax_tabbed_setup(Evas_Object *parent)
+{
+ Elm_Code *code;
+ Elm_Code_Widget *widget;
+
+ code = elm_code_create();
+ code->config.indent_style_efl = EINA_FALSE;
+ widget = efl_add(ELM_CODE_WIDGET_CLASS, parent, elm_obj_code_widget_code_set(efl_added, code));
+ elm_obj_code_widget_editable_set(widget, EINA_TRUE);
+ elm_obj_code_widget_syntax_enabled_set(widget, EINA_TRUE);
+ elm_obj_code_widget_code_get(widget)->file->mime = "text/x-csrc";
+ elm_obj_code_widget_show_whitespace_set(widget, EINA_TRUE);
+ elm_obj_code_widget_line_numbers_set(widget, EINA_TRUE);
+ elm_obj_code_widget_tab_inserts_spaces_set(widget, EINA_FALSE);
+
+ _append_line(code->file, "#include <stdio.h>");
+ _append_line(code->file, "int main(int argc, char **argv)");
+ _append_line(code->file, "{");
+ _append_line(code->file, "\t// display a welcome greeting");
+ _append_line(code->file, "\tif (argc > 0)");
+ _append_line(code->file, "\t\tprintf(\"Hello, %s!\\n\", argv[0]);");
+ _append_line(code->file, "\telse");
+ _append_line(code->file, "\t\tprintf(\"Hello, World!\\n\");");
+ _append_line(code->file, "\treturn 0;");
+ _append_line(code->file, "}");
+
+ evas_object_size_hint_weight_set(widget, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+ evas_object_size_hint_align_set(widget, EVAS_HINT_FILL, EVAS_HINT_FILL);
+ evas_object_show(widget);
+
+ return widget;
+}
+
+static Evas_Object *
_elm_code_test_mirror_setup(Elm_Code *code, char *font_name, Evas_Object *parent)
{
Elm_Code_Widget *widget;
@@ -260,6 +294,22 @@ test_code_syntax(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *eve
}
void
+test_code_syntax_tabbed(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
+{
+ Evas_Object *win, *screen;
+
+ win = _test_code_win_create("code-syntax-tabbed", "Code Syntax (Tabbed)");
+ screen = elm_box_add(win);
+ evas_object_size_hint_weight_set(screen, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+ elm_box_pack_end(screen, _elm_code_test_syntax_tabbed_setup(screen));
+ elm_win_resize_object_add(win, screen);
+ evas_object_show(screen);
+
+ evas_object_show(win);
+}
+
+
+void
test_code_log(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
Evas_Object *win, *screen, *o, *code;
diff --git a/src/lib/elementary/elm_code.c b/src/lib/elementary/elm_code.c
index 38017b7cfb..cfe1f7edf3 100644
--- a/src/lib/elementary/elm_code.c
+++ b/src/lib/elementary/elm_code.c
@@ -21,6 +21,7 @@ elm_code_create(void)
Elm_Code *ret;
ret = calloc(1, sizeof(Elm_Code));
+ ret->config.indent_style_efl = EINA_TRUE;
// create an in-memory backing for this elm_code by default
elm_code_file_new(ret);
diff --git a/src/lib/elementary/elm_code_common.h b/src/lib/elementary/elm_code_common.h
index 2c0c88e922..925721f9c6 100644
--- a/src/lib/elementary/elm_code_common.h
+++ b/src/lib/elementary/elm_code_common.h
@@ -67,6 +67,7 @@ extern "C" {
struct _Elm_Code_Config
{
Eina_Bool trim_whitespace;
+ Eina_Bool indent_style_efl;
};
struct _Elm_Code
diff --git a/src/lib/elementary/elm_code_indent.c b/src/lib/elementary/elm_code_indent.c
index 756186823b..9ce4cb0962 100644
--- a/src/lib/elementary/elm_code_indent.c
+++ b/src/lib/elementary/elm_code_indent.c
@@ -41,6 +41,8 @@ elm_code_line_indent_get(Elm_Code_Line *line)
unsigned int prevlength, count = 0;
char *buf, *ptr;
char next, last;
+ const char *indent = "\t";
+ Eina_Bool eflindent = ((Elm_Code *)line->file->parent)->config.indent_style_efl;
if (line->number <= 1)
return strdup("");
@@ -62,10 +64,14 @@ elm_code_line_indent_get(Elm_Code_Line *line)
strncpy(buf, prevtext, count);
buf[count] = '\0';
- if (elm_code_line_indent_startswith_keyword(prevline))
+ if (eflindent)
{
- strcpy(buf + count, " ");
- count += 2;
+ indent = " ";
+ if (elm_code_line_indent_startswith_keyword(prevline))
+ {
+ strcpy(buf + count, " ");
+ count += 2;
+ }
}
if (count < prevlength)
@@ -97,17 +103,16 @@ elm_code_line_indent_get(Elm_Code_Line *line)
else
strcpy(buf + count, "*");
}
- // VERY simple handling of braces
- else if (last == '{')
+ // Simple handling of braces
+ else if (last == '{' || (!eflindent && elm_code_line_indent_startswith_keyword(prevline)))
{
- strcpy(buf + count, " ");
+ strcpy(buf + count, indent);
}
else if (last == '}')
{
- if (count >= 2)
- buf[count-2] = '\0';
- else if (count >= 1)
- buf[count-1] = '\0';
+ unsigned int offset = strlen(indent) - 1;
+ if (count >= offset)
+ buf[count-offset] = '\0';
}
}
return buf;
diff --git a/src/lib/elementary/elm_code_widget.c b/src/lib/elementary/elm_code_widget.c
index d419d444f9..55b8b3509f 100644
--- a/src/lib/elementary/elm_code_widget.c
+++ b/src/lib/elementary/elm_code_widget.c
@@ -2253,6 +2253,8 @@ _elm_code_widget_tab_inserts_spaces_set(Eo *obj EINA_UNUSED, Elm_Code_Widget_Dat
Eina_Bool spaces)
{
pd->tab_inserts_spaces = spaces;
+ if (!spaces)
+ elm_code_widget_code_get(obj)->config.indent_style_efl = EINA_FALSE;
}
EOLIAN static Eina_Bool
diff --git a/src/tests/elementary/elm_code_test_indent.c b/src/tests/elementary/elm_code_test_indent.c
index 9a35b7ce1f..12645f7bf6 100644
--- a/src/tests/elementary/elm_code_test_indent.c
+++ b/src/tests/elementary/elm_code_test_indent.c
@@ -74,6 +74,7 @@ START_TEST (elm_code_indent_simple_braces)
elm_init(1, NULL);
code = elm_code_create();
file = elm_code_file_new(code);
+ code->config.indent_style_efl = EINA_TRUE;
_indent_check(file, "if() {", " ");
_indent_check(file, "}", "");
@@ -85,6 +86,26 @@ START_TEST (elm_code_indent_simple_braces)
}
END_TEST
+START_TEST (elm_code_indent_tab_simple_braces)
+{
+ Elm_Code *code;
+ Elm_Code_File *file;
+
+ elm_init(1, NULL);
+ code = elm_code_create();
+ file = elm_code_file_new(code);
+ code->config.indent_style_efl = EINA_FALSE;
+
+ _indent_check(file, "if() {", "\t");
+ _indent_check(file, "}", "");
+
+ _indent_check(file, "\t{", "\t\t");
+ _indent_check(file, "\t}", "\t");
+
+ elm_shutdown();
+}
+END_TEST
+
START_TEST (elm_code_indent_matching_braces)
{
Elm_Code_File *file;
@@ -97,7 +118,7 @@ START_TEST (elm_code_indent_matching_braces)
code = elm_code_create();
file = elm_code_file_new(code);
- elm_code_file_line_append(file, "", 8, NULL);
+ elm_code_file_line_append(file, "", 0, NULL);
line = elm_code_file_line_get(file, 1);
elm_code_file_line_insert(file, 1, " if ()", 8, NULL);
@@ -106,14 +127,17 @@ START_TEST (elm_code_indent_matching_braces)
elm_code_file_line_insert(file, 2, " {", 6, NULL);
str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_int_eq(str_len, 5);
ck_assert_strn_eq(str, " ", str_len);
elm_code_file_line_insert(file, 3, " if (){", 14, NULL);
str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_int_eq(str_len, 8);
ck_assert_strn_eq(str, " ", str_len);
elm_code_file_line_insert(file, 4, " }", 9, NULL);
str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_int_eq(str_len, 5);
ck_assert_strn_eq(str, " ", str_len);
elm_code_file_line_insert(file, 5, " }", 6, NULL);
@@ -125,6 +149,39 @@ START_TEST (elm_code_indent_matching_braces)
}
END_TEST
+START_TEST (elm_code_indent_tab_matching_braces)
+{
+ Elm_Code_File *file;
+ Elm_Code_Line *line;
+ Elm_Code *code;
+ const char *str;
+ unsigned int str_len;
+
+ elm_init(1, NULL);
+ code = elm_code_create();
+ file = elm_code_file_new(code);
+
+ elm_code_file_line_append(file, "", 0, NULL);
+ line = elm_code_file_line_get(file, 1);
+
+ elm_code_file_line_insert(file, 1, "\tif ()", 6, NULL);
+ str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_strn_eq(str, "", str_len);
+
+ elm_code_file_line_insert(file, 2, "\t{", 2, NULL);
+ str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_int_eq(str_len, 1);
+ ck_assert_strn_eq(str, "\t", str_len);
+
+ elm_code_file_line_insert(file, 3, "\t}", 2, NULL);
+ str = elm_code_line_indent_matching_braces_get(line, &str_len);
+ ck_assert_strn_eq(str, "", str_len);
+
+ elm_code_free(code);
+ elm_shutdown();
+}
+END_TEST
+
START_TEST (elm_code_indent_startswith_keyword)
{
Elm_Code_File *file;
@@ -165,7 +222,9 @@ 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_tab_simple_braces);
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_tab_matching_braces);
tcase_add_test(tc, elm_code_indent_startswith_keyword);
}