[gnome-builder] editor: remove manual handling of indent/unindent



commit 90e55d85958a057f9b8f616f87d5be0c96c7528a
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Jan 17 14:36:34 2015 +0100

    editor: remove manual handling of indent/unindent
    
    GtkSourceView already implements the same logic
    
    https://bugzilla.gnome.org/show_bug.cgi?id=743076

 src/editor/gb-source-view.c |  208 -------------------------------------------
 src/editor/gb-source-view.h |    2 -
 src/vim/gb-source-vim.c     |   24 +++--
 3 files changed, 14 insertions(+), 220 deletions(-)
---
diff --git a/src/editor/gb-source-view.c b/src/editor/gb-source-view.c
index 6c932af..56a175b 100644
--- a/src/editor/gb-source-view.c
+++ b/src/editor/gb-source-view.c
@@ -427,192 +427,6 @@ gb_source_view_set_show_shadow (GbSourceView *view,
   invalidate_window (view);
 }
 
-void
-gb_source_view_indent_selection (GbSourceView *view)
-{
-  GbSourceViewPrivate *priv;
-  GtkTextIter begin;
-  GtkTextIter end;
-  GtkTextIter iter;
-  GtkTextMark *mark_begin;
-  GtkTextMark *mark_end;
-  gboolean use_spaces = FALSE;
-  guint shift_width = 0;
-
-  ENTRY;
-
-  g_return_if_fail (GB_IS_SOURCE_VIEW (view));
-
-  priv = view->priv;
-
-  g_object_get (view,
-                "insert-spaces-instead-of-tabs", &use_spaces,
-                "indent-width", &shift_width,
-                NULL);
-
-  if (!gtk_text_buffer_get_has_selection (priv->buffer))
-    return;
-
-  gtk_text_buffer_get_selection_bounds (priv->buffer, &begin, &end);
-
-  /*
-   * Expand the iters to the beginning of the first line.
-   */
-  while (!gtk_text_iter_starts_line (&begin))
-    gtk_text_iter_backward_char (&begin);
-
-  /*
-   * Set marks so we can track our end position after every edit.
-   * Also allows us to reselect the whole range at the end.
-   */
-  mark_begin = gtk_text_buffer_create_mark (priv->buffer,
-                                            "tab-selection-begin",
-                                            &begin, TRUE);
-  mark_end = gtk_text_buffer_create_mark (priv->buffer,
-                                          "tab-selection-end",
-                                          &end, FALSE);
-  gtk_text_iter_assign (&iter, &begin);
-
-  /*
-   * Walk through each selected line, adding a tab or the proper number of
-   * spaces at the beginning of each line.
-   */
-  gtk_text_buffer_begin_user_action (priv->buffer);
-  do
-    {
-      if (use_spaces)
-        {
-          guint i;
-
-          for (i = 0; i < shift_width; i++)
-            gtk_text_buffer_insert (priv->buffer, &iter, " ", 1);
-        }
-      else
-        gtk_text_buffer_insert (priv->buffer, &iter, "\t", 1);
-
-      if (!gtk_text_iter_forward_line (&iter))
-        break;
-
-      gtk_text_buffer_get_iter_at_mark (priv->buffer, &end, mark_end);
-    }
-  while (gtk_text_iter_compare (&iter, &end) < 0);
-  gtk_text_buffer_end_user_action (priv->buffer);
-
-  /*
-   * Reselect our expanded range.
-   */
-  gtk_text_buffer_get_iter_at_mark (priv->buffer, &begin, mark_begin);
-  gtk_text_buffer_get_iter_at_mark (priv->buffer, &end, mark_end);
-  gtk_text_buffer_select_range (priv->buffer, &begin, &end);
-
-  /*
-   * Remove our temporary marks.
-   */
-  gtk_text_buffer_delete_mark (priv->buffer, mark_begin);
-  gtk_text_buffer_delete_mark (priv->buffer, mark_end);
-
-  EXIT;
-}
-
-void
-gb_source_view_unindent_selection (GbSourceView *view)
-{
-  GbSourceViewPrivate *priv;
-  GtkTextIter begin;
-  GtkTextIter end;
-  GtkTextIter iter;
-  GtkTextMark *mark_begin;
-  GtkTextMark *mark_end;
-  gboolean use_spaces = FALSE;
-  guint shift_width = 0;
-
-  ENTRY;
-
-  g_return_if_fail (GB_IS_SOURCE_VIEW (view));
-
-  priv = view->priv;
-
-  g_object_get (view,
-                "insert-spaces-instead-of-tabs", &use_spaces,
-                "indent-width", &shift_width,
-                NULL);
-
-  if (!gtk_text_buffer_get_has_selection (priv->buffer))
-    return;
-
-  gtk_text_buffer_get_selection_bounds (priv->buffer, &begin, &end);
-
-  /*
-   * Adjust the starting iter to include the whole line.
-   */
-  while (!gtk_text_iter_starts_line (&begin))
-    gtk_text_iter_backward_char (&begin);
-
-  /*
-   * Set marks so we can track our end position after every edit.
-   * Also allows us to reselect the whole range at the end.
-   */
-  mark_begin = gtk_text_buffer_create_mark (priv->buffer,
-                                            "tab-selection-begin",
-                                            &begin, TRUE);
-  mark_end = gtk_text_buffer_create_mark (priv->buffer,
-                                          "tab-selection-end",
-                                          &end, FALSE);
-  gtk_text_iter_assign (&iter, &begin);
-
-  /*
-   * Walk through each selected line, removing up to `shift_width`
-   * spaces from the beginning of the line, or a single tab.
-   */
-  gtk_text_buffer_begin_user_action (priv->buffer);
-  do
-    {
-      GtkTextIter next;
-      gboolean found_tab = FALSE;
-      guint n_spaces = 0;
-      gunichar ch;
-
-      while (!found_tab && (n_spaces < shift_width))
-        {
-          ch = gtk_text_iter_get_char (&iter);
-
-          if ((ch == '\t') || (ch == ' '))
-            {
-              gtk_text_iter_assign (&next, &iter);
-              gtk_text_iter_forward_char (&next);
-              gtk_text_buffer_delete (priv->buffer, &iter, &next);
-
-              found_tab = (ch == '\t');
-              n_spaces += (ch == ' ');
-            }
-          else
-            break;
-        }
-
-      if (!gtk_text_iter_forward_line (&iter))
-        break;
-
-      gtk_text_buffer_get_iter_at_mark (priv->buffer, &end, mark_end);
-    }
-  while (gtk_text_iter_compare (&iter, &end) < 0);
-  gtk_text_buffer_end_user_action (priv->buffer);
-
-  /*
-   * Reselect our expanded range.
-   */
-  gtk_text_buffer_get_iter_at_mark (priv->buffer, &begin, mark_begin);
-  gtk_text_buffer_get_iter_at_mark (priv->buffer, &end, mark_end);
-  gtk_text_buffer_select_range (priv->buffer, &begin, &end);
-
-  /*
-   * Remove our temporary marks.
-   */
-  gtk_text_buffer_delete_mark (priv->buffer, mark_begin);
-  gtk_text_buffer_delete_mark (priv->buffer, mark_end);
-
-  EXIT;
-}
-
 static void
 get_rect_for_iters (GtkTextView       *text_view,
                     const GtkTextIter *iter1,
@@ -1649,28 +1463,6 @@ gb_source_view_key_press_event (GtkWidget   *widget,
     }
 
   /*
-   * If we come across tab or shift tab (left tab) while we have a selection,
-   * try to (un)indent the selected lines.
-   */
-  if (gtk_text_buffer_get_has_selection (priv->buffer))
-    {
-      switch ((gint) event->keyval)
-        {
-        case GDK_KEY_KP_Tab:
-        case GDK_KEY_Tab:
-          gb_source_view_indent_selection (view);
-          return TRUE;
-
-        case GDK_KEY_ISO_Left_Tab:
-          gb_source_view_unindent_selection (view);
-          return TRUE;
-
-        default:
-          break;
-        }
-    }
-
-  /*
    * Allow the Input Method Context to potentially filter this keystroke.
    */
   if ((event->keyval == GDK_KEY_Return) || (event->keyval == GDK_KEY_KP_Enter))
diff --git a/src/editor/gb-source-view.h b/src/editor/gb-source-view.h
index b65c0b8..9746c6f 100644
--- a/src/editor/gb-source-view.h
+++ b/src/editor/gb-source-view.h
@@ -76,7 +76,6 @@ GbSourceAutoIndenter *gb_source_view_get_auto_indenter    (GbSourceView
 gboolean              gb_source_view_get_overwrite_braces (GbSourceView         *view);
 gboolean              gb_source_view_get_show_shadow      (GbSourceView         *view);
 GType                 gb_source_view_get_type             (void);
-void                  gb_source_view_indent_selection     (GbSourceView         *view);
 void                  gb_source_view_push_snippet         (GbSourceView         *view,
                                                            GbSourceSnippet      *snippet);
 void                  gb_source_view_set_font_name        (GbSourceView         *view,
@@ -85,7 +84,6 @@ void                  gb_source_view_set_overwrite_braces (GbSourceView
                                                            gboolean              overwrite_braces);
 void                  gb_source_view_set_show_shadow      (GbSourceView         *view,
                                                            gboolean              show_shadow);
-void                  gb_source_view_unindent_selection   (GbSourceView         *view);
 GbSourceVim          *gb_source_view_get_vim              (GbSourceView         *view);
 
 G_END_DECLS
diff --git a/src/vim/gb-source-vim.c b/src/vim/gb-source-vim.c
index 82b21bb..e03e291 100644
--- a/src/vim/gb-source-vim.c
+++ b/src/vim/gb-source-vim.c
@@ -2777,19 +2777,21 @@ static void
 gb_source_vim_indent (GbSourceVim *vim)
 {
 #ifndef GB_SOURCE_VIM_EXTERNAL
-  GbSourceView *view;
+  GtkSourceView *view;
   GtkTextBuffer *buffer;
+  GtkTextIter iter;
+  GtkTextIter selection;
 
   g_assert (GB_IS_SOURCE_VIM (vim));
 
-  if (!GB_IS_SOURCE_VIEW (vim->priv->text_view))
+  if (!GTK_SOURCE_IS_VIEW (vim->priv->text_view))
     return;
 
-  view = GB_SOURCE_VIEW (vim->priv->text_view);
+  view = GTK_SOURCE_VIEW (vim->priv->text_view);
   buffer = gtk_text_view_get_buffer (vim->priv->text_view);
 
-  if (gtk_text_buffer_get_has_selection (buffer))
-    gb_source_view_indent_selection (view);
+  if (gtk_text_buffer_get_selection_bounds (buffer, &iter, &selection))
+    gtk_source_view_indent_lines (view, &iter, &selection);
 #endif
 }
 
@@ -2797,19 +2799,21 @@ static void
 gb_source_vim_unindent (GbSourceVim *vim)
 {
 #ifndef GB_SOURCE_VIM_EXTERNAL
-  GbSourceView *view;
+  GtkSourceView *view;
   GtkTextBuffer *buffer;
+  GtkTextIter iter;
+  GtkTextIter selection;
 
   g_assert (GB_IS_SOURCE_VIM (vim));
 
-  if (!GB_IS_SOURCE_VIEW (vim->priv->text_view))
+  if (!GTK_SOURCE_IS_VIEW (vim->priv->text_view))
     return;
 
-  view = GB_SOURCE_VIEW (vim->priv->text_view);
+  view = GTK_SOURCE_VIEW (vim->priv->text_view);
   buffer = gtk_text_view_get_buffer (vim->priv->text_view);
 
-  if (gtk_text_buffer_get_has_selection (buffer))
-    gb_source_view_unindent_selection (view);
+  if (gtk_text_buffer_get_selection_bounds (buffer, &iter, &selection))
+    gtk_source_view_unindent_lines (view, &iter, &selection);
 #endif
 }
 


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