2016-07-21 13:30:58 +08:00
|
|
|
/// Functions related to tab-completion.
|
|
|
|
///
|
|
|
|
/// These functions are used for storing and retrieving tab-completion data, as well as for
|
|
|
|
/// performing tab-completion.
|
|
|
|
///
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "complete.h"
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <pwd.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stddef.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <wctype.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2012-04-24 10:29:44 +08:00
|
|
|
#include <algorithm>
|
2018-10-02 00:59:22 +08:00
|
|
|
#include <atomic>
|
2017-08-06 06:08:39 +08:00
|
|
|
#include <cstddef>
|
2019-10-14 06:50:48 +08:00
|
|
|
#include <cwchar>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <functional>
|
2017-08-06 06:08:39 +08:00
|
|
|
#include <iterator>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <list>
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <memory>
|
2018-05-12 13:11:40 +08:00
|
|
|
#include <numeric>
|
2019-09-22 04:38:59 +08:00
|
|
|
#include <set>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2017-08-20 00:55:06 +08:00
|
|
|
#include <unordered_map>
|
2017-07-25 06:54:54 +08:00
|
|
|
#include <unordered_set>
|
2017-08-07 07:05:51 +08:00
|
|
|
#include <utility>
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "autoload.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "builtin.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "common.h"
|
2018-03-07 02:35:14 +08:00
|
|
|
#include "env.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "exec.h"
|
|
|
|
#include "expand.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "function.h"
|
2019-11-05 23:47:01 +08:00
|
|
|
#include "history.h"
|
2013-11-30 15:44:26 +08:00
|
|
|
#include "iothread.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "parse_constants.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "parse_util.h"
|
|
|
|
#include "parser.h"
|
2019-12-30 06:25:42 +08:00
|
|
|
#include "parser_keywords.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "proc.h"
|
2018-07-16 06:50:56 +08:00
|
|
|
#include "reader.h"
|
2018-01-21 03:58:57 +08:00
|
|
|
#include "tnode.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "util.h"
|
2020-01-16 05:16:43 +08:00
|
|
|
#include "wcstringutil.h"
|
2016-05-04 07:23:30 +08:00
|
|
|
#include "wildcard.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Completion description strings, mostly for different types of files, such as sockets, block
|
|
|
|
// devices, etc.
|
|
|
|
//
|
|
|
|
// There are a few more completion description strings defined in expand.c. Maybe all completion
|
|
|
|
// description strings should be defined in the same file?
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Description for ~USER completion.
|
|
|
|
#define COMPLETE_USER_DESC _(L"Home for %ls")
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Description for short variables. The value is concatenated to this description.
|
|
|
|
#define COMPLETE_VAR_DESC_VAL _(L"Variable: %ls")
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2018-10-16 14:36:40 +08:00
|
|
|
/// Description for abbreviations.
|
|
|
|
#define ABBR_DESC _(L"Abbreviation: %ls")
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// The special cased translation macro for completions. The empty string needs to be special cased,
|
|
|
|
/// since it can occur, and should not be translated. (Gettext returns the version information as
|
|
|
|
/// the response).
|
2017-11-16 21:29:42 +08:00
|
|
|
#ifdef HAVE_GETTEXT
|
2016-06-02 13:03:27 +08:00
|
|
|
static const wchar_t *C_(const wcstring &s) {
|
|
|
|
return s.empty() ? L"" : wgettext(s.c_str()).c_str();
|
|
|
|
}
|
2006-11-29 22:18:22 +08:00
|
|
|
#else
|
2016-05-04 07:23:30 +08:00
|
|
|
static const wcstring &C_(const wcstring &s) { return s; }
|
2006-11-29 22:18:22 +08:00
|
|
|
#endif
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Struct describing a completion option entry.
|
|
|
|
///
|
|
|
|
/// If option is empty, the comp field must not be empty and contains a list of arguments to the
|
|
|
|
/// command.
|
|
|
|
///
|
|
|
|
/// The type field determines how the option is to be interpreted: either empty (args_only) or
|
|
|
|
/// short, single-long ("old") or double-long ("GNU"). An invariant is that the option is empty if
|
|
|
|
/// and only if the type is args_only.
|
|
|
|
///
|
|
|
|
/// If option is non-empty, it specifies a switch for the command. If \c comp is also not empty, it
|
|
|
|
/// contains a list of non-switch arguments that may only follow directly after the specified
|
|
|
|
/// switch.
|
|
|
|
typedef struct complete_entry_opt {
|
|
|
|
// Text of the option (like 'foo').
|
2016-01-17 07:46:43 +08:00
|
|
|
wcstring option;
|
2016-05-04 07:23:30 +08:00
|
|
|
// Type of the option: args-oly, short, single_long, or double_long.
|
2016-01-17 07:46:43 +08:00
|
|
|
complete_option_type_t type;
|
2016-05-04 07:23:30 +08:00
|
|
|
// Arguments to the option.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring comp;
|
2016-05-04 07:23:30 +08:00
|
|
|
// Description of the completion.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring desc;
|
2016-05-04 07:23:30 +08:00
|
|
|
// Condition under which to use the option.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring condition;
|
2019-04-26 05:21:06 +08:00
|
|
|
// Determines how completions should be performed on the argument after the switch.
|
|
|
|
completion_mode_t result_mode;
|
2016-05-04 07:23:30 +08:00
|
|
|
// Completion flags.
|
2012-11-19 08:30:30 +08:00
|
|
|
complete_flags_t flags;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-12-22 04:45:44 +08:00
|
|
|
wcstring localized_desc() const { return C_(desc); }
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
size_t expected_dash_count() const {
|
|
|
|
switch (this->type) {
|
2016-01-17 07:46:43 +08:00
|
|
|
case option_type_args_only:
|
|
|
|
return 0;
|
|
|
|
case option_type_short:
|
|
|
|
case option_type_single_long:
|
|
|
|
return 1;
|
|
|
|
case option_type_double_long:
|
|
|
|
return 2;
|
|
|
|
}
|
2016-11-02 12:19:34 +08:00
|
|
|
DIE("unreachable");
|
2016-01-17 07:46:43 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2012-02-09 08:15:53 +08:00
|
|
|
} complete_entry_opt_t;
|
2018-08-07 17:01:19 +08:00
|
|
|
|
2016-08-17 09:23:10 +08:00
|
|
|
/// Last value used in the order field of completion_entry_t.
|
2018-10-02 00:59:22 +08:00
|
|
|
static std::atomic<unsigned int> k_complete_order{0};
|
2012-04-12 09:25:37 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Struct describing a command completion.
|
2019-11-26 08:56:39 +08:00
|
|
|
using option_list_t = std::list<complete_entry_opt_t>;
|
2016-05-04 07:23:30 +08:00
|
|
|
class completion_entry_t {
|
|
|
|
public:
|
|
|
|
/// List of all options.
|
2012-11-19 08:30:30 +08:00
|
|
|
option_list_t options;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
public:
|
|
|
|
/// Command string.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring cmd;
|
2016-05-04 07:23:30 +08:00
|
|
|
/// True if command is a path.
|
2012-11-19 08:30:30 +08:00
|
|
|
const bool cmd_is_path;
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Order for when this completion was created. This aids in outputting completions sorted by
|
|
|
|
/// time.
|
2012-04-12 09:25:37 +08:00
|
|
|
const unsigned int order;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Getters for option list.
|
2012-02-27 06:32:06 +08:00
|
|
|
const option_list_t &get_options() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Adds or removes an option.
|
2012-05-12 09:59:38 +08:00
|
|
|
void add_option(const complete_entry_opt_t &opt);
|
2016-01-17 14:42:14 +08:00
|
|
|
bool remove_option(const wcstring &option, complete_option_type_t type);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-02-19 10:39:03 +08:00
|
|
|
completion_entry_t(wcstring c, bool type)
|
2018-10-02 00:59:22 +08:00
|
|
|
: cmd(std::move(c)), cmd_is_path(type), order(++k_complete_order) {}
|
2012-02-09 06:47:50 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Set of all completion entries.
|
2017-08-20 04:29:52 +08:00
|
|
|
namespace std {
|
2018-01-13 03:15:35 +08:00
|
|
|
template <>
|
|
|
|
struct hash<completion_entry_t> {
|
|
|
|
size_t operator()(const completion_entry_t &c) const {
|
|
|
|
std::hash<wcstring> hasher;
|
2019-11-19 09:08:16 +08:00
|
|
|
return hasher(wcstring(c.cmd));
|
2018-01-13 03:15:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct equal_to<completion_entry_t> {
|
|
|
|
bool operator()(const completion_entry_t &c1, const completion_entry_t &c2) const {
|
|
|
|
return c1.cmd == c2.cmd;
|
|
|
|
}
|
|
|
|
};
|
2018-08-07 15:16:59 +08:00
|
|
|
} // namespace std
|
2019-11-26 08:56:39 +08:00
|
|
|
using completion_entry_set_t = std::unordered_set<completion_entry_t>;
|
2018-10-06 00:05:48 +08:00
|
|
|
static owning_lock<completion_entry_set_t> s_completion_set;
|
2012-02-27 06:32:06 +08:00
|
|
|
|
2019-02-04 08:06:10 +08:00
|
|
|
/// Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list.
|
|
|
|
using wrapper_map_t = std::unordered_map<wcstring, wcstring_list_t>;
|
|
|
|
static owning_lock<wrapper_map_t> wrapper_map;
|
|
|
|
|
2016-08-17 09:23:10 +08:00
|
|
|
/// Comparison function to sort completions by their order field.
|
2018-05-12 13:11:40 +08:00
|
|
|
static bool compare_completions_by_order(const completion_entry_t &p1,
|
|
|
|
const completion_entry_t &p2) {
|
|
|
|
return p1.order < p2.order;
|
2012-04-12 09:25:37 +08:00
|
|
|
}
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
void completion_entry_t::add_option(const complete_entry_opt_t &opt) { options.push_front(opt); }
|
2012-02-27 06:32:06 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
const option_list_t &completion_entry_t::get_options() const { return options; }
|
2012-02-27 06:32:06 +08:00
|
|
|
|
2018-10-16 13:30:13 +08:00
|
|
|
description_func_t const_desc(const wcstring &s) {
|
2019-01-14 07:03:19 +08:00
|
|
|
return [=](const wcstring &ignored) {
|
|
|
|
UNUSED(ignored);
|
|
|
|
return s;
|
|
|
|
};
|
2018-10-16 13:30:13 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 09:23:10 +08:00
|
|
|
/// Clear the COMPLETE_AUTO_SPACE flag, and set COMPLETE_NO_SPACE appropriately depending on the
|
|
|
|
/// suffix of the string.
|
2016-05-04 07:23:30 +08:00
|
|
|
static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_t flags) {
|
2016-06-02 13:03:27 +08:00
|
|
|
complete_flags_t new_flags = flags;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (flags & COMPLETE_AUTO_SPACE) {
|
2016-06-02 13:03:27 +08:00
|
|
|
new_flags &= ~COMPLETE_AUTO_SPACE;
|
2014-01-15 06:28:06 +08:00
|
|
|
size_t len = comp.size();
|
2019-11-19 10:34:50 +08:00
|
|
|
if (len > 0 && (std::wcschr(L"/=@:", comp.at(len - 1)) != nullptr))
|
2019-05-05 18:09:25 +08:00
|
|
|
new_flags |= COMPLETE_NO_SPACE;
|
2012-07-18 22:18:19 +08:00
|
|
|
}
|
2016-06-02 13:03:27 +08:00
|
|
|
return new_flags;
|
2014-01-15 06:28:06 +08:00
|
|
|
}
|
2012-07-18 22:18:19 +08:00
|
|
|
|
2016-08-17 09:40:01 +08:00
|
|
|
/// completion_t functions. Note that the constructor resolves flags!
|
2018-02-17 13:26:15 +08:00
|
|
|
completion_t::completion_t(wcstring comp, wcstring desc, string_fuzzy_match_t mat,
|
2016-05-04 07:23:30 +08:00
|
|
|
complete_flags_t flags_val)
|
2018-02-17 13:26:15 +08:00
|
|
|
: completion(std::move(comp)),
|
|
|
|
description(std::move(desc)),
|
2019-12-22 05:53:48 +08:00
|
|
|
match(mat),
|
2018-02-17 13:26:15 +08:00
|
|
|
flags(resolve_auto_space(completion, flags_val)) {}
|
|
|
|
|
2019-12-18 11:51:00 +08:00
|
|
|
completion_t::completion_t(const completion_t &) = default;
|
2020-01-27 12:59:58 +08:00
|
|
|
completion_t::completion_t(completion_t &&) = default;
|
2019-12-18 11:51:00 +08:00
|
|
|
completion_t &completion_t::operator=(const completion_t &) = default;
|
2020-01-27 12:59:58 +08:00
|
|
|
completion_t &completion_t::operator=(completion_t &&) = default;
|
2018-02-17 13:26:15 +08:00
|
|
|
completion_t::~completion_t() = default;
|
2012-07-18 03:47:01 +08:00
|
|
|
|
2020-01-04 07:53:31 +08:00
|
|
|
__attribute__((always_inline)) static inline bool natural_compare_completions(
|
|
|
|
const completion_t &a, const completion_t &b) {
|
2017-07-25 06:54:54 +08:00
|
|
|
// For this to work, stable_sort must be used because results aren't interchangeable.
|
|
|
|
if (a.flags & b.flags & COMPLETE_DONT_SORT) {
|
|
|
|
// Both completions are from a source with the --keep-order flag.
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-24 02:13:26 +08:00
|
|
|
return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0;
|
2013-09-01 06:01:02 +08:00
|
|
|
}
|
|
|
|
|
2020-01-04 07:53:31 +08:00
|
|
|
bool completion_t::is_naturally_less_than(const completion_t &a, const completion_t &b) {
|
|
|
|
return natural_compare_completions(a, b);
|
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void completion_t::prepend_token_prefix(const wcstring &prefix) {
|
|
|
|
if (this->flags & COMPLETE_REPLACES_TOKEN) {
|
2015-08-03 08:58:37 +08:00
|
|
|
this->completion.insert(0, prefix);
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 06:01:02 +08:00
|
|
|
|
2020-01-04 07:53:31 +08:00
|
|
|
// If these functions aren't force inlined, it is actually faster to call
|
|
|
|
// stable_sort twice rather than to iterate once performing all comparisons in one go!
|
|
|
|
__attribute__((always_inline)) static inline bool compare_completions_by_duplicate_arguments(
|
|
|
|
const completion_t &a, const completion_t &b) {
|
2018-08-07 17:01:19 +08:00
|
|
|
bool ad = a.flags & COMPLETE_DUPLICATES_ARGUMENT;
|
|
|
|
bool bd = b.flags & COMPLETE_DUPLICATES_ARGUMENT;
|
|
|
|
return ad < bd;
|
|
|
|
}
|
|
|
|
|
2020-01-04 07:53:31 +08:00
|
|
|
__attribute__((always_inline)) static inline bool compare_completions_by_tilde(
|
|
|
|
const completion_t &a, const completion_t &b) {
|
|
|
|
if (a.completion.empty() || b.completion.empty()) {
|
2019-06-11 00:50:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ((a.completion.back() == L'~') < (b.completion.back() == L'~'));
|
2019-05-06 23:07:29 +08:00
|
|
|
}
|
2019-06-11 00:50:40 +08:00
|
|
|
|
2019-05-05 09:35:22 +08:00
|
|
|
/// Unique the list of completions, without perturbing their order.
|
2020-01-16 08:13:41 +08:00
|
|
|
static void unique_completions_retaining_order(completion_list_t *comps) {
|
2019-05-05 09:35:22 +08:00
|
|
|
std::unordered_set<wcstring> seen;
|
|
|
|
seen.reserve(comps->size());
|
|
|
|
auto pred = [&seen](const completion_t &c) {
|
|
|
|
// Remove (return true) if insertion fails.
|
|
|
|
bool inserted = seen.insert(c.completion).second;
|
|
|
|
return !inserted;
|
|
|
|
};
|
|
|
|
comps->erase(std::remove_if(comps->begin(), comps->end(), pred), comps->end());
|
2017-07-25 06:54:54 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 08:13:41 +08:00
|
|
|
void completions_sort_and_prioritize(completion_list_t *comps, completion_request_flags_t flags) {
|
2016-05-04 07:23:30 +08:00
|
|
|
// Find the best match type.
|
2016-02-07 07:06:33 +08:00
|
|
|
fuzzy_match_type_t best_type = fuzzy_match_none;
|
2019-11-20 05:46:47 +08:00
|
|
|
for (const auto &comp : *comps) {
|
|
|
|
best_type = std::min(best_type, comp.match.type);
|
2020-01-04 07:53:31 +08:00
|
|
|
if (best_type <= fuzzy_match_prefix) {
|
|
|
|
// We can't get better than this (see below)
|
|
|
|
break;
|
|
|
|
}
|
2016-02-07 07:06:33 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
// If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion
|
|
|
|
// will only show one match if it matches a file exactly. (see issue #959).
|
|
|
|
if (best_type == fuzzy_match_exact) {
|
2016-02-07 07:06:33 +08:00
|
|
|
best_type = fuzzy_match_prefix;
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
// Throw out completions whose match types are less suitable than the best.
|
2018-08-07 15:16:59 +08:00
|
|
|
comps->erase(
|
|
|
|
std::remove_if(comps->begin(), comps->end(),
|
|
|
|
[&](const completion_t &comp) { return comp.match.type > best_type; }),
|
|
|
|
comps->end());
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2019-05-05 09:35:22 +08:00
|
|
|
// Deduplicate both sorted and unsorted results.
|
|
|
|
unique_completions_retaining_order(comps);
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2020-01-04 07:53:31 +08:00
|
|
|
// Sort, provided COMPLETE_DONT_SORT isn't set.
|
|
|
|
stable_sort(comps->begin(), comps->end(), [](const completion_t &a, const completion_t &b) {
|
|
|
|
return a.match.type < b.match.type || natural_compare_completions(a, b);
|
|
|
|
});
|
2018-08-07 17:01:19 +08:00
|
|
|
|
|
|
|
// Lastly, if this is for an autosuggestion, prefer to avoid completions that duplicate
|
2019-05-28 10:47:13 +08:00
|
|
|
// arguments, and penalize files that end in tilde - they're frequently autosave files from e.g.
|
|
|
|
// emacs.
|
2019-05-06 23:07:29 +08:00
|
|
|
if (flags & completion_request_t::autosuggestion) {
|
2020-01-17 07:14:21 +08:00
|
|
|
stable_sort(comps->begin(), comps->end(), [](const completion_t &a, const completion_t &b) {
|
|
|
|
return compare_completions_by_duplicate_arguments(a, b) ||
|
|
|
|
compare_completions_by_tilde(a, b);
|
|
|
|
});
|
2019-05-06 23:07:29 +08:00
|
|
|
}
|
2016-02-07 07:06:33 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Class representing an attempt to compute completions.
|
|
|
|
class completer_t {
|
2020-01-16 09:14:47 +08:00
|
|
|
/// The operation context for this completion.
|
|
|
|
const operation_context_t &ctx;
|
2019-05-05 09:17:18 +08:00
|
|
|
|
2018-09-11 00:58:02 +08:00
|
|
|
/// The command to complete.
|
2018-08-07 15:16:59 +08:00
|
|
|
const wcstring cmd;
|
2018-09-11 00:58:02 +08:00
|
|
|
|
|
|
|
/// Flags associated with the completion request.
|
2013-03-06 12:54:16 +08:00
|
|
|
const completion_request_flags_t flags;
|
2018-09-11 00:58:02 +08:00
|
|
|
|
2019-05-05 09:17:18 +08:00
|
|
|
/// The output completions.
|
2020-01-16 08:13:41 +08:00
|
|
|
completion_list_t completions;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Table of completions conditions that have already been tested and the corresponding test
|
|
|
|
/// results.
|
2017-08-20 07:27:24 +08:00
|
|
|
typedef std::unordered_map<wcstring, bool> condition_cache_t;
|
2012-02-27 05:27:31 +08:00
|
|
|
condition_cache_t condition_cache;
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
enum complete_type_t { COMPLETE_DEFAULT, COMPLETE_AUTOSUGGEST };
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
complete_type_t type() const {
|
2019-05-05 08:55:57 +08:00
|
|
|
return (flags & completion_request_t::autosuggestion) ? COMPLETE_AUTOSUGGEST
|
|
|
|
: COMPLETE_DEFAULT;
|
2013-03-06 12:54:16 +08:00
|
|
|
}
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2019-05-05 08:55:57 +08:00
|
|
|
bool wants_descriptions() const { return flags & completion_request_t::descriptions; }
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2019-05-05 08:55:57 +08:00
|
|
|
bool fuzzy() const { return flags & completion_request_t::fuzzy_match; }
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
fuzzy_match_type_t max_fuzzy_match_type() const {
|
|
|
|
// If we are doing fuzzy matching, request all types; if not request only prefix matching.
|
2019-05-05 08:55:57 +08:00
|
|
|
if (fuzzy()) return fuzzy_match_none;
|
2016-05-05 06:19:47 +08:00
|
|
|
return fuzzy_match_prefix_case_insensitive;
|
2013-05-26 06:41:18 +08:00
|
|
|
}
|
2013-03-06 12:54:16 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool try_complete_variable(const wcstring &str);
|
|
|
|
bool try_complete_user(const wcstring &str);
|
2012-02-25 10:43:10 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
bool complete_param(const wcstring &cmd_orig, const wcstring &popt, const wcstring &str,
|
2012-11-19 08:30:30 +08:00
|
|
|
bool use_switches);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void complete_param_expand(const wcstring &str, bool do_file,
|
|
|
|
bool handle_as_special_cd = false);
|
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
void complete_cmd(const wcstring &str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-10-16 14:36:40 +08:00
|
|
|
/// Attempt to complete an abbreviation for the given string.
|
2019-11-19 08:54:36 +08:00
|
|
|
void complete_abbr(const wcstring &cmd);
|
2018-10-16 14:36:40 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void complete_from_args(const wcstring &str, const wcstring &args, const wcstring &desc,
|
2012-11-19 08:30:30 +08:00
|
|
|
complete_flags_t flags);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
void complete_cmd_desc(const wcstring &str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-08-02 07:32:52 +08:00
|
|
|
bool complete_variable(const wcstring &str, size_t start_offset);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
bool condition_test(const wcstring &condition);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-10-16 13:30:13 +08:00
|
|
|
void complete_strings(const wcstring &wc_escaped, const description_func_t &desc_func,
|
2020-01-16 08:13:41 +08:00
|
|
|
const completion_list_t &possible_comp, complete_flags_t flags);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
expand_flags_t expand_flags() const {
|
|
|
|
// Never do command substitution in autosuggestions. Sadly, we also can't yet do job
|
|
|
|
// expansion because it's not thread safe.
|
2019-04-26 02:22:17 +08:00
|
|
|
expand_flags_t result{};
|
2019-04-26 02:34:49 +08:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST) result |= expand_flag::skip_cmdsubst;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Allow fuzzy matching.
|
2019-04-26 02:34:49 +08:00
|
|
|
if (this->fuzzy()) result |= expand_flag::fuzzy_match;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2012-03-10 12:16:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
2018-08-07 15:16:59 +08:00
|
|
|
|
|
|
|
bool empty() const { return completions.empty(); }
|
|
|
|
|
2019-08-24 23:23:39 +08:00
|
|
|
void escape_opening_brackets(const wcstring &argument);
|
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
void mark_completions_duplicating_arguments(const wcstring &prefix,
|
|
|
|
const std::vector<tok_t> &args);
|
2018-08-07 17:01:19 +08:00
|
|
|
|
2018-08-07 15:16:59 +08:00
|
|
|
public:
|
2020-01-16 09:14:47 +08:00
|
|
|
completer_t(const operation_context_t &ctx, wcstring c, completion_request_flags_t f)
|
|
|
|
: ctx(ctx), cmd(std::move(c)), flags(f) {}
|
2018-08-07 15:16:59 +08:00
|
|
|
|
|
|
|
void perform();
|
|
|
|
|
2020-01-16 08:13:41 +08:00
|
|
|
completion_list_t acquire_completions() { return std::move(completions); }
|
2012-02-25 10:43:10 +08:00
|
|
|
};
|
|
|
|
|
2019-04-28 04:05:12 +08:00
|
|
|
// Autoloader for completions.
|
2019-04-28 06:42:34 +08:00
|
|
|
static owning_lock<autoload_t> completion_autoloader{autoload_t(L"fish_complete_path")};
|
2017-01-30 10:56:55 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Create a new completion entry.
|
2020-01-16 08:13:41 +08:00
|
|
|
void append_completion(completion_list_t *completions, wcstring comp, wcstring desc,
|
2020-01-04 07:53:31 +08:00
|
|
|
complete_flags_t flags, string_fuzzy_match_t &&match) {
|
|
|
|
completions->emplace_back(std::move(comp), std::move(desc), match, flags);
|
2007-02-17 19:05:55 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Test if the specified script returns zero. The result is cached, so that if multiple completions
|
|
|
|
/// use the same condition, it needs only be evaluated once. condition_cache_clear must be called
|
|
|
|
/// after a completion run to make sure that there are no stale completions.
|
|
|
|
bool completer_t::condition_test(const wcstring &condition) {
|
|
|
|
if (condition.empty()) {
|
2019-03-13 05:06:01 +08:00
|
|
|
// std::fwprintf( stderr, L"No condition specified\n" );
|
2018-05-12 13:11:40 +08:00
|
|
|
return true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2020-01-16 09:14:47 +08:00
|
|
|
if (!ctx.parser) {
|
2018-05-12 13:11:40 +08:00
|
|
|
return false;
|
2012-02-25 10:43:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-02-09 08:15:53 +08:00
|
|
|
bool test_res;
|
2012-02-27 05:27:31 +08:00
|
|
|
condition_cache_t::iterator cached_entry = condition_cache.find(condition);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (cached_entry == condition_cache.end()) {
|
|
|
|
// Compute new value and reinsert it.
|
2020-01-16 09:14:47 +08:00
|
|
|
test_res =
|
|
|
|
(0 == exec_subshell(condition, *ctx.parser, false /* don't apply exit status */));
|
2012-02-09 08:15:53 +08:00
|
|
|
condition_cache[condition] = test_res;
|
2016-05-04 07:23:30 +08:00
|
|
|
} else {
|
|
|
|
// Use the old value.
|
2012-02-09 08:15:53 +08:00
|
|
|
test_res = cached_entry->second;
|
|
|
|
}
|
|
|
|
return test_res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Locate the specified entry. Create it if it doesn't exist. Must be called while locked.
|
2018-10-06 00:05:48 +08:00
|
|
|
static completion_entry_t &complete_get_exact_entry(completion_entry_set_t &completion_set,
|
|
|
|
const wcstring &cmd, bool cmd_is_path) {
|
2020-01-04 07:53:31 +08:00
|
|
|
auto ins = completion_set.emplace(cmd, cmd_is_path);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// NOTE SET_ELEMENTS_ARE_IMMUTABLE: Exposing mutable access here is only okay as long as callers
|
|
|
|
// do not change any field that matters to ordering - affecting order without telling std::set
|
|
|
|
// invalidates its internal state.
|
|
|
|
return const_cast<completion_entry_t &>(*ins.first);
|
2007-01-28 21:40:59 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void complete_add(const wchar_t *cmd, bool cmd_is_path, const wcstring &option,
|
2019-04-26 05:21:06 +08:00
|
|
|
complete_option_type_t option_type, completion_mode_t result_mode,
|
|
|
|
const wchar_t *condition, const wchar_t *comp, const wchar_t *desc,
|
|
|
|
complete_flags_t flags) {
|
2019-05-28 08:24:19 +08:00
|
|
|
assert(cmd && "Null command");
|
2018-05-12 13:11:40 +08:00
|
|
|
// option should be empty iff the option type is arguments only.
|
2016-01-17 07:46:43 +08:00
|
|
|
assert(option.empty() == (option_type == option_type_args_only));
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Lock the lock that allows us to edit the completion entry list.
|
2018-10-06 00:05:48 +08:00
|
|
|
auto completion_set = s_completion_set.acquire();
|
|
|
|
completion_entry_t &c = complete_get_exact_entry(*completion_set, cmd, cmd_is_path);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Create our new option.
|
2012-11-19 08:30:30 +08:00
|
|
|
complete_entry_opt_t opt;
|
2016-01-17 07:46:43 +08:00
|
|
|
opt.option = option;
|
|
|
|
opt.type = option_type;
|
2012-11-19 08:30:30 +08:00
|
|
|
opt.result_mode = result_mode;
|
2012-02-09 08:15:53 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (comp) opt.comp = comp;
|
|
|
|
if (condition) opt.condition = condition;
|
|
|
|
if (desc) opt.desc = desc;
|
|
|
|
opt.flags = flags;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-03-19 06:14:16 +08:00
|
|
|
c.add_option(opt);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Remove all completion options in the specified entry that match the 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(const wcstring &option, complete_option_type_t type) {
|
2016-01-17 14:42:14 +08:00
|
|
|
option_list_t::iterator iter = this->options.begin();
|
2016-05-04 07:23:30 +08:00
|
|
|
while (iter != this->options.end()) {
|
|
|
|
if (iter->option == option && iter->type == type) {
|
2016-01-17 14:42:14 +08:00
|
|
|
iter = this->options.erase(iter);
|
2016-05-04 07:23:30 +08:00
|
|
|
} else {
|
|
|
|
// Just go to the next one.
|
2016-01-17 14:42:14 +08:00
|
|
|
++iter;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-05-12 09:59:38 +08:00
|
|
|
return this->options.empty();
|
2006-12-14 07:58:38 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option,
|
|
|
|
complete_option_type_t type) {
|
2018-10-06 00:05:48 +08:00
|
|
|
auto completion_set = s_completion_set.acquire();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-12-20 01:05:18 +08:00
|
|
|
completion_entry_t tmp_entry(cmd, cmd_is_path);
|
2018-10-06 00:05:48 +08:00
|
|
|
completion_entry_set_t::iterator iter = completion_set->find(tmp_entry);
|
|
|
|
if (iter != completion_set->end()) {
|
2016-05-04 07:23:30 +08:00
|
|
|
// const_cast: See SET_ELEMENTS_ARE_IMMUTABLE.
|
|
|
|
completion_entry_t &entry = const_cast<completion_entry_t &>(*iter);
|
2016-03-19 06:14:16 +08:00
|
|
|
|
|
|
|
bool delete_it = entry.remove_option(option, type);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (delete_it) {
|
2018-10-06 00:05:48 +08:00
|
|
|
completion_set->erase(iter);
|
2012-02-09 06:47:50 +08:00
|
|
|
}
|
2012-04-10 11:17:06 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
void complete_remove_all(const wcstring &cmd, bool cmd_is_path) {
|
2018-10-06 00:05:48 +08:00
|
|
|
auto completion_set = s_completion_set.acquire();
|
2016-12-20 01:05:18 +08:00
|
|
|
completion_entry_t tmp_entry(cmd, cmd_is_path);
|
2018-10-06 00:05:48 +08:00
|
|
|
completion_set->erase(tmp_entry);
|
2016-01-17 14:42:14 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Find the full path and commandname from a command string 'str'.
|
2018-09-17 03:48:50 +08:00
|
|
|
static void parse_cmd_string(const wcstring &str, wcstring *path, wcstring *cmd,
|
|
|
|
const environment_t &vars) {
|
|
|
|
if (!path_get_path(str, path, vars)) {
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Use the empty string as the 'path' for commands that can not be found.
|
2019-03-15 06:12:14 +08:00
|
|
|
path->clear();
|
2012-02-08 14:44:10 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Make sure the path is not included in the command.
|
2012-02-08 14:44:10 +08:00
|
|
|
size_t last_slash = str.find_last_of(L'/');
|
2016-05-04 07:23:30 +08:00
|
|
|
if (last_slash != wcstring::npos) {
|
2018-09-17 03:48:50 +08:00
|
|
|
*cmd = str.substr(last_slash + 1);
|
2016-05-04 07:23:30 +08:00
|
|
|
} else {
|
2018-09-17 03:48:50 +08:00
|
|
|
*cmd = str;
|
2012-02-08 14:44:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:40:01 +08:00
|
|
|
/// Copy any strings in possible_comp which have the specified prefix to the
|
|
|
|
/// completer's completion array. The prefix may contain wildcards. The output
|
|
|
|
/// will consist of completion_t structs.
|
2016-05-04 07:23:30 +08:00
|
|
|
///
|
2016-08-17 09:40:01 +08:00
|
|
|
/// There are three ways to specify descriptions for each completion. Firstly,
|
|
|
|
/// if a description has already been added to the completion, it is _not_
|
|
|
|
/// replaced. Secondly, if the desc_func function is specified, use it to
|
|
|
|
/// determine a dynamic completion. Thirdly, if none of the above are available,
|
|
|
|
/// the desc string is used as a description.
|
2016-05-04 07:23:30 +08:00
|
|
|
///
|
2016-08-17 09:40:01 +08:00
|
|
|
/// @param wc_escaped
|
|
|
|
/// the prefix, possibly containing wildcards. The wildcard should not have
|
|
|
|
/// been unescaped, i.e. '*' should be used for any string, not the
|
|
|
|
/// ANY_STRING character.
|
|
|
|
/// @param desc_func
|
2018-10-16 13:30:13 +08:00
|
|
|
/// the function that generates a description for those completions without an
|
2016-08-17 09:40:01 +08:00
|
|
|
/// embedded description
|
|
|
|
/// @param possible_comp
|
|
|
|
/// the list of possible completions to iterate over
|
|
|
|
/// @param flags
|
|
|
|
/// The flags
|
2018-10-16 13:30:13 +08:00
|
|
|
void completer_t::complete_strings(const wcstring &wc_escaped, const description_func_t &desc_func,
|
2020-01-16 08:13:41 +08:00
|
|
|
const completion_list_t &possible_comp, complete_flags_t flags) {
|
2012-01-30 18:23:58 +08:00
|
|
|
wcstring tmp = wc_escaped;
|
2019-04-26 02:22:17 +08:00
|
|
|
if (!expand_one(tmp,
|
2019-04-26 02:34:49 +08:00
|
|
|
this->expand_flags() | expand_flag::skip_cmdsubst | expand_flag::skip_wildcards,
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx))
|
2012-01-30 18:23:58 +08:00
|
|
|
return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2015-08-20 02:35:24 +08:00
|
|
|
const wcstring wc = parse_util_unescape_wildcards(tmp);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-10-16 14:36:40 +08:00
|
|
|
for (const auto &comp : possible_comp) {
|
|
|
|
const wcstring &comp_str = comp.completion;
|
|
|
|
if (!comp_str.empty()) {
|
|
|
|
wildcard_complete(comp_str, wc.c_str(), desc_func, &this->completions,
|
2016-05-04 07:23:30 +08:00
|
|
|
this->expand_flags(), flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2007-02-17 19:05:55 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// If command to complete is short enough, substitute the description with the whatis information
|
|
|
|
/// for the executable.
|
|
|
|
void completer_t::complete_cmd_desc(const wcstring &str) {
|
2012-02-25 04:13:35 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2020-01-16 09:14:47 +08:00
|
|
|
if (!ctx.parser) return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-06-02 02:14:29 +08:00
|
|
|
wcstring cmd;
|
|
|
|
size_t pos = str.find_last_of(L'/');
|
|
|
|
if (pos != std::string::npos) {
|
2018-06-03 23:59:11 +08:00
|
|
|
if (pos + 1 > str.length()) return;
|
|
|
|
cmd = wcstring(str, pos + 1);
|
2018-06-02 02:14:29 +08:00
|
|
|
} else {
|
|
|
|
cmd = str;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Using apropos with a single-character search term produces far to many results - require at
|
|
|
|
// least two characters if we don't know the location of the whatis-database.
|
2018-06-02 02:14:29 +08:00
|
|
|
if (cmd.length() < 2) return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-11-26 08:36:13 +08:00
|
|
|
if (wildcard_has(cmd, false)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
bool skip = true;
|
|
|
|
for (const auto &c : completions) {
|
2016-05-04 07:23:30 +08:00
|
|
|
if (c.completion.empty() || (c.completion[c.completion.size() - 1] != L'/')) {
|
2018-05-12 13:11:40 +08:00
|
|
|
skip = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
if (skip) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-02-03 04:04:04 +08:00
|
|
|
wcstring lookup_cmd(L"__fish_describe_command ");
|
2019-05-25 00:17:48 +08:00
|
|
|
lookup_cmd.append(escape_string(cmd, ESCAPE_ALL));
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// First locate a list of possible descriptions using a single call to apropos or a direct
|
|
|
|
// search if we know the location of the whatis database. This can take some time on slower
|
|
|
|
// systems with a large set of manuals, but it should be ok since apropos is only called once.
|
2020-01-26 10:14:38 +08:00
|
|
|
wcstring_list_t list;
|
2020-01-24 09:34:46 +08:00
|
|
|
(void)exec_subshell(lookup_cmd, *ctx.parser, list, false /* don't apply exit status */);
|
2020-01-26 10:14:38 +08:00
|
|
|
|
2020-01-24 09:34:46 +08:00
|
|
|
// Then discard anything that is not a possible completion and put the result into a
|
|
|
|
// hashtable with the completion as key and the description as value.
|
2020-01-26 10:14:38 +08:00
|
|
|
std::unordered_map<wcstring, wcstring> lookup;
|
|
|
|
// A typical entry is the command name, followed by a tab, followed by a description.
|
2020-01-24 09:34:46 +08:00
|
|
|
for (const wcstring &elstr : list) {
|
2020-01-26 10:14:38 +08:00
|
|
|
// Skip keys that are too short.
|
|
|
|
if (elstr.size() < cmd.size()) continue;
|
|
|
|
|
|
|
|
// Skip cases without a tab, or without a description, or bizarre cases where the tab is
|
|
|
|
// part of the command.
|
|
|
|
size_t tab_idx = elstr.find(L'\t');
|
|
|
|
if (tab_idx == wcstring::npos || tab_idx + 1 >= elstr.size() || tab_idx < cmd.size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Make the key. This is the stuff after the command.
|
|
|
|
// For example:
|
|
|
|
// elstr = lsmod
|
|
|
|
// cmd = ls
|
|
|
|
// key = mod
|
|
|
|
// Note an empty key is common and natural, if 'cmd' were already valid.
|
|
|
|
wcstring key(elstr, cmd.size(), tab_idx - cmd.size());
|
|
|
|
wcstring val(elstr, tab_idx + 1);
|
|
|
|
assert(!val.empty() && "tab index should not have been at the end.");
|
2020-01-24 09:34:46 +08:00
|
|
|
|
|
|
|
// And once again I make sure the first character is uppercased because I like it that
|
|
|
|
// way, and I get to decide these things.
|
2020-01-26 10:14:38 +08:00
|
|
|
val.at(0) = towupper(val.at(0));
|
|
|
|
lookup.insert(std::make_pair(std::move(key), std::move(val)));
|
2020-01-24 09:34:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then do a lookup on every completion and if a match is found, change to the new
|
|
|
|
// description.
|
|
|
|
for (auto &completion : completions) {
|
|
|
|
const wcstring &el = completion.completion;
|
|
|
|
auto new_desc_iter = lookup.find(el);
|
|
|
|
if (new_desc_iter != lookup.end()) completion.description = new_desc_iter->second;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Returns a description for the specified function, or an empty string if none.
|
|
|
|
static wcstring complete_function_desc(const wcstring &fn) {
|
2012-05-18 10:37:46 +08:00
|
|
|
wcstring result;
|
2018-09-27 09:35:30 +08:00
|
|
|
bool has_description = function_get_desc(fn, result);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (!has_description) {
|
2018-09-27 09:35:30 +08:00
|
|
|
function_get_definition(fn, result);
|
2020-01-09 00:53:46 +08:00
|
|
|
// A completion description is a single line.
|
2019-11-05 16:49:01 +08:00
|
|
|
for (wchar_t &c : result) {
|
|
|
|
if (c == L'\n') c = L' ';
|
|
|
|
}
|
2012-05-18 10:37:46 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return result;
|
2006-01-24 07:32:13 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Complete the specified command name. Search for executables in the path, executables defined
|
|
|
|
/// using an absolute path, functions, builtins and directories for implicit cd commands.
|
|
|
|
///
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param str_cmd the command string to find completions for
|
2019-10-29 20:32:26 +08:00
|
|
|
void completer_t::complete_cmd(const wcstring &str_cmd) {
|
2020-01-16 08:13:41 +08:00
|
|
|
completion_list_t possible_comp;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Append all possible executables
|
|
|
|
expand_result_t result =
|
|
|
|
expand_string(str_cmd, &this->completions,
|
|
|
|
this->expand_flags() | expand_flag::special_for_command |
|
|
|
|
expand_flag::for_completions | expand_flag::executables_only,
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx);
|
2020-01-23 03:58:10 +08:00
|
|
|
if (result == expand_result_t::cancel) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-24 09:34:46 +08:00
|
|
|
if (result == expand_result_t::ok && this->wants_descriptions()) {
|
2019-10-29 20:32:26 +08:00
|
|
|
this->complete_cmd_desc(str_cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't really care if this succeeds or fails. If it succeeds this->completions will be
|
|
|
|
// updated with choices for the user.
|
|
|
|
expand_result_t ignore =
|
|
|
|
// Append all matching directories
|
|
|
|
expand_string(
|
|
|
|
str_cmd, &this->completions,
|
|
|
|
this->expand_flags() | expand_flag::for_completions | expand_flag::directories_only,
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx);
|
2019-10-29 20:32:26 +08:00
|
|
|
UNUSED(ignore);
|
2016-10-23 02:21:13 +08:00
|
|
|
|
2019-11-04 06:55:54 +08:00
|
|
|
if (str_cmd.empty() || (str_cmd.find(L'/') == wcstring::npos && str_cmd.at(0) != L'~')) {
|
|
|
|
bool include_hidden = !str_cmd.empty() && str_cmd.at(0) == L'_';
|
|
|
|
wcstring_list_t names = function_get_names(include_hidden);
|
2019-10-29 20:32:26 +08:00
|
|
|
for (wcstring &name : names) {
|
|
|
|
// Append all known matching functions
|
|
|
|
append_completion(&possible_comp, std::move(name));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
this->complete_strings(str_cmd, complete_function_desc, possible_comp, 0);
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
possible_comp.clear();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Append all matching builtins
|
|
|
|
builtin_get_names(&possible_comp);
|
|
|
|
this->complete_strings(str_cmd, builtin_get_desc, possible_comp, 0);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 14:36:40 +08:00
|
|
|
void completer_t::complete_abbr(const wcstring &cmd) {
|
2020-01-16 09:14:47 +08:00
|
|
|
std::map<wcstring, wcstring> abbrs = get_abbreviations(ctx.vars);
|
2020-01-16 08:13:41 +08:00
|
|
|
completion_list_t possible_comp;
|
2018-10-16 14:36:40 +08:00
|
|
|
possible_comp.reserve(abbrs.size());
|
|
|
|
for (const auto &kv : abbrs) {
|
|
|
|
possible_comp.emplace_back(kv.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto desc_func = [&](const wcstring &key) {
|
|
|
|
auto iter = abbrs.find(key);
|
|
|
|
assert(iter != abbrs.end() && "Abbreviation not found");
|
|
|
|
return format_string(ABBR_DESC, iter->second.c_str());
|
|
|
|
};
|
|
|
|
this->complete_strings(cmd, desc_func, possible_comp, COMPLETE_NO_SPACE);
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:23:10 +08:00
|
|
|
/// Evaluate the argument list (as supplied by complete -a) and insert any
|
|
|
|
/// return matching completions. Matching is done using @c
|
|
|
|
/// copy_strings_with_prefix, meaning the completion may contain wildcards.
|
|
|
|
/// Logically, this is not always the right thing to do, but I have yet to come
|
2016-05-04 07:23:30 +08:00
|
|
|
/// up with a case where this matters.
|
|
|
|
///
|
2016-08-17 09:23:10 +08:00
|
|
|
/// @param str
|
|
|
|
/// The string to complete.
|
|
|
|
/// @param args
|
|
|
|
/// The list of option arguments to be evaluated.
|
|
|
|
/// @param desc
|
|
|
|
/// Description of the completion
|
|
|
|
/// @param flags
|
2019-09-10 18:58:17 +08:00
|
|
|
/// The flags
|
2016-08-17 09:23:10 +08:00
|
|
|
///
|
2016-05-04 07:23:30 +08:00
|
|
|
void completer_t::complete_from_args(const wcstring &str, const wcstring &args,
|
|
|
|
const wcstring &desc, complete_flags_t flags) {
|
2013-03-06 12:54:16 +08:00
|
|
|
bool is_autosuggest = (this->type() == COMPLETE_AUTOSUGGEST);
|
2012-05-06 05:33:24 +08:00
|
|
|
|
2019-05-28 05:52:48 +08:00
|
|
|
bool saved_interactive = false;
|
2020-01-16 09:14:47 +08:00
|
|
|
if (ctx.parser) {
|
|
|
|
saved_interactive = ctx.parser->libdata().is_interactive;
|
|
|
|
ctx.parser->libdata().is_interactive = false;
|
2016-02-28 10:25:58 +08:00
|
|
|
}
|
2012-02-26 10:54:49 +08:00
|
|
|
|
2019-04-26 02:22:17 +08:00
|
|
|
expand_flags_t eflags{};
|
2016-05-04 07:23:30 +08:00
|
|
|
if (is_autosuggest) {
|
2019-04-26 02:34:49 +08:00
|
|
|
eflags |= expand_flag::no_descriptions;
|
|
|
|
eflags |= expand_flag::skip_cmdsubst;
|
2016-02-28 10:37:59 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
completion_list_t possible_comp = parser_t::expand_argument_list(args, eflags, ctx);
|
2012-01-29 16:41:39 +08:00
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
if (ctx.parser) {
|
|
|
|
ctx.parser->libdata().is_interactive = saved_interactive;
|
2016-02-28 10:25:58 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-10-16 13:30:13 +08:00
|
|
|
this->complete_strings(escape_string(str, ESCAPE_ALL), const_desc(desc), possible_comp, flags);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
static size_t leading_dash_count(const wchar_t *str) {
|
2016-01-17 07:46:43 +08:00
|
|
|
size_t cursor = 0;
|
2016-05-04 07:23:30 +08:00
|
|
|
while (str[cursor] == L'-') {
|
2016-01-17 07:46:43 +08:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
return cursor;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Match a parameter.
|
|
|
|
static bool param_match(const complete_entry_opt_t *e, const wchar_t *optstr) {
|
2016-01-17 07:46:43 +08:00
|
|
|
bool result = false;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (e->type != option_type_args_only) {
|
2016-01-17 07:46:43 +08:00
|
|
|
size_t dashes = leading_dash_count(optstr);
|
|
|
|
result = (dashes == e->expected_dash_count() && e->option == &optstr[dashes]);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2016-01-17 07:46:43 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Test if a string is an option with an argument, like --color=auto or -I/usr/include.
|
|
|
|
static const wchar_t *param_match2(const complete_entry_opt_t *e, const wchar_t *optstr) {
|
|
|
|
// We may get a complete_entry_opt_t with no options if it's just arguments.
|
|
|
|
if (e->option.empty()) {
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2016-01-17 07:46:43 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Verify leading dashes.
|
2016-01-17 07:46:43 +08:00
|
|
|
size_t cursor = leading_dash_count(optstr);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (cursor != e->expected_dash_count()) {
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2016-01-17 07:46:43 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
// Verify options match.
|
|
|
|
if (!string_prefixes_string(e->option, &optstr[cursor])) {
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2016-01-17 07:46:43 +08:00
|
|
|
}
|
|
|
|
cursor += e->option.length();
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
// Short options are like -DNDEBUG. Long options are like --color=auto. So check for an equal
|
|
|
|
// sign for long options.
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
assert(e->type != option_type_short);
|
|
|
|
if (optstr[cursor] != L'=') {
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
cursor += 1;
|
2016-01-17 07:46:43 +08:00
|
|
|
return &optstr[cursor];
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
/// Parses a token of short options plus one optional parameter like
|
|
|
|
/// '-xzPARAM', where x and z are short options.
|
|
|
|
///
|
|
|
|
/// Returns the position of the last option character (e.g. the position of z which is 2).
|
|
|
|
/// Everything after that is assumed to be part of the parameter.
|
|
|
|
/// Returns wcstring::npos if there is no valid short option.
|
|
|
|
size_t short_option_pos(const wcstring &arg, const option_list_t &options) {
|
|
|
|
if (arg.size() <= 1 || leading_dash_count(arg.c_str()) != 1) {
|
|
|
|
return wcstring::npos;
|
2016-01-16 09:14:44 +08:00
|
|
|
}
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
for (size_t pos = 1; pos < arg.size(); pos++) {
|
|
|
|
wchar_t arg_char = arg.at(pos);
|
|
|
|
const complete_entry_opt_t *match = nullptr;
|
|
|
|
for (const complete_entry_opt_t &o : options) {
|
|
|
|
if (o.type == option_type_short && o.option.at(0) == arg_char) {
|
|
|
|
match = &o;
|
2016-01-16 09:14:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
if (match == nullptr) {
|
|
|
|
// The first character after the dash is not a valid option.
|
|
|
|
if (pos == 1) return wcstring::npos;
|
|
|
|
return pos - 1;
|
|
|
|
}
|
|
|
|
if (match->result_mode.requires_param) {
|
|
|
|
return pos;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
return arg.size() - 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:30:49 +08:00
|
|
|
/// Load command-specific completions for the specified command.
|
2019-05-13 09:23:00 +08:00
|
|
|
static void complete_load(const wcstring &name) {
|
2016-05-04 07:23:30 +08:00
|
|
|
// We have to load this as a function, since it may define a --wraps or signature.
|
|
|
|
// See issue #2466.
|
2019-05-05 11:20:52 +08:00
|
|
|
auto &parser = parser_t::principal_parser();
|
|
|
|
function_load(name, parser);
|
2019-04-28 04:05:12 +08:00
|
|
|
|
|
|
|
// It's important to NOT hold the lock around completion loading.
|
|
|
|
// We need to take the lock to decide what to load, drop it to perform the load, then reacquire
|
|
|
|
// it.
|
2019-07-13 03:19:00 +08:00
|
|
|
// Note we only look at the global fish_function_path and fish_completion_path.
|
|
|
|
maybe_t<wcstring> path_to_load =
|
|
|
|
completion_autoloader.acquire()->resolve_command(name, env_stack_t::globals());
|
2019-04-28 04:05:12 +08:00
|
|
|
if (path_to_load) {
|
2019-05-05 11:20:52 +08:00
|
|
|
autoload_t::perform_autoload(*path_to_load, parser);
|
2019-04-28 04:05:12 +08:00
|
|
|
completion_autoloader.acquire()->mark_autoload_finished(name);
|
|
|
|
}
|
2006-02-08 17:20:05 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// complete_param: Given a command, find completions for the argument str of command cmd_orig with
|
|
|
|
/// previous option popt.
|
|
|
|
///
|
|
|
|
/// Examples in format (cmd, popt, str):
|
|
|
|
///
|
|
|
|
/// echo hello world <tab> -> ("echo", "world", "")
|
|
|
|
/// echo hello world<tab> -> ("echo", "hello", "world")
|
|
|
|
///
|
|
|
|
/// Insert results into comp_out. Return true to perform file completion, false to disable it.
|
2018-10-20 21:33:26 +08:00
|
|
|
bool completer_t::complete_param(const wcstring &cmd_orig, const wcstring &popt,
|
|
|
|
const wcstring &str, bool use_switches) {
|
2019-05-31 01:12:49 +08:00
|
|
|
bool use_common = true, use_files = true, has_force = false;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring cmd, path;
|
2020-01-16 09:14:47 +08:00
|
|
|
parse_cmd_string(cmd_orig, &path, &cmd, ctx.vars);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2018-03-14 08:41:24 +08:00
|
|
|
// mqudsi: run_on_main_thread() already just runs `func` if we're on the main thread,
|
|
|
|
// but it makes a kcall to get the current thread id to ascertain that. Perhaps even
|
|
|
|
// that single kcall proved to be a source of slowdown so this test on a local variable
|
|
|
|
// is used to make that determination instead? I don't know.
|
2018-08-07 15:16:59 +08:00
|
|
|
auto run_on_main_thread = [&](std::function<void(void)> &&func) {
|
2018-03-14 08:41:24 +08:00
|
|
|
if (this->type() == COMPLETE_DEFAULT) {
|
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
func();
|
2018-08-07 15:16:59 +08:00
|
|
|
} else if (this->type() == COMPLETE_AUTOSUGGEST) {
|
|
|
|
iothread_perform_on_main([&]() { func(); });
|
|
|
|
} else {
|
2018-03-14 08:41:24 +08:00
|
|
|
assert(false && "this->type() is unknown!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-30 17:54:09 +08:00
|
|
|
// FLOGF(error, L"\nThinking about looking up completions for %ls\n", cmd.c_str());
|
2018-03-14 08:41:24 +08:00
|
|
|
bool head_exists = builtin_exists(cmd);
|
|
|
|
// Only reload environment variables if builtin_exists returned false, as an optimization
|
2019-11-26 08:51:28 +08:00
|
|
|
if (!head_exists) {
|
2019-11-11 05:01:47 +08:00
|
|
|
head_exists = function_exists_no_autoload(cmd);
|
2018-08-07 15:16:59 +08:00
|
|
|
// While it may seem like first testing `path_get_path` before resorting to an env lookup
|
|
|
|
// may be faster, path_get_path can potentially do a lot of FS/IO access, so env.get() +
|
|
|
|
// function_exists() should still be faster.
|
2019-09-19 16:46:56 +08:00
|
|
|
// Use cmd_orig here as it is potentially pathed.
|
2020-01-16 09:14:47 +08:00
|
|
|
head_exists = head_exists || path_get_path(cmd_orig, nullptr, ctx.vars);
|
2018-04-04 03:05:07 +08:00
|
|
|
}
|
2018-03-14 08:41:24 +08:00
|
|
|
|
|
|
|
if (!head_exists) {
|
2018-08-07 15:16:59 +08:00
|
|
|
// Do not load custom completions if the head does not exist
|
|
|
|
// This prevents errors caused during the execution of completion providers for
|
|
|
|
// tools that do not exist. Applies to both manual completions ("cm<TAB>", "cmd <TAB>")
|
|
|
|
// and automatic completions ("gi" autosuggestion provider -> git)
|
2020-01-19 21:39:17 +08:00
|
|
|
FLOG(complete, "Skipping completions for non-existent head");
|
2018-08-07 15:16:59 +08:00
|
|
|
} else {
|
2019-05-13 09:23:00 +08:00
|
|
|
run_on_main_thread([&]() { complete_load(cmd); });
|
2012-02-27 12:11:34 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Make a list of lists of all options that we care about.
|
2016-01-16 09:14:44 +08:00
|
|
|
std::vector<option_list_t> all_options;
|
2012-05-12 09:59:38 +08:00
|
|
|
{
|
2018-10-06 00:05:48 +08:00
|
|
|
auto completion_set = s_completion_set.acquire();
|
|
|
|
for (const completion_entry_t &i : *completion_set) {
|
2016-03-19 06:14:16 +08:00
|
|
|
const wcstring &match = i.cmd_is_path ? path : cmd;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (wildcard_match(match, i.cmd)) {
|
|
|
|
// Copy all of their options into our list.
|
|
|
|
all_options.push_back(i.get_options()); // Oof, this is a lot of copying
|
2012-05-12 09:59:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Now release the lock and test each option that we captured above. We have to do this outside
|
|
|
|
// the lock because callouts (like the condition) may add or remove completions. See issue 2.
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const option_list_t &options : all_options) {
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
size_t short_opt_pos = short_option_pos(str, options);
|
|
|
|
bool last_option_requires_param = false;
|
2019-11-26 08:36:13 +08:00
|
|
|
use_common = true;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (use_switches) {
|
|
|
|
if (str[0] == L'-') {
|
|
|
|
// Check if we are entering a combined option and argument (like --color=auto or
|
|
|
|
// -I/usr/include).
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const complete_entry_opt_t &o : options) {
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
const wchar_t *arg;
|
|
|
|
if (o.type == option_type_short) {
|
|
|
|
if (short_opt_pos == wcstring::npos) continue;
|
|
|
|
if (o.option.at(0) != str.at(short_opt_pos)) continue;
|
|
|
|
last_option_requires_param = o.result_mode.requires_param;
|
|
|
|
arg = str.c_str() + short_opt_pos + 1;
|
|
|
|
} else {
|
|
|
|
arg = param_match2(&o, str.c_str());
|
|
|
|
}
|
2019-11-19 10:34:50 +08:00
|
|
|
if (arg != nullptr && this->condition_test(o.condition)) {
|
2019-04-26 05:21:06 +08:00
|
|
|
if (o.result_mode.requires_param) use_common = false;
|
|
|
|
if (o.result_mode.no_files) use_files = false;
|
2019-05-31 01:12:49 +08:00
|
|
|
if (o.result_mode.force_files) has_force = true;
|
2018-05-12 13:11:40 +08:00
|
|
|
complete_from_args(arg, o.comp, o.localized_desc(), o.flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
} else if (popt[0] == L'-') {
|
|
|
|
// Set to true if we found a matching old-style switch.
|
2016-12-10 04:14:14 +08:00
|
|
|
// Here we are testing the previous argument,
|
|
|
|
// to see how we should complete the current argument
|
2016-01-17 07:46:43 +08:00
|
|
|
bool old_style_match = false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// If we are using old style long options, check for them first.
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const complete_entry_opt_t &o : options) {
|
2018-10-20 21:33:26 +08:00
|
|
|
if (o.type == option_type_single_long && param_match(&o, popt.c_str()) &&
|
2018-05-12 13:11:40 +08:00
|
|
|
this->condition_test(o.condition)) {
|
2016-10-23 02:21:13 +08:00
|
|
|
old_style_match = true;
|
2019-04-26 05:21:06 +08:00
|
|
|
if (o.result_mode.requires_param) use_common = false;
|
|
|
|
if (o.result_mode.no_files) use_files = false;
|
2019-05-31 01:12:49 +08:00
|
|
|
if (o.result_mode.force_files) has_force = true;
|
2018-05-12 13:11:40 +08:00
|
|
|
complete_from_args(str, o.comp, o.localized_desc(), o.flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// No old style option matched, or we are not using old style options. We check if
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
// any short (or gnu style) options do.
|
2016-05-04 07:23:30 +08:00
|
|
|
if (!old_style_match) {
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
size_t prev_short_opt_pos = short_option_pos(popt, options);
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const complete_entry_opt_t &o : options) {
|
2016-05-04 07:23:30 +08:00
|
|
|
// Gnu-style options with _optional_ arguments must be specified as a single
|
|
|
|
// token, so that it can be differed from a regular argument.
|
2016-12-10 04:14:14 +08:00
|
|
|
// Here we are testing the previous argument for a GNU-style match,
|
|
|
|
// to see how we should complete the current argument
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
if (!o.result_mode.requires_param) continue;
|
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
if (o.type == option_type_short) {
|
|
|
|
match = prev_short_opt_pos != wcstring::npos &&
|
|
|
|
// Only if the option was the last char in the token,
|
|
|
|
// i.e. there is no parameter yet.
|
|
|
|
prev_short_opt_pos + 1 == popt.size() &&
|
|
|
|
o.option.at(0) == popt.at(prev_short_opt_pos);
|
|
|
|
} else if (o.type == option_type_double_long) {
|
|
|
|
match = param_match(&o, popt.c_str());
|
|
|
|
}
|
|
|
|
if (match && this->condition_test(o.condition)) {
|
2019-04-26 05:21:06 +08:00
|
|
|
if (o.result_mode.requires_param) use_common = false;
|
|
|
|
if (o.result_mode.no_files) use_files = false;
|
2019-05-31 01:12:49 +08:00
|
|
|
if (o.result_mode.force_files) has_force = true;
|
2018-05-12 13:11:40 +08:00
|
|
|
complete_from_args(str, o.comp, o.localized_desc(), o.flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
if (!use_common) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-12-10 04:14:14 +08:00
|
|
|
// Now we try to complete an option itself
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const complete_entry_opt_t &o : options) {
|
2016-10-31 07:16:15 +08:00
|
|
|
// If this entry is for the base command, check if any of the arguments match.
|
2018-05-12 13:11:40 +08:00
|
|
|
if (!this->condition_test(o.condition)) continue;
|
|
|
|
if (o.option.empty()) {
|
2019-05-31 01:12:49 +08:00
|
|
|
use_files = use_files && (!(o.result_mode.no_files));
|
2018-05-12 13:11:40 +08:00
|
|
|
complete_from_args(str, o.comp, o.localized_desc(), o.flags);
|
2016-10-31 07:16:15 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2018-10-20 21:33:26 +08:00
|
|
|
if (!use_switches || str.empty()) {
|
2016-10-31 07:16:15 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
// Check if the short style option matches.
|
Completion: complete argument to last of a group of short options
Consider a group of short options, like -xzPARAM, where x and z are options and z takes an argument.
This commit enables completion of the argument to the last option (z), both within the same
token (-xzP) or in the next one (-xz P).
complete -C'-xz' will complete only parameters to z.
complete -C'-xz ' will complete only parameters to z if z requires a parameter
otherwise, it will also complete non-option parameters
To do so this implements a heuristic to differentiate such strings from single long options. To
detect whether our token contains some short options, we only require the first character after the
dash (here x) to be an option. Previously, all characters had to be short options. The last option
in our example is z. Everything after the last option is assumed to be a parameter to the last
option.
Assume there is also a single long option -x-foo, then complete -C'-x' will suggest both -x-foo and
-xy. However, when the single option x requires an argument, this will not suggest -x-foo.
However, I assume this will almost never happen in practise since completions very rarely mix
short and single long options.
Fixes #332
2019-09-10 20:12:56 +08:00
|
|
|
if (o.type == option_type_short) {
|
|
|
|
wchar_t optchar = o.option.at(0);
|
|
|
|
if (short_opt_pos == wcstring::npos) {
|
|
|
|
// str has no short option at all (but perhaps it is the
|
|
|
|
// prefix of a single long option).
|
|
|
|
// Only complete short options if there is no character after the dash.
|
|
|
|
if (str != L"-") continue;
|
|
|
|
} else {
|
|
|
|
// Only complete when the last short option has no parameter yet..
|
|
|
|
if (short_opt_pos + 1 != str.size()) continue;
|
|
|
|
// .. and it does not require one ..
|
|
|
|
if (last_option_requires_param) continue;
|
|
|
|
// .. and the option is not already there.
|
|
|
|
if (str.find(optchar) != wcstring::npos) continue;
|
|
|
|
}
|
2016-10-31 07:16:15 +08:00
|
|
|
// It's a match.
|
2018-10-11 08:38:38 +08:00
|
|
|
wcstring desc = o.localized_desc();
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append a short-style option
|
2018-10-11 08:38:38 +08:00
|
|
|
append_completion(&this->completions, o.option, std::move(desc), 0);
|
2016-10-31 07:16:15 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
// Check if the long style option matches.
|
2018-05-12 13:11:40 +08:00
|
|
|
if (o.type != option_type_single_long && o.type != option_type_double_long) {
|
2016-10-31 07:16:15 +08:00
|
|
|
continue;
|
|
|
|
}
|
2007-03-01 05:43:27 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
wcstring whole_opt(o.expected_dash_count(), L'-');
|
|
|
|
whole_opt.append(o.option);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
int match = string_prefixes_string(str, whole_opt);
|
2016-10-31 07:16:15 +08:00
|
|
|
if (!match) {
|
2018-10-20 21:33:26 +08:00
|
|
|
bool match_no_case = wcsncasecmp(str.c_str(), whole_opt.c_str(), str.length()) == 0;
|
2016-10-31 07:16:15 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
if (!match_no_case) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-31 07:16:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int has_arg = 0; // does this switch have any known arguments
|
|
|
|
int req_arg = 0; // does this switch _require_ an argument
|
|
|
|
size_t offset = 0;
|
|
|
|
complete_flags_t flags = 0;
|
|
|
|
|
|
|
|
if (match) {
|
2018-10-20 21:33:26 +08:00
|
|
|
offset = str.length();
|
2016-10-31 07:16:15 +08:00
|
|
|
} else {
|
|
|
|
flags = COMPLETE_REPLACES_TOKEN;
|
|
|
|
}
|
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
has_arg = !o.comp.empty();
|
2019-04-26 05:21:06 +08:00
|
|
|
req_arg = o.result_mode.requires_param;
|
2016-10-31 07:16:15 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
if (o.type == option_type_double_long && (has_arg && !req_arg)) {
|
2016-10-31 07:16:15 +08:00
|
|
|
// Optional arguments to a switch can only be handled using the '=', so we add it as
|
|
|
|
// a completion. By default we avoid using '=' and instead rely on '--switch
|
|
|
|
// switch-arg', since it is more commonly supported by homebrew getopt-like
|
|
|
|
// functions.
|
|
|
|
wcstring completion = format_string(L"%ls=", whole_opt.c_str() + offset);
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append a long-style option with a mandatory trailing equal sign
|
2018-10-11 08:38:38 +08:00
|
|
|
append_completion(&this->completions, std::move(completion), C_(o.desc), flags);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2016-10-31 07:16:15 +08:00
|
|
|
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append a long-style option
|
2018-10-11 08:38:38 +08:00
|
|
|
append_completion(&this->completions, whole_opt.substr(offset), C_(o.desc), flags);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:12:49 +08:00
|
|
|
return has_force || use_files;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Perform generic (not command-specific) expansions on the specified string.
|
|
|
|
void completer_t::complete_param_expand(const wcstring &str, bool do_file,
|
|
|
|
bool handle_as_special_cd) {
|
2020-01-16 09:14:47 +08:00
|
|
|
if (ctx.check_cancel()) return;
|
2019-04-26 02:34:49 +08:00
|
|
|
expand_flags_t flags =
|
|
|
|
this->expand_flags() | expand_flag::skip_cmdsubst | expand_flag::for_completions;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-04-26 02:34:49 +08:00
|
|
|
if (!do_file) flags |= expand_flag::skip_wildcards;
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
if (handle_as_special_cd && do_file) {
|
2018-01-09 05:36:20 +08:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST) {
|
2019-04-26 02:34:49 +08:00
|
|
|
flags |= expand_flag::special_for_cd_autosuggestion;
|
2018-01-09 05:36:20 +08:00
|
|
|
}
|
2019-04-26 02:34:49 +08:00
|
|
|
flags |= expand_flags_t{expand_flag::directories_only, expand_flag::special_for_cd,
|
|
|
|
expand_flag::no_descriptions};
|
2016-02-07 06:39:47 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Squelch file descriptions per issue #254.
|
2019-04-26 02:34:49 +08:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST || do_file) flags |= expand_flag::no_descriptions;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// We have the following cases:
|
|
|
|
//
|
|
|
|
// --foo=bar => expand just bar
|
|
|
|
// -foo=bar => expand just bar
|
|
|
|
// foo=bar => expand the whole thing, and also just bar
|
|
|
|
//
|
|
|
|
// We also support colon separator (#2178). If there's more than one, prefer the last one.
|
2019-09-19 14:32:40 +08:00
|
|
|
size_t sep_index = str.find_last_of(L"=:");
|
|
|
|
bool complete_from_separator = (sep_index != wcstring::npos);
|
2015-07-28 04:43:20 +08:00
|
|
|
bool complete_from_start = !complete_from_separator || !string_prefixes_string(L"-", str);
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
if (complete_from_separator) {
|
2019-09-19 14:32:40 +08:00
|
|
|
// FIXME: This just cuts the token,
|
|
|
|
// so any quoting or braces gets lost.
|
|
|
|
// See #4954.
|
2015-07-28 04:43:20 +08:00
|
|
|
const wcstring sep_string = wcstring(str, sep_index + 1);
|
2020-01-16 08:13:41 +08:00
|
|
|
completion_list_t local_completions;
|
2020-01-16 09:14:47 +08:00
|
|
|
if (expand_string(sep_string, &local_completions, flags, ctx) == expand_result_t::error) {
|
2020-01-19 21:39:17 +08:00
|
|
|
FLOGF(complete, L"Error while expanding string '%ls'", sep_string.c_str());
|
2015-07-28 04:43:20 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
// Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by
|
|
|
|
// inserting our separator and prefix.
|
2015-07-28 04:43:20 +08:00
|
|
|
const wcstring prefix_with_sep = wcstring(str, 0, sep_index + 1);
|
2018-02-18 05:18:00 +08:00
|
|
|
for (completion_t &comp : local_completions) {
|
|
|
|
comp.prepend_token_prefix(prefix_with_sep);
|
2015-07-28 04:43:20 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
this->completions.insert(this->completions.end(), local_completions.begin(),
|
2015-07-28 04:43:20 +08:00
|
|
|
local_completions.end());
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
if (complete_from_start) {
|
|
|
|
// Don't do fuzzy matching for files if the string begins with a dash (issue #568). We could
|
|
|
|
// consider relaxing this if there was a preceding double-dash argument.
|
2019-04-26 02:34:49 +08:00
|
|
|
if (string_prefixes_string(L"-", str)) flags.clear(expand_flag::fuzzy_match);
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
if (expand_string(str, &this->completions, flags, ctx) == expand_result_t::error) {
|
2020-01-19 21:39:17 +08:00
|
|
|
FLOGF(complete, L"Error while expanding string '%ls'", str.c_str());
|
2015-07-28 04:43:20 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-05-06 05:21:21 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Complete the specified string as an environment variable.
|
|
|
|
bool completer_t::complete_variable(const wcstring &str, size_t start_offset) {
|
|
|
|
const wchar_t *const whole_var = str.c_str();
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *var = &whole_var[start_offset];
|
2018-10-20 21:33:26 +08:00
|
|
|
size_t varlen = str.length() - start_offset;
|
2013-05-26 06:41:18 +08:00
|
|
|
bool res = false;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2020-01-16 09:14:47 +08:00
|
|
|
for (const wcstring &env_name : ctx.vars.get_names(0)) {
|
2016-05-04 07:23:30 +08:00
|
|
|
string_fuzzy_match_t match =
|
|
|
|
string_fuzzy_match_string(var, env_name, this->max_fuzzy_match_type());
|
|
|
|
if (match.type == fuzzy_match_none) {
|
|
|
|
continue; // no match
|
2013-05-26 06:41:18 +08:00
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2013-05-26 06:41:18 +08:00
|
|
|
wcstring comp;
|
|
|
|
int flags = 0;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
if (!match_type_requires_full_replacement(match.type)) {
|
|
|
|
// Take only the suffix.
|
2013-05-26 06:41:18 +08:00
|
|
|
comp.append(env_name.c_str() + varlen);
|
2016-05-04 07:23:30 +08:00
|
|
|
} else {
|
2013-05-26 06:41:18 +08:00
|
|
|
comp.append(whole_var, start_offset);
|
|
|
|
comp.append(env_name);
|
|
|
|
flags = COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE;
|
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2013-05-26 06:41:18 +08:00
|
|
|
wcstring desc;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (this->wants_descriptions()) {
|
2017-08-06 09:22:49 +08:00
|
|
|
if (this->type() != COMPLETE_AUTOSUGGEST) {
|
2019-12-30 06:25:42 +08:00
|
|
|
// $history can be huge, don't put all of it in the completion description; see
|
|
|
|
// #6288.
|
2019-11-05 19:59:49 +08:00
|
|
|
if (env_name == L"history") {
|
2019-12-30 06:25:42 +08:00
|
|
|
history_t *history =
|
2020-01-16 09:14:47 +08:00
|
|
|
&history_t::history_with_name(history_session_id(ctx.vars));
|
2019-12-30 00:43:05 +08:00
|
|
|
for (size_t i = 1; i < history->size() && desc.size() < 64; i++) {
|
|
|
|
if (i > 1) desc += L' ';
|
|
|
|
desc += expand_escape_string(history->item_at_index(i).str());
|
|
|
|
}
|
2019-11-05 19:59:49 +08:00
|
|
|
} else {
|
2020-01-16 09:14:47 +08:00
|
|
|
// Can't use ctx.vars here, it could be any variable.
|
|
|
|
auto var = ctx.vars.get(env_name);
|
2019-11-05 19:59:49 +08:00
|
|
|
if (!var) continue;
|
|
|
|
|
|
|
|
wcstring value = expand_escape_variable(*var);
|
|
|
|
desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
|
|
|
|
}
|
2017-08-06 09:22:49 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append matching environment variables
|
2020-01-04 07:53:31 +08:00
|
|
|
append_completion(&this->completions, std::move(comp), std::move(desc), flags,
|
|
|
|
std::move(match));
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2013-05-26 06:41:18 +08:00
|
|
|
res = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
bool completer_t::try_complete_variable(const wcstring &str) {
|
|
|
|
enum { e_unquoted, e_single_quoted, e_double_quoted } mode = e_unquoted;
|
2014-02-10 07:27:04 +08:00
|
|
|
const size_t len = str.size();
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Get the position of the dollar heading a (possibly empty) run of valid variable characters.
|
|
|
|
// npos means none.
|
2016-02-28 08:28:47 +08:00
|
|
|
size_t variable_start = wcstring::npos;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
for (size_t in_pos = 0; in_pos < len; in_pos++) {
|
2014-02-10 07:27:04 +08:00
|
|
|
wchar_t c = str.at(in_pos);
|
2017-04-20 14:43:02 +08:00
|
|
|
if (!valid_var_name_char(c)) {
|
2016-05-04 07:23:30 +08:00
|
|
|
// This character cannot be in a variable, reset the dollar.
|
2014-02-10 07:27:04 +08:00
|
|
|
variable_start = -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
switch (c) {
|
2016-11-03 09:29:14 +08:00
|
|
|
case L'\\': {
|
2014-02-10 07:27:04 +08:00
|
|
|
in_pos++;
|
|
|
|
break;
|
2016-11-03 09:29:14 +08:00
|
|
|
}
|
|
|
|
case L'$': {
|
2016-05-04 07:23:30 +08:00
|
|
|
if (mode == e_unquoted || mode == e_double_quoted) {
|
2014-02-10 07:27:04 +08:00
|
|
|
variable_start = in_pos;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 09:29:14 +08:00
|
|
|
}
|
|
|
|
case L'\'': {
|
2016-05-04 07:23:30 +08:00
|
|
|
if (mode == e_single_quoted) {
|
2014-02-10 07:27:04 +08:00
|
|
|
mode = e_unquoted;
|
2016-05-04 07:23:30 +08:00
|
|
|
} else if (mode == e_unquoted) {
|
2014-02-10 07:27:04 +08:00
|
|
|
mode = e_single_quoted;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 09:29:14 +08:00
|
|
|
}
|
|
|
|
case L'"': {
|
2016-05-04 07:23:30 +08:00
|
|
|
if (mode == e_double_quoted) {
|
2014-02-10 07:27:04 +08:00
|
|
|
mode = e_unquoted;
|
2016-05-04 07:23:30 +08:00
|
|
|
} else if (mode == e_unquoted) {
|
2014-02-10 07:27:04 +08:00
|
|
|
mode = e_double_quoted;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 09:29:14 +08:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break; // all other chars ignored here
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Now complete if we have a variable start. Note the variable text may be empty; in that case
|
|
|
|
// don't generate an autosuggestion, but do allow tab completion.
|
2019-05-05 08:55:57 +08:00
|
|
|
bool allow_empty = !(this->flags & completion_request_t::autosuggestion);
|
2016-02-28 08:28:47 +08:00
|
|
|
bool text_is_empty = (variable_start == len);
|
2014-02-10 07:27:04 +08:00
|
|
|
bool result = false;
|
2016-05-04 07:23:30 +08:00
|
|
|
if (variable_start != wcstring::npos && (allow_empty || !text_is_empty)) {
|
2014-02-10 07:27:04 +08:00
|
|
|
result = this->complete_variable(str, variable_start + 1);
|
|
|
|
}
|
|
|
|
return result;
|
2012-02-25 10:43:10 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Try to complete the specified string as a username. This is used by ~USER type expansion.
|
|
|
|
///
|
2017-05-11 13:07:01 +08:00
|
|
|
/// \return false if unable to complete, true otherwise
|
2016-05-04 07:23:30 +08:00
|
|
|
bool completer_t::try_complete_user(const wcstring &str) {
|
2016-10-16 08:20:53 +08:00
|
|
|
#ifndef HAVE_GETPWENT
|
|
|
|
// The getpwent() function does not exist on Android. A Linux user on Android isn't
|
|
|
|
// really a user - each installed app gets an UID assigned. Listing all UID:s is not
|
|
|
|
// possible without root access, and doing a ~USER type expansion does not make sense
|
|
|
|
// since every app is sandboxed and can't access eachother.
|
|
|
|
return false;
|
|
|
|
#else
|
2012-02-27 05:27:31 +08:00
|
|
|
const wchar_t *cmd = str.c_str();
|
2016-05-04 07:23:30 +08:00
|
|
|
const wchar_t *first_char = cmd;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2019-03-13 05:06:01 +08:00
|
|
|
if (*first_char != L'~' || std::wcschr(first_char, L'/')) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
const wchar_t *user_name = first_char + 1;
|
2019-03-13 05:06:01 +08:00
|
|
|
const wchar_t *name_end = std::wcschr(user_name, L'~');
|
2017-05-11 13:07:01 +08:00
|
|
|
if (name_end) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
double start_time = timef();
|
|
|
|
bool result = false;
|
2018-10-20 21:33:26 +08:00
|
|
|
size_t name_len = str.length() - 1;
|
2016-10-31 07:16:15 +08:00
|
|
|
|
2019-04-29 06:56:49 +08:00
|
|
|
static std::mutex s_setpwent_lock;
|
|
|
|
scoped_lock locker(s_setpwent_lock);
|
2016-10-31 07:16:15 +08:00
|
|
|
setpwent();
|
2017-05-11 13:07:01 +08:00
|
|
|
// cppcheck-suppress getpwentCalled
|
2018-07-16 06:50:56 +08:00
|
|
|
while (struct passwd *pw = getpwent()) {
|
2020-01-16 09:14:47 +08:00
|
|
|
if (ctx.check_cancel()) {
|
2018-07-16 06:50:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-10-31 07:16:15 +08:00
|
|
|
const wcstring pw_name_str = str2wcstring(pw->pw_name);
|
|
|
|
const wchar_t *pw_name = pw_name_str.c_str();
|
2019-03-13 05:06:01 +08:00
|
|
|
if (std::wcsncmp(user_name, pw_name, name_len) == 0) {
|
2016-10-31 07:16:15 +08:00
|
|
|
wcstring desc = format_string(COMPLETE_USER_DESC, pw_name);
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append a user name
|
2020-01-04 07:53:31 +08:00
|
|
|
append_completion(&this->completions, &pw_name[name_len], std::move(desc),
|
|
|
|
COMPLETE_NO_SPACE);
|
2016-10-31 07:16:15 +08:00
|
|
|
result = true;
|
|
|
|
} else if (wcsncasecmp(user_name, pw_name, name_len) == 0) {
|
|
|
|
wcstring name = format_string(L"~%ls", pw_name);
|
|
|
|
wcstring desc = format_string(COMPLETE_USER_DESC, pw_name);
|
2018-04-15 12:55:35 +08:00
|
|
|
// Append a user name
|
2020-01-04 07:53:31 +08:00
|
|
|
append_completion(&this->completions, std::move(name), std::move(desc),
|
2016-12-04 12:12:53 +08:00
|
|
|
COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE | COMPLETE_NO_SPACE);
|
2016-10-31 07:16:15 +08:00
|
|
|
result = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2017-05-11 13:07:01 +08:00
|
|
|
|
|
|
|
// If we've spent too much time (more than 200 ms) doing this give up.
|
|
|
|
if (timef() - start_time > 0.2) break;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2017-05-11 13:07:01 +08:00
|
|
|
|
2016-10-31 07:16:15 +08:00
|
|
|
endpwent();
|
|
|
|
return result;
|
2016-10-16 08:20:53 +08:00
|
|
|
#endif
|
2009-02-03 06:46:45 +08:00
|
|
|
}
|
2007-02-09 17:33:50 +08:00
|
|
|
|
2019-09-22 04:38:59 +08:00
|
|
|
// The callback type for walk_wrap_chain.
|
2018-02-27 11:21:46 +08:00
|
|
|
using wrap_chain_visitor_t = std::function<void(const wcstring &, const wcstring &, size_t depth)>;
|
|
|
|
|
2019-09-22 04:38:59 +08:00
|
|
|
// A set tracking which (command, wrap) pairs we have seen.
|
|
|
|
using wrap_chain_visited_set_t = std::set<std::pair<wcstring, wcstring>>;
|
|
|
|
|
|
|
|
// Recursive implementation of walk_wrap_chain().
|
|
|
|
static void walk_wrap_chain_recursive(const wcstring &command_line, source_range_t command_range,
|
|
|
|
const wrap_chain_visitor_t &visitor,
|
2020-01-16 09:14:47 +08:00
|
|
|
const cancel_checker_t &cancel_checker,
|
2019-09-22 04:38:59 +08:00
|
|
|
wrap_chain_visited_set_t *visited, size_t depth) {
|
2018-02-27 11:21:46 +08:00
|
|
|
// Limit our recursion depth. This prevents cycles in the wrap chain graph from overflowing.
|
|
|
|
if (depth > 24) return;
|
2020-01-17 07:11:24 +08:00
|
|
|
if (cancel_checker()) return;
|
2018-02-27 11:21:46 +08:00
|
|
|
|
|
|
|
// Extract command from the command line and invoke the receiver with it.
|
|
|
|
wcstring command(command_line, command_range.start, command_range.length);
|
|
|
|
visitor(command, command_line, depth);
|
|
|
|
|
|
|
|
wcstring_list_t targets = complete_get_wrap_targets(command);
|
|
|
|
for (const wcstring &wt : targets) {
|
|
|
|
// Construct a fake command line containing the wrap target.
|
|
|
|
wcstring faux_commandline = command_line;
|
|
|
|
faux_commandline.replace(command_range.start, command_range.length, wt);
|
|
|
|
|
|
|
|
// Try to extract the command from the faux commandline.
|
|
|
|
// We do this by simply getting the first token. This is a hack; for example one might
|
|
|
|
// imagine the first token being 'builtin' or similar. Nevertheless that is simpler than
|
|
|
|
// re-parsing everything.
|
|
|
|
wcstring wrapped_command = tok_first(wt);
|
|
|
|
if (!wrapped_command.empty()) {
|
|
|
|
size_t where = faux_commandline.find(wrapped_command, command_range.start);
|
|
|
|
if (where != wcstring::npos) {
|
2019-09-22 04:38:59 +08:00
|
|
|
// Do not recurse if we have already seen this.
|
|
|
|
if (visited->insert({command, wrapped_command}).second) {
|
|
|
|
// Recurse with our new command and command line.
|
|
|
|
source_range_t faux_source_range{uint32_t(where),
|
|
|
|
uint32_t(wrapped_command.size())};
|
2020-01-16 09:14:47 +08:00
|
|
|
walk_wrap_chain_recursive(faux_commandline, faux_source_range, visitor,
|
|
|
|
cancel_checker, visited, depth + 1);
|
2019-09-22 04:38:59 +08:00
|
|
|
}
|
2018-02-27 11:21:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 04:38:59 +08:00
|
|
|
// Helper to complete a parameter for a command and its transitive wrap chain.
|
|
|
|
// Given a command line \p command_line and the range of the command itself within the command line
|
|
|
|
// as \p command_range, invoke the \p receiver with the command and the command line. Then, for each
|
|
|
|
// target wrapped by the given command, update the command line with that target and invoke this
|
|
|
|
// recursively.
|
|
|
|
static void walk_wrap_chain(const wcstring &command_line, source_range_t command_range,
|
2020-01-16 09:14:47 +08:00
|
|
|
const wrap_chain_visitor_t &visitor,
|
|
|
|
const cancel_checker_t &cancel_checker) {
|
2019-09-22 04:38:59 +08:00
|
|
|
wrap_chain_visited_set_t visited;
|
2020-01-16 09:14:47 +08:00
|
|
|
walk_wrap_chain_recursive(command_line, command_range, visitor, cancel_checker, &visited, 0);
|
2019-09-22 04:38:59 +08:00
|
|
|
}
|
|
|
|
|
2019-08-24 23:23:39 +08:00
|
|
|
/// If the argument contains a '[' typed by the user, completion by appending to the argument might
|
|
|
|
/// produce an invalid token (#5831).
|
|
|
|
///
|
|
|
|
/// Check if there is any unescaped, unquoted '['; if yes, make the completions replace the entire
|
|
|
|
/// argument instead of appending, so '[' will be escaped.
|
|
|
|
void completer_t::escape_opening_brackets(const wcstring &argument) {
|
|
|
|
bool have_unquoted_unescaped_bracket = false;
|
|
|
|
wchar_t quote = L'\0';
|
|
|
|
bool escaped = false;
|
|
|
|
for (wchar_t c : argument) {
|
|
|
|
have_unquoted_unescaped_bracket |= (c == L'[') && !quote && !escaped;
|
2019-09-09 06:39:50 +08:00
|
|
|
if (escaped) {
|
|
|
|
escaped = false;
|
|
|
|
} else if (c == L'\\') {
|
|
|
|
escaped = true;
|
|
|
|
} else if (c == L'\'' || c == L'"') {
|
|
|
|
if (quote == c) {
|
|
|
|
// Closing a quote.
|
|
|
|
quote = L'\0';
|
|
|
|
} else if (quote == L'\0') {
|
|
|
|
// Opening a quote.
|
|
|
|
quote = c;
|
|
|
|
}
|
2019-08-24 23:23:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!have_unquoted_unescaped_bracket) return;
|
|
|
|
// Since completion_apply_to_command_line will escape the completion, we need to provide an
|
|
|
|
// unescaped version.
|
|
|
|
wcstring unescaped_argument;
|
|
|
|
if (!unescape_string(argument, &unescaped_argument, UNESCAPE_INCOMPLETE)) return;
|
|
|
|
for (completion_t &comp : completions) {
|
|
|
|
if (comp.flags & COMPLETE_REPLACES_TOKEN) continue;
|
|
|
|
comp.flags |= COMPLETE_REPLACES_TOKEN;
|
|
|
|
if (comp.flags & COMPLETE_DONT_ESCAPE) {
|
|
|
|
// If the completion won't be escaped, we need to do it here.
|
|
|
|
// Currently, this will probably never happen since COMPLETE_DONT_ESCAPE
|
|
|
|
// is only set for user or variable names which cannot contain '['.
|
|
|
|
unescaped_argument = escape_string(unescaped_argument, ESCAPE_ALL);
|
|
|
|
}
|
|
|
|
comp.completion = unescaped_argument + comp.completion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 17:01:19 +08:00
|
|
|
/// Set the DUPLICATES_ARG flag in any completion that duplicates an argument.
|
|
|
|
void completer_t::mark_completions_duplicating_arguments(const wcstring &prefix,
|
2019-10-29 20:32:26 +08:00
|
|
|
const std::vector<tok_t> &args) {
|
2018-08-07 17:01:19 +08:00
|
|
|
// Get all the arguments, unescaped, into an array that we're going to bsearch.
|
|
|
|
wcstring_list_t arg_strs;
|
|
|
|
for (const auto &arg : args) {
|
|
|
|
wcstring argstr = arg.get_source(cmd);
|
|
|
|
wcstring argstr_unesc;
|
|
|
|
if (unescape_string(argstr, &argstr_unesc, UNESCAPE_DEFAULT)) {
|
|
|
|
arg_strs.push_back(std::move(argstr_unesc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(arg_strs.begin(), arg_strs.end());
|
|
|
|
|
|
|
|
wcstring comp_str;
|
|
|
|
for (completion_t &comp : completions) {
|
|
|
|
comp_str = comp.completion;
|
|
|
|
if (!(comp.flags & COMPLETE_REPLACES_TOKEN)) {
|
|
|
|
comp_str.insert(0, prefix);
|
|
|
|
}
|
|
|
|
if (std::binary_search(arg_strs.begin(), arg_strs.end(), comp_str)) {
|
|
|
|
comp.flags |= COMPLETE_DUPLICATES_ARGUMENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
void completer_t::perform() {
|
|
|
|
const size_t cursor_pos = cmd.size();
|
|
|
|
|
|
|
|
// Find the plain statement to operate on. The cursor may be past it (#1261), so backtrack
|
|
|
|
// until we know we're no longer in a space. But the space may actually be part of the
|
|
|
|
// argument (#2477).
|
|
|
|
size_t position_in_statement = cursor_pos;
|
|
|
|
while (position_in_statement > 0 && cmd.at(position_in_statement - 1) == L' ') {
|
|
|
|
position_in_statement--;
|
2018-08-07 15:16:59 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Get all the arguments.
|
|
|
|
std::vector<tok_t> tokens;
|
|
|
|
parse_util_process_extent(cmd.c_str(), position_in_statement, nullptr, nullptr, &tokens);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Hack: fix autosuggestion by removing prefixing "and"s #6249.
|
|
|
|
if (flags & completion_request_t::autosuggestion) {
|
2019-12-20 16:20:50 +08:00
|
|
|
while (!tokens.empty() && parser_keywords_is_subcommand(tokens.front().get_source(cmd)))
|
2019-10-29 20:32:26 +08:00
|
|
|
tokens.erase(tokens.begin());
|
|
|
|
}
|
|
|
|
// Empty process (cursor is after one of ;, &, |, \n, &&, || modulo whitespace).
|
|
|
|
if (tokens.empty()) {
|
|
|
|
// Don't autosuggest anything based on the empty string (generalizes #1631).
|
|
|
|
if (flags & completion_request_t::autosuggestion) return;
|
|
|
|
|
2019-11-04 06:55:54 +08:00
|
|
|
complete_cmd(L"");
|
|
|
|
complete_abbr(L"");
|
2019-10-29 20:32:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
const tok_t &cmd_tok = tokens.front();
|
|
|
|
const tok_t &cur_tok = tokens.back();
|
|
|
|
// Since fish does not currently support redirect in command position, we return here.
|
|
|
|
if (cmd_tok.type != token_type_t::string) return;
|
|
|
|
if (cur_tok.type == token_type_t::error) return;
|
|
|
|
for (const auto &tok : tokens) { // If there was an error, it was in the last token.
|
|
|
|
assert(tok.type == token_type_t::string || tok.type == token_type_t::redirect);
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
// If we are completing a variable name or a tilde expansion user name, we do that and return.
|
|
|
|
// No need for any other completions.
|
2019-10-29 20:32:26 +08:00
|
|
|
const wcstring current_token = cur_tok.get_source(cmd);
|
2020-01-19 03:20:22 +08:00
|
|
|
if (cur_tok.location_in_or_at_end_of_source_range(cursor_pos)) {
|
|
|
|
if (try_complete_variable(current_token) || try_complete_user(current_token)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
if (cmd_tok.location_in_or_at_end_of_source_range(cursor_pos)) {
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
|
|
|
maybe_t<size_t> equal_sign_pos = variable_assignment_equals_pos(current_token);
|
|
|
|
if (equal_sign_pos) {
|
|
|
|
complete_param_expand(current_token.substr(*equal_sign_pos + 1), true /* do_file */);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
// Complete command filename.
|
|
|
|
complete_cmd(current_token);
|
|
|
|
complete_abbr(current_token);
|
|
|
|
return;
|
2018-08-07 15:16:59 +08:00
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
// See whether we are in an argument, in a redirection or in the whitespace in between.
|
|
|
|
bool in_redirection = cur_tok.type == token_type_t::redirect;
|
|
|
|
|
|
|
|
bool had_ddash = false;
|
|
|
|
wcstring current_argument, previous_argument;
|
|
|
|
if (cur_tok.type == token_type_t::string &&
|
|
|
|
cur_tok.location_in_or_at_end_of_source_range(position_in_statement)) {
|
|
|
|
// If the cursor is in whitespace, then the "current" argument is empty and the
|
|
|
|
// previous argument is the matching one. But if the cursor was in or at the end
|
|
|
|
// of the argument, then the current argument is the matching one, and the
|
|
|
|
// previous argument is the one before it.
|
|
|
|
bool cursor_in_whitespace = !cur_tok.location_in_or_at_end_of_source_range(cursor_pos);
|
|
|
|
if (cursor_in_whitespace) {
|
|
|
|
current_argument.clear();
|
|
|
|
previous_argument = current_token;
|
|
|
|
} else {
|
|
|
|
current_argument = current_token;
|
|
|
|
if (tokens.size() >= 2) {
|
|
|
|
tok_t prev_tok = tokens.at(tokens.size() - 2);
|
2019-11-10 08:07:33 +08:00
|
|
|
if (prev_tok.type == token_type_t::string)
|
|
|
|
previous_argument = prev_tok.get_source(cmd);
|
2019-12-16 19:37:41 +08:00
|
|
|
in_redirection = prev_tok.type == token_type_t::redirect;
|
2014-08-25 05:27:58 +08:00
|
|
|
}
|
2018-08-07 15:16:59 +08:00
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
|
|
|
|
// Check to see if we have a preceding double-dash.
|
|
|
|
for (size_t i = 0; i < tokens.size() - 1; i++) {
|
|
|
|
if (tokens.at(i).get_source(cmd) == L"--") {
|
|
|
|
had_ddash = true;
|
2018-08-07 15:16:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
bool do_file = false, handle_as_special_cd = false;
|
|
|
|
if (in_redirection) {
|
|
|
|
do_file = true;
|
|
|
|
} else {
|
|
|
|
// Try completing as an argument.
|
|
|
|
wcstring current_command = cmd_tok.get_source(cmd), current_command_unescape,
|
|
|
|
previous_argument_unescape, current_argument_unescape;
|
|
|
|
if (unescape_string(current_command, ¤t_command_unescape, UNESCAPE_DEFAULT) &&
|
|
|
|
unescape_string(previous_argument, &previous_argument_unescape, UNESCAPE_DEFAULT) &&
|
|
|
|
unescape_string(current_argument, ¤t_argument_unescape, UNESCAPE_INCOMPLETE)) {
|
|
|
|
// Have to walk over the command and its entire wrap chain. If any command
|
|
|
|
// disables do_file, then they all do.
|
|
|
|
do_file = true;
|
|
|
|
auto receiver = [&](const wcstring &cmd, const wcstring &cmdline, size_t depth) {
|
|
|
|
// Perhaps set a transient commandline so that custom completions
|
|
|
|
// buitin_commandline will refer to the wrapped command. But not if
|
|
|
|
// we're doing autosuggestions.
|
|
|
|
bool wants_transient = depth > 0 && !(flags & completion_request_t::autosuggestion);
|
|
|
|
if (wants_transient) {
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx.parser->libdata().transient_commandlines.push_back(cmdline);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2020-01-17 21:14:06 +08:00
|
|
|
maybe_t<size_t> equals_pos = variable_assignment_equals_pos(cmd);
|
|
|
|
bool is_variable_assignment = bool(equals_pos);
|
2020-01-16 09:14:47 +08:00
|
|
|
if (is_variable_assignment && ctx.parser) {
|
2020-01-17 21:14:06 +08:00
|
|
|
// clone of parse_execution_context_t::apply_variable_assignments
|
|
|
|
// but this is not smart enough to report correct error locations, so we ignore
|
|
|
|
// errors and this create one scope for each assignment instead of just one;
|
|
|
|
// that should hardly matter
|
2020-01-18 00:27:18 +08:00
|
|
|
const block_t *block =
|
2020-01-17 21:14:06 +08:00
|
|
|
ctx.parser->push_block(block_t::variable_assignment_block());
|
|
|
|
const wcstring variable_name = cmd.substr(0, *equals_pos);
|
|
|
|
const wcstring expression = cmd.substr(*equals_pos + 1);
|
|
|
|
completion_list_t expression_expanded;
|
|
|
|
auto expand_ret = expand_string(expression, &expression_expanded,
|
|
|
|
expand_flag::no_descriptions, ctx);
|
|
|
|
wcstring_list_t vals;
|
2020-01-24 09:34:46 +08:00
|
|
|
if (expand_ret == expand_result_t::ok) {
|
2020-01-17 21:14:06 +08:00
|
|
|
for (auto &completion : expression_expanded)
|
|
|
|
vals.emplace_back(std::move(completion.completion));
|
|
|
|
ctx.parser->vars().set(variable_name, ENV_LOCAL | ENV_EXPORT,
|
|
|
|
std::move(vals));
|
|
|
|
}
|
|
|
|
cleanup_t scope([&] {
|
|
|
|
if (block) ctx.parser->pop_block(block);
|
|
|
|
});
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
|
|
|
// To avoid issues like #2705 we complete commands starting with variable
|
|
|
|
// assignments by recursively calling complete for the command suffix
|
|
|
|
// without the first variable assignment token.
|
|
|
|
wcstring unaliased_cmd;
|
2020-01-16 09:14:47 +08:00
|
|
|
if (ctx.parser->libdata().transient_commandlines.empty()) {
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
|
|
|
unaliased_cmd = cmdline;
|
|
|
|
} else {
|
2020-01-16 09:14:47 +08:00
|
|
|
unaliased_cmd = ctx.parser->libdata().transient_commandlines.back();
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
|
|
|
}
|
|
|
|
tokenizer_t tok(unaliased_cmd.c_str(), TOK_ACCEPT_UNFINISHED);
|
|
|
|
maybe_t<tok_t> cmd_tok = tok.next();
|
|
|
|
assert(cmd_tok);
|
2019-11-30 15:57:19 +08:00
|
|
|
unaliased_cmd =
|
|
|
|
unaliased_cmd.replace(0, cmd_tok->offset + cmd_tok->length, L"");
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx.parser->libdata().transient_commandlines.push_back(unaliased_cmd);
|
2019-11-30 15:57:19 +08:00
|
|
|
cleanup_t remove_transient(
|
2020-01-16 09:14:47 +08:00
|
|
|
[&] { ctx.parser->libdata().transient_commandlines.pop_back(); });
|
|
|
|
vec_append(this->completions,
|
|
|
|
complete(unaliased_cmd, completion_request_t::fuzzy_match, ctx));
|
Support FOO=bar syntax for passing variables to individual commands
This adds initial support for statements with prefixed variable assignments.
Statments like this are supported:
a=1 b=$a echo $b # outputs 1
Just like in other shells, the left-hand side of each assignment must
be a valid variable identifier (no quoting/escaping). Array indexing
(PATH[1]=/bin ls $PATH) is *not* yet supported, but can be added fairly
easily.
The right hand side may be any valid string token, like a command
substitution, or a brace expansion.
Since `a=* foo` is equivalent to `begin set -lx a *; foo; end`,
the assignment, like `set`, uses nullglob behavior, e.g. below command
can safely be used to check if a directory is empty.
x=/nothing/{,.}* test (count $x) -eq 0
Generic file completion is done after the equal sign, so for example
pressing tab after something like `HOME=/` completes files in the
root directory
Subcommand completion works, so something like
`GIT_DIR=repo.git and command git ` correctly calls git completions
(but the git completion does not use the variable as of now).
The variable assignment is highlighted like an argument.
Closes #6048
2019-10-23 09:13:29 +08:00
|
|
|
do_file = false;
|
2019-11-30 15:57:19 +08:00
|
|
|
} else if (!complete_param(
|
|
|
|
cmd, previous_argument_unescape, current_argument_unescape,
|
|
|
|
!had_ddash)) { // Invoke any custom completions for this command.
|
2019-10-29 20:32:26 +08:00
|
|
|
do_file = false;
|
2014-05-02 16:22:39 +08:00
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
if (wants_transient) {
|
2020-01-16 09:14:47 +08:00
|
|
|
ctx.parser->libdata().transient_commandlines.pop_back();
|
2018-08-07 15:16:59 +08:00
|
|
|
}
|
2019-10-29 20:32:26 +08:00
|
|
|
};
|
|
|
|
assert(cmd_tok.offset < std::numeric_limits<uint32_t>::max());
|
|
|
|
assert(cmd_tok.length < std::numeric_limits<uint32_t>::max());
|
|
|
|
source_range_t range = {static_cast<uint32_t>(cmd_tok.offset),
|
|
|
|
static_cast<uint32_t>(cmd_tok.length)};
|
2020-01-16 09:14:47 +08:00
|
|
|
walk_wrap_chain(cmd, range, receiver, ctx.cancel_checker);
|
2019-10-29 20:32:26 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Hack. If we're cd, handle it specially (issue #1059, others).
|
|
|
|
handle_as_special_cd = (current_command_unescape == L"cd");
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// And if we're autosuggesting, and the token is empty, don't do file suggestions.
|
|
|
|
if ((flags & completion_request_t::autosuggestion) && current_argument_unescape.empty()) {
|
|
|
|
do_file = false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 15:16:59 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// This function wants the unescaped string.
|
|
|
|
complete_param_expand(current_argument, do_file, handle_as_special_cd);
|
2018-08-07 17:01:19 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Escape '[' in the argument before completing it.
|
|
|
|
escape_opening_brackets(current_argument);
|
2019-08-24 23:23:39 +08:00
|
|
|
|
2019-10-29 20:32:26 +08:00
|
|
|
// Lastly mark any completions that appear to already be present in arguments.
|
|
|
|
mark_completions_duplicating_arguments(current_token, tokens);
|
2018-08-07 15:16:59 +08:00
|
|
|
}
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2020-01-16 08:41:30 +08:00
|
|
|
completion_list_t complete(const wcstring &cmd_with_subcmds, completion_request_flags_t flags,
|
2020-01-16 09:14:47 +08:00
|
|
|
const operation_context_t &ctx) {
|
2018-08-07 15:16:59 +08:00
|
|
|
// Determine the innermost subcommand.
|
|
|
|
const wchar_t *cmdsubst_begin, *cmdsubst_end;
|
|
|
|
parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin,
|
|
|
|
&cmdsubst_end);
|
2019-11-19 10:34:50 +08:00
|
|
|
assert(cmdsubst_begin != nullptr && cmdsubst_end != nullptr && cmdsubst_end >= cmdsubst_begin);
|
2018-08-07 15:16:59 +08:00
|
|
|
wcstring cmd = wcstring(cmdsubst_begin, cmdsubst_end - cmdsubst_begin);
|
2020-01-16 09:14:47 +08:00
|
|
|
completer_t completer(ctx, std::move(cmd), flags);
|
2018-08-07 15:16:59 +08:00
|
|
|
completer.perform();
|
2020-01-16 08:41:30 +08:00
|
|
|
return completer.acquire_completions();
|
2012-02-25 10:43:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-19 16:46:56 +08:00
|
|
|
/// Print the short switch \c opt, and the argument \c arg to the specified
|
|
|
|
/// wcstring, but only if \c argument isn't an empty string.
|
2019-12-27 13:54:21 +08:00
|
|
|
static void append_switch(wcstring &out, wchar_t opt, const wcstring &arg) {
|
2019-09-19 16:46:56 +08:00
|
|
|
if (arg.empty()) return;
|
|
|
|
append_format(out, L" -%lc %ls", opt, escape_string(arg, ESCAPE_ALL).c_str());
|
|
|
|
}
|
2019-12-27 13:54:21 +08:00
|
|
|
static void append_switch(wcstring &out, const wcstring &opt, const wcstring &arg) {
|
2019-09-19 19:21:24 +08:00
|
|
|
if (arg.empty()) return;
|
|
|
|
append_format(out, L" --%ls %ls", opt.c_str(), escape_string(arg, ESCAPE_ALL).c_str());
|
|
|
|
}
|
2019-09-22 04:07:19 +08:00
|
|
|
static void append_switch(wcstring &out, wchar_t opt) { append_format(out, L" -%lc", opt); }
|
2019-12-27 13:54:21 +08:00
|
|
|
static void append_switch(wcstring &out, const wcstring &opt) {
|
2019-09-19 19:21:24 +08:00
|
|
|
append_format(out, L" --%ls", opt.c_str());
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-09-19 16:46:56 +08:00
|
|
|
/// Use by the bare `complete`, loaded completions are printed out as commands
|
2016-05-04 07:23:30 +08:00
|
|
|
wcstring complete_print() {
|
2015-09-22 02:24:49 +08:00
|
|
|
wcstring out;
|
2020-01-04 07:53:31 +08:00
|
|
|
out.reserve(40); // just a guess
|
2018-10-06 00:05:48 +08:00
|
|
|
auto completion_set = s_completion_set.acquire();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Get a list of all completions in a vector, then sort it by order.
|
2018-05-12 13:11:40 +08:00
|
|
|
std::vector<std::reference_wrapper<const completion_entry_t>> all_completions;
|
2020-01-05 22:44:05 +08:00
|
|
|
// These should be "c"begin/end, but then gcc from ~~the dark ages~~ RHEL 7 would complain.
|
2020-01-17 07:14:21 +08:00
|
|
|
all_completions.insert(all_completions.begin(), completion_set->begin(), completion_set->end());
|
2012-04-12 09:25:37 +08:00
|
|
|
sort(all_completions.begin(), all_completions.end(), compare_completions_by_order);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
for (const completion_entry_t &e : all_completions) {
|
|
|
|
const option_list_t &options = e.get_options();
|
|
|
|
for (const complete_entry_opt_t &o : options) {
|
2019-09-19 16:46:56 +08:00
|
|
|
out.append(L"complete");
|
2019-09-19 17:01:33 +08:00
|
|
|
|
|
|
|
if (o.flags & COMPLETE_DONT_SORT) append_switch(out, L'k');
|
|
|
|
|
2019-04-26 05:21:06 +08:00
|
|
|
if (o.result_mode.no_files && o.result_mode.requires_param) {
|
2019-09-19 19:21:24 +08:00
|
|
|
append_switch(out, L"exclusive");
|
2019-04-26 05:21:06 +08:00
|
|
|
} else if (o.result_mode.no_files) {
|
2019-09-19 19:21:24 +08:00
|
|
|
append_switch(out, L"no-files");
|
2019-05-31 01:12:49 +08:00
|
|
|
} else if (o.result_mode.force_files) {
|
2019-09-19 19:21:24 +08:00
|
|
|
append_switch(out, L"force-files");
|
2019-04-26 05:21:06 +08:00
|
|
|
} else if (o.result_mode.requires_param) {
|
2019-09-19 19:21:24 +08:00
|
|
|
append_switch(out, L"requires-param");
|
2019-04-26 05:21:06 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-09-19 16:46:56 +08:00
|
|
|
append_switch(out, e.cmd_is_path ? L'p' : L'c', e.cmd);
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
switch (o.type) {
|
2016-05-04 07:23:30 +08:00
|
|
|
case option_type_args_only: {
|
2016-01-17 07:46:43 +08:00
|
|
|
break;
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
|
|
|
case option_type_short: {
|
2019-09-19 16:46:56 +08:00
|
|
|
append_switch(out, L's', wcstring(1, o.option.at(0)));
|
2016-01-17 07:46:43 +08:00
|
|
|
break;
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
2016-01-17 07:46:43 +08:00
|
|
|
case option_type_single_long:
|
2016-05-04 07:23:30 +08:00
|
|
|
case option_type_double_long: {
|
2019-09-19 16:46:56 +08:00
|
|
|
append_switch(out, o.type == option_type_single_long ? L'o' : L'l', o.option);
|
2016-01-17 07:46:43 +08:00
|
|
|
break;
|
2016-05-04 07:23:30 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-09-19 16:46:56 +08:00
|
|
|
append_switch(out, L'd', C_(o.desc));
|
|
|
|
append_switch(out, L'a', o.comp);
|
|
|
|
append_switch(out, L'n', o.condition);
|
2012-11-19 08:30:30 +08:00
|
|
|
out.append(L"\n");
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2019-02-04 08:06:10 +08:00
|
|
|
// Append wraps.
|
|
|
|
auto locked_wrappers = wrapper_map.acquire();
|
|
|
|
for (const auto &entry : *locked_wrappers) {
|
|
|
|
const wcstring &src = entry.first;
|
|
|
|
for (const wcstring &target : entry.second) {
|
2019-09-19 19:21:24 +08:00
|
|
|
out.append(L"complete");
|
|
|
|
append_switch(out, L'c', src);
|
|
|
|
append_switch(out, L"wraps", target);
|
|
|
|
out.append(L"\n");
|
2019-02-04 08:06:10 +08:00
|
|
|
}
|
2014-08-16 09:14:36 +08:00
|
|
|
}
|
2015-09-22 02:24:49 +08:00
|
|
|
return out;
|
2014-08-16 09:14:36 +08:00
|
|
|
}
|
|
|
|
|
2019-04-28 04:05:12 +08:00
|
|
|
void complete_invalidate_path() {
|
|
|
|
// TODO: here we unload all completions for commands that are loaded by the autoloader. We also
|
|
|
|
// unload any completions that the user may specified on the command line. We should in
|
|
|
|
// principle track those completions loaded by the autoloader alone.
|
|
|
|
wcstring_list_t cmds = completion_autoloader.acquire()->get_autoloaded_commands();
|
|
|
|
for (const wcstring &cmd : cmds) {
|
|
|
|
complete_remove_all(cmd, false /* not a path */);
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 13:58:02 +08:00
|
|
|
|
2017-04-12 13:11:45 +08:00
|
|
|
/// Add a new target that wraps a command. Example: __fish_XYZ (function) wraps XYZ (target).
|
2016-05-04 07:23:30 +08:00
|
|
|
bool complete_add_wrapper(const wcstring &command, const wcstring &new_target) {
|
|
|
|
if (command.empty() || new_target.empty()) {
|
2014-08-16 09:14:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2019-01-17 16:49:50 +08:00
|
|
|
// If the command and the target are the same,
|
|
|
|
// there's no point in following the wrap-chain because we'd only complete the same thing.
|
|
|
|
// TODO: This should maybe include full cycle detection.
|
|
|
|
if (command == new_target) return false;
|
|
|
|
|
2018-09-29 12:20:50 +08:00
|
|
|
auto locked_map = wrapper_map.acquire();
|
|
|
|
wrapper_map_t &wraps = *locked_map;
|
2014-08-16 09:14:36 +08:00
|
|
|
wcstring_list_t *targets = &wraps[command];
|
2016-05-04 07:23:30 +08:00
|
|
|
// If it's already present, we do nothing.
|
2018-08-12 10:55:06 +08:00
|
|
|
if (!contains(*targets, new_target)) {
|
2014-08-16 09:14:36 +08:00
|
|
|
targets->push_back(new_target);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_remove) {
|
|
|
|
if (command.empty() || target_to_remove.empty()) {
|
2014-08-16 09:14:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-04 07:23:30 +08:00
|
|
|
|
2018-09-29 12:20:50 +08:00
|
|
|
auto locked_map = wrapper_map.acquire();
|
|
|
|
wrapper_map_t &wraps = *locked_map;
|
2014-08-16 09:14:36 +08:00
|
|
|
bool result = false;
|
|
|
|
wrapper_map_t::iterator current_targets_iter = wraps.find(command);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (current_targets_iter != wraps.end()) {
|
2014-08-16 09:14:36 +08:00
|
|
|
wcstring_list_t *targets = ¤t_targets_iter->second;
|
2018-08-12 10:55:06 +08:00
|
|
|
auto where = std::find(targets->begin(), targets->end(), target_to_remove);
|
2016-05-04 07:23:30 +08:00
|
|
|
if (where != targets->end()) {
|
2014-08-16 09:14:36 +08:00
|
|
|
targets->erase(where);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-27 11:21:46 +08:00
|
|
|
wcstring_list_t complete_get_wrap_targets(const wcstring &command) {
|
2016-05-04 07:23:30 +08:00
|
|
|
if (command.empty()) {
|
2018-02-27 11:21:46 +08:00
|
|
|
return {};
|
2014-08-16 09:14:36 +08:00
|
|
|
}
|
2018-09-29 12:20:50 +08:00
|
|
|
auto locked_map = wrapper_map.acquire();
|
|
|
|
wrapper_map_t &wraps = *locked_map;
|
2018-02-27 11:21:46 +08:00
|
|
|
auto iter = wraps.find(command);
|
|
|
|
if (iter == wraps.end()) return {};
|
|
|
|
return iter->second;
|
2014-08-16 09:14:36 +08:00
|
|
|
}
|