[gnome-builder/wip/libide] libide: add ide_buffer_get_diagnostic_at_iter()



commit 855a368a5b6e5156afcaee13d834d9fbf68ac1ac
Author: Christian Hergert <christian hergert me>
Date:   Wed Feb 25 19:12:31 2015 -0800

    libide: add ide_buffer_get_diagnostic_at_iter()
    
    This is a helper function to get a diagnostic that can be found at a
    given iter in the source view. This is handy if you want to display a
    tooltip.
    
    This is not particularly optimized, but that can come later. Ideally,
    we'll have some sort of bloom filter to know if there is a diagnostic
    on a line before traversing the structure.

 libide/ide-buffer.c |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libide/ide-buffer.h |    2 +
 2 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide-buffer.c b/libide/ide-buffer.c
index fd5cd02..190088d 100644
--- a/libide/ide-buffer.c
+++ b/libide/ide-buffer.c
@@ -657,3 +657,62 @@ ide_buffer_set_highlight_diagnostics (IdeBuffer *self,
       g_object_notify_by_pspec (G_OBJECT (self), gParamSpecs [PROP_HIGHLIGHT_DIAGNOSTICS]);
     }
 }
+
+/**
+ * ide_buffer_get_diagnostic_at_iter:
+ *
+ * Gets the first diagnostic that overlaps the position
+ *
+ * Returns: (transfer none) (nullable): An #IdeDiagnostic or %NULL.
+ */
+IdeDiagnostic *
+ide_buffer_get_diagnostic_at_iter (IdeBuffer         *self,
+                                   const GtkTextIter *iter)
+{
+  g_return_val_if_fail (IDE_IS_BUFFER (self), NULL);
+  g_return_val_if_fail (iter, NULL);
+
+  if (self->diagnostics)
+    {
+      IdeDiagnostic *diagnostic = NULL;
+      guint distance = G_MAXUINT;
+      gsize size;
+      gsize i;
+      guint line;
+
+      line = gtk_text_iter_get_line (iter);
+
+      size = ide_diagnostics_get_size (self->diagnostics);
+
+      for (i = 0; i < size; i++)
+        {
+          IdeDiagnostic *diag;
+          IdeSourceLocation *location;
+          GtkTextIter pos;
+
+          diag = ide_diagnostics_index (self->diagnostics, i);
+          location = ide_diagnostic_get_location (diag);
+          if (!location)
+            continue;
+
+          ide_buffer_get_iter_at_location (self, &pos, location);
+
+          if (line == gtk_text_iter_get_line (&pos))
+            {
+              guint offset;
+
+              offset = ABS (gtk_text_iter_get_offset (iter) - gtk_text_iter_get_offset (&pos));
+
+              if (offset < distance)
+                {
+                  distance = offset;
+                  diagnostic = diag;
+                }
+            }
+        }
+
+      return diagnostic;
+    }
+
+  return NULL;
+}
diff --git a/libide/ide-buffer.h b/libide/ide-buffer.h
index 00945a4..ccb88c4 100644
--- a/libide/ide-buffer.h
+++ b/libide/ide-buffer.h
@@ -55,6 +55,8 @@ void                ide_buffer_set_highlight_diagnostics (IdeBuffer         *sel
                                                           gboolean           highlight_diagnostics);
 IdeBufferLineFlags  ide_buffer_get_line_flags            (IdeBuffer         *buffer,
                                                           guint              line);
+IdeDiagnostic      *ide_buffer_get_diagnostic_at_iter    (IdeBuffer         *self,
+                                                          const GtkTextIter *iter);
 
 G_END_DECLS
 


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