[gnome-builder/wip/slaf/xml-pack: 241/254] xml-pack: enhance IdeXmlPosition



commit cbb3858aa3261f3228b854ad640d657807a15902
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Tue Jun 27 23:21:30 2017 +0200

    xml-pack: enhance IdeXmlPosition
    
    We add some new members:
    - prefix: name of a partial element node
    - child_node: partial node
    - detail: position in an element node

 plugins/xml-pack/ide-xml-position.c |  103 +++++++++++++++++++++++++++++++++--
 plugins/xml-pack/ide-xml-position.h |   66 +++++++++++++----------
 plugins/xml-pack/ide-xml-types.h    |    8 +++
 3 files changed, 143 insertions(+), 34 deletions(-)
---
diff --git a/plugins/xml-pack/ide-xml-position.c b/plugins/xml-pack/ide-xml-position.c
index ca421be..70824b1 100644
--- a/plugins/xml-pack/ide-xml-position.c
+++ b/plugins/xml-pack/ide-xml-position.c
@@ -21,8 +21,10 @@
 G_DEFINE_BOXED_TYPE (IdeXmlPosition, ide_xml_position, ide_xml_position_ref, ide_xml_position_unref)
 
 IdeXmlPosition *
-ide_xml_position_new (IdeXmlSymbolNode   *node,
-                      IdeXmlPositionKind  kind)
+ide_xml_position_new (IdeXmlSymbolNode     *node,
+                      const gchar          *prefix,
+                      IdeXmlPositionKind    kind,
+                      IdeXmlPositionDetail  detail)
 {
   IdeXmlPosition *self;
 
@@ -32,7 +34,11 @@ ide_xml_position_new (IdeXmlSymbolNode   *node,
   self->ref_count = 1;
 
   self->node = (IDE_IS_XML_SYMBOL_NODE (node)) ? g_object_ref (node) : NULL;
+  if (!ide_str_empty0 (prefix))
+    self->prefix = g_strdup (prefix);
+
   self->kind = kind;
+  self->detail = detail;
   self->child_pos = -1;
 
   return self;
@@ -47,7 +53,9 @@ ide_xml_position_copy (IdeXmlPosition *self)
   g_return_val_if_fail (self->ref_count, NULL);
 
   copy = ide_xml_position_new (self->node,
-                               self->kind);
+                               self->prefix,
+                               self->kind,
+                               self->detail);
 
   if (self->analysis != NULL)
     copy->analysis = ide_xml_analysis_ref (self->analysis);
@@ -72,7 +80,10 @@ ide_xml_position_free (IdeXmlPosition *self)
   if (self->analysis != NULL)
     ide_xml_analysis_unref (self->analysis);
 
+  g_clear_pointer (&self->prefix, g_free);
+
   g_clear_object (&self->node);
+  g_clear_object (&self->child_node);
   g_clear_object (&self->previous_sibling_node);
   g_clear_object (&self->next_sibling_node);
 
@@ -178,12 +189,43 @@ ide_xml_position_kind_get_str (IdeXmlPositionKind kind)
   return kind_str;
 }
 
+const gchar *
+ide_xml_position_detail_get_str (IdeXmlPositionDetail detail)
+{
+  const gchar *detail_str;
+
+  switch (detail)
+    {
+    case IDE_XML_POSITION_DETAIL_NONE:
+      detail_str = "none";
+      break;
+
+    case IDE_XML_POSITION_DETAIL_IN_NAME:
+      detail_str = "in name";
+      break;
+
+    case IDE_XML_POSITION_DETAIL_IN_ATTRIBUTE_NAME:
+      detail_str = "in attribute name";
+      break;
+
+    case IDE_XML_POSITION_DETAIL_IN_ATTRIBUTE_VALUE:
+      detail_str = "in attribute value";
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+
+  return detail_str;
+}
+
 void
 ide_xml_position_print (IdeXmlPosition *self)
 {
   const gchar *p_sibling_str;
   const gchar *n_sibling_str;
   const gchar *kind_str;
+  const gchar *detail_str;
   IdeXmlSymbolNode *parent_node;
   gint n_children;
 
@@ -196,12 +238,15 @@ ide_xml_position_print (IdeXmlPosition *self)
     ide_xml_symbol_node_get_element_name (self->next_sibling_node);
 
   kind_str = ide_xml_position_kind_get_str (self->kind);
+  detail_str = ide_xml_position_detail_get_str (self->detail);
 
   parent_node = ide_xml_symbol_node_get_parent (self->node);
-  printf ("POSITION: parent: %s node: %s kind:%s",
+  printf ("POSITION: parent: %s node: %s kind:%s detail:'%s' prefix:'%s'\n",
           (parent_node != NULL) ? ide_xml_symbol_node_get_element_name (parent_node) : "none",
           (self->node != NULL) ? ide_xml_symbol_node_get_element_name (self->node) : "none",
-          kind_str);
+          kind_str,
+          detail_str,
+          self->prefix);
 
   if (self->child_pos != -1)
     {
@@ -214,7 +259,7 @@ ide_xml_position_print (IdeXmlPosition *self)
           else
             printf (" pos: |0..%d\n", n_children - 1);
         }
-      else if (self->child_pos == (n_children + 1))
+      else if (self->child_pos == n_children)
         {
           if (n_children == 1)
             printf (" pos: 0|\n");
@@ -224,12 +269,15 @@ ide_xml_position_print (IdeXmlPosition *self)
       else
         printf (" pos: %d|%d\n", self->child_pos - 1, self->child_pos);
     }
+  else if (self->child_node != NULL)
+    printf (" child node:%s\n", ide_xml_symbol_node_get_element_name (self->child_node));
   else
     printf ("\n");
 
   if (self->node != NULL)
     {
       const gchar **attributes_names;
+      IdeXmlSymbolNode *node;
 
       if (NULL != (attributes_names = ide_xml_symbol_node_get_attributes_names (self->node)))
         {
@@ -239,6 +287,15 @@ ide_xml_position_print (IdeXmlPosition *self)
               ++attributes_names;
             }
         }
+
+      if ((n_children = ide_xml_symbol_node_get_n_direct_children (self->node)) > 0)
+        printf ("children: %d\n", n_children);
+
+      for (gint i = 0; i < n_children; ++i)
+        {
+          node = (IdeXmlSymbolNode *)ide_xml_symbol_node_get_nth_direct_child (self->node, i);
+          printf ("name:'%s'\n", ide_xml_symbol_node_get_element_name (node));
+        }
     }
 }
 
@@ -258,6 +315,22 @@ ide_xml_position_get_kind (IdeXmlPosition *self)
   return self->kind;
 }
 
+IdeXmlPositionDetail
+ide_xml_position_get_detail (IdeXmlPosition *self)
+{
+  g_return_val_if_fail (self, IDE_XML_POSITION_DETAIL_NONE);
+
+  return self->detail;
+}
+
+const gchar *
+ide_xml_position_get_prefix (IdeXmlPosition *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->prefix;
+}
+
 gint
 ide_xml_position_get_child_pos (IdeXmlPosition *self)
 {
@@ -273,3 +346,21 @@ void ide_xml_position_set_child_pos (IdeXmlPosition *self,
 
   self->child_pos = child_pos;
 }
+
+IdeXmlSymbolNode *
+ide_xml_position_get_child_node (IdeXmlPosition *self)
+{
+  g_return_val_if_fail (self, NULL);
+
+  return self->child_node;
+}
+
+/* TODO: ide_xml_position_take_child_node ? */
+void ide_xml_position_set_child_node (IdeXmlPosition   *self,
+                                      IdeXmlSymbolNode *child_node)
+{
+  g_return_if_fail (self);
+
+  g_clear_object (&self->child_node);
+  self->child_node = child_node;
+}
diff --git a/plugins/xml-pack/ide-xml-position.h b/plugins/xml-pack/ide-xml-position.h
index a2320ca..d5f48de 100644
--- a/plugins/xml-pack/ide-xml-position.h
+++ b/plugins/xml-pack/ide-xml-position.h
@@ -33,38 +33,48 @@ typedef struct _IdeXmlPosition IdeXmlPosition;
 
 struct _IdeXmlPosition
 {
-  IdeXmlAnalysis     *analysis;
-  IdeXmlSymbolNode   *node;
-  IdeXmlSymbolNode   *previous_sibling_node;
-  IdeXmlSymbolNode   *next_sibling_node;
-  IdeXmlPositionKind  kind;
-  gint                child_pos;
+  IdeXmlAnalysis       *analysis;
+  IdeXmlSymbolNode     *node;
+  IdeXmlSymbolNode     *child_node;
+  IdeXmlSymbolNode     *previous_sibling_node;
+  IdeXmlSymbolNode     *next_sibling_node;
+  gchar                *prefix;
+  IdeXmlPositionKind    kind;
+  IdeXmlPositionDetail  detail;
+  gint                  child_pos;
 
-  guint               ref_count;
+  guint                 ref_count;
 };
 
-IdeXmlPosition     *ide_xml_position_new                  (IdeXmlSymbolNode   *node,
-                                                           IdeXmlPositionKind  kind);
-IdeXmlPosition     *ide_xml_position_copy                 (IdeXmlPosition     *self);
-IdeXmlPosition     *ide_xml_position_ref                  (IdeXmlPosition     *self);
-void                ide_xml_position_unref                (IdeXmlPosition     *self);
-IdeXmlAnalysis     *ide_xml_position_get_analysis         (IdeXmlPosition     *self);
-gint                ide_xml_position_get_child_pos        (IdeXmlPosition     *self);
-IdeXmlPositionKind  ide_xml_position_get_kind             (IdeXmlPosition     *self);
-IdeXmlSymbolNode   *ide_xml_position_get_next_sibling     (IdeXmlPosition     *self);
-IdeXmlSymbolNode   *ide_xml_position_get_node             (IdeXmlPosition     *self);
-IdeXmlSymbolNode   *ide_xml_position_get_parent_node      (IdeXmlPosition     *self);
-IdeXmlSymbolNode   *ide_xml_position_get_previous_sibling (IdeXmlPosition     *self);
-void                ide_xml_position_set_analysis         (IdeXmlPosition     *self,
-                                                           IdeXmlAnalysis     *analysis);
-void                ide_xml_position_set_child_pos        (IdeXmlPosition     *self,
-                                                           gint                child_pos);
-void                ide_xml_position_set_siblings         (IdeXmlPosition     *self,
-                                                           IdeXmlSymbolNode   *previous_sibling_node,
-                                                           IdeXmlSymbolNode   *next_sibling_node);
+IdeXmlPosition           *ide_xml_position_new                  (IdeXmlSymbolNode      *node,
+                                                                 const gchar           *prefix,
+                                                                 IdeXmlPositionKind     kind,
+                                                                 IdeXmlPositionDetail   detail);
+IdeXmlPosition           *ide_xml_position_copy                 (IdeXmlPosition        *self);
+IdeXmlPosition           *ide_xml_position_ref                  (IdeXmlPosition        *self);
+void                      ide_xml_position_unref                (IdeXmlPosition        *self);
+IdeXmlAnalysis           *ide_xml_position_get_analysis         (IdeXmlPosition        *self);
+gint                      ide_xml_position_get_child_pos        (IdeXmlPosition        *self);
+IdeXmlPositionDetail      ide_xml_position_get_detail           (IdeXmlPosition        *self);
+IdeXmlPositionKind        ide_xml_position_get_kind             (IdeXmlPosition        *self);
+IdeXmlSymbolNode         *ide_xml_position_get_next_sibling     (IdeXmlPosition        *self);
+IdeXmlSymbolNode         *ide_xml_position_get_child_node       (IdeXmlPosition        *self);
+IdeXmlSymbolNode         *ide_xml_position_get_node             (IdeXmlPosition        *self);
+IdeXmlSymbolNode         *ide_xml_position_get_parent_node      (IdeXmlPosition        *self);
+const gchar              *ide_xml_position_get_prefix           (IdeXmlPosition        *self);
+IdeXmlSymbolNode         *ide_xml_position_get_previous_sibling (IdeXmlPosition        *self);
+void                      ide_xml_position_set_analysis         (IdeXmlPosition        *self,
+                                                                 IdeXmlAnalysis        *analysis);
+void                      ide_xml_position_set_child_node       (IdeXmlPosition        *self,
+                                                                 IdeXmlSymbolNode      *child_node);
+void                      ide_xml_position_set_child_pos        (IdeXmlPosition        *self,
+                                                                 gint                   child_pos);
+void                      ide_xml_position_set_siblings         (IdeXmlPosition        *self,
+                                                                 IdeXmlSymbolNode      
*previous_sibling_node,
+                                                                 IdeXmlSymbolNode      *next_sibling_node);
 
-void                ide_xml_position_print                (IdeXmlPosition     *self);
-const gchar        *ide_xml_position_kind_get_str         (IdeXmlPositionKind  kind);
+void                      ide_xml_position_print                (IdeXmlPosition        *self);
+const gchar              *ide_xml_position_kind_get_str         (IdeXmlPositionKind     kind);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeXmlPosition, ide_xml_position_unref)
 
diff --git a/plugins/xml-pack/ide-xml-types.h b/plugins/xml-pack/ide-xml-types.h
index b42889b..87537b0 100644
--- a/plugins/xml-pack/ide-xml-types.h
+++ b/plugins/xml-pack/ide-xml-types.h
@@ -31,6 +31,14 @@ typedef enum
   IDE_XML_POSITION_KIND_IN_CONTENT
 } IdeXmlPositionKind;
 
+typedef enum
+{
+  IDE_XML_POSITION_DETAIL_NONE,
+  IDE_XML_POSITION_DETAIL_IN_NAME,
+  IDE_XML_POSITION_DETAIL_IN_ATTRIBUTE_NAME,
+  IDE_XML_POSITION_DETAIL_IN_ATTRIBUTE_VALUE
+} IdeXmlPositionDetail;
+
 G_END_DECLS
 
 #endif /* IDE_XML_TYPES_H */


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