elm_code: fix wrong selection delete
authorYeongJong Lee <cleanlyj@naver.com>
Tue, 30 May 2017 07:33:10 +0000 (08:33 +0100)
committerAndy Williams <andy@andywilliams.me>
Tue, 30 May 2017 07:50:18 +0000 (08:50 +0100)
Summary:
If multi selection have 0 column and delete the selection, 0 + 1 column also
deleted
This fix that bug.

Also, remove needless code.

Test Plan:
1. select multi line with 0 column.
2. delete the selection

Reviewers: ajwillia.ms

Reviewed By: ajwillia.ms

Subscribers: cedric, jpeg

Differential Revision: https://phab.enlightenment.org/D4922

src/lib/elementary/elm_code_widget_selection.c
src/tests/elementary/elm_code_test_widget_selection.c

index c8c1dac..9cac479 100644 (file)
@@ -186,7 +186,8 @@ _elm_code_widget_selection_delete_multi(Elm_Code_Widget *widget, Elm_Code_Widget
 {
    Elm_Code_Line *line;
    const char *first, *last;
-   unsigned int last_length, start, end, length, i;
+   unsigned int last_length, start, length, i;
+   int end;
    char *content;
    Elm_Code_Widget_Selection_Data *selection;
 
@@ -200,22 +201,16 @@ _elm_code_widget_selection_delete_multi(Elm_Code_Widget *widget, Elm_Code_Widget
 
    line = elm_code_file_line_get(pd->code->file, selection->end_line);
    last = elm_code_line_text_get(line, &last_length);
-   end = elm_code_widget_line_text_position_for_column_get(widget, line, selection->end_col);
+   end = (int)elm_code_widget_line_text_position_for_column_get(widget, line, selection->end_col);
 
-   if (last_length == end)
-     {
-        length = start + last_length - end;
-        content = malloc(sizeof(char) * length);
-        strncpy(content, first, start);
-     }
-   else
-     {
-        length = start + last_length - (end + 1);
-        content = malloc(sizeof(char) * length);
-        strncpy(content, first, start);
+   if (selection->end_col == 0)
+     end = -1;
 
-        strncpy(content + start, last + end + 1, last_length - (end + 1));
-     }
+   length = start + last_length - (end + 1);
+   content = malloc(sizeof(char) * length);
+   strncpy(content, first, start);
+   if (last_length > 0)
+     strncpy(content + start, last + end + 1, last_length - (end + 1));
 
    for (i = line->number; i > selection->start_line; i--)
      elm_code_file_line_remove(pd->code->file, i);
index e394300..db046a0 100644 (file)
@@ -346,13 +346,14 @@ START_TEST (elm_code_test_widget_selection_delete_twoline)
    file = elm_code_file_new(code);
    elm_code_file_line_append(file, "text", 4, NULL);
    elm_code_file_line_append(file, "TEXT", 4, NULL);
+   elm_code_file_line_append(file, "remove", 6, NULL);
 
    win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
    widget = elm_code_widget_add(win, code);
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, NULL);
    ck_assert_str_eq("text", text);
-   ck_assert_int_eq(2, elm_code_file_lines_get(file));
+   ck_assert_int_eq(3, elm_code_file_lines_get(file));
 
    elm_code_widget_selection_start(widget, 1, 3);
    elm_code_widget_selection_end(widget, 2, 2);
@@ -361,6 +362,15 @@ START_TEST (elm_code_test_widget_selection_delete_twoline)
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, &length);
    ck_assert_strn_eq("teXT", text, length);
+   ck_assert_int_eq(2, elm_code_file_lines_get(file));
+
+   elm_code_widget_selection_start(widget, 1, 5);
+   elm_code_widget_selection_end(widget, 2, 0);
+   elm_code_widget_selection_delete(widget);
+
+   line = elm_code_file_line_get(file, 1);
+   text = elm_code_line_text_get(line, &length);
+   ck_assert_strn_eq("teXTremove", text, length);
    ck_assert_int_eq(1, elm_code_file_lines_get(file));
 
    elm_code_free(code);
@@ -383,13 +393,14 @@ START_TEST (elm_code_test_widget_selection_reverse_delete_twoline)
    file = elm_code_file_new(code);
    elm_code_file_line_append(file, "text", 4, NULL);
    elm_code_file_line_append(file, "TEXT", 4, NULL);
+   elm_code_file_line_append(file, "remove", 6, NULL);
 
    win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
    widget = elm_code_widget_add(win, code);
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, NULL);
    ck_assert_str_eq("text", text);
-   ck_assert_int_eq(2, elm_code_file_lines_get(file));
+   ck_assert_int_eq(3, elm_code_file_lines_get(file));
 
    elm_code_widget_selection_start(widget, 2, 2);
    elm_code_widget_selection_end(widget, 1, 3);
@@ -398,6 +409,15 @@ START_TEST (elm_code_test_widget_selection_reverse_delete_twoline)
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, &length);
    ck_assert_strn_eq("teXT", text, length);
+   ck_assert_int_eq(2, elm_code_file_lines_get(file));
+
+   elm_code_widget_selection_start(widget, 2, 0);
+   elm_code_widget_selection_end(widget, 1, 5);
+   elm_code_widget_selection_delete(widget);
+
+   line = elm_code_file_line_get(file, 1);
+   text = elm_code_line_text_get(line, &length);
+   ck_assert_strn_eq("teXTremove", text, length);
    ck_assert_int_eq(1, elm_code_file_lines_get(file));
 
    elm_code_free(code);
@@ -421,13 +441,15 @@ START_TEST (elm_code_test_widget_selection_delete_multiline)
    elm_code_file_line_append(file, "text", 4, NULL);
    elm_code_file_line_append(file, "remove", 6, NULL);
    elm_code_file_line_append(file, "TEXT", 4, NULL);
+   elm_code_file_line_append(file, "delete", 6, NULL);
+   elm_code_file_line_append(file, "REMOVE", 6, NULL);
 
    win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
    widget = elm_code_widget_add(win, code);
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, NULL);
    ck_assert_str_eq("text", text);
-   ck_assert_int_eq(3, elm_code_file_lines_get(file));
+   ck_assert_int_eq(5, elm_code_file_lines_get(file));
 
    elm_code_widget_selection_start(widget, 1, 3);
    elm_code_widget_selection_end(widget, 3, 2);
@@ -436,6 +458,15 @@ START_TEST (elm_code_test_widget_selection_delete_multiline)
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, &length);
    ck_assert_strn_eq("teXT", text, length);
+   ck_assert_int_eq(3, elm_code_file_lines_get(file));
+
+   elm_code_widget_selection_start(widget, 1, 5);
+   elm_code_widget_selection_end(widget, 3, 0);
+   elm_code_widget_selection_delete(widget);
+
+   line = elm_code_file_line_get(file, 1);
+   text = elm_code_line_text_get(line, &length);
+   ck_assert_strn_eq("teXTREMOVE", text, length);
    ck_assert_int_eq(1, elm_code_file_lines_get(file));
 
    elm_code_free(code);
@@ -459,13 +490,15 @@ START_TEST (elm_code_test_widget_selection_reverse_delete_multiline)
    elm_code_file_line_append(file, "text", 4, NULL);
    elm_code_file_line_append(file, "remove", 6, NULL);
    elm_code_file_line_append(file, "TEXT", 4, NULL);
+   elm_code_file_line_append(file, "delete", 6, NULL);
+   elm_code_file_line_append(file, "REMOVE", 6, NULL);
 
    win = elm_win_add(NULL, "code", ELM_WIN_BASIC);
    widget = elm_code_widget_add(win, code);
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, NULL);
    ck_assert_str_eq("text", text);
-   ck_assert_int_eq(3, elm_code_file_lines_get(file));
+   ck_assert_int_eq(5, elm_code_file_lines_get(file));
 
    elm_code_widget_selection_start(widget, 3, 2);
    elm_code_widget_selection_end(widget, 1, 3);
@@ -474,6 +507,15 @@ START_TEST (elm_code_test_widget_selection_reverse_delete_multiline)
    line = elm_code_file_line_get(file, 1);
    text = elm_code_line_text_get(line, &length);
    ck_assert_strn_eq("teXT", text, length);
+   ck_assert_int_eq(3, elm_code_file_lines_get(file));
+
+   elm_code_widget_selection_start(widget, 3, 0);
+   elm_code_widget_selection_end(widget, 1, 5);
+   elm_code_widget_selection_delete(widget);
+
+   line = elm_code_file_line_get(file, 1);
+   text = elm_code_line_text_get(line, &length);
+   ck_assert_strn_eq("teXTREMOVE", text, length);
    ck_assert_int_eq(1, elm_code_file_lines_get(file));
 
    elm_code_free(code);