From 9cf4ef360857c4238768e86fe15e81e89b9f78af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Granjoux?= Date: Tue, 17 Nov 2009 23:03:35 +0100 Subject: [PATCH] Improve anjuta_token functions --- libanjuta/anjuta-token-style.c | 6 ++-- libanjuta/anjuta-token.c | 50 +++++++++++++++++++++++----------- libanjuta/anjuta-token.h | 1 + src/am-project.c | 36 ++++++------------------ 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/libanjuta/anjuta-token-style.c b/libanjuta/anjuta-token-style.c index 41a466b..2e7735f 100644 --- a/libanjuta/anjuta-token-style.c +++ b/libanjuta/anjuta-token-style.c @@ -183,7 +183,7 @@ anjuta_token_style_update (AnjutaTokenStyle *style, AnjutaToken *list) line_width += len; } - for (token = anjuta_token_next_child (list); token != NULL; token = next_token) + for (token = anjuta_token_first_group (list); token != NULL; token = next_token) { gchar *value = NULL; const gchar *eol; @@ -314,7 +314,7 @@ anjuta_token_style_format (AnjutaTokenStyle *style, AnjutaToken *list) if (style->sep == NULL) { - for (arg = anjuta_token_next_child (list); arg != NULL; arg = anjuta_token_next_sibling (arg)) + for (arg = anjuta_token_first_child (list); arg != NULL; arg = anjuta_token_next_sibling (arg)) { if ((anjuta_token_get_type (arg) == ANJUTA_TOKEN_SPACE) && (anjuta_token_get_flags (arg) & (ANJUTA_TOKEN_ADDED))) { @@ -325,7 +325,7 @@ anjuta_token_style_format (AnjutaTokenStyle *style, AnjutaToken *list) } else { - AnjutaToken *bol = anjuta_token_next_child (list); + AnjutaToken *bol = anjuta_token_first_child (list); gboolean modified = FALSE; for (arg = bol; arg != NULL; arg = anjuta_token_next_sibling (arg)) diff --git a/libanjuta/anjuta-token.c b/libanjuta/anjuta-token.c index bd4f681..6aa03db 100644 --- a/libanjuta/anjuta-token.c +++ b/libanjuta/anjuta-token.c @@ -216,12 +216,6 @@ anjuta_token_next_sibling (AnjutaToken *token) return token ? token->next : NULL; } -AnjutaToken * -anjuta_token_next_child (AnjutaToken *token) -{ - return token ? token->children : NULL; -} - AnjutaToken * anjuta_token_previous_sibling (AnjutaToken *token) { @@ -234,6 +228,10 @@ anjuta_token_last (AnjutaToken *token) AnjutaToken *last; for (last = token; last->last != NULL; last = last->last); + if (last->children != NULL) + { + for (last = last->children; last->next != NULL; last = last->next); + } return last; } @@ -243,7 +241,7 @@ anjuta_token_last_child (AnjutaToken *token) { AnjutaToken *last = NULL; - for (token = anjuta_token_next_child (token); token != NULL; token = anjuta_token_next_sibling (token)) + for (token = anjuta_token_first_child (token); token != NULL; token = anjuta_token_next_sibling (token)) { last = token; } @@ -254,9 +252,11 @@ anjuta_token_last_child (AnjutaToken *token) AnjutaToken * anjuta_token_first_group (AnjutaToken *list) { - if ((list == NULL) || (list->last == NULL)) return NULL; + if (list == NULL) return NULL; + if (list->children != NULL) return list->children; + if (list->last != NULL) return list->next; - return list->next; + return NULL; } AnjutaToken * @@ -272,6 +272,28 @@ anjuta_token_next_group (AnjutaToken *group) return last->next; } +AnjutaToken* +anjuta_token_first_child (AnjutaToken *parent) +{ + AnjutaToken *next = anjuta_token_next (parent); + + return ((next != NULL) && (next->parent == parent)) ? next : NULL; +} + +AnjutaToken * +anjuta_token_next_child (AnjutaToken *child) +{ + if (child == NULL) return NULL; + do + { + if ((child->parent != NULL) && (child->parent->last == child)) return NULL; + if ((child->next != NULL) && (child->next->parent == child->parent)) return child->next; + child = child->last; + } while (child != NULL); + + return NULL; +} + AnjutaToken * anjuta_token_first_item (AnjutaToken *list) { @@ -599,11 +621,7 @@ anjuta_token_evaluate_group (AnjutaToken *token, GString *value, gboolean raw) { anjuta_token_evaluate_token (child, value, raw); - if (child == last) - { - if (child->children) anjuta_token_evaluate_child (child->children, value, FALSE); - break; - } + if (child == last) break; } } @@ -773,7 +791,7 @@ anjuta_token_copy (AnjutaToken *token) AnjutaToken *last; last = NULL; - for (child = anjuta_token_next_child (token); child != NULL; child = anjuta_token_next_sibling (child)) + for (child = anjuta_token_first_child (token); child != NULL; child = anjuta_token_next_sibling (child)) { AnjutaToken *new_child = anjuta_token_copy (child); last = last == NULL ? anjuta_token_insert_child (copy, new_child) : anjuta_token_insert_after (last, new_child); @@ -942,7 +960,7 @@ AnjutaToken *anjuta_token_ungroup (AnjutaToken *token) AnjutaToken *last = token; AnjutaToken *child; - for (child = anjuta_token_next_child (token); child != NULL; child = anjuta_token_next_child (token)) + for (child = anjuta_token_first_child (token); child != NULL; child = anjuta_token_first_child (token)) { anjuta_token_unlink (child); last = anjuta_token_insert_after (last, child); diff --git a/libanjuta/anjuta-token.h b/libanjuta/anjuta-token.h index 443311a..2b11ee8 100644 --- a/libanjuta/anjuta-token.h +++ b/libanjuta/anjuta-token.h @@ -139,6 +139,7 @@ void anjuta_token_foreach (AnjutaToken *token, AnjutaTokenForeachFunc, gpointer AnjutaToken *anjuta_token_first_group (AnjutaToken *list); AnjutaToken *anjuta_token_next_group (AnjutaToken *item); +AnjutaToken *anjuta_token_first_child (AnjutaToken *parent); AnjutaToken *anjuta_token_first_item (AnjutaToken *list); AnjutaToken *anjuta_token_nth_item (AnjutaToken *list, guint n); AnjutaToken *anjuta_token_next_item (AnjutaToken *item); diff --git a/src/am-project.c b/src/am-project.c index 8f5dfa9..683aa86 100644 --- a/src/am-project.c +++ b/src/am-project.c @@ -1034,18 +1034,8 @@ amp_project_load_module (AmpProject *project, AnjutaToken *module) pack = NULL; compare = NULL; - for (item = anjuta_token_next_child (list); item != NULL; item = anjuta_token_next_item (item)) + for (item = anjuta_token_first_item (list); item != NULL; item = anjuta_token_next_item (item)) { - switch (anjuta_token_get_type (item)) - { - case ANJUTA_TOKEN_NEXT: - case ANJUTA_TOKEN_START: - case ANJUTA_TOKEN_LAST: - continue; - default: - break; - } - value = anjuta_token_evaluate (item); if (value == NULL) continue; /* Empty value, a comment of a quote by example */ if (*value == '\0') @@ -1103,21 +1093,11 @@ amp_project_load_config (AmpProject *project, AnjutaToken *arg_list) list = amp_ac_scanner_parse_token (scanner, arg, AC_SPACE_LIST_STATE, NULL); amp_ac_scanner_free (scanner); - for (item = anjuta_token_next_child (list); item != NULL; item = anjuta_token_next_item (item)) + for (item = anjuta_token_first_item (list); item != NULL; item = anjuta_token_next_item (item)) { gchar *value; AmpConfigFile *cfg; - switch (anjuta_token_get_type (item)) - { - case ANJUTA_TOKEN_NEXT: - case ANJUTA_TOKEN_START: - case ANJUTA_TOKEN_LAST: - continue; - default: - break; - } - value = anjuta_token_evaluate (item); if (value == NULL) continue; @@ -1783,7 +1763,7 @@ amp_project_add_group (AmpProject *project, AnjutaToken *list; /* Skip comment and one space at the beginning */ - for (prev_token = anjuta_token_next_child (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); prev_token != NULL; prev_token = anjuta_token_next_sibling (prev_token)) + for (prev_token = anjuta_token_first_child (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); prev_token != NULL; prev_token = anjuta_token_next_sibling (prev_token)) { switch (anjuta_token_get_type (prev_token)) { @@ -1805,7 +1785,7 @@ amp_project_add_group (AmpProject *project, if (prev_token == NULL) { - prev_token = anjuta_token_next_child (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); + prev_token = anjuta_token_first_child (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); /*if (prev_token) { prev_token = anjuta_token_file_last (AMP_GROUP_DATA (parent)->tfile); @@ -1968,7 +1948,7 @@ amp_project_add_target (AmpProject *project, if (last == NULL) { - prev_token = anjuta_token_next_child (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); + prev_token = anjuta_token_first_group (anjuta_token_file_get_content (AMP_GROUP_DATA (parent)->tfile)); if (prev_token != NULL) { /* Add at the end of the file */ @@ -2005,13 +1985,13 @@ amp_project_add_target (AmpProject *project, { for (token = (AnjutaToken *)last->data; anjuta_token_get_type (token) != ANJUTA_TOKEN_LIST; token = anjuta_token_next_sibling (token)); - if (anjuta_token_next_child (token) == NULL) + if (anjuta_token_next_group(token) == NULL) { - token = anjuta_token_insert_child (token, anjuta_token_new_static (ANJUTA_TOKEN_SPACE | ANJUTA_TOKEN_ADDED, " ")); + token = anjuta_token_merge (token, anjuta_token_new_static (ANJUTA_TOKEN_SPACE | ANJUTA_TOKEN_ADDED, " ")); } else { - token = anjuta_token_next_child (token); + token = anjuta_token_next_item (token); } for (; anjuta_token_next_sibling (token) != NULL; token = anjuta_token_next_sibling (token));