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



commit 9130344c48f33a0ee6f5b671271dbeee028b1580
Author: Sébastien Granjoux <seb sfo free fr>
Date:   Wed Oct 6 22:31:04 2010 +0200

    Move creating and destruction of group token in am_writer.c

 plugins/am-project/am-node.c    |   13 +++
 plugins/am-project/am-node.h    |    1 +
 plugins/am-project/am-project.c |  202 ++--------------------------------
 plugins/am-project/am-project.h |    2 +
 plugins/am-project/am-writer.c  |  228 +++++++++++++++++++++++++++++++++++++++
 plugins/am-project/am-writer.h  |    3 +
 6 files changed, 257 insertions(+), 192 deletions(-)
---
diff --git a/plugins/am-project/am-node.c b/plugins/am-project/am-node.c
index 39f9a14..40f6d31 100644
--- a/plugins/am-project/am-node.c
+++ b/plugins/am-project/am-node.c
@@ -458,6 +458,19 @@ amp_group_update_makefile (AnjutaAmGroupNode *group, AnjutaToken *token)
 	return anjuta_token_file_update (group->tfile, token);
 }
 
+gchar *
+amp_group_get_makefile_name (AnjutaAmGroupNode *group)
+{
+	gchar *basename = NULL;
+	
+	if (group->makefile != NULL) 
+	{
+		basename = g_file_get_basename (group->makefile);
+	}
+
+	return basename;
+}
+
 AnjutaAmGroupNode*
 amp_group_new (GFile *file, gboolean dist_only, GError **error)
 {
diff --git a/plugins/am-project/am-node.h b/plugins/am-project/am-node.h
index a2dca7c..038f11c 100644
--- a/plugins/am-project/am-node.h
+++ b/plugins/am-project/am-node.h
@@ -61,6 +61,7 @@ AnjutaToken* amp_group_get_first_token (AnjutaAmGroupNode *group, AmpGroupTokenC
 void amp_group_set_dist_only (AnjutaAmGroupNode *group, gboolean dist_only);
 AnjutaTokenFile* amp_group_set_makefile (AnjutaAmGroupNode *group, GFile *makefile, GObject* project);
 AnjutaToken* amp_group_get_makefile_token (AnjutaAmGroupNode *group);
+gchar *amp_group_get_makefile_name (AnjutaAmGroupNode *group);
 gboolean amp_group_update_makefile (AnjutaAmGroupNode *group, AnjutaToken *token);
 AnjutaAmGroupNode* amp_group_new (GFile *file, gboolean dist_only, GError **error);
 void amp_group_free (AnjutaAmGroupNode *node);
diff --git a/plugins/am-project/am-project.c b/plugins/am-project/am-project.c
index 20cc0f7..0534667 100644
--- a/plugins/am-project/am-project.c
+++ b/plugins/am-project/am-project.c
@@ -545,196 +545,6 @@ remove_config_file (gpointer data, GObject *object, gboolean is_last_ref)
 	}
 }
 
-static gboolean 
-amp_group_fill_token (AmpProject  *project, AnjutaAmGroupNode *group, GError **error)
-{
-	AnjutaAmGroupNode *last;
-	GFile *directory;
-	GFile *makefile;
-	AnjutaToken *list;
-	gchar *basename;
-	gchar *uri;
-	AnjutaTokenFile* tfile;
-	AnjutaAmTargetNode *sibling;
-	AnjutaAmGroupNode *parent;
-	gboolean after;
-	gchar *name;
-	
-	/* Check that the new group doesn't already exist */
-	/*directory = g_file_get_child (AMP_GROUP_DATA (parent)->base.file, name);
-	uri = g_file_get_uri (directory);
-	if (g_hash_table_lookup (project->groups, uri) != NULL)
-	{
-		g_free (uri);
-		error_set (error, IANJUTA_PROJECT_ERROR_DOESNT_EXIST,
-			_("Group already exists"));
-		return NULL;
-	}*/
-
-	/* Get parent target */
-	parent = ANJUTA_AM_GROUP_NODE (anjuta_project_node_parent(ANJUTA_PROJECT_NODE (group)));
-	name = anjuta_project_node_get_name (ANJUTA_PROJECT_NODE (group));
-	directory = g_file_get_child (anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (parent)), name);
-
-	/* Find a sibling if possible */
-	if (anjuta_project_node_prev_sibling (ANJUTA_PROJECT_NODE (group)) != NULL)
-	{
-		sibling = anjuta_project_node_prev_sibling (ANJUTA_PROJECT_NODE (group));
-		after = TRUE;
-	}
-	else if (anjuta_project_node_next_sibling (ANJUTA_PROJECT_NODE (group)) != NULL)
-	{
-		sibling = anjuta_project_node_next_sibling (ANJUTA_PROJECT_NODE (group));
-		after = FALSE;
-	}
-	else
-	{
-		sibling = NULL;
-		after = TRUE;
-	}
-	
-	/* Create directory */
-	g_file_make_directory (directory, NULL, NULL);
-
-	/* Create Makefile.am */
-	basename = AMP_GROUP_DATA (parent)->makefile != NULL ? g_file_get_basename (AMP_GROUP_DATA (parent)->makefile) : NULL;
-	if (basename != NULL)
-	{
-		makefile = g_file_get_child (directory, basename);
-		g_free (basename);
-	}
-	else
-	{
-		makefile = g_file_get_child (directory, "Makefile.am");
-	}
-	g_file_replace_contents (makefile, "", 0, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, NULL);
-	tfile = amp_group_set_makefile (group, makefile, project);
-	g_hash_table_insert (project->files, makefile, tfile);
-	g_object_add_toggle_ref (G_OBJECT (tfile), remove_config_file, project);
-
-	if (sibling == NULL)
-	{
-		/* Find a sibling before */
-		for (last = anjuta_project_node_prev_sibling (group); (last != NULL) && (anjuta_project_node_get_node_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_prev_sibling (last));
-		if (last != NULL)
-		{
-			sibling = last;
-			after = TRUE;
-		}
-		else
-		{
-			/* Find a sibling after */
-			for (last = anjuta_project_node_next_sibling (group); (last != NULL) && (anjuta_project_node_get_node_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_next_sibling (last));
-			if (last != NULL)
-			{
-				sibling = last;
-				after = FALSE;
-			}
-		}
-	}
-	
-	/* Add in configure */
-	list = NULL;
-	if (sibling) list = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
-	if (list == NULL) list= amp_group_get_first_token (parent, AM_GROUP_TOKEN_CONFIGURE);
-	if (list != NULL) list = anjuta_token_list (list);
-	if (list == NULL)
-	{
-		list = amp_project_write_config_list (project);
-		list = anjuta_token_next (list);
-	}
-	if (list != NULL)
-	{
-		gchar *relative_make;
-		gchar *ext;
-		AnjutaToken *prev = NULL;
-
-		if (sibling)
-		{
-			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
-			/*if ((prev != NULL) && after)
-			{
-				prev = anjuta_token_next_word (prev);
-			}*/
-		}
-		//prev_token = (AnjutaToken *)token_list->data;
-
-		relative_make = g_file_get_relative_path (anjuta_project_node_get_file (project->root), makefile);
-		ext = relative_make + strlen (relative_make) - 3;
-		if (strcmp (ext, ".am") == 0)
-		{
-			*ext = '\0';
-		}
-		//token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED,  relative_make);
-		amp_project_write_config_file (project, list, after, prev, relative_make);
-		g_free (relative_make);
-		
-		//style = anjuta_token_style_new (NULL," ","\n",NULL,0);
-		//anjuta_token_add_word (prev_token, token, style);
-		//anjuta_token_style_free (style);
-	}
-
-	/* Add in Makefile.am */
-	if (sibling == NULL)
-	{
-		AnjutaToken *pos;
-		static gint eol_type[] = {ANJUTA_TOKEN_EOL, ANJUTA_TOKEN_SPACE, ANJUTA_TOKEN_COMMENT, 0};
-	
-		pos = anjuta_token_find_type (AMP_GROUP_DATA (parent)->make_token, ANJUTA_TOKEN_SEARCH_NOT, eol_type);
-		if (pos == NULL)
-		{
-			pos = anjuta_token_prepend_child (AMP_GROUP_DATA (parent)->make_token, anjuta_token_new_static (ANJUTA_TOKEN_SPACE, "\n"));
-		}
-
-		list = anjuta_token_insert_token_list (FALSE, pos,
-		    	ANJUTA_TOKEN_SPACE, "\n");
-		list = anjuta_token_insert_token_list (FALSE, list,
-	    		AM_TOKEN_SUBDIRS, "SUBDIRS",
-		    	ANJUTA_TOKEN_SPACE, " ",
-		    	ANJUTA_TOKEN_OPERATOR, "=",
-	    		ANJUTA_TOKEN_LIST, NULL,
-	    		ANJUTA_TOKEN_LAST, NULL,
-	    		NULL);
-		list = anjuta_token_next (anjuta_token_next ( anjuta_token_next (list)));
-	}
-	else
-	{
-		AnjutaToken *prev;
-		
-		prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
-		list = anjuta_token_list (prev);
-	}
-
-	if (list != NULL)
-	{
-		AnjutaToken *token;
-		AnjutaToken *prev;
-
-		if (sibling)
-		{
-			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
-		}
-		
-		token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED, name);
-		if (after)
-		{
-			anjuta_token_insert_word_after (list, prev, token);
-		}
-		else
-		{
-			anjuta_token_insert_word_before (list, prev, token);
-		}
-	
-		anjuta_token_style_format (project->am_space_list, list);
-		anjuta_token_file_update (AMP_GROUP_DATA (parent)->tfile, token);
-		
-		amp_group_add_token (group, token, AM_GROUP_TOKEN_SUBDIRS);
-	}
-	g_free (name);
-
-	return TRUE;
-}
-
 /* Target objects
  *---------------------------------------------------------------------------*/
 
@@ -2429,6 +2239,13 @@ amp_project_get_property_list (AmpProject *project)
 	return project->properties;
 }
 
+void
+amp_project_add_file (AmpProject *project, GFile *file, AnjutaTokenFile* token)
+{
+	g_hash_table_insert (project->files, file, token);
+	g_object_add_toggle_ref (G_OBJECT (token), remove_config_file, project);
+}
+
 #if 0
 gchar *
 amp_project_get_property (AmpProject *project, AmpPropertyType type)
@@ -2542,7 +2359,7 @@ iproject_add_node_before (IAnjutaProject *obj, AnjutaProjectNode *parent, Anjuta
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
 			if (directory != NULL) g_object_unref (directory);
 			anjuta_project_node_insert_before (parent, sibling, node);
-			amp_group_fill_token (AMP_PROJECT (obj), node, NULL);
+			amp_group_create_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		case ANJUTA_PROJECT_TARGET:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
@@ -2580,7 +2397,7 @@ iproject_add_node_after (IAnjutaProject *obj, AnjutaProjectNode *parent, AnjutaP
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
 			if (directory != NULL) g_object_unref (directory);
 			anjuta_project_node_insert_after (parent, sibling, node);
-			amp_group_fill_token (AMP_PROJECT (obj), node, NULL);
+			amp_group_create_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		case ANJUTA_PROJECT_TARGET:
 			node = project_node_new (AMP_PROJECT (obj), type, file, name, err);
@@ -2611,6 +2428,7 @@ iproject_remove_node (IAnjutaProject *obj, AnjutaProjectNode *node, GError **err
 	switch (type & ANJUTA_PROJECT_TYPE_MASK)
 	{
 		case ANJUTA_PROJECT_GROUP:
+			amp_group_delete_token (AMP_PROJECT (obj), node, NULL);
 			break;
 		case ANJUTA_PROJECT_TARGET:
 			amp_target_delete_token (AMP_PROJECT (obj), node, NULL);
diff --git a/plugins/am-project/am-project.h b/plugins/am-project/am-project.h
index 046e664..a64828b 100644
--- a/plugins/am-project/am-project.h
+++ b/plugins/am-project/am-project.h
@@ -100,6 +100,8 @@ gboolean amp_project_dump (AmpProject *project, AnjutaProjectNode *node);
 gchar * amp_project_get_uri (AmpProject *project);
 GFile* amp_project_get_file (AmpProject *project);
 
+void amp_project_add_file (AmpProject *project, GFile *file, AnjutaTokenFile* token);
+
 AnjutaAmGroupNode* amp_project_add_group (AmpProject  *project, AnjutaAmGroupNode *parent, const gchar *name, GError **error);
 AnjutaAmGroupNode* amp_project_add_sibling_group (AmpProject  *project, AnjutaAmGroupNode *parent, const gchar *name, gboolean after, AnjutaAmGroupNode *sibling, GError **error);
 void amp_project_remove_group (AmpProject  *project, AnjutaAmGroupNode *group, GError **error);
diff --git a/plugins/am-project/am-writer.c b/plugins/am-project/am-writer.c
index 368ce9d..30f8542 100644
--- a/plugins/am-project/am-writer.c
+++ b/plugins/am-project/am-writer.c
@@ -30,6 +30,7 @@
 
 #include "am-project-private.h"
 #include "am-node.h"
+#include "am-scanner.h"
 
 #include <libanjuta/anjuta-debug.h>
 #include <libanjuta/anjuta-utils.h>
@@ -139,6 +140,233 @@ amp_project_write_config_file (AmpProject *project, AnjutaToken *list, gboolean
 	return token;
 }
 
+
+/* Target objects
+ *---------------------------------------------------------------------------*/
+
+gboolean 
+amp_group_create_token (AmpProject  *project, AnjutaAmGroupNode *group, GError **error)
+{
+	AnjutaAmGroupNode *last;
+	GFile *directory;
+	GFile *makefile;
+	AnjutaToken *list;
+	gchar *basename;
+	AnjutaTokenFile* tfile;
+	AnjutaAmGroupNode *sibling;
+	AnjutaAmGroupNode *parent;
+	gboolean after;
+	gchar *name;
+	
+	/* Get parent target */
+	parent = ANJUTA_AM_GROUP_NODE (anjuta_project_node_parent(ANJUTA_PROJECT_NODE (group)));
+	name = anjuta_project_node_get_name (ANJUTA_PROJECT_NODE (group));
+	directory = g_file_get_child (anjuta_project_node_get_file (ANJUTA_PROJECT_NODE (parent)), name);
+
+	/* Find a sibling if possible */
+	if (anjuta_project_node_prev_sibling (ANJUTA_PROJECT_NODE (group)) != NULL)
+	{
+		sibling = ANJUTA_AM_GROUP_NODE (anjuta_project_node_prev_sibling (ANJUTA_PROJECT_NODE (group)));
+		after = TRUE;
+	}
+	else if (anjuta_project_node_next_sibling (ANJUTA_PROJECT_NODE (group)) != NULL)
+	{
+		sibling = ANJUTA_AM_GROUP_NODE (anjuta_project_node_next_sibling (ANJUTA_PROJECT_NODE (group)));
+		after = FALSE;
+	}
+	else
+	{
+		sibling = NULL;
+		after = TRUE;
+	}
+	
+	/* Create directory */
+	g_file_make_directory (directory, NULL, NULL);
+
+	/* Create Makefile.am */
+	basename = amp_group_get_makefile_name  (parent);
+	if (basename != NULL)
+	{
+		makefile = g_file_get_child (directory, basename);
+		g_free (basename);
+	}
+	else
+	{
+		makefile = g_file_get_child (directory, "Makefile.am");
+	}
+	g_file_replace_contents (makefile, "", 0, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, NULL);
+	tfile = amp_group_set_makefile (group, makefile, G_OBJECT (project));
+	amp_project_add_file (project, makefile, tfile);
+
+	if (sibling == NULL)
+	{
+		/* Find a sibling before */
+		for (last = anjuta_project_node_prev_sibling (group); (last != NULL) && (anjuta_project_node_get_node_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_prev_sibling (last));
+		if (last != NULL)
+		{
+			sibling = last;
+			after = TRUE;
+		}
+		else
+		{
+			/* Find a sibling after */
+			for (last = anjuta_project_node_next_sibling (group); (last != NULL) && (anjuta_project_node_get_node_type (last) != ANJUTA_PROJECT_GROUP); last = anjuta_project_node_next_sibling (last));
+			if (last != NULL)
+			{
+				sibling = last;
+				after = FALSE;
+			}
+		}
+	}
+	
+	/* Add in configure */
+	list = NULL;
+	if (sibling) list = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
+	if (list == NULL) list= amp_group_get_first_token (parent, AM_GROUP_TOKEN_CONFIGURE);
+	if (list != NULL) list = anjuta_token_list (list);
+	if (list == NULL)
+	{
+		list = amp_project_write_config_list (project);
+		list = anjuta_token_next (list);
+	}
+	if (list != NULL)
+	{
+		gchar *relative_make;
+		gchar *ext;
+		AnjutaToken *prev = NULL;
+
+		if (sibling)
+		{
+			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_CONFIGURE);
+			/*if ((prev != NULL) && after)
+			{
+				prev = anjuta_token_next_word (prev);
+			}*/
+		}
+		//prev_token = (AnjutaToken *)token_list->data;
+
+		relative_make = g_file_get_relative_path (anjuta_project_node_get_file (project->root), makefile);
+		ext = relative_make + strlen (relative_make) - 3;
+		if (strcmp (ext, ".am") == 0)
+		{
+			*ext = '\0';
+		}
+		//token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED,  relative_make);
+		amp_project_write_config_file (project, list, after, prev, relative_make);
+		g_free (relative_make);
+		
+		//style = anjuta_token_style_new (NULL," ","\n",NULL,0);
+		//anjuta_token_add_word (prev_token, token, style);
+		//anjuta_token_style_free (style);
+	}
+
+	/* Add in Makefile.am */
+	if (sibling == NULL)
+	{
+		AnjutaToken *pos;
+		AnjutaToken *makefile;
+		static gint eol_type[] = {ANJUTA_TOKEN_EOL, ANJUTA_TOKEN_SPACE, ANJUTA_TOKEN_COMMENT, 0};
+	
+		makefile = amp_group_get_makefile_token (group);
+		pos = anjuta_token_find_type (makefile, ANJUTA_TOKEN_SEARCH_NOT, eol_type);
+		if (pos == NULL)
+		{
+			pos = anjuta_token_prepend_child (makefile, anjuta_token_new_static (ANJUTA_TOKEN_SPACE, "\n"));
+		}
+
+		list = anjuta_token_insert_token_list (FALSE, pos,
+		    	ANJUTA_TOKEN_SPACE, "\n");
+		list = anjuta_token_insert_token_list (FALSE, list,
+	    		AM_TOKEN_SUBDIRS, "SUBDIRS",
+		    	ANJUTA_TOKEN_SPACE, " ",
+		    	ANJUTA_TOKEN_OPERATOR, "=",
+	    		ANJUTA_TOKEN_LIST, NULL,
+	    		ANJUTA_TOKEN_LAST, NULL,
+	    		NULL);
+		list = anjuta_token_next (anjuta_token_next ( anjuta_token_next (list)));
+	}
+	else
+	{
+		AnjutaToken *prev;
+		
+		prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
+		list = anjuta_token_list (prev);
+	}
+
+	if (list != NULL)
+	{
+		AnjutaToken *token;
+		AnjutaToken *prev;
+		AnjutaTokenStyle *style;
+
+		style = anjuta_token_style_new_from_base (project->am_space_list);
+		anjuta_token_style_update (style, list);
+
+		if (sibling)
+		{
+			prev = amp_group_get_first_token (sibling, AM_GROUP_TOKEN_SUBDIRS);
+		}
+		
+		token = anjuta_token_new_string (ANJUTA_TOKEN_NAME | ANJUTA_TOKEN_ADDED, name);
+		if (after)
+		{
+			anjuta_token_insert_word_after (list, prev, token);
+		}
+		else
+		{
+			anjuta_token_insert_word_before (list, prev, token);
+		}
+
+		/* Try to use the same style than the current group list */
+		anjuta_token_style_format (style, list);
+		anjuta_token_style_free (style);
+		
+		amp_group_update_makefile (parent, token);
+		
+		amp_group_add_token (group, token, AM_GROUP_TOKEN_SUBDIRS);
+	}
+	g_free (name);
+
+	return TRUE;
+}
+
+gboolean 
+amp_group_delete_token (AmpProject  *project, AnjutaAmGroupNode *group, GError **error)
+{
+	GList *item;
+	AnjutaProjectNode *parent;
+
+	/* Get parent target */
+	parent =  anjuta_project_node_parent (ANJUTA_PROJECT_NODE (group));
+	if (anjuta_project_node_get_node_type  (parent) != ANJUTA_PROJECT_GROUP) return FALSE;
+
+	for (item = amp_group_get_token (group, AM_GROUP_TOKEN_SUBDIRS); item != NULL; item = g_list_next (item))
+	{
+		AnjutaToken *token = (AnjutaToken *)item->data;
+		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 (parent), args);
+	}
+
+	return TRUE;
+}
+
+
+/* Target objects
+ *---------------------------------------------------------------------------*/
+
 static AnjutaToken *
 amp_project_write_target (AnjutaAmGroupNode *group, gint type, const gchar *name, gboolean after, AnjutaToken* sibling)
 {
diff --git a/plugins/am-project/am-writer.h b/plugins/am-project/am-writer.h
index 2fb7e1e..4f51b91 100644
--- a/plugins/am-project/am-writer.h
+++ b/plugins/am-project/am-writer.h
@@ -33,6 +33,9 @@ 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);
 



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