mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-11-10 02:40:11 +00:00
textview: Fix cursor/anchor mixup when retrieving surrounding text
Previously the code for calculating the cursor and anchor for the flipped case was mixed up with the logic for the non-flipped case. Additionally make the code more readable by making the anchor/selection bound the start and the cursor/insert the end of a selection. Thus a selection made from left to right goes from start to end. Selections from right to left, i.e. end to start are considered flipped. Also update the test to use the correct cursor/anchor and change some variable names to make it more readable. Closes: https://gitlab.gnome.org/GNOME/gtk/-/issues/6460
This commit is contained in:
parent
7926ee68b8
commit
575bd64698
@ -8735,11 +8735,11 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
|
||||
gboolean flip;
|
||||
|
||||
gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &start,
|
||||
gtk_text_buffer_get_insert (text_view->priv->buffer));
|
||||
gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &end,
|
||||
gtk_text_buffer_get_selection_bound (text_view->priv->buffer));
|
||||
gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &end,
|
||||
gtk_text_buffer_get_insert (text_view->priv->buffer));
|
||||
|
||||
flip = gtk_text_iter_compare (&start, &end) < 0;
|
||||
flip = gtk_text_iter_compare (&start, &end) > 0;
|
||||
|
||||
gtk_text_iter_order (&start, &end);
|
||||
|
||||
@ -8765,13 +8765,13 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
|
||||
|
||||
if (flip)
|
||||
{
|
||||
anchor_pos = strlen (pre);
|
||||
cursor_pos = anchor_pos + strlen (sel);
|
||||
cursor_pos = strlen (pre);
|
||||
anchor_pos = cursor_pos + strlen (sel);
|
||||
}
|
||||
else
|
||||
{
|
||||
cursor_pos = strlen (pre);
|
||||
anchor_pos = cursor_pos + strlen (sel);
|
||||
anchor_pos = strlen (pre);
|
||||
cursor_pos = anchor_pos + strlen (sel);
|
||||
}
|
||||
|
||||
text = g_strconcat (pre, sel, post, NULL);
|
||||
|
@ -73,9 +73,10 @@ test_textview_surrounding (void)
|
||||
GtkTextBuffer *buffer;
|
||||
GtkTextIter iter;
|
||||
GtkTextIter start, end;
|
||||
GtkTextIter bound, insert;
|
||||
gboolean ret;
|
||||
char *text;
|
||||
int cursor_pos, selection_bound;
|
||||
int anchor_pos, cursor_pos;
|
||||
|
||||
widget = gtk_text_view_new ();
|
||||
controller = gtk_text_view_get_key_controller (GTK_TEXT_VIEW (widget));
|
||||
@ -89,12 +90,12 @@ test_textview_surrounding (void)
|
||||
ret = gtk_im_context_get_surrounding_with_selection (context,
|
||||
&text,
|
||||
&cursor_pos,
|
||||
&selection_bound);
|
||||
&anchor_pos);
|
||||
|
||||
g_assert_true (ret);
|
||||
g_assert_cmpstr (text, ==, "abcd\nefgh\nijkl");
|
||||
g_assert_cmpint (cursor_pos, ==, 7);
|
||||
g_assert_cmpint (selection_bound, ==, 7);
|
||||
g_assert_cmpint (anchor_pos, ==, 7);
|
||||
|
||||
g_free (text);
|
||||
|
||||
@ -122,19 +123,19 @@ test_textview_surrounding (void)
|
||||
|
||||
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
|
||||
gtk_text_buffer_set_text (buffer, "ab cd\nef gh\nijkl", -1);
|
||||
gtk_text_buffer_get_iter_at_line_offset (buffer, &start, 1, 4);
|
||||
gtk_text_buffer_get_iter_at_line_offset (buffer, &end, 2, 2);
|
||||
gtk_text_buffer_select_range (buffer, &start, &end);
|
||||
gtk_text_buffer_get_iter_at_line_offset (buffer, &bound, 1, 4);
|
||||
gtk_text_buffer_get_iter_at_line_offset (buffer, &insert, 2, 2);
|
||||
gtk_text_buffer_select_range (buffer, &insert, &bound);
|
||||
|
||||
ret = gtk_im_context_get_surrounding_with_selection (context,
|
||||
&text,
|
||||
&cursor_pos,
|
||||
&selection_bound);
|
||||
&anchor_pos);
|
||||
|
||||
g_assert_true (ret);
|
||||
g_assert_cmpstr (text, ==, "cd\nef gh\nijkl");
|
||||
g_assert_cmpint (anchor_pos, ==, 7);
|
||||
g_assert_cmpint (cursor_pos, ==, 11);
|
||||
g_assert_cmpint (selection_bound, ==, 7);
|
||||
|
||||
g_free (text);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user