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
This commit is contained in:
Andy Williams 2018-03-24 10:39:32 +00:00
parent 0a4ea24207
commit 1485cf9df5
7 changed files with 133 additions and 13 deletions

View File

@ -332,6 +332,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);
@ -848,9 +849,10 @@ add_tests:
ADD_TEST_EO(NULL, "Entries", "Efl.Ui.Tags", test_ui_tags);
//------------------------------//
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", "Entry Markup", test_code_welcome);
ADD_TEST(NULL, "Advanced Entries", "Text Editor", test_code_editor);
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);

View File

@ -155,6 +155,40 @@ _elm_code_test_syntax_setup(Evas_Object *parent)
return widget;
}
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)
{
@ -262,6 +296,22 @@ test_code_syntax(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *eve
evas_object_show(win);
}
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)
{

View File

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

View File

@ -67,6 +67,7 @@ extern "C" {
struct _Elm_Code_Config
{
Eina_Bool trim_whitespace;
Eina_Bool indent_style_efl;
};
struct _Elm_Code

View File

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

View File

@ -2231,6 +2231,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

View File

@ -77,6 +77,7 @@ EFL_START_TEST (elm_code_indent_simple_braces)
elm_init(1, args);
code = elm_code_create();
file = elm_code_file_new(code);
code->config.indent_style_efl = EINA_TRUE;
_indent_check(file, "if() {", " ");
_indent_check(file, "}", "");
@ -88,6 +89,26 @@ EFL_START_TEST (elm_code_indent_simple_braces)
}
EFL_END_TEST
EFL_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();
}
EFL_END_TEST
EFL_START_TEST (elm_code_indent_matching_braces)
{
Elm_Code_File *file;
@ -101,7 +122,7 @@ EFL_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);
@ -110,14 +131,17 @@ EFL_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);
@ -129,6 +153,39 @@ EFL_START_TEST (elm_code_indent_matching_braces)
}
EFL_END_TEST
EFL_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
EFL_START_TEST (elm_code_indent_startswith_keyword)
{
Elm_Code_File *file;
@ -170,7 +227,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);
}