Skip to content

Commit

Permalink
Fix memory domain for cached command completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mawww committed Oct 29, 2022
1 parent cc66237 commit 0a21dc0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ void make_directory(StringView dir, mode_t mode)
}
}

template<typename Filter>
Vector<String> list_files(StringView dirname, Filter filter)
template<MemoryDomain domain = MemoryDomain::Undefined, typename Filter>
Vector<String, domain> list_files(StringView dirname, Filter filter)
{
char buffer[PATH_MAX+1];
format_to(buffer, "{}", dirname);
Expand All @@ -446,7 +446,7 @@ Vector<String> list_files(StringView dirname, Filter filter)

auto close_dir = on_scope_end([dir]{ closedir(dir); });

Vector<String> result;
Vector<String, domain> result;
while (dirent* entry = readdir(dir))
{
StringView filename = entry->d_name;
Expand All @@ -466,13 +466,19 @@ Vector<String> list_files(StringView dirname, Filter filter)
return result;
}

Vector<String> list_files(StringView directory)
template<MemoryDomain domain = MemoryDomain::Undefined>
Vector<String, domain> list_files(StringView directory)
{
return list_files(directory, [](const dirent& entry, const struct stat&) {
return StringView{entry.d_name}.substr(0_byte, 1_byte) != ".";
});
}

Vector<String> list_files(StringView directory)
{
return list_files<>(directory);
}

static CandidateList candidates(ConstArrayView<RankedMatch> matches, StringView dirname)
{
CandidateList res;
Expand Down Expand Up @@ -547,7 +553,7 @@ CandidateList complete_command(StringView prefix, ByteCount cursor_pos)
struct CommandCache
{
TimeSpec mtim = {};
Vector<String> commands;
Vector<String, MemoryDomain::Completion> commands;
};
static HashMap<String, CommandCache, MemoryDomain::Commands> command_cache;

Expand All @@ -570,7 +576,7 @@ CandidateList complete_command(StringView prefix, ByteCount cursor_pos)
return S_ISREG(st.st_mode) and executable;
};

cache.commands = list_files(dirname, filter);
cache.commands = list_files<MemoryDomain::Completion>(dirname, filter);
memcpy(&cache.mtim, &st.st_mtim, sizeof(TimeSpec));
}
for (auto& cmd : cache.commands)
Expand Down

0 comments on commit 0a21dc0

Please sign in to comment.