[gnome-builder] libide/sourceview: implement overwrite-braces
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide/sourceview: implement overwrite-braces
- Date: Tue, 6 Sep 2022 06:06:44 +0000 (UTC)
commit 9cdd84e27fd271869fa23eb1d114e24f55457e87
Author: Christian Hergert <chergert redhat com>
Date: Mon Sep 5 23:05:52 2022 -0700
libide/sourceview: implement overwrite-braces
Much simpler version than before, but also doesn't work with Vim due to
how wayland input methods are swalling everything.
src/libide/sourceview/ide-source-view-private.h | 1 +
src/libide/sourceview/ide-source-view.c | 97 +++++++++++++++++++++++++
src/libide/sourceview/ide-source-view.h | 5 ++
3 files changed, 103 insertions(+)
---
diff --git a/src/libide/sourceview/ide-source-view-private.h b/src/libide/sourceview/ide-source-view-private.h
index 8f9b8722f..a47ffaaa6 100644
--- a/src/libide/sourceview/ide-source-view-private.h
+++ b/src/libide/sourceview/ide-source-view-private.h
@@ -73,6 +73,7 @@ struct _IdeSourceView
/* Bitfield values go here */
guint highlight_current_line : 1;
guint insert_matching_brace : 1;
+ guint overwrite_braces : 1;
};
diff --git a/src/libide/sourceview/ide-source-view.c b/src/libide/sourceview/ide-source-view.c
index d51be41fc..335090c09 100644
--- a/src/libide/sourceview/ide-source-view.c
+++ b/src/libide/sourceview/ide-source-view.c
@@ -35,6 +35,7 @@ enum {
PROP_FONT_SCALE,
PROP_INSERT_MATCHING_BRACE,
PROP_LINE_HEIGHT,
+ PROP_OVERWRITE_BRACES,
PROP_ZOOM_LEVEL,
N_PROPS,
@@ -318,6 +319,64 @@ ide_source_view_key_pressed_cb (IdeSourceView *self,
buffer = GTK_TEXT_BUFFER (self->buffer);
+ if (self->overwrite_braces &&
+ !gtk_text_buffer_get_has_selection (buffer))
+ {
+ GtkTextIter iter;
+ gunichar ch;
+ gboolean overwrite;
+
+ gtk_text_buffer_get_selection_bounds (buffer, &iter, NULL);
+ ch = gtk_text_iter_get_char (&iter);
+
+ switch (keyval)
+ {
+ case GDK_KEY_quotedbl:
+ overwrite = ch == '"';
+ break;
+
+ case GDK_KEY_parenright:
+ overwrite = ch == ')';
+ break;
+
+ case GDK_KEY_apostrophe:
+ overwrite = ch == '\'';
+ break;
+
+ case GDK_KEY_greater:
+ overwrite = ch == '>';
+ break;
+
+ case GDK_KEY_bracketright:
+ overwrite = ch == ']';
+ break;
+
+ case GDK_KEY_braceright:
+ overwrite = ch == '}';
+ break;
+
+ default:
+ overwrite = FALSE;
+ break;
+ }
+
+ if (overwrite)
+ {
+ GtkTextIter next = iter;
+ g_autofree char *text = NULL;
+
+ gtk_text_iter_forward_char (&next);
+ gtk_text_buffer_begin_user_action (buffer);
+ gtk_text_buffer_select_range (buffer, &iter, &next);
+ text = gtk_text_iter_get_slice (&iter, &next);
+ gtk_text_buffer_delete (buffer, &iter, &next);
+ gtk_text_buffer_insert (buffer, &iter, text, -1);
+ gtk_text_buffer_end_user_action (buffer);
+
+ return TRUE;
+ }
+ }
+
if (self->insert_matching_brace &&
!gtk_text_buffer_get_has_selection (buffer))
{
@@ -1006,6 +1065,10 @@ ide_source_view_get_property (GObject *object,
g_value_set_double (value, self->line_height);
break;
+ case PROP_OVERWRITE_BRACES:
+ g_value_set_boolean (value, ide_source_view_get_overwrite_braces (self));
+ break;
+
case PROP_ZOOM_LEVEL:
g_value_set_double (value, ide_source_view_get_zoom_level (self));
break;
@@ -1047,6 +1110,10 @@ ide_source_view_set_property (GObject *object,
ide_source_view_set_line_height (self, g_value_get_double (value));
break;
+ case PROP_OVERWRITE_BRACES:
+ ide_source_view_set_overwrite_braces (self, g_value_get_boolean (value));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -1100,6 +1167,13 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
FALSE,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+ properties [PROP_OVERWRITE_BRACES] =
+ g_param_spec_boolean ("overwrite-braces",
+ "Overwrite Braces",
+ "Overwrite a matching brace/bracket/quotation/parenthesis",
+ FALSE,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
properties [PROP_ZOOM_LEVEL] =
g_param_spec_double ("zoom-level",
"Zoom Level",
@@ -1590,3 +1664,26 @@ ide_source_view_set_insert_matching_brace (IdeSourceView *self,
g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_INSERT_MATCHING_BRACE]);
}
}
+
+gboolean
+ide_source_view_get_overwrite_braces (IdeSourceView *self)
+{
+ g_return_val_if_fail (IDE_IS_SOURCE_VIEW (self), FALSE);
+
+ return self->overwrite_braces;
+}
+
+void
+ide_source_view_set_overwrite_braces (IdeSourceView *self,
+ gboolean overwrite_braces)
+{
+ g_return_if_fail (IDE_IS_SOURCE_VIEW (self));
+
+ overwrite_braces = !!overwrite_braces;
+
+ if (overwrite_braces != self->overwrite_braces)
+ {
+ self->overwrite_braces = overwrite_braces;
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_OVERWRITE_BRACES]);
+ }
+}
diff --git a/src/libide/sourceview/ide-source-view.h b/src/libide/sourceview/ide-source-view.h
index 85f3a7417..a1ab5342c 100644
--- a/src/libide/sourceview/ide-source-view.h
+++ b/src/libide/sourceview/ide-source-view.h
@@ -83,5 +83,10 @@ gboolean ide_source_view_get_insert_matching_brace (IdeSour
IDE_AVAILABLE_IN_ALL
void ide_source_view_set_insert_matching_brace (IdeSourceView *self,
gboolean
insert_matching_brace);
+IDE_AVAILABLE_IN_ALL
+gboolean ide_source_view_get_overwrite_braces (IdeSourceView *self);
+IDE_AVAILABLE_IN_ALL
+void ide_source_view_set_overwrite_braces (IdeSourceView *self,
+ gboolean
overwrite_braces);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]