Commit a855af72 authored by Christian Hergert's avatar Christian Hergert
Browse files

GbSourceView: add option to insert a matching "'}]) on key press

parent 5ad5c080
......@@ -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>
......
......@@ -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
......
......@@ -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,
......@@ -111,6 +113,28 @@ gb_source_view_get_vim (GbSourceView *view)
return view->priv->vim;
}
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)
{
......@@ -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);
......@@ -1300,6 +1327,63 @@ gb_source_view_maybe_overwrite (GbSourceView *view,
return FALSE;
}
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"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment