From c89c72f431cd1753cec18fd8febb6a55e2ac00d7 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 27 Sep 2020 16:49:12 -0700 Subject: [PATCH] Invert sense of expand_flag::no_descriptions When expanding a string, you may or may not want to generate descriptions alongside the expanded string. Usually you don't want to but descriptions were opt out. This commit makes them opt in. --- src/builtin_complete.cpp | 4 +++- src/complete.cpp | 15 +++++---------- src/expand.cpp | 7 ++----- src/expand.h | 4 ++-- src/parse_execution.cpp | 11 +++++------ src/wildcard.cpp | 12 +++++++----- 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp index bf1812d8c..64a8f4d3d 100644 --- a/src/builtin_complete.cpp +++ b/src/builtin_complete.cpp @@ -365,7 +365,9 @@ maybe_t builtin_complete(parser_t &parser, io_streams_t &streams, wchar_t * parser.libdata().builtin_complete_current_commandline = true; completion_list_t comp = - complete(do_complete_param, completion_request_t::fuzzy_match, parser.context()); + complete(do_complete_param, + {completion_request_t::fuzzy_match, completion_request_t::descriptions}, + parser.context()); for (const auto &next : comp) { // Make a fake commandline, and then apply the completion to it. diff --git a/src/complete.cpp b/src/complete.cpp index ab358c04e..929b9410c 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -366,14 +366,10 @@ class completer_t { const completion_list_t &possible_comp, complete_flags_t flags); 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. expand_flags_t result{}; if (this->type() == COMPLETE_AUTOSUGGEST) result |= expand_flag::skip_cmdsubst; - - // Allow fuzzy matching. if (this->fuzzy()) result |= expand_flag::fuzzy_match; - + if (this->wants_descriptions()) result |= expand_flag::gen_descriptions; return result; } @@ -786,7 +782,6 @@ void completer_t::complete_from_args(const wcstring &str, const wcstring &args, expand_flags_t eflags{}; if (is_autosuggest) { - eflags |= expand_flag::no_descriptions; eflags |= expand_flag::skip_cmdsubst; } @@ -1128,12 +1123,12 @@ void completer_t::complete_param_expand(const wcstring &str, bool do_file, if (this->type() == COMPLETE_AUTOSUGGEST) { flags |= expand_flag::special_for_cd_autosuggestion; } - flags |= expand_flags_t{expand_flag::directories_only, expand_flag::special_for_cd, - expand_flag::no_descriptions}; + flags |= expand_flag::directories_only; + flags |= expand_flag::special_for_cd; } // Squelch file descriptions per issue #254. - if (this->type() == COMPLETE_AUTOSUGGEST || do_file) flags |= expand_flag::no_descriptions; + if (this->type() == COMPLETE_AUTOSUGGEST || do_file) flags.clear(expand_flag::gen_descriptions); // We have the following cases: // @@ -1367,7 +1362,7 @@ const block_t *completer_t::apply_var_assignments(const custom_arg_data_t *ad) { // VAR=(launch_missiles) cmd // should not launch missiles. // Note we also do NOT send --on-variable events. - const expand_flags_t expand_flags{expand_flag::no_descriptions, expand_flag::skip_cmdsubst}; + const expand_flags_t expand_flags = expand_flag::skip_cmdsubst; const block_t *block = ctx.parser->push_block(block_t::variable_assignment_block()); for (const wcstring &var_assign : ad->var_assignments) { maybe_t equals_pos = variable_assignment_equals_pos(var_assign); diff --git a/src/expand.cpp b/src/expand.cpp index fac80f586..8c938ef01 100644 --- a/src/expand.cpp +++ b/src/expand.cpp @@ -1121,8 +1121,7 @@ bool expand_one(wcstring &string, expand_flags_t flags, const operation_context_ return true; } - if (expand_string(std::move(string), &completions, flags | expand_flag::no_descriptions, ctx, - errors) == expand_result_t::ok && + if (expand_string(std::move(string), &completions, flags, ctx, errors) == expand_result_t::ok && completions.size() == 1) { string = std::move(completions.at(0).completion); return true; @@ -1141,9 +1140,7 @@ expand_result_t expand_to_command_and_args(const wcstring &instr, const operatio completion_list_t completions; expand_result_t expand_err = expand_string( - instr, &completions, - {expand_flag::skip_cmdsubst, expand_flag::no_descriptions, expand_flag::skip_jobs}, ctx, - errors); + instr, &completions, {expand_flag::skip_cmdsubst, expand_flag::skip_jobs}, ctx, errors); if (expand_err == expand_result_t::ok) { // The first completion is the command, any remaning are arguments. bool first = true; diff --git a/src/expand.h b/src/expand.h index 0e09b61e6..893e524ff 100644 --- a/src/expand.h +++ b/src/expand.h @@ -38,8 +38,8 @@ enum class expand_flag { executables_only, /// Only match directories. directories_only, - /// Don't generate descriptions. - no_descriptions, + /// Generate descriptions, stored in the description field of completions. + gen_descriptions, /// Don't expand jobs (but still expand processes). skip_jobs, /// Don't expand home directories. diff --git a/src/parse_execution.cpp b/src/parse_execution.cpp index 3425b4ee3..ec03f4df5 100644 --- a/src/parse_execution.cpp +++ b/src/parse_execution.cpp @@ -507,8 +507,8 @@ end_execution_reason_t parse_execution_context_t::run_switch_statement( // Expand it. We need to offset any errors by the position of the string. completion_list_t switch_values_expanded; parse_error_list_t errors; - auto expand_ret = expand_string(switch_value, &switch_values_expanded, - expand_flag::no_descriptions, ctx, &errors); + auto expand_ret = + expand_string(switch_value, &switch_values_expanded, expand_flags_t{}, ctx, &errors); parse_error_offset_source_start(&errors, statement.argument.range.start); switch (expand_ret.result) { @@ -935,8 +935,7 @@ end_execution_reason_t parse_execution_context_t::expand_arguments_from_nodes( // Expand this string. parse_error_list_t errors; arg_expanded.clear(); - auto expand_ret = - expand_string(arg_str, &arg_expanded, expand_flag::no_descriptions, ctx, &errors); + auto expand_ret = expand_string(arg_str, &arg_expanded, expand_flags_t{}, ctx, &errors); parse_error_offset_source_start(&errors, arg_node->range.start); switch (expand_ret.result) { case expand_result_t::error: { @@ -1091,8 +1090,8 @@ end_execution_reason_t parse_execution_context_t::apply_variable_assignments( completion_list_t expression_expanded; parse_error_list_t errors; // TODO this is mostly copied from expand_arguments_from_nodes, maybe extract to function - auto expand_ret = expand_string(expression, &expression_expanded, - expand_flag::no_descriptions, ctx, &errors); + auto expand_ret = + expand_string(expression, &expression_expanded, expand_flags_t{}, ctx, &errors); parse_error_offset_source_start(&errors, variable_assignment.range.start + *equals_pos + 1); switch (expand_ret.result) { case expand_result_t::error: diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 7a76ccd52..9cb3aaf0b 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -172,8 +172,10 @@ static wcstring resolve_description(const wcstring &full_completion, wcstring *c completion->resize(complete_sep_loc); return description; } - if (expand_flags & expand_flag::no_descriptions) return {}; - return desc_func ? desc_func(full_completion) : wcstring{}; + if (desc_func && (expand_flags & expand_flag::gen_descriptions)) { + return desc_func(full_completion); + } + return wcstring{}; } // A transient parameter pack needed by wildcard_complete. @@ -513,7 +515,7 @@ static bool wildcard_test_flags_then_complete(const wcstring &filepath, const wc // Compute the description. wcstring desc; - if (!(expand_flags & expand_flag::no_descriptions)) { + if (expand_flags & expand_flag::gen_descriptions) { desc = file_get_desc(lstat_res, lstat_buf, stat_res, stat_buf, stat_errno); if (file_size >= 0) { @@ -988,11 +990,11 @@ wildcard_expand_result_t wildcard_expand_string(const wcstring &wc, assert(flags.get(expand_flag::for_completions) || !flags.get(expand_flag::fuzzy_match)); // expand_flag::special_for_cd requires expand_flag::directories_only and - // expand_flag::for_completions and expand_flag::no_descriptions. + // expand_flag::for_completions and !expand_flag::gen_descriptions. assert(!(flags.get(expand_flag::special_for_cd)) || ((flags.get(expand_flag::directories_only)) && (flags.get(expand_flag::for_completions)) && - (flags.get(expand_flag::no_descriptions)))); + (!flags.get(expand_flag::gen_descriptions)))); // Hackish fix for issue #1631. We are about to call c_str(), which will produce a string // truncated at any embedded nulls. We could fix this by passing around the size, etc. However