[gnome-builder] libide: implement vim style word ends



commit 6980973a912e7183c2dcb72ec600b7a62fc28bb0
Author: Christian Hergert <christian hergert me>
Date:   Thu Mar 5 19:45:40 2015 -0800

    libide: implement vim style word ends
    
    These are going to take a bunch of adjustment, since before we used
    overwrite mode to emulate blocks. But that will come in the not too
    distance future (ie: after i eat)
    
    :)

 libide/ide-source-view-movements.c |   24 ++++++++++++++-
 libide/ide-vim-iter.c              |   54 +++++++++++++++++++++++++++++++++++-
 libide/ide-vim-iter.h              |    3 +-
 3 files changed, 77 insertions(+), 4 deletions(-)
---
diff --git a/libide/ide-source-view-movements.c b/libide/ide-source-view-movements.c
index aa03d18..3fb1193 100644
--- a/libide/ide-source-view-movements.c
+++ b/libide/ide-source-view-movements.c
@@ -789,11 +789,21 @@ ide_source_view_movements_next_word_end (IdeSourceView         *self,
                                          gboolean               extend_selection,
                                          gint                   param)
 {
+  GtkTextIter copy;
   GtkTextIter insert;
   GtkTextIter selection;
 
   ide_source_view_movements_get_selection (self, &insert, &selection);
-  _ide_source_iter_forward_visible_word_end (&insert);
+
+  copy = insert;
+
+  _ide_vim_iter_forward_word_end (&insert);
+
+  /* prefer an empty line before word */
+  text_iter_forward_to_empty_line (&copy, &insert);
+  if (gtk_text_iter_compare (&copy, &insert) < 0)
+    insert = copy;
+
   ide_source_view_movements_select_range (self, &insert, &selection, extend_selection);
 }
 
@@ -803,11 +813,21 @@ ide_source_view_movements_next_full_word_end (IdeSourceView         *self,
                                               gboolean               extend_selection,
                                               gint                   param)
 {
+  GtkTextIter copy;
   GtkTextIter insert;
   GtkTextIter selection;
 
   ide_source_view_movements_get_selection (self, &insert, &selection);
-  _ide_source_iter_forward_full_word_end (&insert);
+
+  copy = insert;
+
+  _ide_vim_iter_forward_WORD_end (&insert);
+
+  /* prefer an empty line before word */
+  text_iter_forward_to_empty_line (&copy, &insert);
+  if (gtk_text_iter_compare (&copy, &insert) < 0)
+    insert = copy;
+
   ide_source_view_movements_select_range (self, &insert, &selection, extend_selection);
 }
 
diff --git a/libide/ide-vim-iter.c b/libide/ide-vim-iter.c
index 37fa560..9986d2a 100644
--- a/libide/ide-vim-iter.c
+++ b/libide/ide-vim-iter.c
@@ -303,7 +303,7 @@ _ide_vim_iter_backward_sentence_start (GtkTextIter *iter)
 
 static gboolean
 _ide_vim_iter_forward_classified_start (GtkTextIter *iter,
-                                        gint (*classify) (gunichar ch))
+                                        gint (*classify) (gunichar))
 {
   gint begin_class;
   gint cur_class;
@@ -359,3 +359,55 @@ _ide_vim_iter_forward_WORD_start (GtkTextIter *iter)
 {
   return _ide_vim_iter_forward_classified_start (iter, _ide_vim_WORD_classify);
 }
+
+gboolean
+_ide_vim_iter_forward_classified_end (GtkTextIter *iter,
+                                      gint (*classify) (gunichar))
+{
+  gunichar ch;
+  gint begin_class;
+  gint cur_class;
+
+  g_assert (iter);
+
+  if (!gtk_text_iter_forward_char (iter))
+    return FALSE;
+
+  /* If we are on space, walk to the start of the next word. */
+  ch = gtk_text_iter_get_char (iter);
+  if (classify (ch) == CLASS_SPACE)
+    if (!_ide_vim_iter_forward_classified_start (iter, classify))
+      return FALSE;
+
+  ch = gtk_text_iter_get_char (iter);
+  begin_class = classify (ch);
+
+  for (;;)
+    {
+      if (!gtk_text_iter_forward_char (iter))
+        return FALSE;
+
+      ch = gtk_text_iter_get_char (iter);
+      cur_class = classify (ch);
+
+      if (cur_class != begin_class)
+        {
+          gtk_text_iter_backward_char (iter);
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
+gboolean
+_ide_vim_iter_forward_word_end (GtkTextIter *iter)
+{
+  return _ide_vim_iter_forward_classified_end (iter, _ide_vim_word_classify);
+}
+
+gboolean
+_ide_vim_iter_forward_WORD_end (GtkTextIter *iter)
+{
+  return _ide_vim_iter_forward_classified_end (iter, _ide_vim_WORD_classify);
+}
diff --git a/libide/ide-vim-iter.h b/libide/ide-vim-iter.h
index a286105..09578db 100644
--- a/libide/ide-vim-iter.h
+++ b/libide/ide-vim-iter.h
@@ -25,7 +25,8 @@ G_BEGIN_DECLS
 
 gboolean _ide_vim_iter_forward_word_start       (GtkTextIter *iter);
 gboolean _ide_vim_iter_forward_WORD_start       (GtkTextIter *iter);
-
+gboolean _ide_vim_iter_forward_word_end         (GtkTextIter *iter);
+gboolean _ide_vim_iter_forward_WORD_end         (GtkTextIter *iter);
 gboolean _ide_vim_iter_backward_paragraph_start (GtkTextIter *iter);
 gboolean _ide_vim_iter_forward_paragraph_end    (GtkTextIter *iter);
 gboolean _ide_vim_iter_backward_sentence_start  (GtkTextIter *iter);


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