[gnome-builder] cindent: convert spaces to tabs if view requests it



commit 38e31f5a2bda6d142572dc6ee8c9146f7f2c016c
Author: Christian Hergert <christian hergert me>
Date:   Sun Mar 29 16:25:47 2015 -0700

    cindent: convert spaces to tabs if view requests it
    
    If GtkSourceView:insert-spaces-instead-of-tabs is FALSE, then we can try
    to resolve the indent to a combination of tabs+spaces.
    
    We still don't have a way to prime the options to the c indenter, so it
    wont help too much yet, but it serves as an example for the future.

 libide/c/ide-c-indenter.c |   38 +++++++++++++++++++++++++++++++++++---
 1 files changed, 35 insertions(+), 3 deletions(-)
---
diff --git a/libide/c/ide-c-indenter.c b/libide/c/ide-c-indenter.c
index 1f5388c..9dd58ee 100644
--- a/libide/c/ide-c-indenter.c
+++ b/libide/c/ide-c-indenter.c
@@ -20,6 +20,7 @@
 
 #include "c-parse-helper.h"
 #include "ide-c-indenter.h"
+#include "ide-source-view.h"
 
 #define ITER_INIT_LINE_START(iter, other) \
   gtk_text_buffer_get_iter_at_line( \
@@ -36,6 +37,9 @@ struct _IdeCIndenter
 {
   IdeIndenter parent_instance;
 
+  /* no reference */
+  IdeSourceView *view;
+
   gint  scope_indent;
   gint  condition_indent;
   gint  directive_indent;
@@ -68,16 +72,23 @@ text_iter_peek_prev_char (const GtkTextIter *location)
 
 static inline void
 build_indent (IdeCIndenter *c,
-              guint                  line_offset,
-              GtkTextIter           *matching_line,
-              GString               *str)
+              guint         line_offset,
+              GtkTextIter  *matching_line,
+              GString      *str)
 {
+  GtkSourceView *view = (GtkSourceView *)c->view;
+  guint tab_width = gtk_source_view_get_tab_width (view);
+  gint indent_width = gtk_source_view_get_indent_width (view);
   GtkTextIter iter;
   gunichar ch;
+  guint i;
 
   if (!line_offset)
     return;
 
+  if (indent_width == -1)
+    indent_width = tab_width;
+
   gtk_text_buffer_get_iter_at_line (gtk_text_iter_get_buffer (matching_line),
                                     &iter,
                                     gtk_text_iter_get_line (matching_line));
@@ -88,6 +99,10 @@ build_indent (IdeCIndenter *c,
     switch (ch)
       {
       case '\t':
+        for (i = 0; i < tab_width; i++)
+          g_string_append (str, " ");
+        break;
+
       case ' ':
         g_string_append_unichar (str, ch);
         break;
@@ -102,6 +117,20 @@ build_indent (IdeCIndenter *c,
 
   while (str->len < line_offset)
     g_string_append_c (str, ' ');
+
+  if (!gtk_source_view_get_insert_spaces_instead_of_tabs (view) && (str->len >= tab_width))
+    {
+      guint n_tabs = str->len / tab_width;
+      guint n_spaces = str->len % tab_width;
+
+      g_string_truncate (str, 0);
+
+      for (i = 0; i < n_tabs; i++)
+        g_string_append (str, "\t");
+
+      for (i = 0; i < n_spaces; i++)
+        g_string_append (str, " ");
+    }
 }
 
 static gboolean
@@ -1300,9 +1329,12 @@ ide_c_indenter_format (IdeIndenter    *indenter,
   GtkTextBuffer *buffer;
 
   g_return_val_if_fail (IDE_IS_C_INDENTER (c), NULL);
+  g_return_val_if_fail (IDE_IS_SOURCE_VIEW (view), NULL);
 
   buffer = gtk_text_view_get_buffer (view);
 
+  c->view = IDE_SOURCE_VIEW (view);
+
   switch (event->keyval) {
   case GDK_KEY_Return:
   case GDK_KEY_KP_Enter:


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