From 020d4a2848d6456d8c87195d6040fa44d24c728d Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 14 Apr 2019 15:38:58 -0700 Subject: [PATCH] Adopt env_scoped_t::snapshot() and remove env_var_snapshot_t Remove the env_var_snapshot_t class and switch everything to the new snapshot function of env_scoped_t. Fixes #5658. Fixes #5571. --- src/env.cpp | 34 ---------------------------------- src/env.h | 24 +----------------------- src/fish_tests.cpp | 2 +- src/reader.cpp | 14 ++++++-------- 4 files changed, 8 insertions(+), 66 deletions(-) diff --git a/src/env.cpp b/src/env.cpp index 35034c80c..d40f98c75 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -181,7 +181,6 @@ wcstring environment_t::get_pwd_slash() const { return pwd; } -null_environment_t::null_environment_t() = default; null_environment_t::~null_environment_t() = default; maybe_t null_environment_t::get(const wcstring &key, env_mode_flags_t mode) const { UNUSED(key); @@ -1140,39 +1139,6 @@ env_stack_t &env_stack_t::globals() { return s_global; } -env_vars_snapshot_t::env_vars_snapshot_t(const environment_t &source, const wchar_t *const *keys) { - ASSERT_IS_MAIN_THREAD(); - wcstring key; - for (size_t i = 0; keys[i]; i++) { - key.assign(keys[i]); - const auto var = source.get(key); - if (var) { - vars[key] = std::move(*var); - } - } - names = source.get_names(0); -} - -env_vars_snapshot_t::~env_vars_snapshot_t() = default; - -maybe_t env_vars_snapshot_t::get(const wcstring &key, env_mode_flags_t mode) const { - UNUSED(mode); - auto iter = vars.find(key); - if (iter == vars.end()) return none(); - return iter->second; -} - -wcstring_list_t env_vars_snapshot_t::get_names(int flags) const { - UNUSED(flags); - return names; -} - -const wchar_t *const env_vars_snapshot_t::highlighting_keys[] = { - L"PATH", L"CDPATH", L"fish_function_path", L"PWD", L"HOME", NULL}; - -const wchar_t *const env_vars_snapshot_t::completing_keys[] = { - L"PATH", L"CDPATH", L"fish_function_path", L"PWD", L"HOME", NULL}; - #if defined(__APPLE__) || defined(__CYGWIN__) static int check_runtime_path(const char *path) { UNUSED(path); diff --git a/src/env.h b/src/env.h index c43cd92b4..b61b03922 100644 --- a/src/env.h +++ b/src/env.h @@ -186,7 +186,7 @@ class environment_t { /// The null environment contains nothing. class null_environment_t : public environment_t { public: - null_environment_t(); + null_environment_t() = default; ~null_environment_t() override; maybe_t get(const wcstring &key, env_mode_flags_t mode = ENV_DEFAULT) const override; @@ -296,28 +296,6 @@ class env_stack_t final : public env_scoped_t { static env_stack_t &globals(); }; -class env_vars_snapshot_t : public environment_t { - std::map vars; - wcstring_list_t names; - - public: - env_vars_snapshot_t() = default; - env_vars_snapshot_t(const env_vars_snapshot_t &) = default; - env_vars_snapshot_t &operator=(const env_vars_snapshot_t &) = default; - env_vars_snapshot_t(const environment_t &source, const wchar_t *const *keys); - ~env_vars_snapshot_t() override; - - maybe_t get(const wcstring &key, env_mode_flags_t mode = ENV_DEFAULT) const override; - - wcstring_list_t get_names(int flags) const override; - - // Vars necessary for highlighting. - static const wchar_t *const highlighting_keys[]; - - // Vars necessary for completion. - static const wchar_t *const completing_keys[]; -}; - extern int g_fork_count; extern bool g_use_posix_spawn; diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 136a50680..5741b20b6 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2914,7 +2914,7 @@ static void test_autosuggest_suggest_special() { static void perform_one_autosuggestion_should_ignore_test(const wcstring &command, long line) { completion_list_t comps; - complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION, env_vars_snapshot_t{}); + complete(command, &comps, COMPLETION_REQUEST_AUTOSUGGESTION, null_environment_t{}); do_test(comps.empty()); if (!comps.empty()) { const wcstring &suggestion = comps.front().completion; diff --git a/src/reader.cpp b/src/reader.cpp index 90f2ad51e..d7a43da5d 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -1276,10 +1276,9 @@ void reader_data_t::completion_insert(const wchar_t *val, complete_flags_t flags // on a background thread) to determine the autosuggestion static std::function get_autosuggestion_performer( const wcstring &search_string, size_t cursor_pos, history_t *history) { - const auto &parser_vars = parser_t::principal_parser().vars(); const unsigned int generation_count = read_generation_count(); - const wcstring working_directory = parser_vars.get_pwd_slash(); - env_vars_snapshot_t vars(parser_vars, env_vars_snapshot_t::completing_keys); + auto vars = parser_t::principal_parser().vars().snapshot(); + const wcstring working_directory = vars->get_pwd_slash(); // TODO: suspicious use of 'history' here // This is safe because histories are immortal, but perhaps // this should use shared_ptr @@ -1306,7 +1305,7 @@ static std::function get_autosuggestion_performer // Skip items with newlines because they make terrible autosuggestions. if (item.str().find('\n') != wcstring::npos) continue; - if (autosuggest_validate_from_history(item, working_directory, vars)) { + if (autosuggest_validate_from_history(item, working_directory, *vars)) { // The command autosuggestion was handled specially, so we're done. return {searcher.current_string(), search_string}; } @@ -1328,7 +1327,7 @@ static std::function get_autosuggestion_performer // Try normal completions. completion_request_flags_t complete_flags = COMPLETION_REQUEST_AUTOSUGGESTION; std::vector completions; - complete(search_string, &completions, complete_flags, vars); + complete(search_string, &completions, complete_flags, *vars); completions_sort_and_prioritize(&completions, complete_flags); if (!completions.empty()) { const completion_t &comp = completions.at(0); @@ -2041,8 +2040,7 @@ void reader_data_t::highlight_complete(highlight_result_t result) { // return a function that performs highlighting. The function may be invoked on a background thread. static std::function get_highlight_performer( const wcstring &text, long match_highlight_pos, highlight_function_t highlight_func) { - env_vars_snapshot_t vars(parser_t::principal_parser().vars(), - env_vars_snapshot_t::highlighting_keys); + auto vars = parser_t::principal_parser().vars().snapshot(); unsigned int generation_count = read_generation_count(); return [=]() -> highlight_result_t { if (text.empty()) return {}; @@ -2052,7 +2050,7 @@ static std::function get_highlight_performer( } s_thread_generation = generation_count; std::vector colors(text.size(), highlight_spec_t{}); - highlight_func(text, colors, match_highlight_pos, NULL /* error */, vars); + highlight_func(text, colors, match_highlight_pos, NULL /* error */, *vars); return {std::move(colors), text}; }; }