[gtksourceview/wip/chergert/vim: 62/73] more sentence work




commit e81830600e0bd8b478e202d94df81154f271f554
Author: Christian Hergert <chergert redhat com>
Date:   Sun Oct 24 12:17:43 2021 -0700

    more sentence work

 gtksourceview/vim/gtk-source-vim-motion.c | 206 +++++++-----------------------
 1 file changed, 43 insertions(+), 163 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index 2821ea5b..ca2bdbb3 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -799,188 +799,69 @@ motion_backward_paragraph_start (GtkTextIter        *iter,
        return !gtk_text_iter_equal (&before, iter);
 }
 
-typedef enum
-{
-  SENTENCE_OK,
-  SENTENCE_PARA,
-  SENTENCE_FAILED,
-} SentenceStatus;
-
 static gboolean
-sentence_end_chars (gunichar ch,
-                    gpointer user_data)
-{
-       switch (ch)
-       {
-               case '!':
-               case '.':
-               case '?':
-                       return TRUE;
-
-               default:
-                       return FALSE;
-       }
-}
-
-static SentenceStatus
-backward_sentence_end (GtkTextIter        *iter,
-                       GtkSourceVimMotion *state)
+motion_backward_sentence_start (GtkTextIter        *iter,
+                                GtkSourceVimMotion *state)
 {
-       GtkTextIter end_bounds;
-       GtkTextIter start_bounds;
-       gboolean found_para;
-
-       end_bounds = *iter;
-       start_bounds = *iter;
-       found_para = motion_backward_paragraph_start (&start_bounds, state);
-
-       if (!found_para)
-       {
-               gtk_text_buffer_get_start_iter (gtk_text_iter_get_buffer (iter),
-                                               &start_bounds);
-       }
-
-       while ((gtk_text_iter_compare (iter, &start_bounds) > 0) &&
-              gtk_text_iter_backward_char (iter))
-       {
-               if (gtk_text_iter_backward_find_char (iter, sentence_end_chars, NULL, &end_bounds))
-               {
-                       GtkTextIter copy = *iter;
-
-                       while (gtk_text_iter_forward_char (&copy) &&
-                              gtk_text_iter_compare (&copy, &end_bounds) < 0)
-                       {
-                               gunichar ch = gtk_text_iter_get_char (&copy);
-
-                               switch (ch)
-                               {
-                                       case ']':
-                                       case ')':
-                                       case '"':
-                                       case '\'':
-                                               continue;
-
-                                       case ' ':
-                                       case '\n':
-                                               *iter = copy;
-                                               return SENTENCE_OK;
-
-                                       default:
-                                               break;
-                               }
-                       }
-               }
-       }
-
-       *iter = start_bounds;
-
-       if (found_para)
-               return SENTENCE_PARA;
-
-       return SENTENCE_FAILED;
+       return gtk_text_iter_backward_sentence_start (iter);
 }
 
 static gboolean
-motion_forward_sentence_end (GtkTextIter        *iter,
-                             GtkSourceVimMotion *state)
+motion_forward_sentence_start (GtkTextIter        *iter,
+                               GtkSourceVimMotion *state)
 {
-       GtkTextIter end_bounds;
-       gboolean found_para;
-
-       end_bounds = *iter;
-       found_para = motion_forward_paragraph_end (&end_bounds, state);
+       GtkTextIter before = *iter;
+       gboolean last_was_newline = FALSE;
 
-       if (!found_para)
+       do
        {
-               gtk_text_buffer_get_end_iter (gtk_text_iter_get_buffer (iter),
-                                             &end_bounds);
-       }
+               if (!gtk_text_iter_forward_char (iter))
+                       break;
 
-       while ((gtk_text_iter_compare (iter, &end_bounds) < 0) &&
-              gtk_text_iter_forward_char (iter))
-       {
-               if (gtk_text_iter_forward_find_char (iter, sentence_end_chars, NULL, &end_bounds))
+               switch (gtk_text_iter_get_char (iter))
                {
-                       GtkTextIter copy = *iter;
-
-                       while (gtk_text_iter_forward_char (&copy) &&
-                              gtk_text_iter_compare (&copy, &end_bounds) < 0)
-                       {
-                               gunichar ch = gtk_text_iter_get_char (&copy);
-                               gboolean invalid = FALSE;
-
-                               switch (ch)
+                       case '\n':
+                               /* \n\n or more is a paragraph break */
+                               if (last_was_newline)
                                {
-                                       case ']':
-                                       case ')':
-                                       case '"':
-                                       case '\'':
-                                               continue;
+                                       while (gtk_text_iter_ends_line (iter) &&
+                                              gtk_text_iter_forward_char (iter)) { /* do nothing */ }
 
-                                       case ' ':
-                                       case '\n':
-                                               *iter = copy;
-                                               return SENTENCE_OK;
+                                       while (!gtk_text_iter_ends_line (iter) &&
+                                              g_unichar_isspace (gtk_text_iter_get_char (iter)))
+                                       {
+                                               if (!gtk_text_iter_forward_char (iter))
+                                                       break;
+                                       }
 
-                                       default:
-                                               invalid = TRUE;
-                                               break;
+                                       return TRUE;
                                }
 
-                               if (invalid)
-                                       break;
-                       }
-               }
-       }
-
-       *iter = end_bounds;
-
-       if (found_para)
-               return SENTENCE_PARA;
-
-       return SENTENCE_FAILED;
-}
-
-static gboolean
-motion_backward_sentence_start (GtkTextIter        *iter,
-                                GtkSourceVimMotion *state)
-{
-       GtkTextIter tmp;
-       SentenceStatus status;
-
-       tmp = *iter;
-       status = backward_sentence_end (&tmp, state);
-
-       switch (status)
-       {
-               case SENTENCE_PARA:
-               case SENTENCE_OK:
-               {
-                       GtkTextIter copy = tmp;
+                               last_was_newline = TRUE;
+                               break;
 
-                       /*
-                        * try to work forward to first non-whitespace char.
-                        * if we land where we started, discard the walk.
-                        */
-                       while (g_unichar_isspace (gtk_text_iter_get_char (&copy)))
-                       {
-                               if (!gtk_text_iter_forward_char (&copy))
-                                       break;
-                       }
+                       case '.':
+                       case '!':
+                       case '?':
+                               do
+                               {
+                                       if (!gtk_text_iter_forward_char (iter))
+                                               return TRUE;
 
-                       if (gtk_text_iter_compare (&copy, iter) < 0)
-                               tmp = copy;
+                                       if (g_unichar_isspace (gtk_text_iter_get_char (iter)))
+                                               continue;
 
-                       *iter = tmp;
+                                       return TRUE;
+                               } while (TRUE);
+                               break;
 
-                       return TRUE;
+                       default:
+                               last_was_newline = FALSE;
+                               break;
                }
+       } while (TRUE);
 
-               case SENTENCE_FAILED:
-               default:
-                       gtk_text_buffer_get_start_iter (gtk_text_iter_get_buffer (iter), iter);
-                       return FALSE;
-       }
+       return !gtk_text_iter_equal (&before, iter);
 }
 
 GtkSourceVimState *
@@ -1167,8 +1048,7 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                        return gtk_source_vim_motion_complete (self, motion_backward_sentence_start);
 
                case GDK_KEY_parenright:
-                       /* this needs to be chagned to sentence start */
-                       return gtk_source_vim_motion_complete (self, motion_forward_sentence_end);
+                       return gtk_source_vim_motion_complete (self, motion_forward_sentence_start);
 
                case GDK_KEY_braceleft:
                        return gtk_source_vim_motion_complete (self, motion_backward_paragraph_start);


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