Reorganize and improve commenting of autosuggest_validate_from_history

No behavior change expected here.
This commit is contained in:
ridiculousfish 2020-11-27 13:21:21 -08:00
parent 2a86099cfd
commit d9ebe13cb4
2 changed files with 21 additions and 24 deletions

View file

@ -434,9 +434,6 @@ bool autosuggest_validate_from_history(const history_item_t &item,
const operation_context_t &ctx) { const operation_context_t &ctx) {
ASSERT_IS_BACKGROUND_THREAD(); ASSERT_IS_BACKGROUND_THREAD();
bool handled = false;
bool suggestion_ok = false;
// Parse the string. // Parse the string.
wcstring parsed_command; wcstring parsed_command;
wcstring cd_dir; wcstring cd_dir;
@ -447,35 +444,35 @@ bool autosuggest_validate_from_history(const history_item_t &item,
return true; return true;
} }
// We handle cd specially.
if (parsed_command == L"cd" && !cd_dir.empty()) { if (parsed_command == L"cd" && !cd_dir.empty()) {
// We can possibly handle this specially.
if (expand_one(cd_dir, expand_flag::skip_cmdsubst, ctx)) { if (expand_one(cd_dir, expand_flag::skip_cmdsubst, ctx)) {
handled = true; if (string_prefixes_string(cd_dir, L"--help") ||
bool is_help = string_prefixes_string(cd_dir, L"-h")) {
string_prefixes_string(cd_dir, L"--help") || string_prefixes_string(cd_dir, L"-h"); // cd --help is always valid.
if (!is_help) { return true;
} else {
// Check the directory target, respecting CDPATH.
// Permit the autosuggestion if the path is valid and not our directory.
auto path = path_get_cdpath(cd_dir, working_directory, ctx.vars); auto path = path_get_cdpath(cd_dir, working_directory, ctx.vars);
if (path && !paths_are_same_file(working_directory, *path)) { return path && !paths_are_same_file(working_directory, *path);
suggestion_ok = true;
}
} }
} }
} }
if (handled) { // Not handled specially. Is the command valid?
return suggestion_ok;
}
// Not handled specially so handle it here.
bool cmd_ok = builtin_exists(parsed_command) || function_exists_no_autoload(parsed_command) || bool cmd_ok = builtin_exists(parsed_command) || function_exists_no_autoload(parsed_command) ||
path_get_path(parsed_command, nullptr, ctx.vars); path_get_path(parsed_command, nullptr, ctx.vars);
if (!cmd_ok) {
if (cmd_ok) { return false;
const path_list_t &paths = item.get_required_paths();
suggestion_ok = all_paths_are_valid(paths, working_directory);
} }
return suggestion_ok; // Did the historical command have arguments that look like paths, which aren't paths now?
if (!all_paths_are_valid(item.get_required_paths(), working_directory)) {
return false;
}
return true;
} }
// Highlights the variable starting with 'in', setting colors within the 'colors' array. Returns the // Highlights the variable starting with 'in', setting colors within the 'colors' array. Returns the

View file

@ -116,9 +116,9 @@ struct highlight_color_resolver_t {
const environment_t &vars) const; const environment_t &vars) const;
}; };
/// Given a command 'str' from the history, try to determine whether we ought to suggest it by /// Given an item \p item from the history which is a proposed autosuggestion, return whether the
/// specially recognizing the command. Returns true if we validated the command. If so, returns by /// autosuggestion is valid. It may not be valid if e.g. it is attempting to cd into a directory
/// reference whether the suggestion is valid or not. /// which does not exist.
bool autosuggest_validate_from_history(const history_item_t &item, bool autosuggest_validate_from_history(const history_item_t &item,
const wcstring &working_directory, const wcstring &working_directory,
const operation_context_t &ctx); const operation_context_t &ctx);