[gtk/textview-im-surrounding] textview: Provide more context to input methods




commit aafbcbdf3a39afc6a9cc61ab71bf4990776bf53f
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Dec 1 22:38:19 2021 -0500

    textview: Provide more context to input methods
    
    When returning surrounding context to input methods,
    include at least 2 words before and after the insertion
    point.
    
    Update the affected input method tests.

 gtk/gtktextview.c         | 14 ++++++++++++++
 testsuite/gtk/imcontext.c | 16 ++++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index c1b83eb9b3..65a59c98c0 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -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);
@@ -8606,6 +8618,8 @@ gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
 
   text = g_strconcat (pre, sel, post, NULL);
 
+  g_print ("surrounding: %s\n", text);
+
   g_free (pre);
   g_free (sel);
   g_free (post);
diff --git a/testsuite/gtk/imcontext.c b/testsuite/gtk/imcontext.c
index f6f3acf2e4..6f52c96ade 100644
--- a/testsuite/gtk/imcontext.c
+++ b/testsuite/gtk/imcontext.c
@@ -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);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]