[gnome-builder] libide: add IdeSourceRange implementation



commit 5510039389e0725660b4336b42f39a3cba2c29ca
Author: Christian Hergert <christian hergert me>
Date:   Thu Feb 12 13:15:54 2015 -0800

    libide: add IdeSourceRange implementation

 libide/Makefile.am        |    2 +
 libide/ide-diagnostic.c   |   27 ++++++++++
 libide/ide-private.h      |   10 +++-
 libide/ide-source-range.c |  120 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 157 insertions(+), 2 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 70551c5..673eb1a 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -96,6 +96,8 @@ libide_la_SOURCES = \
        libide/ide-service.h \
        libide/ide-source-location.c \
        libide/ide-source-location.h \
+       libide/ide-source-range.c \
+       libide/ide-source-range.h \
        libide/ide-symbol-resolver.c \
        libide/ide-symbol-resolver.h \
        libide/ide-symbol.h \
diff --git a/libide/ide-diagnostic.c b/libide/ide-diagnostic.c
index d758c99..93a2a4c 100644
--- a/libide/ide-diagnostic.c
+++ b/libide/ide-diagnostic.c
@@ -17,6 +17,9 @@
  */
 
 #include "ide-diagnostic.h"
+#include "ide-private.h"
+#include "ide-source-location.h"
+#include "ide-source-range.h"
 
 G_DEFINE_BOXED_TYPE (IdeDiagnostic, ide_diagnostic,
                      ide_diagnostic_ref, ide_diagnostic_unref)
@@ -26,6 +29,7 @@ struct _IdeDiagnostic
   volatile gint          ref_count;
   IdeDiagnosticSeverity  severity;
   gchar                 *text;
+  GPtrArray             *ranges;
 };
 
 IdeDiagnostic *
@@ -82,6 +86,29 @@ _ide_diagnostic_new (IdeDiagnosticSeverity  severity,
   return ret;
 }
 
+void
+_ide_diagnostic_add_range (IdeDiagnostic     *self,
+                           IdeSourceLocation *begin,
+                           IdeSourceLocation *end)
+{
+  IdeSourceRange *range;
+
+  g_return_if_fail (self);
+  g_return_if_fail (begin);
+  g_return_if_fail (end);
+
+  if (!self->ranges)
+    {
+      self->ranges = g_ptr_array_new ();
+      g_ptr_array_set_free_func (self->ranges,
+                                 (GDestroyNotify)ide_source_range_unref);
+    }
+
+  range = _ide_source_range_new (begin, end);
+  if (range)
+    g_ptr_array_add (self->ranges, range);
+}
+
 GType
 ide_diagnostic_severity_get_type (void)
 {
diff --git a/libide/ide-private.h b/libide/ide-private.h
index 33f9657..f0b25c5 100644
--- a/libide/ide-private.h
+++ b/libide/ide-private.h
@@ -42,8 +42,14 @@ void _ide_diagnostician_remove_provider (IdeDiagnostician      *self,
 
 IdeDiagnostics *_ide_diagnostics_new (GPtrArray *ar);
 
-IdeDiagnostic *_ide_diagnostic_new (IdeDiagnosticSeverity  severity,
-                                    const gchar           *text);
+IdeDiagnostic *_ide_diagnostic_new       (IdeDiagnosticSeverity  severity,
+                                          const gchar           *text);
+void           _ide_diagnostic_add_range (IdeDiagnostic         *self,
+                                          IdeSourceLocation     *begin,
+                                          IdeSourceLocation     *end);
+
+IdeSourceRange *_ide_source_range_new (IdeSourceLocation *begin,
+                                       IdeSourceLocation *end);
 
 
 G_END_DECLS
diff --git a/libide/ide-source-range.c b/libide/ide-source-range.c
new file mode 100644
index 0000000..ff747a2
--- /dev/null
+++ b/libide/ide-source-range.c
@@ -0,0 +1,120 @@
+/* ide-source-range.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This file is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ide-source-location.h"
+#include "ide-source-range.h"
+
+G_DEFINE_BOXED_TYPE (IdeSourceRange, ide_source_range,
+                     ide_source_range_ref, ide_source_range_unref)
+
+struct _IdeSourceRange
+{
+  volatile gint      ref_count;
+  IdeSourceLocation *begin;
+  IdeSourceLocation *end;
+};
+
+IdeSourceRange *
+_ide_source_range_new (IdeSourceLocation *begin,
+                       IdeSourceLocation *end)
+{
+  IdeSourceRange *ret;
+
+  g_return_val_if_fail (begin, NULL);
+  g_return_val_if_fail (end, NULL);
+  g_return_val_if_fail (ide_file_equal (ide_source_location_get_file (begin),
+                                        ide_source_location_get_file (end)),
+                        NULL);
+
+  ret = g_slice_new0 (IdeSourceRange);
+  ret->ref_count = 1;
+  ret->begin = ide_source_location_ref (begin);
+  ret->end = ide_source_location_ref (end);
+
+  return ret;
+}
+
+/**
+ * ide_source_range_get_begin:
+ *
+ * Gets the beginning of the source range.
+ *
+ * Returns: (transfer none): An #IdeSourceLocation.
+ */
+IdeSourceLocation *
+ide_source_range_get_begin (IdeSourceRange *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->begin;
+}
+
+/**
+ * ide_source_range_get_end:
+ *
+ * Gets the end of the source range.
+ *
+ * Returns: (transfer none): An #IdeSourceLocation.
+ */
+IdeSourceLocation *
+ide_source_range_get_end (IdeSourceRange *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->end;
+}
+
+/**
+ * ide_source_range_ref:
+ * @self: An #IdeSourceRange
+ *
+ * Increments the reference count of @self by one. When you are done with
+ * @self, release it by calling ide_source_range_unref().
+ *
+ * Returns: (transfer full): @self
+ */
+IdeSourceRange *
+ide_source_range_ref (IdeSourceRange *self)
+{
+  g_return_val_if_fail (self, NULL);
+  g_return_val_if_fail (self->ref_count > 0, NULL);
+
+  g_atomic_int_inc (&self->ref_count);
+
+  return self;
+}
+
+/**
+ * ide_source_range_unref:
+ * @self: (transfer full): An #IdeSourceRange
+ *
+ * Decrements the reference count of @self by one.
+ */
+void
+ide_source_range_unref (IdeSourceRange *self)
+{
+  g_return_if_fail (self);
+  g_return_if_fail (self->ref_count > 0);
+
+  if (g_atomic_int_dec_and_test (&self->ref_count))
+    {
+      ide_source_location_unref (self->begin);
+      ide_source_location_unref (self->end);
+      g_slice_free (IdeSourceRange, self);
+    }
+}


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