[gnome-builder] libide/sourceview: add duplicate line action
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] libide/sourceview: add duplicate line action
- Date: Mon, 5 Sep 2022 22:17:47 +0000 (UTC)
commit d55a2b038253fa4ed36292f67ea4b4ce96220701
Author: Christian Hergert <chergert redhat com>
Date: Mon Sep 5 15:17:36 2022 -0700
libide/sourceview: add duplicate line action
Fixes #1760
src/libide/sourceview/gtk/menus.ui | 5 ++++
src/libide/sourceview/ide-source-view.c | 50 +++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
---
diff --git a/src/libide/sourceview/gtk/menus.ui b/src/libide/sourceview/gtk/menus.ui
index 0a07b7ac0..95e9b3548 100644
--- a/src/libide/sourceview/gtk/menus.ui
+++ b/src/libide/sourceview/gtk/menus.ui
@@ -129,6 +129,11 @@
<attribute name="target" type="(bb)">(false, false)</attribute>
<attribute name="accel"><shift><control>j</attribute>
</item>
+ <item>
+ <attribute name="label" translatable="yes">D_uplicate Line</attribute>
+ <attribute name="action">buffer.duplicate-line</attribute>
+ <attribute name="accel"><control><alt>d</attribute>
+ </item>
</section>
<section id="ide-source-view-popup-menu-format-section">
<attribute name="page" translatable="yes">Text Editor</attribute>
diff --git a/src/libide/sourceview/ide-source-view.c b/src/libide/sourceview/ide-source-view.c
index b034cd6c9..c1948e820 100644
--- a/src/libide/sourceview/ide-source-view.c
+++ b/src/libide/sourceview/ide-source-view.c
@@ -786,6 +786,54 @@ ide_source_view_action_delete_line (GtkWidget *widget,
gdk_clipboard_set_text (gtk_widget_get_primary_clipboard (widget), text);
}
+static void
+ide_source_view_action_duplicate_line (GtkWidget *widget,
+ const char *action_name,
+ GVariant *param)
+{
+ g_autofree char *text = NULL;
+ g_autofree char *duplicate_line = NULL;
+ GtkTextIter begin, end;
+ GtkTextMark *cursor;
+ GtkTextBuffer *buffer;
+ gboolean selected;
+
+ g_assert (IDE_IS_MAIN_THREAD ());
+ g_assert (IDE_IS_SOURCE_VIEW (widget));
+
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
+ cursor = gtk_text_buffer_get_insert (buffer);
+
+ gtk_text_buffer_begin_user_action (buffer);
+
+ selected = gtk_text_buffer_get_selection_bounds (buffer, &begin, &end);
+
+ if (selected)
+ {
+ duplicate_line = gtk_text_iter_get_text (&begin, &end);
+ gtk_text_buffer_insert (buffer, &begin, duplicate_line, -1);
+ }
+ else
+ {
+ gtk_text_buffer_get_iter_at_mark (buffer, &begin, cursor);
+ end = begin;
+
+ gtk_text_iter_set_line_offset (&begin, 0);
+
+ if (!gtk_text_iter_ends_line (&end))
+ gtk_text_iter_forward_to_line_end (&end);
+
+ if (gtk_text_iter_get_line (&begin) == gtk_text_iter_get_line (&end))
+ {
+ text = gtk_text_iter_get_text (&begin, &end);
+ duplicate_line = g_strconcat (text, "\n", NULL);
+ gtk_text_buffer_insert (buffer, &begin, duplicate_line, -1);
+ }
+ }
+
+ gtk_text_buffer_end_user_action (buffer);
+}
+
static void
ide_source_view_dispose (GObject *object)
{
@@ -955,12 +1003,14 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
gtk_widget_class_install_action (widget_class, "selection.join", NULL, ide_source_view_selection_join);
gtk_widget_class_install_action (widget_class, "buffer.select-line", NULL,
ide_source_view_action_select_line);
gtk_widget_class_install_action (widget_class, "buffer.delete-line", NULL,
ide_source_view_action_delete_line);
+ gtk_widget_class_install_action (widget_class, "buffer.duplicate-line", NULL,
ide_source_view_action_duplicate_line);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_plus, GDK_CONTROL_MASK, "zoom.in", NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_minus, GDK_CONTROL_MASK, "zoom.out", NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_0, GDK_CONTROL_MASK, "zoom.one", NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_l, GDK_CONTROL_MASK, "buffer.select-line",
NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_d, GDK_CONTROL_MASK, "buffer.delete-line",
NULL);
+ gtk_widget_class_add_binding_action (widget_class, GDK_KEY_d, GDK_CONTROL_MASK|GDK_ALT_MASK,
"buffer.duplicate-line", NULL);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]