summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2018-03-24 10:39:32 +0000
committerAndy Williams <andy@andywilliams.me>2018-07-22 17:02:23 +0100
commit1485cf9df5f0a601fdfb17c9647168b8a47ef07f (patch)
tree349e1ee1118c233c2d75345908bac24bb5520b23
parent0a4ea24207363fb7b13aebaaee947c7e1291aef5 (diff)
elm_code: (cherry-pick) 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.c6
-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, 133 insertions, 13 deletions
diff --git a/src/bin/elementary/test.c b/src/bin/elementary/test.c
index e4c65b13b9..8d8d9701d5 100644
--- a/src/bin/elementary/test.c
+++ b/src/bin/elementary/test.c
@@ -332,6 +332,7 @@ void test_colorclass(void *data, Evas_Object *obj, void *event_info);
332void test_code_welcome(void *data, Evas_Object *obj, void *event_info); 332void test_code_welcome(void *data, Evas_Object *obj, void *event_info);
333void test_code_editor(void *data, Evas_Object *obj, void *event_info); 333void test_code_editor(void *data, Evas_Object *obj, void *event_info);
334void test_code_syntax(void *data, Evas_Object *obj, void *event_info); 334void test_code_syntax(void *data, Evas_Object *obj, void *event_info);
335void test_code_syntax_tabbed(void *data, Evas_Object *obj, void *event_info);
335void test_code_mirror(void *data, Evas_Object *obj, void *event_info); 336void test_code_mirror(void *data, Evas_Object *obj, void *event_info);
336void test_code_log(void *data, Evas_Object *obj, void *event_info); 337void test_code_log(void *data, Evas_Object *obj, void *event_info);
337void test_code_diff(void *data, Evas_Object *obj, void *event_info); 338void test_code_diff(void *data, Evas_Object *obj, void *event_info);
@@ -848,9 +849,10 @@ add_tests:
848 ADD_TEST_EO(NULL, "Entries", "Efl.Ui.Tags", test_ui_tags); 849 ADD_TEST_EO(NULL, "Entries", "Efl.Ui.Tags", test_ui_tags);
849 850
850 //------------------------------// 851 //------------------------------//
852 ADD_TEST(NULL, "Advanced Entries", "Code Entry Markup", test_code_welcome);
853 ADD_TEST(NULL, "Advanced Entries", "Code Editor", test_code_editor);
851 ADD_TEST(NULL, "Advanced Entries", "Code Syntax", test_code_syntax); 854 ADD_TEST(NULL, "Advanced Entries", "Code Syntax", test_code_syntax);
852 ADD_TEST(NULL, "Advanced Entries", "Entry Markup", test_code_welcome); 855 ADD_TEST(NULL, "Advanced Entries", "Code Syntax (Tabbed)", test_code_syntax_tabbed);
853 ADD_TEST(NULL, "Advanced Entries", "Text Editor", test_code_editor);
854 ADD_TEST(NULL, "Advanced Entries", "Mirrored Editor", test_code_mirror); 856 ADD_TEST(NULL, "Advanced Entries", "Mirrored Editor", test_code_mirror);
855 ADD_TEST(NULL, "Advanced Entries", "Logger", test_code_log); 857 ADD_TEST(NULL, "Advanced Entries", "Logger", test_code_log);
856 ADD_TEST(NULL, "Advanced Entries", "Diff Comparison", test_code_diff); 858 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 b6dad3ea2f..bb010ce5e6 100644
--- a/src/bin/elementary/test_code.c
+++ b/src/bin/elementary/test_code.c
@@ -156,6 +156,40 @@ _elm_code_test_syntax_setup(Evas_Object *parent)
156} 156}
157 157
158static Evas_Object * 158static Evas_Object *
159_elm_code_test_syntax_tabbed_setup(Evas_Object *parent)
160{
161 Elm_Code *code;
162 Elm_Code_Widget *widget;
163
164 code = elm_code_create();
165 code->config.indent_style_efl = EINA_FALSE;
166 widget = efl_add(ELM_CODE_WIDGET_CLASS, parent, elm_obj_code_widget_code_set(efl_added, code));
167 elm_obj_code_widget_editable_set(widget, EINA_TRUE);
168 elm_obj_code_widget_syntax_enabled_set(widget, EINA_TRUE);
169 elm_obj_code_widget_code_get(widget)->file->mime = "text/x-csrc";
170 elm_obj_code_widget_show_whitespace_set(widget, EINA_TRUE);
171 elm_obj_code_widget_line_numbers_set(widget, EINA_TRUE);
172 elm_obj_code_widget_tab_inserts_spaces_set(widget, EINA_FALSE);
173
174 _append_line(code->file, "#include <stdio.h>");
175 _append_line(code->file, "int main(int argc, char **argv)");
176 _append_line(code->file, "{");
177 _append_line(code->file, "\t// display a welcome greeting");
178 _append_line(code->file, "\tif (argc > 0)");
179 _append_line(code->file, "\t\tprintf(\"Hello, %s!\\n\", argv[0]);");
180 _append_line(code->file, "\telse");
181 _append_line(code->file, "\t\tprintf(\"Hello, World!\\n\");");
182 _append_line(code->file, "\treturn 0;");
183 _append_line(code->file, "}");
184
185 evas_object_size_hint_weight_set(widget, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186 evas_object_size_hint_align_set(widget, EVAS_HINT_FILL, EVAS_HINT_FILL);
187 evas_object_show(widget);
188
189 return widget;
190}
191
192static Evas_Object *
159_elm_code_test_mirror_setup(Elm_Code *code, char *font_name, Evas_Object *parent) 193_elm_code_test_mirror_setup(Elm_Code *code, char *font_name, Evas_Object *parent)
160{ 194{
161 Elm_Code_Widget *widget; 195 Elm_Code_Widget *widget;
@@ -263,6 +297,22 @@ test_code_syntax(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *eve
263} 297}
264 298
265void 299void
300test_code_syntax_tabbed(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
301{
302 Evas_Object *win, *screen;
303
304 win = _test_code_win_create("code-syntax-tabbed", "Code Syntax (Tabbed)");
305 screen = elm_box_add(win);
306 evas_object_size_hint_weight_set(screen, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
307 elm_box_pack_end(screen, _elm_code_test_syntax_tabbed_setup(screen));
308 elm_win_resize_object_add(win, screen);
309 evas_object_show(screen);
310
311 evas_object_show(win);
312}
313
314
315void
266test_code_log(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED) 316test_code_log(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
267{ 317{
268 Evas_Object *win, *screen, *o, *code; 318 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)
21 Elm_Code *ret; 21 Elm_Code *ret;
22 22
23 ret = calloc(1, sizeof(Elm_Code)); 23 ret = calloc(1, sizeof(Elm_Code));
24 ret->config.indent_style_efl = EINA_TRUE;
24 25
25 // create an in-memory backing for this elm_code by default 26 // create an in-memory backing for this elm_code by default
26 elm_code_file_new(ret); 27 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" {
67struct _Elm_Code_Config 67struct _Elm_Code_Config
68{ 68{
69 Eina_Bool trim_whitespace; 69 Eina_Bool trim_whitespace;
70 Eina_Bool indent_style_efl;
70}; 71};
71 72
72struct _Elm_Code 73struct _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)
41 unsigned int prevlength, count = 0; 41 unsigned int prevlength, count = 0;
42 char *buf, *ptr; 42 char *buf, *ptr;
43 char next, last; 43 char next, last;
44 const char *indent = "\t";
45 Eina_Bool eflindent = ((Elm_Code *)line->file->parent)->config.indent_style_efl;
44 46
45 if (line->number <= 1) 47 if (line->number <= 1)
46 return strdup(""); 48 return strdup("");
@@ -62,10 +64,14 @@ elm_code_line_indent_get(Elm_Code_Line *line)
62 strncpy(buf, prevtext, count); 64 strncpy(buf, prevtext, count);
63 buf[count] = '\0'; 65 buf[count] = '\0';
64 66
65 if (elm_code_line_indent_startswith_keyword(prevline)) 67 if (eflindent)
66 { 68 {
67 strcpy(buf + count, " "); 69 indent = " ";
68 count += 2; 70 if (elm_code_line_indent_startswith_keyword(prevline))
71 {
72 strcpy(buf + count, " ");
73 count += 2;
74 }
69 } 75 }
70 76
71 if (count < prevlength) 77 if (count < prevlength)
@@ -97,17 +103,16 @@ elm_code_line_indent_get(Elm_Code_Line *line)
97 else 103 else
98 strcpy(buf + count, "*"); 104 strcpy(buf + count, "*");
99 } 105 }
100 // VERY simple handling of braces 106 // Simple handling of braces
101 else if (last == '{') 107 else if (last == '{' || (!eflindent && elm_code_line_indent_startswith_keyword(prevline)))
102 { 108 {
103 strcpy(buf + count, " "); 109 strcpy(buf + count, indent);
104 } 110 }
105 else if (last == '}') 111 else if (last == '}')
106 { 112 {
107 if (count >= 2) 113 unsigned int offset = strlen(indent) - 1;
108 buf[count-2] = '\0'; 114 if (count >= offset)
109 else if (count >= 1) 115 buf[count-offset] = '\0';
110 buf[count-1] = '\0';
111 } 116 }
112 } 117 }
113 return buf; 118 return buf;
diff --git a/src/lib/elementary/elm_code_widget.c b/src/lib/elementary/elm_code_widget.c
index c39ffe6f9d..d34b40379e 100644
--- a/src/lib/elementary/elm_code_widget.c
+++ b/src/lib/elementary/elm_code_widget.c
@@ -2231,6 +2231,8 @@ _elm_code_widget_tab_inserts_spaces_set(Eo *obj EINA_UNUSED, Elm_Code_Widget_Dat
2231 Eina_Bool spaces) 2231 Eina_Bool spaces)
2232{ 2232{
2233 pd->tab_inserts_spaces = spaces; 2233 pd->tab_inserts_spaces = spaces;
2234 if (!spaces)
2235 elm_code_widget_code_get(obj)->config.indent_style_efl = EINA_FALSE;
2234} 2236}
2235 2237
2236EOLIAN static Eina_Bool 2238EOLIAN static Eina_Bool
diff --git a/src/tests/elementary/elm_code_test_indent.c b/src/tests/elementary/elm_code_test_indent.c
index 78c9160ede..6b564420f0 100644
--- a/src/tests/elementary/elm_code_test_indent.c
+++ b/src/tests/elementary/elm_code_test_indent.c
@@ -77,6 +77,7 @@ EFL_START_TEST (elm_code_indent_simple_braces)
77 elm_init(1, args); 77 elm_init(1, args);
78 code = elm_code_create(); 78 code = elm_code_create();
79 file = elm_code_file_new(code); 79 file = elm_code_file_new(code);
80 code->config.indent_style_efl = EINA_TRUE;
80 81
81 _indent_check(file, "if() {", " "); 82 _indent_check(file, "if() {", " ");
82 _indent_check(file, "}", ""); 83 _indent_check(file, "}", "");
@@ -88,6 +89,26 @@ EFL_START_TEST (elm_code_indent_simple_braces)
88} 89}
89EFL_END_TEST 90EFL_END_TEST
90 91
92EFL_START_TEST (elm_code_indent_tab_simple_braces)
93{
94 Elm_Code *code;
95 Elm_Code_File *file;
96
97 elm_init(1, NULL);
98 code = elm_code_create();
99 file = elm_code_file_new(code);
100 code->config.indent_style_efl = EINA_FALSE;
101
102 _indent_check(file, "if() {", "\t");
103 _indent_check(file, "}", "");
104
105 _indent_check(file, "\t{", "\t\t");
106 _indent_check(file, "\t}", "\t");
107
108 elm_shutdown();
109}
110EFL_END_TEST
111
91EFL_START_TEST (elm_code_indent_matching_braces) 112EFL_START_TEST (elm_code_indent_matching_braces)
92{ 113{
93 Elm_Code_File *file; 114 Elm_Code_File *file;
@@ -101,7 +122,7 @@ EFL_START_TEST (elm_code_indent_matching_braces)
101 code = elm_code_create(); 122 code = elm_code_create();
102 file = elm_code_file_new(code); 123 file = elm_code_file_new(code);
103 124
104 elm_code_file_line_append(file, "", 8, NULL); 125 elm_code_file_line_append(file, "", 0, NULL);
105 line = elm_code_file_line_get(file, 1); 126 line = elm_code_file_line_get(file, 1);
106 127
107 elm_code_file_line_insert(file, 1, " if ()", 8, NULL); 128 elm_code_file_line_insert(file, 1, " if ()", 8, NULL);
@@ -110,14 +131,17 @@ EFL_START_TEST (elm_code_indent_matching_braces)
110 131
111 elm_code_file_line_insert(file, 2, " {", 6, NULL); 132 elm_code_file_line_insert(file, 2, " {", 6, NULL);
112 str = elm_code_line_indent_matching_braces_get(line, &str_len); 133 str = elm_code_line_indent_matching_braces_get(line, &str_len);
134 ck_assert_int_eq(str_len, 5);
113 ck_assert_strn_eq(str, " ", str_len); 135 ck_assert_strn_eq(str, " ", str_len);
114 136
115 elm_code_file_line_insert(file, 3, " if (){", 14, NULL); 137 elm_code_file_line_insert(file, 3, " if (){", 14, NULL);
116 str = elm_code_line_indent_matching_braces_get(line, &str_len); 138 str = elm_code_line_indent_matching_braces_get(line, &str_len);
139 ck_assert_int_eq(str_len, 8);
117 ck_assert_strn_eq(str, " ", str_len); 140 ck_assert_strn_eq(str, " ", str_len);
118 141
119 elm_code_file_line_insert(file, 4, " }", 9, NULL); 142 elm_code_file_line_insert(file, 4, " }", 9, NULL);
120 str = elm_code_line_indent_matching_braces_get(line, &str_len); 143 str = elm_code_line_indent_matching_braces_get(line, &str_len);
144 ck_assert_int_eq(str_len, 5);
121 ck_assert_strn_eq(str, " ", str_len); 145 ck_assert_strn_eq(str, " ", str_len);
122 146
123 elm_code_file_line_insert(file, 5, " }", 6, NULL); 147 elm_code_file_line_insert(file, 5, " }", 6, NULL);
@@ -129,6 +153,39 @@ EFL_START_TEST (elm_code_indent_matching_braces)
129} 153}
130EFL_END_TEST 154EFL_END_TEST
131 155
156EFL_START_TEST (elm_code_indent_tab_matching_braces)
157{
158 Elm_Code_File *file;
159 Elm_Code_Line *line;
160 Elm_Code *code;
161 const char *str;
162 unsigned int str_len;
163
164 elm_init(1, NULL);
165 code = elm_code_create();
166 file = elm_code_file_new(code);
167
168 elm_code_file_line_append(file, "", 0, NULL);
169 line = elm_code_file_line_get(file, 1);
170
171 elm_code_file_line_insert(file, 1, "\tif ()", 6, NULL);
172 str = elm_code_line_indent_matching_braces_get(line, &str_len);
173 ck_assert_strn_eq(str, "", str_len);
174
175 elm_code_file_line_insert(file, 2, "\t{", 2, NULL);
176 str = elm_code_line_indent_matching_braces_get(line, &str_len);
177 ck_assert_int_eq(str_len, 1);
178 ck_assert_strn_eq(str, "\t", str_len);
179
180 elm_code_file_line_insert(file, 3, "\t}", 2, NULL);
181 str = elm_code_line_indent_matching_braces_get(line, &str_len);
182 ck_assert_strn_eq(str, "", str_len);
183
184 elm_code_free(code);
185 elm_shutdown();
186}
187END_TEST
188
132EFL_START_TEST (elm_code_indent_startswith_keyword) 189EFL_START_TEST (elm_code_indent_startswith_keyword)
133{ 190{
134 Elm_Code_File *file; 191 Elm_Code_File *file;
@@ -170,7 +227,9 @@ void elm_code_test_indent(TCase *tc)
170{ 227{
171 tcase_add_test(tc, elm_code_indent_whitespace_test); 228 tcase_add_test(tc, elm_code_indent_whitespace_test);
172 tcase_add_test(tc, elm_code_indent_comments_test); 229 tcase_add_test(tc, elm_code_indent_comments_test);
230 tcase_add_test(tc, elm_code_indent_tab_simple_braces);
173 tcase_add_test(tc, elm_code_indent_simple_braces); 231 tcase_add_test(tc, elm_code_indent_simple_braces);
174 tcase_add_test(tc, elm_code_indent_matching_braces); 232 tcase_add_test(tc, elm_code_indent_matching_braces);
233 tcase_add_test(tc, elm_code_indent_tab_matching_braces);
175 tcase_add_test(tc, elm_code_indent_startswith_keyword); 234 tcase_add_test(tc, elm_code_indent_startswith_keyword);
176} 235}