Merge branch 'textview-im-surrounding' into 'main'

textview: Provide more context to input methods

See merge request GNOME/gtk!4209
This commit is contained in:
Matthias Clasen 2021-12-03 20:26:54 +00:00
commit 2f7fa10434
2 changed files with 20 additions and 8 deletions

View File

@ -8566,6 +8566,8 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
GtkTextIter end;
GtkTextIter start1;
GtkTextIter end1;
GtkTextIter start2;
GtkTextIter end2;
int cursor_pos;
int anchor_pos;
char *text;
@ -8589,6 +8591,16 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
gtk_text_iter_set_line_offset (&start1, 0);
gtk_text_iter_forward_to_line_end (&end1);
start2 = start;
gtk_text_iter_backward_word_starts (&start2, 3);
if (gtk_text_iter_compare (&start2, &start1) < 0)
start1 = start2;
end2 = end;
gtk_text_iter_forward_word_ends (&end2, 3);
if (gtk_text_iter_compare (&end2, &end1) > 0)
end1 = end2;
pre = gtk_text_iter_get_slice (&start1, &start);
sel = gtk_text_iter_get_slice (&start, &end);
post = gtk_text_iter_get_slice (&end, &end1);

View File

@ -92,9 +92,9 @@ test_textview_surrounding (void)
&selection_bound);
g_assert_true (ret);
g_assert_cmpstr (text, ==, "efgh");
g_assert_cmpint (cursor_pos, ==, 2);
g_assert_cmpint (selection_bound, ==, 2);
g_assert_cmpstr (text, ==, "abcd\nefgh\nijkl");
g_assert_cmpint (cursor_pos, ==, 7);
g_assert_cmpint (selection_bound, ==, 7);
g_free (text);
@ -121,8 +121,8 @@ test_textview_surrounding (void)
g_assert_cmpint (gtk_text_iter_get_line_offset (&start), ==, 1);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
gtk_text_buffer_set_text (buffer, "abcd\nefgh\nijkl", -1);
gtk_text_buffer_get_iter_at_line_offset (buffer, &start, 1, 2);
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);
@ -132,9 +132,9 @@ test_textview_surrounding (void)
&selection_bound);
g_assert_true (ret);
g_assert_cmpstr (text, ==, "efgh\nijkl");
g_assert_cmpint (cursor_pos, ==, 7);
g_assert_cmpint (selection_bound, ==, 2);
g_assert_cmpstr (text, ==, "cd\nef gh\nijkl");
g_assert_cmpint (cursor_pos, ==, 11);
g_assert_cmpint (selection_bound, ==, 7);
g_free (text);