ajwillia-ms pushed a commit to branch master. http://git.enlightenment.org/tools/edi.git/commit/?id=cfa86db7502c7284909171a84340635f88f1a1e9
commit cfa86db7502c7284909171a84340635f88f1a1e9 Author: Andy Williams <a...@andywilliams.me> Date: Mon Jun 8 23:13:03 2015 +0100 elm_code: Fix tab behaviour following API changes Moved the tabwidth calculations to widget as that's where they actually make sense --- elm_code/src/lib/elm_code_text.c | 6 --- elm_code/src/lib/elm_code_text.h | 2 - elm_code/src/lib/widget/elm_code_widget.c | 11 ++-- elm_code/src/lib/widget/elm_code_widget.eo | 6 +++ elm_code/src/lib/widget/elm_code_widget_text.c | 22 +++++--- elm_code/src/tests/Makefile.am | 1 + elm_code/src/tests/elm_code_suite.c | 1 + elm_code/src/tests/elm_code_suite.h | 1 + .../src/tests/widget/elm_code_test_widget_text.c | 59 ++++++++++++++++++++++ 9 files changed, 89 insertions(+), 20 deletions(-) diff --git a/elm_code/src/lib/elm_code_text.c b/elm_code/src/lib/elm_code_text.c index 7480b4a..9e7a7c0 100644 --- a/elm_code/src/lib/elm_code_text.c +++ b/elm_code/src/lib/elm_code_text.c @@ -209,12 +209,6 @@ elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, int length /* generic text functions */ -EAPI unsigned int -elm_code_text_tabwidth_at_position(unsigned int position, unsigned int tabstop) -{ - return tabstop - (position % tabstop); -} - EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen) { diff --git a/elm_code/src/lib/elm_code_text.h b/elm_code/src/lib/elm_code_text.h index 07e139d..2370599 100644 --- a/elm_code/src/lib/elm_code_text.h +++ b/elm_code/src/lib/elm_code_text.h @@ -48,8 +48,6 @@ EAPI void elm_code_line_text_remove(Elm_Code_Line *line, unsigned int position, * */ -EAPI unsigned int elm_code_text_tabwidth_at_position(unsigned int position, unsigned int tabstop); - EAPI int elm_code_text_strnpos(const char *text, unsigned int length, const char *search, int offset); EAPI int elm_code_text_newlinenpos(const char *text, unsigned int length, short *nllen); diff --git a/elm_code/src/lib/widget/elm_code_widget.c b/elm_code/src/lib/widget/elm_code_widget.c index a2888d2..ac2a058 100644 --- a/elm_code/src/lib/widget/elm_code_widget.c +++ b/elm_code/src/lib/widget/elm_code_widget.c @@ -350,7 +350,7 @@ _elm_code_widget_fill_line(Elm_Code_Widget *widget, Elm_Code_Line *line) charwidth = 1; if (unichr == '\t') - charwidth = elm_code_text_tabwidth_at_position(x - gutter, pd->tabstop); + charwidth = elm_code_widget_text_tabwidth_at_column_get(widget, x - gutter + 1); for (i = x + 1; i < x + charwidth; i++) { cells[i].codepoint = 0; @@ -1081,11 +1081,12 @@ _elm_code_widget_backspace(Elm_Code_Widget *widget) line = elm_code_file_line_get(code->file, row); - position = elm_code_widget_line_text_position_for_column_get(widget, line, col - 1); - char_width = elm_code_widget_line_text_position_for_column_get(widget, line, col) - position; - start_col = elm_code_widget_line_text_column_width_to_position(widget, line, position); + position = elm_code_widget_line_text_position_for_column_get(widget, line, col); + start_col = elm_code_widget_line_text_column_width_to_position(widget, line, + elm_code_widget_line_text_position_for_column_get(widget, line, col - 1)); + char_width = position - elm_code_widget_line_text_position_for_column_get(widget, line, start_col); - elm_code_line_text_remove(line, position, char_width?char_width:1); + elm_code_line_text_remove(line, position - char_width, char_width); eo_do(widget, elm_obj_code_widget_cursor_position_set(start_col, row)); diff --git a/elm_code/src/lib/widget/elm_code_widget.eo b/elm_code/src/lib/widget/elm_code_widget.eo index fde1902..294d860 100644 --- a/elm_code/src/lib/widget/elm_code_widget.eo +++ b/elm_code/src/lib/widget/elm_code_widget.eo @@ -245,6 +245,12 @@ class Elm.Code_Widget (Elm.Layout, Elm_Interface_Atspi_Text) } return: uint; } + text_tabwidth_at_column_get { + params { + column: uint; + } + return: uint; + } } implements { class.constructor; diff --git a/elm_code/src/lib/widget/elm_code_widget_text.c b/elm_code/src/lib/widget/elm_code_widget_text.c index b3be117..b7b131e 100644 --- a/elm_code/src/lib/widget/elm_code_widget_text.c +++ b/elm_code/src/lib/widget/elm_code_widget_text.c @@ -35,7 +35,7 @@ _elm_code_widget_text_left_gutter_width_get(Eo *obj, Elm_Code_Widget_Data *pd) } static unsigned int -_elm_code_widget_line_text_column_width_to_position(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd, Elm_Code_Line *line, unsigned int position) +_elm_code_widget_line_text_column_width_to_position(Eo *obj, Elm_Code_Widget_Data *pd EINA_UNUSED, Elm_Code_Line *line, unsigned int position) { Eina_Unicode unicode; unsigned int count = 1; @@ -59,7 +59,7 @@ _elm_code_widget_line_text_column_width_to_position(Eo *obj EINA_UNUSED, Elm_Cod break; if (unicode == '\t') - count += elm_code_text_tabwidth_at_position(count, pd->tabstop); + count += elm_code_widget_text_tabwidth_at_column_get(obj, count); else count++; } @@ -74,10 +74,10 @@ _elm_code_widget_line_text_column_width_get(Eo *obj, Elm_Code_Widget_Data *pd, E } static unsigned int -_elm_code_widget_line_text_position_for_column_get(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd, Elm_Code_Line *line, unsigned int column) +_elm_code_widget_line_text_position_for_column_get(Eo *obj, Elm_Code_Widget_Data *pd EINA_UNUSED, Elm_Code_Line *line, unsigned int column) { Eina_Unicode unicode; - unsigned int count = 1; + unsigned int count = 1, position = 0; int index = 0; const char *chars; @@ -89,17 +89,25 @@ _elm_code_widget_line_text_position_for_column_get(Eo *obj EINA_UNUSED, Elm_Code else chars = line->content; - while ((unsigned int) count < column && index < (int) line->length) + while ((unsigned int) count <= column && index <= (int) line->length) { + position = (unsigned int) index; unicode = eina_unicode_utf8_next_get(chars, &index); if (unicode == 0) return line->length; else if (unicode == '\t') - count += elm_code_text_tabwidth_at_position(count, pd->tabstop); + count += elm_code_widget_text_tabwidth_at_column_get(obj, count); else count++; } - return (unsigned int) index; + return position; +} + +static unsigned int +_elm_code_widget_text_tabwidth_at_column_get(Eo *obj EINA_UNUSED, Elm_Code_Widget_Data *pd, unsigned int column) +{ + return pd->tabstop - ((column - 1) % pd->tabstop); } + diff --git a/elm_code/src/tests/Makefile.am b/elm_code/src/tests/Makefile.am index 4bb0cc0..86acce2 100644 --- a/elm_code/src/tests/Makefile.am +++ b/elm_code/src/tests/Makefile.am @@ -13,6 +13,7 @@ elm_code_test_line.c \ elm_code_test_parse.c \ elm_code_test_text.c \ widget/elm_code_test_widget.c \ +widget/elm_code_test_widget_text.c \ widget/elm_code_test_widget_selection.c \ elm_code_suite.c diff --git a/elm_code/src/tests/elm_code_suite.c b/elm_code/src/tests/elm_code_suite.c index d459768..f562b80 100644 --- a/elm_code/src/tests/elm_code_suite.c +++ b/elm_code/src/tests/elm_code_suite.c @@ -19,6 +19,7 @@ static const struct { { "text", elm_code_test_text }, { "basic", elm_code_test_basic }, { "widget", elm_code_test_widget }, + { "widget_text", elm_code_test_widget_text }, { "widget_selection", elm_code_test_widget_selection }, }; diff --git a/elm_code/src/tests/elm_code_suite.h b/elm_code/src/tests/elm_code_suite.h index 5a7f4e3..489e8fd 100644 --- a/elm_code/src/tests/elm_code_suite.h +++ b/elm_code/src/tests/elm_code_suite.h @@ -12,6 +12,7 @@ void elm_code_test_line(TCase *tc); void elm_code_test_parse(TCase *tc); void elm_code_test_text(TCase *tc); void elm_code_test_widget(TCase *tc); +void elm_code_test_widget_text(TCase *tc); void elm_code_test_widget_selection(TCase *tc); #endif /* _EDLM_CODE_SUITE_H */ diff --git a/elm_code/src/tests/widget/elm_code_test_widget_text.c b/elm_code/src/tests/widget/elm_code_test_widget_text.c new file mode 100644 index 0000000..86d4a33 --- /dev/null +++ b/elm_code/src/tests/widget/elm_code_test_widget_text.c @@ -0,0 +1,59 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "elm_code_suite.h" + +START_TEST (elm_code_test_widget_text_tab_width) +{ + Elm_Code *code; + Elm_Code_Widget *widget; + Evas_Object *win; + + elm_init(1, NULL); + code = elm_code_create(); + win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); + widget = elm_code_widget_add(win, code); + elm_code_widget_tabstop_set(widget, 8); + + ck_assert_int_eq(8, elm_code_widget_text_tabwidth_at_column_get(widget, 1)); + ck_assert_int_eq(8, elm_code_widget_text_tabwidth_at_column_get(widget, 9)); + ck_assert_int_eq(6, elm_code_widget_text_tabwidth_at_column_get(widget, 3)); + + elm_code_free(code); + elm_shutdown(); +} +END_TEST + +START_TEST (elm_code_test_widget_text_position) +{ + Elm_Code *code; + Elm_Code_File *file; + Elm_Code_Line *line; + Elm_Code_Widget *widget; + Evas_Object *win; + + elm_init(1, NULL); + code = elm_code_create(); + file = elm_code_file_new(code); + elm_code_file_line_append(file, "a\tb", 4, NULL); + line = elm_code_file_line_get(file, 1); + + win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); + widget = elm_code_widget_add(win, code); + elm_code_widget_tabstop_set(widget, 8); + + ck_assert_int_eq(0, elm_code_widget_line_text_position_for_column_get(widget, line, 1)); + ck_assert_int_eq(1, elm_code_widget_line_text_position_for_column_get(widget, line, 2)); + + ck_assert_int_eq(2, elm_code_widget_line_text_position_for_column_get(widget, line, 9)); + ck_assert_int_eq(1, elm_code_widget_line_text_position_for_column_get(widget, line, 7)); +} +END_TEST + +void elm_code_test_widget_text(TCase *tc) +{ + tcase_add_test(tc, elm_code_test_widget_text_tab_width); + tcase_add_test(tc, elm_code_test_widget_text_position); +} + --