[gnome-builder/gnome-builder-3-18] libide: add IdeSourceView::reindent()
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/gnome-builder-3-18] libide: add IdeSourceView::reindent()
- Date: Tue, 13 Oct 2015 07:38:14 +0000 (UTC)
commit b8355ad79f37820c95d264b036161d05c6f63d1c
Author: Christian Hergert <chergert redhat com>
Date: Sat Oct 10 17:28:15 2015 -0400
libide: add IdeSourceView::reindent()
This G_SIGNAL_ACTION can be used to reindent a line based on what the
indenter would have done.
libide/ide-source-view.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide-source-view.c b/libide/ide-source-view.c
index 300973f..01bff7a 100644
--- a/libide/ide-source-view.c
+++ b/libide/ide-source-view.c
@@ -41,6 +41,7 @@
#include "ide-file.h"
#include "ide-file-settings.h"
#include "ide-fixit.h"
+#include "ide-gdk.h"
#include "ide-highlighter.h"
#include "ide-internal.h"
#include "ide-indenter.h"
@@ -234,6 +235,7 @@ enum {
PUSH_SELECTION,
PUSH_SNIPPET,
REBUILD_HIGHLIGHT,
+ REINDENT,
REPLAY_MACRO,
REQUEST_DOCUMENTATION,
RESET_FONT_SIZE,
@@ -3638,6 +3640,60 @@ ide_source_view_real_set_search_text (IdeSourceView *self,
}
static void
+ide_source_view_real_reindent (IdeSourceView *self)
+{
+ IdeSourceViewPrivate *priv = ide_source_view_get_instance_private (self);
+ GtkTextBuffer *buffer;
+ IdeIndenter *indenter;
+ GtkTextMark *mark;
+ GtkTextIter begin;
+ GtkTextIter end;
+ GdkEventKey *event;
+ GdkWindow *window;
+ gint cursor_offset = 0;
+ gchar *ret;
+
+ g_assert (IDE_IS_SOURCE_VIEW (self));
+
+ if (priv->buffer == NULL)
+ return;
+
+ indenter = ide_source_view_get_indenter (self);
+ if (indenter == NULL)
+ return;
+
+ buffer = GTK_TEXT_BUFFER (priv->buffer);
+ mark = gtk_text_buffer_get_insert (buffer);
+ gtk_text_buffer_get_iter_at_mark (buffer, &begin, mark);
+ gtk_text_iter_set_line_offset (&begin, 0);
+
+ end = begin;
+ while (!gtk_text_iter_ends_line (&end) &&
+ g_unichar_isspace (gtk_text_iter_get_char (&end)))
+ gtk_text_iter_forward_char (&end);
+
+ gtk_text_buffer_begin_user_action (buffer);
+
+ if (gtk_text_iter_equal (&begin, &end))
+ gtk_text_buffer_delete (buffer, &begin, &end);
+
+ window = gtk_text_view_get_window (GTK_TEXT_VIEW (self), GTK_TEXT_WINDOW_TEXT);
+ event = ide_gdk_synthesize_event_key (window, '\n');
+ ret = ide_indenter_format (indenter, GTK_TEXT_VIEW (self), &begin, &end, &cursor_offset, event);
+
+ if (ret != NULL)
+ {
+ gtk_text_buffer_delete (buffer, &begin, &end);
+ gtk_text_buffer_insert (buffer, &begin, ret, -1);
+ g_free (ret);
+ }
+
+ gtk_text_buffer_end_user_action (buffer);
+
+ gdk_event_free ((GdkEvent *)event);
+}
+
+static void
ide_source_view_constructed (GObject *object)
{
IdeSourceView *self = (IdeSourceView *)object;
@@ -5920,6 +5976,15 @@ ide_source_view_class_init (IdeSourceViewClass *klass)
G_TYPE_NONE,
0);
+ gSignals [REINDENT] =
+ g_signal_new_class_handler ("reindent",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_CALLBACK (ide_source_view_real_reindent),
+ NULL, NULL, NULL,
+ G_TYPE_NONE,
+ 0);
+
/**
* IdeSourceView:replay-macro:
* @self: an #IdeSourceView.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]