From dcc0daee87364ab2e0b312829c6dd612c33e6a54 Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Fri, 18 Oct 2019 22:26:55 +0200 Subject: [PATCH] hide some completion structures --- examples/readline/main.c | 5 ++- include/ecoli_complete.h | 68 ++++++++++++++++++++++++++-------------- src/ecoli_complete.c | 53 ++++++++++++++++++++++++++++++- src/ecoli_editline.c | 2 +- src/ecoli_node_dynamic.c | 2 +- 5 files changed, 100 insertions(+), 30 deletions(-) diff --git a/examples/readline/main.c b/examples/readline/main.c index a30953d..6095808 100644 --- a/examples/readline/main.c +++ b/examples/readline/main.c @@ -70,7 +70,7 @@ static char *my_completion_entry(const char *s, int state) return NULL; item_str = ec_comp_item_get_str(item); - if (c->count_full == 1) { + if (ec_comp_count(c, EC_COMP_FULL) == 1) { /* don't add the trailing space for partial completions */ if (state == 0) { @@ -113,8 +113,7 @@ static char *get_node_help(const struct ec_comp_item *item) const char *node_desc = NULL; grp = ec_comp_item_get_grp(item); - state = grp->state; - for (state = grp->state; state != NULL; + for (state = ec_comp_group_get_state(grp); state != NULL; state = ec_parse_get_parent(state)) { node = ec_parse_get_node(state); if (node_help == NULL) diff --git a/include/ecoli_complete.h b/include/ecoli_complete.h index b28aa4a..c57eb67 100644 --- a/include/ecoli_complete.h +++ b/include/ecoli_complete.h @@ -33,29 +33,8 @@ enum ec_comp_type { /* XXX should be a define */ }; struct ec_comp_item; - -TAILQ_HEAD(ec_comp_item_list, ec_comp_item); - -struct ec_comp_group { - TAILQ_ENTRY(ec_comp_group) next; - const struct ec_node *node; - struct ec_comp_item_list items; - struct ec_parse *state; - struct ec_dict *attrs; -}; - -TAILQ_HEAD(ec_comp_group_list, ec_comp_group); - -struct ec_comp { - unsigned count; - unsigned count_full; - unsigned count_partial; - unsigned count_unknown; - struct ec_parse *cur_state; - struct ec_comp_group *cur_group; - struct ec_comp_group_list groups; - struct ec_dict *attrs; -}; +struct ec_comp_group; +struct ec_comp; /* * return a comp object filled with items @@ -101,7 +80,23 @@ void ec_comp_dump(FILE *out, int ec_comp_merge(struct ec_comp *to, struct ec_comp *from); -struct ec_parse *ec_comp_get_state(struct ec_comp *comp); +/** + * Get current completion state. + * + */ +struct ec_parse *ec_comp_get_state(const struct ec_comp *comp); + +/** + * Get current completion group. + * + */ +struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp); + +/** + * Get completion group attributes. + * + */ +struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp); /* shortcut for ec_comp_item() + ec_comp_item_add() */ int ec_comp_add_item(struct ec_comp *comp, @@ -180,6 +175,31 @@ int ec_comp_item_set_display(struct ec_comp_item *item, int ec_comp_item_set_completion(struct ec_comp_item *item, const char *completion); +/** + * Get the completion group node. + * + * + */ +const struct ec_node * +ec_comp_group_get_node(const struct ec_comp_group *grp); + +/** + * Get the completion group parsed state. + * + * + */ +const struct ec_parse * +ec_comp_group_get_state(const struct ec_comp_group *grp); + +/** + * Get the completion group attributes. + * + * + */ +const struct ec_dict * +ec_comp_group_get_attrs(const struct ec_comp_group *grp); + + /** * * diff --git a/src/ecoli_complete.c b/src/ecoli_complete.c index e2f150a..22459bb 100644 --- a/src/ecoli_complete.c +++ b/src/ecoli_complete.c @@ -34,6 +34,29 @@ struct ec_comp_item { struct ec_dict *attrs; }; +TAILQ_HEAD(ec_comp_item_list, ec_comp_item); + +struct ec_comp_group { + TAILQ_ENTRY(ec_comp_group) next; + const struct ec_node *node; + struct ec_comp_item_list items; + struct ec_parse *state; + struct ec_dict *attrs; +}; + +TAILQ_HEAD(ec_comp_group_list, ec_comp_group); + +struct ec_comp { + unsigned count; + unsigned count_full; + unsigned count_partial; + unsigned count_unknown; + struct ec_parse *cur_state; + struct ec_comp_group *cur_group; + struct ec_comp_group_list groups; + struct ec_dict *attrs; +}; + struct ec_comp *ec_comp(struct ec_parse *state) { struct ec_comp *comp = NULL; @@ -60,11 +83,21 @@ struct ec_comp *ec_comp(struct ec_parse *state) return NULL; } -struct ec_parse *ec_comp_get_state(struct ec_comp *comp) +struct ec_parse *ec_comp_get_state(const struct ec_comp *comp) { return comp->cur_state; } +struct ec_comp_group *ec_comp_get_group(const struct ec_comp *comp) +{ + return comp->cur_group; +} + +struct ec_dict *ec_comp_get_attrs(const struct ec_comp *comp) +{ + return comp->attrs; +} + int ec_node_complete_child(const struct ec_node *node, struct ec_comp *comp, @@ -485,6 +518,24 @@ static void ec_comp_group_free(struct ec_comp_group *grp) ec_free(grp); } +const struct ec_node * +ec_comp_group_get_node(const struct ec_comp_group *grp) +{ + return grp->node; +} + +const struct ec_parse * +ec_comp_group_get_state(const struct ec_comp_group *grp) +{ + return grp->state; +} + +const struct ec_dict * +ec_comp_group_get_attrs(const struct ec_comp_group *grp) +{ + return grp->attrs; +} + void ec_comp_free(struct ec_comp *comp) { struct ec_comp_group *grp; diff --git a/src/ecoli_editline.c b/src/ecoli_editline.c index ed8ea21..2610bf5 100644 --- a/src/ecoli_editline.c +++ b/src/ecoli_editline.c @@ -393,7 +393,7 @@ static int get_node_help(const struct ec_comp_item *item, grp = ec_comp_item_get_grp(item); - for (state = grp->state; state != NULL; + for (state = ec_comp_group_get_state(grp); state != NULL; state = ec_parse_get_parent(state)) { node = ec_parse_get_node(state); if (node_help == NULL) diff --git a/src/ecoli_node_dynamic.c b/src/ecoli_node_dynamic.c index 2b79f7a..e511f68 100644 --- a/src/ecoli_node_dynamic.c +++ b/src/ecoli_node_dynamic.c @@ -80,7 +80,7 @@ ec_node_dynamic_complete(const struct ec_node *node, /* add the node pointer in the attributes, so it will be freed * when parse is freed */ snprintf(key, sizeof(key), "_dyn_%p", child); - ret = ec_dict_set(comp->attrs, key, child, + ret = ec_dict_set(ec_comp_get_attrs(comp), key, child, (void *)node_free); if (ret < 0) { child = NULL; /* already freed */ -- 2.39.5