aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-01-16 22:42:14 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2016-01-16 22:42:14 -0800
commit766176443d6edbbd47f1dbe1080d9d1becf2b0c0 (patch)
treed8021e46acd13d2c6898cb5ab21de33f30a1e4d9
parent3552d073f62149ca908efbcb2ea5acae103ff382 (diff)
Clean up completion removal
Rather than passing a triple of short, long, is_old, pass an option as a string and then a type
-rw-r--r--src/builtin_complete.cpp110
-rw-r--r--src/complete.cpp85
-rw-r--r--src/complete.h9
3 files changed, 67 insertions, 137 deletions
diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp
index 6fd4349f..ee7f235a 100644
--- a/src/builtin_complete.cpp
+++ b/src/builtin_complete.cpp
@@ -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;
}
-}
-
-/**
- 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 (i=0; i < old_opt.size(); i++)
{
- 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);
- }
- }
+ complete_remove(cmd, cmd_type, old_opt.at(i), option_type_single_long);
+ removed = true;
}
- else if (gnu_opt.empty() && old_opt.empty())
+
+ for (i=0; i < gnu_opt.size(); i++)
{
- complete_remove(cmd,
- cmd_type,
- 0,
- 0,
- 0);
+ complete_remove(cmd, cmd_type, gnu_opt.at(i), option_type_double_long);
+ removed = true;
}
- else
+
+ if (! removed)
{
- builtin_complete_remove3(cmd,
- cmd_type,
- 0,
- gnu_opt,
- 0);
- builtin_complete_remove3(cmd,
- cmd_type,
- 0,
- old_opt,
- 1);
-
+ // This means that all loops were empty
+ complete_remove_all(cmd, cmd_type);
}
-
-
}
-/**
- 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);
}
}
diff --git a/src/complete.cpp b/src/complete.cpp
index d90b5cdb..5aa25305 100644
--- a/src/complete.cpp
+++ b/src/complete.cpp
@@ -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 completion_entry_t::remove_all_options()
+{
+ ASSERT_IS_LOCKED(completion_lock);
+ this->options.clear();
+}
+
-void complete_remove(const wchar_t *cmd,
- bool cmd_is_path,
- wchar_t short_opt,
- const wchar_t *long_opt,
- int old_mode)
+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'.
*/
diff --git a/src/complete.h b/src/complete.h
index 38b56bc0..4972981f 100644
--- a/src/complete.h
+++ b/src/complete.h
@@ -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.
*/