Skip to content

Commit

Permalink
Improve anjuta_token functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sgranjoux committed Nov 17, 2009
1 parent 9feb47b commit 9cf4ef3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
6 changes: 3 additions & 3 deletions libanjuta/anjuta-token-style.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)))
{
Expand All @@ -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))
Expand Down
50 changes: 34 additions & 16 deletions libanjuta/anjuta-token.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 *
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions libanjuta/anjuta-token.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 8 additions & 28 deletions src/am-project.c
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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))
{
Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 9cf4ef3

Please sign in to comment.