diff --git a/elm_code/src/lib/elm_code_widget_selection.c b/elm_code/src/lib/elm_code_widget_selection.c index 671ccd3..4a3bd2c 100644 --- a/elm_code/src/lib/elm_code_widget_selection.c +++ b/elm_code/src/lib/elm_code_widget_selection.c @@ -102,24 +102,58 @@ elm_code_widget_selection_clear(Evas_Object *widget) eo_do(widget, eo_event_callback_call(ELM_CODE_WIDGET_EVENT_SELECTION_CLEARED, widget)); } +static char * +_elm_code_widget_selection_text_single_get(Elm_Code_Widget_Data *pd) +{ + Elm_Code_Line *line; + + line = elm_code_file_line_get(pd->code->file, pd->selection->start_line); + + return elm_code_line_text_substr(line, pd->selection->start_col - 1, + pd->selection->end_col - pd->selection->start_col + 1); +} + +static char * +_elm_code_widget_selection_text_multi_get(Elm_Code_Widget_Data *pd) +{ + Elm_Code_Line *line; + char *first, *last, *ret; + const char *newline; + short newline_len; + int ret_len; + + newline = elm_code_file_line_ending_chars_get(pd->code->file, &newline_len); + + line = elm_code_file_line_get(pd->code->file, pd->selection->start_line); + first = elm_code_line_text_substr(line, pd->selection->start_col - 1, + line->length - pd->selection->start_col + 1); + + line = elm_code_file_line_get(pd->code->file, pd->selection->end_line); + last = elm_code_line_text_substr(line, 0, pd->selection->end_col + 1); + + ret_len = strlen(first) + strlen(last) + newline_len; + ret = malloc(sizeof(char) * (ret_len + 1)); + snprintf(ret, ret_len, "%s%s%s", first, newline, last); + + free(first); + free(last); + return ret; +} + EAPI char * elm_code_widget_selection_text_get(Evas_Object *widget) { Elm_Code_Widget_Data *pd; - Elm_Code_Line *line; pd = eo_data_scope_get(widget, ELM_CODE_WIDGET_CLASS); - if (!pd->selection) + if (!pd->selection || pd->selection->end_line < pd->selection->start_line) return strdup(""); if (pd->selection->start_line == pd->selection->end_line) - { - line = elm_code_file_line_get(pd->code->file, pd->selection->start_line); - - return elm_code_line_text_substr(line, pd->selection->start_col - 1, - pd->selection->end_col - pd->selection->start_col + 1); - } + return _elm_code_widget_selection_text_single_get(pd); + else + return _elm_code_widget_selection_text_multi_get(pd); return strdup("TODO"); } diff --git a/elm_code/src/tests/elm_code_test_widget_selection.c b/elm_code/src/tests/elm_code_test_widget_selection.c index 512cb3c..a475344 100644 --- a/elm_code/src/tests/elm_code_test_widget_selection.c +++ b/elm_code/src/tests/elm_code_test_widget_selection.c @@ -65,9 +65,40 @@ START_TEST (elm_code_test_widget_selection_text_get) } END_TEST +START_TEST (elm_code_test_widget_selection_text_get_multiline) +{ + Elm_Code *code; + Elm_Code_File *file; + Elm_Code_Widget *widget; + Evas_Object *win; + char *selection; + + elm_init(1, NULL); + code = elm_code_create(); + file = elm_code_file_new(code); + elm_code_file_line_append(file, "test", 4, NULL); + elm_code_file_line_append(file, "test", 4, NULL); + + win = elm_win_add(NULL, "entry", ELM_WIN_BASIC); + widget = eo_add(ELM_CODE_WIDGET_CLASS, win, + elm_code_widget_code_set(code)); + + elm_code_widget_selection_start(widget, 1, 3); + elm_code_widget_selection_end(widget, 2, 2); + + selection = elm_code_widget_selection_text_get(widget); + ck_assert_str_eq("st\nte", selection); + free(selection); + + elm_code_free(code); + elm_shutdown(); +} +END_TEST + void elm_code_test_widget_selection(TCase *tc) { tcase_add_test(tc, elm_code_test_widget_selection_set); tcase_add_test(tc, elm_code_test_widget_selection_text_get); + tcase_add_test(tc, elm_code_test_widget_selection_text_get_multiline); }