Skip to content

Commit

Permalink
:squash: remove map_augroup_name_to_id
Browse files Browse the repository at this point in the history
  • Loading branch information
altermo committed Apr 21, 2024
1 parent 4d1efc3 commit bf397ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 40 deletions.
51 changes: 12 additions & 39 deletions src/nvim/autocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,11 @@ static bool autocmd_include_groups = false;
static char *old_termresponse = NULL;

// Map of autocmd group names and ids.
// name -> ID
// ID -> name
static Map(String, int) map_augroup_name_to_id = MAP_INIT;
static Map(int, String) map_augroup_id_to_name = MAP_INIT;

static void augroup_map_del(int id, const char *name)
{
if (name != NULL) {
String key;
map_del(String, int)(&map_augroup_name_to_id, cstr_as_string(name), &key);
api_free_string(key);
}
if (id > 0) {
String mapped = map_del(int, String)(&map_augroup_id_to_name, id, NULL);
api_free_string(mapped);
}
}

static inline const char *get_deleted_augroup(void) FUNC_ATTR_ALWAYS_INLINE
Expand All @@ -135,6 +124,11 @@ static inline const char *get_deleted_augroup(void) FUNC_ATTR_ALWAYS_INLINE
return deleted_augroup;
}

static int get_deleted_augroup_id(void)
{
return augroup_add(get_deleted_augroup());
}

static void au_show_for_all_events(int group, const char *pat)
{
FOR_ALL_AUEVENTS(event) {
Expand Down Expand Up @@ -394,23 +388,13 @@ int augroup_add(const char *name)
{
assert(STRICMP(name, "end") != 0);

int existing_id = augroup_find(name);
if (existing_id > 0) {
assert(existing_id != AUGROUP_DELETED);
return existing_id;
}
int id = nvim_create_namespace(cstr_as_string(name));

if (existing_id == AUGROUP_DELETED) {
augroup_map_del(existing_id, name);
if (map_get(int, String(&map_augroup_id_to_name, id)).data == NULL) {
map_put(int, String)(&map_augroup_id_to_name, id, cstr_to_string(name));
}

int next_id = next_namespace_id++;
String name_key = cstr_to_string(name);
String name_val = cstr_to_string(name);
map_put(String, int)(&map_augroup_name_to_id, name_key, next_id);
map_put(int, String)(&map_augroup_id_to_name, next_id, name_val);

return next_id;
return id;
}

/// Delete the augroup that matches name.
Expand Down Expand Up @@ -442,7 +426,7 @@ void augroup_del(char *name, bool stupid_legacy_mode)
AutoPat *const ap = kv_A(*acs, i).pat;
if (ap != NULL && ap->group == group) {
give_warning(_("W19: Deleting augroup that is still in use"), true);
map_put(String, int)(&map_augroup_name_to_id, cstr_as_string(name), AUGROUP_DELETED);
ap->group = get_deleted_augroup_id();
augroup_map_del(ap->group, NULL);
return;
}
Expand Down Expand Up @@ -473,11 +457,7 @@ void augroup_del(char *name, bool stupid_legacy_mode)
int augroup_find(const char *name)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
{
int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string(name));
if (existing_id == AUGROUP_DELETED) {
return existing_id;
}

int existing_id = map_get(String, int)(&namespace_ids, cstr_as_string(name));
if (existing_id > 0) {
return existing_id;
}
Expand All @@ -490,10 +470,6 @@ char *augroup_name(int group)
{
assert(group != 0);

if (group == AUGROUP_DELETED) {
return (char *)get_deleted_augroup();
}

if (group == AUGROUP_ALL) {
group = current_augroup;
}
Expand Down Expand Up @@ -551,11 +527,9 @@ void do_augroup(char *arg, bool del_group)

String name;
int value;
map_foreach(&map_augroup_name_to_id, name, value, {
map_foreach(&namespace_ids, name, value, {
if (value > 0) {
msg_puts(name.data);
} else {
msg_puts(augroup_name(value));
}

msg_puts(" ");
Expand Down Expand Up @@ -2025,7 +1999,6 @@ static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc)
abort(); // unreachable
case AUGROUP_DEFAULT:
case AUGROUP_ALL:
case AUGROUP_DELETED:
// omit group in these cases
break;
default:
Expand Down
1 change: 0 additions & 1 deletion src/nvim/autocmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ enum {
AUGROUP_DEFAULT = -1, ///< default autocmd group
AUGROUP_ERROR = -2, ///< erroneous autocmd group
AUGROUP_ALL = -3, ///< all autocmd groups
AUGROUP_DELETED = -4, ///< all autocmd groups
// AUGROUP_NS = -5, // TODO(tjdevries): Support namespaced based augroups
};

Expand Down

0 comments on commit bf397ab

Please sign in to comment.