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.
This commit is contained in:
ridiculousfish 2019-04-14 15:38:58 -07:00
parent 64584a6624
commit 020d4a2848
4 changed files with 8 additions and 66 deletions

View File

@ -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<env_var_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_var_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);

View File

@ -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<env_var_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<wcstring, env_var_t> 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<env_var_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;

View File

@ -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;

View File

@ -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<autosuggestion_result_t(void)> 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<autosuggestion_result_t(void)> 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<autosuggestion_result_t(void)> get_autosuggestion_performer
// Try normal completions.
completion_request_flags_t complete_flags = COMPLETION_REQUEST_AUTOSUGGESTION;
std::vector<completion_t> 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<highlight_result_t(void)> 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<highlight_result_t(void)> get_highlight_performer(
}
s_thread_generation = generation_count;
std::vector<highlight_spec_t> 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};
};
}