[gnome-builder/auto-indent] auto-indent: check if we are in a c89 comment.



commit 91417911addbf2b8af6a4d64a7ad457e0c3a4c60
Author: Christian Hergert <christian hergert me>
Date:   Wed Sep 17 02:33:12 2014 -0700

    auto-indent: check if we are in a c89 comment.

 src/editor/gb-source-auto-indenter-c.c |   58 ++++++++++++++++++++++++++++++++
 1 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/src/editor/gb-source-auto-indenter-c.c b/src/editor/gb-source-auto-indenter-c.c
index b7adb4b..bd369a5 100644
--- a/src/editor/gb-source-auto-indenter-c.c
+++ b/src/editor/gb-source-auto-indenter-c.c
@@ -155,6 +155,49 @@ backward_find_stmt_expr (GtkTextIter *iter)
   return FALSE;
 }
 
+static gboolean
+in_c89_comment (GtkTextIter *iter)
+{
+  GtkTextIter cur;
+  GtkTextIter after_cur;
+  GtkTextIter match_begin;
+  GtkTextIter match_end;
+
+  gtk_text_iter_assign (&cur, iter);
+
+  gtk_text_iter_assign (&after_cur, iter);
+  gtk_text_iter_forward_char (&after_cur);
+
+  /*
+   * This works by first looking for the end of a comment. Afterwards,
+   * we then walk forward looking for the beginning of a comment. If we
+   * find one, then we are still in a comment.
+   *
+   * Not perfect, since we could be in a string, but it's a good start.
+   */
+
+  if (gtk_text_iter_backward_search (&after_cur, "*/",
+                                     GTK_TEXT_SEARCH_TEXT_ONLY, &match_begin,
+                                     &match_end, NULL))
+    gtk_text_iter_assign (iter, &match_end);
+  else
+    gtk_text_buffer_get_start_iter (gtk_text_iter_get_buffer (iter), iter);
+
+  /*
+   * Walk forwards until we find begin of a comment.
+   */
+  if (gtk_text_iter_forward_search (iter, "/*", GTK_TEXT_SEARCH_TEXT_ONLY,
+                                    &match_begin, &match_end, &after_cur))
+    {
+      gtk_text_iter_assign (iter, &match_begin);
+      return TRUE;
+    }
+
+  gtk_text_iter_assign (iter, &cur);
+
+  return FALSE;
+}
+
 static gchar *
 gb_source_auto_indenter_c_query (GbSourceAutoIndenter *indenter,
                                  GtkTextView          *view,
@@ -198,6 +241,21 @@ gb_source_auto_indenter_c_query (GbSourceAutoIndenter *indenter,
   ch = gtk_text_iter_get_char (iter);
 
   /*
+   * If we are in a c89 multi-line comment, try to match the previous comment
+   * line. Function will leave iter at original position unless it matched.
+   * If so, it will be at the beginning of the comment.
+   */
+  if (in_c89_comment (iter))
+    {
+      guint offset;
+
+      offset = gtk_text_iter_get_line_offset (iter);
+      build_indent (c, offset + 1, iter, str);
+      g_string_append (str, "* ");
+      GOTO (cleanup);
+    }
+
+  /*
    * If we just placed a terminating parenthesis, we need to work our way back
    * to it's match. That way we can peak at what it was and determine
    * indentation from that.


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