[gnome-builder] GbSourceView: add option to insert a matching "'}]) on key press
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] GbSourceView: add option to insert a matching "'}]) on key press
- Date: Fri, 19 Dec 2014 06:58:28 +0000 (UTC)
commit a855af7253359f38f53f76608390b679a6b4e0a3
Author: Christian Hergert <christian hergert me>
Date: Thu Dec 18 22:57:06 2014 -0800
GbSourceView: add option to insert a matching "'}]) on key press
...rg.gnome.builder.editor.language.gschema.xml.in | 3 +
src/auto-indent/gb-source-auto-indenter-c.c | 20 +---
src/editor/gb-source-view.c | 106 ++++++++++++++++++++
3 files changed, 113 insertions(+), 16 deletions(-)
---
diff --git a/data/org.gnome.builder.editor.language.gschema.xml.in
b/data/org.gnome.builder.editor.language.gschema.xml.in
index 52e887d..50b8196 100644
--- a/data/org.gnome.builder.editor.language.gschema.xml.in
+++ b/data/org.gnome.builder.editor.language.gschema.xml.in
@@ -12,6 +12,9 @@
<key name="insert-spaces-instead-of-tabs" type="b">
<default>false</default>
</key>
+ <key name="insert-matching-brace" type="b">
+ <default>false</default>
+ </key>
<key name="overwrite-braces" type="b">
<default>false</default>
</key>
diff --git a/src/auto-indent/gb-source-auto-indenter-c.c b/src/auto-indent/gb-source-auto-indenter-c.c
index 09e1421..cb8c0b3 100644
--- a/src/auto-indent/gb-source-auto-indenter-c.c
+++ b/src/auto-indent/gb-source-auto-indenter-c.c
@@ -910,6 +910,7 @@ line_starts_with_fuzzy (const GtkTextIter *iter,
return ret;
}
+#if 0
static gchar *
maybe_space_before_paren (GbSourceAutoIndenterC *c,
GtkTextIter *begin,
@@ -954,6 +955,7 @@ maybe_space_before_paren (GbSourceAutoIndenterC *c,
return NULL;
}
+#endif
static gchar *
format_parameter (const Parameter *param,
@@ -1093,6 +1095,7 @@ maybe_align_parameters (GbSourceAutoIndenterC *c,
RETURN (ret);
}
+#if 0
static gchar *
maybe_add_brace (GbSourceAutoIndenterC *c,
GtkTextIter *begin,
@@ -1139,6 +1142,7 @@ maybe_add_brace (GbSourceAutoIndenterC *c,
return NULL;
}
+#endif
static gboolean
line_is_case (const GtkTextIter *line)
@@ -1298,12 +1302,10 @@ gb_source_auto_indenter_c_is_trigger (GbSourceAutoIndenter *indenter,
return FALSE;
/* Fall through */
- case GDK_KEY_braceleft:
case GDK_KEY_braceright:
case GDK_KEY_colon:
case GDK_KEY_numbersign:
case GDK_KEY_parenright:
- case GDK_KEY_parenleft:
case GDK_KEY_slash:
return TRUE;
@@ -1367,13 +1369,6 @@ gb_source_auto_indenter_c_format (GbSourceAutoIndenter *indenter,
break;
- case GDK_KEY_braceleft:
- /*
- * If we are starting a new scope, maybe add a match closing brace.
- */
- ret = maybe_add_brace (c, begin, end, cursor_offset);
- break;
-
case GDK_KEY_braceright:
/*
* Probably need to unindent this line.
@@ -1396,13 +1391,6 @@ gb_source_auto_indenter_c_format (GbSourceAutoIndenter *indenter,
ret = maybe_unindent_hash (c, begin, end);
break;
- case GDK_KEY_parenleft:
- /*
- * Possibly add a space before the ( if our config requests so.
- */
- ret = maybe_space_before_paren (c, begin, end);
- break;
-
case GDK_KEY_parenright:
/*
* If we are closing a function declaration, adjust the spacing of
diff --git a/src/editor/gb-source-view.c b/src/editor/gb-source-view.c
index d655514..be5e41e 100644
--- a/src/editor/gb-source-view.c
+++ b/src/editor/gb-source-view.c
@@ -67,6 +67,7 @@ struct _GbSourceViewPrivate
guint auto_indent : 1;
guint enable_word_completion : 1;
+ guint insert_matching_brace : 1;
guint show_shadow : 1;
guint overwrite_braces : 1;
};
@@ -83,6 +84,7 @@ enum {
PROP_AUTO_INDENT,
PROP_ENABLE_WORD_COMPLETION,
PROP_FONT_NAME,
+ PROP_INSERT_MATCHING_BRACE,
PROP_OVERWRITE_BRACES,
PROP_SEARCH_HIGHLIGHTER,
PROP_SHOW_SHADOW,
@@ -112,6 +114,28 @@ gb_source_view_get_vim (GbSourceView *view)
}
gboolean
+gb_source_view_get_insert_matching_brace (GbSourceView *view)
+{
+ g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), FALSE);
+
+ return view->priv->insert_matching_brace;
+}
+
+void
+gb_source_view_set_insert_matching_brace (GbSourceView *view,
+ gboolean insert_matching_brace)
+{
+ g_return_if_fail (GB_IS_SOURCE_VIEW (view));
+
+ if (view->priv->insert_matching_brace != insert_matching_brace)
+ {
+ view->priv->insert_matching_brace = insert_matching_brace;
+ g_object_notify_by_pspec (G_OBJECT (view),
+ gParamSpecs [PROP_INSERT_MATCHING_BRACE]);
+ }
+}
+
+gboolean
gb_source_view_get_overwrite_braces (GbSourceView *view)
{
g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), FALSE);
@@ -238,6 +262,9 @@ gb_source_view_connect_settings (GbSourceView *view)
g_settings_bind (settings, "highlight-matching-brackets",
buffer, "highlight-matching-brackets",
G_SETTINGS_BIND_GET);
+ g_settings_bind (settings, "insert-matching-brace",
+ view, "insert-matching-brace",
+ G_SETTINGS_BIND_GET);
g_settings_bind (settings, "insert-spaces-instead-of-tabs",
view, "insert-spaces-instead-of-tabs",
G_SETTINGS_BIND_GET);
@@ -1301,6 +1328,63 @@ gb_source_view_maybe_overwrite (GbSourceView *view,
}
static gboolean
+gb_source_view_maybe_insert_match (GbSourceView *view,
+ GdkEventKey *event)
+{
+ GtkTextIter iter;
+ GtkTextBuffer *buffer;
+ GtkTextMark *insert;
+ gchar ch = 0;
+
+ g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), FALSE);
+ g_return_val_if_fail (event, FALSE);
+
+ if (!view->priv->insert_matching_brace)
+ return FALSE;
+
+ switch (event->keyval)
+ {
+ case GDK_KEY_braceleft:
+ ch = '}';
+ break;
+
+ case GDK_KEY_parenleft:
+ ch = ')';
+ break;
+
+ case GDK_KEY_bracketleft:
+ ch = ']';
+ break;
+
+ case GDK_KEY_quotedbl:
+ ch = '"';
+ break;
+
+ case GDK_KEY_quoteleft:
+ case GDK_KEY_quoteright:
+ ch = '\'';
+ break;
+
+ default:
+ break;
+ }
+
+ if (ch)
+ {
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+ insert = gtk_text_buffer_get_insert (buffer);
+ gtk_text_buffer_insert_at_cursor (buffer, &ch, 1);
+ gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
+ gtk_text_iter_backward_char (&iter);
+ gtk_text_buffer_select_range (buffer, &iter, &iter);
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
gb_source_view_key_press_event (GtkWidget *widget,
GdkEventKey *event)
{
@@ -1450,6 +1534,9 @@ gb_source_view_key_press_event (GtkWidget *widget,
ret = GTK_WIDGET_CLASS (gb_source_view_parent_class)->key_press_event (widget, event);
+ if (ret)
+ gb_source_view_maybe_insert_match (view, event);
+
return ret;
}
@@ -1961,6 +2048,11 @@ gb_source_view_get_property (GObject *object,
gb_source_view_get_enable_word_completion (view));
break;
+ case PROP_INSERT_MATCHING_BRACE:
+ g_value_set_boolean (value,
+ gb_source_view_get_insert_matching_brace (view));
+ break;
+
case PROP_OVERWRITE_BRACES:
g_value_set_boolean (value,
gb_source_view_get_overwrite_braces (view));
@@ -2003,6 +2095,11 @@ gb_source_view_set_property (GObject *object,
gb_source_view_set_font_name (view, g_value_get_string (value));
break;
+ case PROP_INSERT_MATCHING_BRACE:
+ gb_source_view_set_insert_matching_brace (view,
+ g_value_get_boolean (value));
+ break;
+
case PROP_OVERWRITE_BRACES:
gb_source_view_set_overwrite_braces (view, g_value_get_boolean (value));
break;
@@ -2071,6 +2168,15 @@ gb_source_view_class_init (GbSourceViewClass *klass)
g_object_class_install_property (object_class, PROP_FONT_NAME,
gParamSpecs [PROP_FONT_NAME]);
+ gParamSpecs [PROP_INSERT_MATCHING_BRACE] =
+ g_param_spec_boolean ("insert-matching-brace",
+ _("Insert Matching Brace"),
+ _("If we should insert matching braces."),
+ FALSE,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_INSERT_MATCHING_BRACE,
+ gParamSpecs [PROP_INSERT_MATCHING_BRACE]);
+
gParamSpecs [PROP_OVERWRITE_BRACES] =
g_param_spec_boolean ("overwrite-braces",
_("Overwrite Braces"),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]