[anjuta/newproject] Move creating and destruction of source token in am_writer.c



commit 93a3698dda7536e580cd22c8978f4a4f2a2c11cf
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Wed Oct 6 22:56:43 2010 +0200

    Move creating and destruction of source token in am_writer.c

 plugins/am-project/am-node.c    |   12 +++
 plugins/am-project/am-node.h    |    2 +
 plugins/am-project/am-project.c |   96 +------------------------
 plugins/am-project/am-project.h |    3 +
 plugins/am-project/am-writer.c  |  150 +++++++++++++++++++++++++++++++++++++-
 plugins/am-project/am-writer.h  |    6 +-
 6 files changed, 169 insertions(+), 100 deletions(-)
---
diff --git a/plugins/am-project/am-node.c b/plugins/am-project/am-node.c
index 40f6d31..7af4996 100644
--- a/plugins/am-project/am-node.c
+++ b/plugins/am-project/am-node.c
@@ -720,6 +720,17 @@ anjuta_am_target_node_class_init (AnjutaAmTargetNodeClass *klass)
 /* Source object
  *---------------------------------------------------------------------------*/
 
+AnjutaToken *
+amp_source_get_token (AnjutaAmSourceNode *node)
+{
+	return node->token;
+}
+
+void
+amp_source_add_token (AnjutaAmSourceNode *node, AnjutaToken *token)
+{
+	node->token = token;
+}
 
 AnjutaProjectNode*
 amp_source_new (GFile *file, GError **error)
@@ -733,6 +744,7 @@ amp_source_new (GFile *file, GError **error)
 	node->base.name = NULL;
 	node->base.file = g_object_ref (file);
 	node->base.state = ANJUTA_PROJECT_CAN_REMOVE;
+	node->token = NULL;
 
 	return ANJUTA_PROJECT_NODE (node);
 }
diff --git a/plugins/am-project/am-node.h b/plugins/am-project/am-node.h
index 038f11c..5b6c17e 100644
--- a/plugins/am-project/am-node.h
+++ b/plugins/am-project/am-node.h
@@ -85,6 +85,8 @@ void amp_target_free (AnjutaAmTargetNode *node);
 
 AnjutaProjectNode* amp_source_new (GFile *file, GError **error);
 void amp_source_free (AnjutaAmSourceNode *node);
+AnjutaToken *amp_source_get_token (AnjutaAmSourceNode *node);
+void amp_source_add_token (AnjutaAmSourceNode *node, AnjutaToken *token);
 
 G_END_DECLS
 
diff --git a/plugins/am-project/am-project.c b/plugins/am-project/am-project.c
index 0534667..c45f0f9 100644
--- a/plugins/am-project/am-project.c
+++ b/plugins/am-project/am-project.c
@@ -310,7 +310,7 @@ file_type (GFile *file, const gchar *filename)
  *---------------------------------------------------------------------------*/
 
 /* Remove invalid character according to automake rules */
-static gchar*
+gchar*
 canonicalize_automake_variable (gchar *name)
 {
 	gchar *canon_name = g_strdup (name);
@@ -589,95 +589,6 @@ find_canonical_target (AnjutaProjectNode *node, gpointer data)
 /* Source objects
  *---------------------------------------------------------------------------*/
 
-static gboolean 
-amp_source_fill_token (AmpProject  *project, AnjutaAmSourceNode *source, GError **error)
-{
-	AnjutaAmGroupNode *group;
-	AnjutaAmTargetNode *target;
-	gboolean after;
-	AnjutaToken *token;
-	AnjutaToken *prev;
-	AnjutaToken *args;
-	gchar *relative_name;
-
-	/* Get parent target */
-	target = ANJUTA_AM_TARGET_NODE (anjuta_project_node_parent (ANJUTA_PROJECT_NODE (source)));
-	if ((target == NULL) || (anjuta_project_node_get_node_type (target) != ANJUTA_PROJECT_TARGET)) return FALSE;
-	
-	group = ANJUTA_AM_GROUP_NODE (anjuta_project_node_parent (ANJUTA_PROJECT_NODE (target)));
-	relative_name = g_file_get_relative_path (AMP_GROUP_DATA (group)->base.file, AMP_SOURCE_DATA (source)->base.file);
-
-	/* Add in Makefile.am */
-	/* Find a sibling if possible */
-	if (source->base.prev != NULL)
-	{
-		prev = AMP_SOURCE_DATA (source->base.prev)->token;
-		after = TRUE;
-		args = anjuta_token_list (prev);
-	}
-	else if (source->base.next != NULL)
-	{
-		prev = AMP_SOURCE_DATA (source->base.next)->token;
-		after = FALSE;
-		args = anjuta_token_list (prev);
-	}
-	else
-	{
-		prev = NULL;
-		args = NULL;
-	}
-	
-	if (args == NULL)
-	{
-		gchar *target_var;
-		gchar *canon_name;
-		AnjutaToken *var;
-		GList *list;
-		
-		canon_name = canonicalize_automake_variable (AMP_TARGET_DATA (target)->base.name);
-		target_var = g_strconcat (canon_name,  "_SOURCES", NULL);
-
-		/* Search where the target is declared */
-		var = NULL;
-		list = amp_target_get_token (target);
-		if (list != NULL)
-		{
-			var = (AnjutaToken *)list->data;
-			if (var != NULL)
-			{
-				var = anjuta_token_list (var);
-				if (var != NULL)
-				{
-					var = anjuta_token_list (var);
-				}
-			}
-		}
-		
-		args = amp_project_write_source_list (AMP_GROUP_DATA (group)->make_token, target_var, after, var);
-		g_free (target_var);
-	}
-	
-	if (args != NULL)
-	{
-		token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED, relative_name);
-		if (after)
-		{
-			anjuta_token_insert_word_after (args, prev, token);
-		}
-		else
-		{
-			anjuta_token_insert_word_before (args, prev, token);
-		}
-	
-		anjuta_token_style_format (project->am_space_list, args);
-		anjuta_token_file_update (AMP_GROUP_DATA (group)->tfile, token);
-	}
-	
-	AMP_SOURCE_DATA (source)->token = token;
-
-	return TRUE;
-}
-
 /*
  * File monitoring support --------------------------------
  * FIXME: review these
@@ -2369,7 +2280,7 @@ iproject_add_node_before (IAnjutaProject *obj, AnjutaProjectNode *parent, Anjuta
 		case ANJUTA_PROJECT_SOURCE:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
 			anjuta_project_node_insert_before (parent, sibling, node);
-			amp_source_fill_token (AMP_PROJECT (obj), node, NULL);
+			amp_source_create_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		default:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
@@ -2407,7 +2318,7 @@ iproject_add_node_after (IAnjutaProject *obj, AnjutaProjectNode *parent, AnjutaP
 		case ANJUTA_PROJECT_SOURCE:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
 			anjuta_project_node_insert_after (parent, sibling, node);
-			amp_source_fill_token (AMP_PROJECT (obj), node, NULL);
+			amp_source_create_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		default:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
@@ -2434,6 +2345,7 @@ iproject_remove_node (IAnjutaProject *obj, AnjutaProjectNode *node, GError **err
 			amp_target_delete_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		case ANJUTA_PROJECT_SOURCE:
+			amp_source_delete_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		default:
 			break;
diff --git a/plugins/am-project/am-project.h b/plugins/am-project/am-project.h
index a64828b..111031f 100644
--- a/plugins/am-project/am-project.h
+++ b/plugins/am-project/am-project.h
@@ -148,6 +148,9 @@ void amp_source_free (AnjutaAmSourceNode *node);
 gchar *amp_source_get_id (AnjutaAmSourceNode *source);
 GFile *amp_source_get_file (AnjutaAmSourceNode *source);
 
+gchar* canonicalize_automake_variable (gchar *name);
+
+
 G_END_DECLS
 
 #endif /* _AM_PROJECT_H_ */
diff --git a/plugins/am-project/am-writer.c b/plugins/am-project/am-writer.c
index 30f8542..ed56d3f 100644
--- a/plugins/am-project/am-writer.c
+++ b/plugins/am-project/am-writer.c
@@ -50,7 +50,7 @@
 /* Public functions
  *---------------------------------------------------------------------------*/
 
-AnjutaToken *
+static AnjutaToken *
 amp_project_write_config_list (AmpProject *project)
 {
 	AnjutaToken *pos;
@@ -111,7 +111,7 @@ amp_project_write_subdirs_list (AnjutaAmGroupNode *project)
 	return token;
 }
 
-AnjutaToken *
+static AnjutaToken *
 amp_project_write_config_file (AmpProject *project, AnjutaToken *list, gboolean after, AnjutaToken *sibling, const gchar *filename)
 {
 	AnjutaToken *token;
@@ -587,15 +587,17 @@ amp_target_delete_token (AmpProject  *project, AnjutaAmTargetNode *target, GErro
 	return TRUE;
 }
 
-AnjutaToken *
-amp_project_write_source_list (AnjutaToken *makefile, const gchar *name, gboolean after, AnjutaToken* sibling)
+static AnjutaToken *
+amp_project_write_source_list (AnjutaAmGroupNode *group, const gchar *name, gboolean after, AnjutaToken* sibling)
 {
 	AnjutaToken *pos;
 	AnjutaToken *token;
+	AnjutaToken *makefile;
 	static gint eol_type[] = {ANJUTA_TOKEN_EOL, 0};
 	
 	if (sibling == NULL)
 	{
+		makefile = amp_group_get_makefile_token (group);
 		pos = anjuta_token_first_item (makefile);
 		
 		/* Add at the end of the file */
@@ -621,6 +623,7 @@ amp_project_write_source_list (AnjutaToken *makefile, const gchar *name, gboolea
 			pos = anjuta_token_insert_token_list (after, pos,
 			    ANJUTA_TOKEN_EOL, "\n",
 			    NULL);
+			amp_group_update_makefile (group, pos);
 		}
 	}
 	
@@ -635,3 +638,142 @@ amp_project_write_source_list (AnjutaToken *makefile, const gchar *name, gboolea
 
 	return anjuta_token_last_item (token);
 }
+
+
+/* Source objects
+ *---------------------------------------------------------------------------*/
+
+gboolean 
+amp_source_create_token (AmpProject  *project, AnjutaAmSourceNode *source, GError **error)
+{
+	AnjutaAmGroupNode *group;
+	AnjutaAmTargetNode *target;
+	gboolean after;
+	AnjutaToken *token;
+	AnjutaToken *prev;
+	AnjutaToken *args;
+	gchar *relative_name;
+
+	/* Get parent target */
+	target = ANJUTA_AM_TARGET_NODE (anjuta_project_node_parent (ANJUTA_PROJECT_NODE (source)));
+	if ((target == NULL) || (anjuta_project_node_get_node_type (target) != ANJUTA_PROJECT_TARGET)) return FALSE;
+	
+	group = ANJUTA_AM_GROUP_NODE (anjuta_project_node_parent (ANJUTA_PROJECT_NODE (target)));
+	relative_name = g_file_get_relative_path (anjuta_project_node_get_file (group), anjuta_project_node_get_file (source));
+
+	/* Add in Makefile.am */
+	/* Find a sibling if possible */
+	if (source->base.prev != NULL)
+	{
+		prev =  ANJUTA_AM_SOURCE_NODE (source->base.prev)->token;
+		after = TRUE;
+		args = anjuta_token_list (prev);
+	}
+	else if (source->base.next != NULL)
+	{
+		prev = ANJUTA_AM_SOURCE_NODE (source->base.next)->token;
+		after = FALSE;
+		args = anjuta_token_list (prev);
+	}
+	else
+	{
+		prev = NULL;
+		args = NULL;
+	}
+	
+	if (args == NULL)
+	{
+		gchar *target_var;
+		gchar *canon_name;
+		AnjutaToken *var;
+		GList *list;
+		
+		canon_name = canonicalize_automake_variable (ANJUTA_AM_TARGET_NODE (target)->base.name);
+		target_var = g_strconcat (canon_name,  "_SOURCES", NULL);
+
+		/* Search where the target is declared */
+		var = NULL;
+		list = amp_target_get_token (target);
+		if (list != NULL)
+		{
+			var = (AnjutaToken *)list->data;
+			if (var != NULL)
+			{
+				var = anjuta_token_list (var);
+				if (var != NULL)
+				{
+					var = anjuta_token_list (var);
+				}
+			}
+		}
+		
+		args = amp_project_write_source_list (group, target_var, after, var);
+		g_free (target_var);
+	}
+	
+	if (args != NULL)
+	{
+		AnjutaTokenStyle *style;
+
+		style = anjuta_token_style_new_from_base (project->am_space_list);
+		anjuta_token_style_update (style, args);
+
+		token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED, relative_name);
+		if (after)
+		{
+			anjuta_token_insert_word_after (args, prev, token);
+		}
+		else
+		{
+			anjuta_token_insert_word_before (args, prev, token);
+		}
+
+		/* Try to use the same style than the current target list */
+		anjuta_token_style_format (style, args);
+		anjuta_token_style_free (style);
+		
+		amp_group_update_makefile (group, token);
+		
+		amp_source_add_token (source, token);
+	}
+
+	return TRUE;
+}
+
+gboolean 
+amp_source_delete_token (AmpProject  *project, AnjutaAmSourceNode *source, GError **error)
+{
+	AnjutaProjectNode *group;
+	AnjutaToken *token;
+
+	/* Get parent group */
+	group = anjuta_project_node_parent (ANJUTA_PROJECT_NODE (source));
+	if (anjuta_project_node_get_node_type (group) == ANJUTA_PROJECT_TARGET)
+	{
+		group = anjuta_project_node_parent (ANJUTA_PROJECT_NODE (group));
+	}
+	if (group == NULL) return FALSE;
+
+	token = amp_source_get_token (source);
+	if (token != NULL)
+	{
+		AnjutaToken *args;
+		AnjutaTokenStyle *style;
+
+		args = anjuta_token_list (token);
+
+		/* Try to use the same style than the current target list */
+		style = anjuta_token_style_new_from_base (project->am_space_list);
+		anjuta_token_style_update (style, args);
+		
+		anjuta_token_remove_word (token);
+		
+		anjuta_token_style_format (style, args);
+		anjuta_token_style_free (style);
+
+		amp_group_update_makefile (ANJUTA_AM_GROUP_NODE (group), args);
+	}
+
+	return TRUE;
+}
+
diff --git a/plugins/am-project/am-writer.h b/plugins/am-project/am-writer.h
index 4f51b91..4af47a7 100644
--- a/plugins/am-project/am-writer.h
+++ b/plugins/am-project/am-writer.h
@@ -29,16 +29,14 @@
 
 G_BEGIN_DECLS
 
-AnjutaToken *amp_project_write_config_list (AmpProject *project);
-AnjutaToken *amp_project_write_config_file (AmpProject *project, AnjutaToken *list, gboolean after, AnjutaToken *sibling, const gchar *filename);
-AnjutaToken *amp_project_write_source_list (AnjutaToken *makefile, const gchar *name, gboolean after, AnjutaToken* sibling);
-
 gboolean amp_group_create_token (AmpProject *project, AnjutaAmGroupNode *group, GError **error);
 gboolean amp_group_delete_token (AmpProject  *project, AnjutaAmGroupNode *target, GError **error);
 
 gboolean amp_target_create_token (AmpProject  *project, AnjutaAmTargetNode *target, GError **error);
 gboolean amp_target_delete_token (AmpProject  *project, AnjutaAmTargetNode *target, GError **error);
 
+gboolean amp_source_create_token (AmpProject  *project, AnjutaAmSourceNode *source, GError **error);
+gboolean amp_source_delete_token (AmpProject  *project, AnjutaAmSourceNode *source, GError **error);
 
 G_END_DECLS
 



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