Merge remote-tracking branch 'krobelus/prefer-input-order-over-alphabet'

This commit is contained in:
Maxime Coste 2023-12-12 21:27:31 +11:00
commit 533f51c744
8 changed files with 58 additions and 24 deletions

View File

@ -25,7 +25,7 @@ define-command -params ..1 \
cut -f 1 "$tags" | grep -v '^!' | uniq > "$namecache"
fi
cat "$namecache"
done} \
done | sort } \
-docstring %{
ctags-search [<symbol>]: jump to a symbol's definition
If no symbol is passed then the current selection is used as symbol name

View File

@ -189,7 +189,7 @@ complete-command doc shell-script-candidates %{
/^\[\[[^\]]+\]\]/ { sub(/^\[\[/, ""); sub(/\]\].*/, ""); print }
' < $page | tr '[A-Z ]' '[a-z-]'
fi;;
esac
esac | sort
}
alias global help doc

View File

@ -62,27 +62,49 @@ define-command -params 1.. \
Available commands:
add
apply (alias for "patch git apply")
rm
reset
blame
commit
checkout
commit
diff
edit
grep
hide-blame
hide-diff
init
log
next-hunk
prev-hunk
reset
rm
show
show-branch
show-diff
status
update-diff
grep
} -shell-script-candidates %{
if [ $kak_token_to_complete -eq 0 ]; then
printf "add\napply\nrm\nreset\nblame\ncommit\ncheckout\ndiff\nhide-blame\nhide-diff\nlog\nnext-hunk\nprev-hunk\nshow\nshow-branch\nshow-diff\ninit\nstatus\nupdate-diff\ngrep\nedit\n"
printf %s\\n \
apply \
blame \
checkout \
commit \
diff \
edit \
grep \
hide-blame \
hide-diff \
init \
log \
next-hunk \
prev-hunk \
reset \
rm \
show \
show-branch \
show-diff \
status \
update-diff \
;
else
case "$1" in
commit) printf -- "--amend\n--no-edit\n--all\n--reset-author\n--fixup\n--squash\n"; git ls-files -m ;;

View File

@ -63,7 +63,10 @@ define-command -hidden -params ..3 man-impl %{ evaluate-commands %sh{
define-command -params ..1 \
-shell-script-candidates %{
find /usr/share/man/ $(printf %s "${MANPATH}" | sed 's/:/ /') -name '*.[1-8]*' | sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),'
find /usr/share/man/ $(printf %s "${MANPATH}" |
sed 's/:/ /') -name '*.[1-8]*' |
sed 's,^.*/\(.*\)\.\([1-8][a-zA-Z]*\).*$,\1(\2),' |
sort
} \
-docstring %{
man [<page>]: manpage viewer wrapper

View File

@ -337,10 +337,13 @@ private:
{
UsedLetters query_letters = used_letters(query);
Vector<RankedMatch> matches;
for (auto&& candidate : m_candidates)
for (auto&& [i, candidate] : m_candidates | enumerate())
{
if (RankedMatch m{candidate.first, candidate.second, query, query_letters})
{
m.set_input_sequence_number(i);
matches.push_back(m);
}
}
constexpr size_t max_count = 100;

View File

@ -164,7 +164,7 @@ InsertCompletion complete_word(const SelectionList& sels,
constexpr size_t max_count = 100;
// Gather best max_count matches
InsertCompletion::CandidateList candidates;
candidates.reserve(std::min(matches.size(), max_count));
candidates.reserve(std::min(matches.size(), max_count) + 1);
for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
[&](RankedMatchAndBuffer& m) {
@ -298,10 +298,11 @@ InsertCompletion complete_option(const SelectionList& sels,
StringView query = buffer.substr(coord, cursor_pos);
Vector<RankedMatchAndInfo> matches;
for (auto& candidate : opt.list)
for (auto&& [i, candidate] : opt.list | enumerate())
{
if (RankedMatchAndInfo match{std::get<0>(candidate), query})
{
match.set_input_sequence_number(i);
match.on_select = std::get<1>(candidate);
auto& menu = std::get<2>(candidate);
match.menu_entry = not menu.empty() ?
@ -314,20 +315,19 @@ InsertCompletion complete_option(const SelectionList& sels,
constexpr size_t max_count = 100;
// Gather best max_count matches
auto greater = [](auto& lhs, auto& rhs) { return rhs < lhs; };
auto first = matches.begin(), last = matches.end();
std::make_heap(first, last, greater);
InsertCompletion::CandidateList candidates;
candidates.reserve(std::min(matches.size(), max_count));
candidates.reserve(matches.size());
while (candidates.size() < max_count and first != last)
{
if (candidates.empty() or candidates.back().completion != first->candidate()
or candidates.back().on_select != first->on_select)
candidates.push_back({ first->candidate().str(), first->on_select.str(),
std::move(first->menu_entry) });
std::pop_heap(first, last--, greater);
}
candidates.reserve(std::min(matches.size(), max_count) + 1);
for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
[&](RankedMatchAndInfo& m) {
if (not candidates.empty()
and candidates.back().completion == m.candidate()
and candidates.back().on_select == m.on_select)
return false;
candidates.push_back({ m.candidate().str(), m.on_select.str(),
std::move(m.menu_entry) });
return true;
});
auto end = cursor_pos;
if (match[3].matched)

View File

@ -209,6 +209,9 @@ bool RankedMatch::operator<(const RankedMatch& other) const
if (m_max_index != other.m_max_index)
return m_max_index < other.m_max_index;
if (m_input_sequence_number != other.m_input_sequence_number)
return m_input_sequence_number < other.m_input_sequence_number;
// Reorder codepoints to improve matching behaviour
auto order = [](Codepoint cp) { return cp == '/' ? 0 : cp; };

View File

@ -31,6 +31,8 @@ struct RankedMatch
explicit operator bool() const { return m_matches; }
void set_input_sequence_number(size_t i) { m_input_sequence_number = i; }
private:
template<typename TestFunc>
RankedMatch(StringView candidate, StringView query, TestFunc test);
@ -54,6 +56,7 @@ private:
Flags m_flags = Flags::None;
int m_word_boundary_match_count = 0;
int m_max_index = 0;
size_t m_input_sequence_number = 0;
};
}