[gnome-builder/wip/gtk4-port: 333/1774] libide/sourceview: add position helpers




commit 9010dfad0c02c2e3a5e905f8c0cdf43d431f6b96
Author: Christian Hergert <chergert redhat com>
Date:   Sat Apr 2 11:39:09 2022 -0700

    libide/sourceview: add position helpers

 src/libide/sourceview/ide-source-view.c | 37 +++++++++++++++++++++++++++++++++
 src/libide/sourceview/ide-source-view.h | 10 +++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
---
diff --git a/src/libide/sourceview/ide-source-view.c b/src/libide/sourceview/ide-source-view.c
index dae1ed925..f851cc0b3 100644
--- a/src/libide/sourceview/ide-source-view.c
+++ b/src/libide/sourceview/ide-source-view.c
@@ -22,6 +22,8 @@
 
 #include "config.h"
 
+#include <glib/gi18n.h>
+
 #include "ide-source-view.h"
 
 struct _IdeSourceView
@@ -112,3 +114,38 @@ ide_source_view_scroll_to_insert (IdeSourceView *self)
   /* TODO: use margin to implement  "scroll offset" */
   gtk_text_view_scroll_to_mark (view, mark, .5, FALSE, .0, .0);
 }
+
+void
+ide_source_view_get_visual_position (IdeSourceView *self,
+                                     guint         *line,
+                                     guint         *line_column)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter iter;
+  GtkTextMark *mark;
+
+  g_return_if_fail (IDE_IS_SOURCE_VIEW (self));
+
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self));
+  mark = gtk_text_buffer_get_insert (buffer);
+  gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
+
+  if (line)
+    *line = gtk_text_iter_get_line (&iter);
+
+  if (line_column)
+    *line_column = gtk_source_view_get_visual_column (GTK_SOURCE_VIEW (self), &iter);
+}
+
+char *
+ide_source_view_dup_position_label (IdeSourceView *self)
+{
+  guint line;
+  guint column;
+
+  g_return_val_if_fail (IDE_IS_SOURCE_VIEW (self), NULL);
+
+  ide_source_view_get_visual_position (self, &line, &column);
+
+  return g_strdup_printf (_("Ln %u, Col %u"), line + 1, column + 1);
+}
diff --git a/src/libide/sourceview/ide-source-view.h b/src/libide/sourceview/ide-source-view.h
index f2ebef5c3..b235b6b85 100644
--- a/src/libide/sourceview/ide-source-view.h
+++ b/src/libide/sourceview/ide-source-view.h
@@ -32,8 +32,14 @@ IDE_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (IdeSourceView, ide_source_view, IDE, SOURCE_VIEW, GtkSourceView)
 
 IDE_AVAILABLE_IN_ALL
-GtkWidget *ide_source_view_new              (void);
+GtkWidget *ide_source_view_new                 (void);
 IDE_AVAILABLE_IN_ALL
-void       ide_source_view_scroll_to_insert (IdeSourceView *self);
+void       ide_source_view_scroll_to_insert    (IdeSourceView *self);
+IDE_AVAILABLE_IN_ALL
+char      *ide_source_view_dup_position_label  (IdeSourceView *self);
+IDE_AVAILABLE_IN_ALL
+void       ide_source_view_get_visual_position (IdeSourceView *self,
+                                                guint         *line,
+                                                guint         *line_column);
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]