Clean up completion removal

Rather than passing a triple of short, long, is_old, pass an option
as a string and then a type
This commit is contained in:
ridiculousfish 2016-01-16 22:42:14 -08:00
parent 3552d073f6
commit 766176443d
3 changed files with 74 additions and 144 deletions

View file

@ -163,114 +163,54 @@ static void builtin_complete_add(const wcstring_list_t &cmd,
}
}
/**
Silly function
*/
static void builtin_complete_remove3(const wchar_t *cmd,
int cmd_type,
wchar_t short_opt,
const wcstring_list_t &long_opt,
int long_mode)
static void builtin_complete_remove_cmd(const wcstring &cmd,
int cmd_type,
const wchar_t *short_opt,
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt)
{
for (size_t i=0; i<long_opt.size(); i++)
bool removed = false;
size_t i;
for (i=0; short_opt[i] != L'\0'; i++)
{
complete_remove(cmd,
cmd_type,
short_opt,
long_opt.at(i).c_str(),
long_mode);
complete_remove(cmd, cmd_type, wcstring(1, short_opt[i]), option_type_short);
removed = true;
}
for (i=0; i < old_opt.size(); i++)
{
complete_remove(cmd, cmd_type, old_opt.at(i), option_type_single_long);
removed = true;
}
for (i=0; i < gnu_opt.size(); i++)
{
complete_remove(cmd, cmd_type, gnu_opt.at(i), option_type_double_long);
removed = true;
}
if (! removed)
{
// This means that all loops were empty
complete_remove_all(cmd, cmd_type);
}
}
/**
Silly function
*/
static void builtin_complete_remove2(const wchar_t *cmd,
int cmd_type,
const wchar_t *short_opt,
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt)
{
const wchar_t *s = (wchar_t *)short_opt;
if (*s)
{
for (; *s; s++)
{
if (old_opt.empty() && gnu_opt.empty())
{
complete_remove(cmd,
cmd_type,
*s,
0,
0);
}
else
{
builtin_complete_remove3(cmd,
cmd_type,
*s,
gnu_opt,
0);
builtin_complete_remove3(cmd,
cmd_type,
*s,
old_opt,
1);
}
}
}
else if (gnu_opt.empty() && old_opt.empty())
{
complete_remove(cmd,
cmd_type,
0,
0,
0);
}
else
{
builtin_complete_remove3(cmd,
cmd_type,
0,
gnu_opt,
0);
builtin_complete_remove3(cmd,
cmd_type,
0,
old_opt,
1);
}
}
/**
Silly function
*/
static void builtin_complete_remove(const wcstring_list_t &cmd,
const wcstring_list_t &path,
const wchar_t *short_opt,
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt)
{
for (size_t i=0; i<cmd.size(); i++)
{
builtin_complete_remove2(cmd.at(i).c_str(),
COMMAND,
short_opt,
gnu_opt,
old_opt);
builtin_complete_remove_cmd(cmd.at(i), COMMAND, short_opt, gnu_opt, old_opt);
}
for (size_t i=0; i<path.size(); i++)
{
builtin_complete_remove2(path.at(i).c_str(),
PATH,
short_opt,
gnu_opt,
old_opt);
builtin_complete_remove_cmd(path.at(i), PATH, short_opt, gnu_opt, old_opt);
}
}

View file

@ -185,7 +185,8 @@ public:
/** Adds or removes an option. */
void add_option(const complete_entry_opt_t &opt);
bool remove_option(wchar_t short_opt, const wchar_t *long_opt, int old_mode);
bool remove_option(const wcstring &option, complete_option_type_t type);
void remove_all_options();
completion_entry_t(const wcstring &c, bool type, bool author) :
cmd(c),
@ -419,7 +420,7 @@ completion_autoload_t::completion_autoload_t() : autoload_t(L"fish_complete_path
/** Callback when an autoloaded completion is removed */
void completion_autoload_t::command_removed(const wcstring &cmd)
{
complete_remove(cmd.c_str(), COMMAND, 0, 0, 0);
complete_remove_all(cmd, false /* not a path */);
}
@ -559,62 +560,34 @@ void complete_add(const wchar_t *cmd,
specified short / long option strings. Returns true if it is now
empty and should be deleted, false if it's not empty. Must be called while locked.
*/
bool completion_entry_t::remove_option(wchar_t short_opt, const wchar_t *long_opt, int old_mode)
bool completion_entry_t::remove_option(const wcstring &option, complete_option_type_t type)
{
ASSERT_IS_LOCKED(completion_lock);
if ((short_opt == 0) && (long_opt == 0))
option_list_t::iterator iter = this->options.begin();
while (iter != this->options.end())
{
this->options.clear();
}
else
{
for (option_list_t::iterator iter = this->options.begin(); iter != this->options.end();)
if (iter->option == option && iter->type == type)
{
complete_entry_opt_t &o = *iter;
if (o.option.empty())
{
// Just arguments, no match possible
continue;
}
bool matches = false;
bool mode_is_single = (o.type == option_type_single_long);
if (o.type == option_type_short)
{
matches = (short_opt && o.option.at(0) == short_opt);
}
else
{
matches = (mode_is_single == old_mode && long_opt && o.option == long_opt);
}
if (matches)
{
/* fwprintf( stderr,
L"remove option -%lc --%ls\n",
o->short_opt?o->short_opt:L' ',
o->long_opt );
*/
/* Destroy this option and go to the next one */
iter = this->options.erase(iter);
}
else
{
/* Just go to the next one */
++iter;
}
iter = this->options.erase(iter);
}
else
{
/* Just go to the next one */
++iter;
}
}
return this->options.empty();
}
void complete_remove(const wchar_t *cmd,
bool cmd_is_path,
wchar_t short_opt,
const wchar_t *long_opt,
int old_mode)
void completion_entry_t::remove_all_options()
{
ASSERT_IS_LOCKED(completion_lock);
this->options.clear();
}
void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option, complete_option_type_t type)
{
CHECK(cmd,);
scoped_lock lock(completion_lock);
completion_entry_t tmp_entry(cmd, cmd_is_path, false);
@ -622,7 +595,7 @@ void complete_remove(const wchar_t *cmd,
if (iter != completion_set.end())
{
completion_entry_t *entry = *iter;
bool delete_it = entry->remove_option(short_opt, long_opt, old_mode);
bool delete_it = entry->remove_option(option, type);
if (delete_it)
{
/* Delete this entry */
@ -632,6 +605,22 @@ void complete_remove(const wchar_t *cmd,
}
}
void complete_remove_all(const wcstring &cmd, bool cmd_is_path)
{
scoped_lock lock(completion_lock);
completion_entry_t tmp_entry(cmd, cmd_is_path, false);
completion_entry_set_t::iterator iter = completion_set.find(&tmp_entry);
if (iter != completion_set.end())
{
completion_entry_t *entry = *iter;
entry->remove_all_options();
completion_set.erase(iter);
delete entry;
}
}
/**
Find the full path and commandname from a command string 'str'.
*/

View file

@ -205,12 +205,13 @@ void complete_set_authoritative(const wchar_t *cmd, bool cmd_type, bool authorit
/**
Remove a previously defined completion
*/
void complete_remove(const wchar_t *cmd,
void complete_remove(const wcstring &cmd,
bool cmd_is_path,
wchar_t short_opt,
const wchar_t *long_opt,
int long_mode);
const wcstring &option,
complete_option_type_t type);
/** Removes all completions for a given command */
void complete_remove_all(const wcstring &cmd, bool cmd_is_path);
/** Find all completions of the command cmd, insert them into out.
*/