[gnome-builder] indenter-c: unindent opening brace for use in conditionals
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] indenter-c: unindent opening brace for use in conditionals
- Date: Sat, 10 Oct 2015 05:36:08 +0000 (UTC)
commit 1ffd8866264c0d0e4858f965893d14d63c507ca0
Author: Christian Hergert <chergert redhat com>
Date: Sat Oct 10 01:31:37 2015 -0400
indenter-c: unindent opening brace for use in conditionals
After a conditional like if (), we indent. So if we type a { we should
unindent the { if we are not using indented scope (gnu) style.
plugins/c-pack/ide-c-indenter.c | 85 +++++++++++++++++++++++++++++++++++---
1 files changed, 78 insertions(+), 7 deletions(-)
---
diff --git a/plugins/c-pack/ide-c-indenter.c b/plugins/c-pack/ide-c-indenter.c
index 0a3b62d..5be4f06 100644
--- a/plugins/c-pack/ide-c-indenter.c
+++ b/plugins/c-pack/ide-c-indenter.c
@@ -791,10 +791,71 @@ maybe_close_comment (IdeCIndenter *c,
}
static gchar *
-maybe_unindent_brace (IdeCIndenter *c,
- GtkTextView *view,
- GtkTextIter *begin,
- GtkTextIter *end)
+maybe_unindent_opening_brace (IdeCIndenter *c,
+ GtkTextView *view,
+ GtkTextIter *begin,
+ GtkTextIter *end,
+ gint *cursor_offset)
+{
+ GtkTextIter copy;
+
+ g_assert (IDE_IS_C_INDENTER (c));
+ g_assert (begin);
+ g_assert (end);
+
+ copy = *begin;
+
+ /*
+ * Make sure we just inserted a { and then move before it.
+ * Ensure that we only have whitespace before the {.
+ */
+ if (!gtk_text_iter_backward_char (©) ||
+ ('{' != gtk_text_iter_get_char (©)) ||
+ !gtk_text_iter_backward_char (©))
+ return NULL;
+
+ /*
+ * Find the opening of the parent scope.
+ * We should be at that + post_scope_indent, which is where
+ * our conditional would have started.
+ */
+ if (line_is_whitespace_until (©) && backward_find_matching_char (©, '}'))
+ {
+ guint offset;
+ GString *str;
+
+ backward_to_line_first_char (©);
+
+ offset = GET_LINE_OFFSET (©);
+ str = g_string_new (NULL);
+ build_indent (c, offset + c->post_scope_indent + c->pre_scope_indent, ©, str);
+ g_string_append_c (str, '{');
+
+ if (ide_source_view_get_insert_matching_brace (IDE_SOURCE_VIEW (view)))
+ {
+ g_string_append_c (str, '}');
+ *cursor_offset = -1;
+ }
+
+ gtk_text_iter_set_line_offset (begin, 0);
+
+ return g_string_free (str, FALSE);
+ }
+
+ if (ide_source_view_get_insert_matching_brace (IDE_SOURCE_VIEW (view)))
+ {
+ *cursor_offset = -1;
+ return g_strdup ("}");
+ }
+
+ return NULL;
+}
+
+static gchar *
+maybe_unindent_closing_brace (IdeCIndenter *c,
+ GtkTextView *view,
+ GtkTextIter *begin,
+ GtkTextIter *end)
{
GtkTextIter saved;
gchar *ret = NULL;
@@ -1198,6 +1259,7 @@ ide_c_indenter_is_trigger (IdeIndenter *indenter,
return FALSE;
/* Fall through */
+ case GDK_KEY_braceleft:
case GDK_KEY_braceright:
case GDK_KEY_colon:
case GDK_KEY_numbersign:
@@ -1272,10 +1334,19 @@ ide_c_indenter_format (IdeIndenter *indenter,
case GDK_KEY_braceright:
/*
- * Probably need to unindent this line.
- * TODO: Maybe overwrite character.
+ * Possibly need to unindent this line.
+ */
+ ret = maybe_unindent_closing_brace (c, view, begin, end);
+ break;
+
+ case GDK_KEY_braceleft:
+ /*
+ * Maybe unindent the opening brace to match the conditional.
+ * This could happen if we are doing k&r/linux/etc where the open
+ * brace has less indentation than the natural single line conditional
+ * child statement.
*/
- ret = maybe_unindent_brace (c, view, begin, end);
+ ret = maybe_unindent_opening_brace (c, view, begin, end, cursor_offset);
break;
case GDK_KEY_colon:
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]