[gnome-builder/wip/gtk4-port] libide/sourceview: add helper to get visual position



commit c4e9f46ac6f2d16b2035553f1774d0fbaaec416e
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jun 1 21:13:01 2022 -0700

    libide/sourceview: add helper to get visual position

 src/libide/sourceview/ide-source-view.c | 38 +++++++++++++++++++++++++++++++++
 src/libide/sourceview/ide-source-view.h |  5 +++++
 2 files changed, 43 insertions(+)
---
diff --git a/src/libide/sourceview/ide-source-view.c b/src/libide/sourceview/ide-source-view.c
index a34e28bed..330cc6816 100644
--- a/src/libide/sourceview/ide-source-view.c
+++ b/src/libide/sourceview/ide-source-view.c
@@ -1136,3 +1136,41 @@ ide_source_view_set_highlight_current_line (IdeSourceView *self,
         g_object_notify (G_OBJECT (self), "highlight-current-line");
     }
 }
+
+void
+ide_source_view_get_iter_at_visual_position (IdeSourceView *self,
+                                             GtkTextIter   *iter,
+                                             guint          line,
+                                             guint          line_offset)
+{
+  guint tab_width;
+  guint pos = 0;
+
+  g_return_if_fail (IDE_IS_SOURCE_VIEW (self));
+  g_return_if_fail (iter != NULL);
+
+  tab_width = gtk_source_view_get_tab_width (GTK_SOURCE_VIEW (self));
+  gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (self->buffer), iter, line);
+
+  while (pos < line_offset)
+    {
+      gunichar ch = gtk_text_iter_get_char (iter);
+
+      switch (ch)
+        {
+        case '\t':
+          pos += tab_width - (pos % tab_width);
+          break;
+
+        case 0:
+        case '\n':
+          return;
+
+        default:
+          pos++;
+          break;
+        }
+
+      gtk_text_iter_forward_char (iter);
+    }
+}
diff --git a/src/libide/sourceview/ide-source-view.h b/src/libide/sourceview/ide-source-view.h
index 28c6f50db..b36bff543 100644
--- a/src/libide/sourceview/ide-source-view.h
+++ b/src/libide/sourceview/ide-source-view.h
@@ -73,5 +73,10 @@ void                        ide_source_view_jump_to_iter        (GtkTextView
                                                                  gboolean                   use_align,
                                                                  double                     xalign,
                                                                  double                     yalign);
+IDE_AVAILABLE_IN_ALL
+void                        ide_source_view_get_iter_at_visual_position (IdeSourceView *self,
+                                                                         GtkTextIter   *iter,
+                                                                         guint          line,
+                                                                         guint          line_offset);
 
 G_END_DECLS


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