2014-04-28 22:54:00 +02:00
|
|
|
#include "insert_completer.hh"
|
|
|
|
|
|
|
|
#include "buffer_manager.hh"
|
|
|
|
#include "buffer_utils.hh"
|
2016-02-27 18:23:13 +01:00
|
|
|
#include "client.hh"
|
2019-04-15 17:42:58 +02:00
|
|
|
#include "command_manager.hh"
|
2017-06-09 14:50:00 +02:00
|
|
|
#include "changes.hh"
|
2014-04-28 22:54:00 +02:00
|
|
|
#include "context.hh"
|
|
|
|
#include "display_buffer.hh"
|
2014-07-11 01:27:04 +02:00
|
|
|
#include "face_registry.hh"
|
2014-04-28 22:54:00 +02:00
|
|
|
#include "file.hh"
|
2014-10-13 14:12:33 +02:00
|
|
|
#include "regex.hh"
|
2014-04-28 22:54:00 +02:00
|
|
|
#include "window.hh"
|
|
|
|
#include "word_db.hh"
|
2018-05-26 02:01:26 +02:00
|
|
|
#include "option_types.hh"
|
2015-10-30 14:57:46 +01:00
|
|
|
#include "utf8_iterator.hh"
|
2016-11-29 20:53:11 +01:00
|
|
|
#include "user_interface.hh"
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2015-09-02 22:15:16 +02:00
|
|
|
#include <numeric>
|
2017-01-08 23:30:15 +01:00
|
|
|
#include <utility>
|
2015-09-02 21:59:45 +02:00
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-12 14:24:30 +01:00
|
|
|
using StringList = Vector<String, MemoryDomain::Options>;
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2014-08-19 19:56:11 +02:00
|
|
|
String option_to_string(const InsertCompleterDesc& opt)
|
|
|
|
{
|
|
|
|
switch (opt.mode)
|
|
|
|
{
|
|
|
|
case InsertCompleterDesc::Word:
|
|
|
|
return "word=" + (opt.param ? *opt.param : "");
|
|
|
|
case InsertCompleterDesc::Filename:
|
|
|
|
return "filename";
|
|
|
|
case InsertCompleterDesc::Option:
|
|
|
|
return "option=" + (opt.param ? *opt.param : "");
|
2017-10-02 18:45:11 +02:00
|
|
|
case InsertCompleterDesc::Line:
|
2017-10-26 13:04:28 +02:00
|
|
|
return "line=" + (opt.param ? *opt.param : "");
|
2014-08-19 19:56:11 +02:00
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-05-27 05:00:04 +02:00
|
|
|
InsertCompleterDesc option_from_string(Meta::Type<InsertCompleterDesc>, StringView str)
|
2014-08-19 19:56:11 +02:00
|
|
|
{
|
|
|
|
if (str.substr(0_byte, 7_byte) == "option=")
|
2018-05-27 05:00:04 +02:00
|
|
|
return {InsertCompleterDesc::Option, str.substr(7_byte).str()};
|
2014-08-19 19:56:11 +02:00
|
|
|
else if (str.substr(0_byte, 5_byte) == "word=")
|
|
|
|
{
|
|
|
|
auto param = str.substr(5_byte);
|
|
|
|
if (param == "all" or param == "buffer")
|
2018-05-27 05:00:04 +02:00
|
|
|
return {InsertCompleterDesc::Word, param.str()};
|
2014-08-19 19:56:11 +02:00
|
|
|
}
|
|
|
|
else if (str == "filename")
|
2018-05-27 05:00:04 +02:00
|
|
|
return {InsertCompleterDesc::Filename, {}};
|
2017-10-26 13:04:28 +02:00
|
|
|
else if (str.substr(0_byte, 5_byte) == "line=")
|
2017-10-02 18:45:11 +02:00
|
|
|
{
|
2017-10-26 13:04:28 +02:00
|
|
|
auto param = str.substr(5_byte);
|
|
|
|
if (param == "all" or param == "buffer")
|
2018-05-27 05:00:04 +02:00
|
|
|
return {InsertCompleterDesc::Line, param.str()};
|
2017-10-02 18:45:11 +02:00
|
|
|
}
|
2015-06-01 22:15:59 +02:00
|
|
|
throw runtime_error(format("invalid completer description: '{}'", str));
|
2014-08-19 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-10-18 17:55:21 +02:00
|
|
|
template<bool other_buffers>
|
2018-04-07 07:36:39 +02:00
|
|
|
InsertCompletion complete_word(const SelectionList& sels,
|
|
|
|
const OptionManager& options,
|
|
|
|
const FaceRegistry& faces)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2017-06-26 16:28:41 +02:00
|
|
|
ConstArrayView<Codepoint> extra_word_chars = options["extra_word_chars"].get<Vector<Codepoint, MemoryDomain::Options>>();
|
|
|
|
auto is_word_pred = [extra_word_chars](Codepoint c) { return is_word(c, extra_word_chars); };
|
2016-08-21 21:25:11 +02:00
|
|
|
|
2016-04-08 14:50:06 +02:00
|
|
|
const Buffer& buffer = sels.buffer();
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord cursor_pos = sels.main().cursor();
|
2016-04-08 14:50:06 +02:00
|
|
|
|
2015-10-30 14:57:46 +01:00
|
|
|
using Utf8It = utf8::iterator<BufferIterator>;
|
|
|
|
Utf8It pos{buffer.iterator_at(cursor_pos), buffer};
|
2016-08-21 21:25:11 +02:00
|
|
|
if (pos == buffer.begin() or not is_word_pred(*(pos-1)))
|
2015-10-30 14:57:46 +01:00
|
|
|
return {};
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord word_begin;
|
2017-02-20 20:19:26 +01:00
|
|
|
StringView prefix;
|
2017-03-07 01:30:54 +01:00
|
|
|
HashMap<StringView, int> sel_word_counts;
|
2016-04-08 14:58:33 +02:00
|
|
|
for (int i = 0; i < sels.size(); ++i)
|
|
|
|
{
|
2020-11-07 00:19:11 +01:00
|
|
|
int len = 0;
|
|
|
|
auto is_short_enough_word = [&] (Codepoint c) { return len++ < WordDB::max_word_len && is_word_pred(c); };
|
|
|
|
|
2016-04-09 08:57:55 +02:00
|
|
|
Utf8It end{buffer.iterator_at(sels[i].cursor()), buffer};
|
|
|
|
Utf8It begin = end-1;
|
2020-11-07 00:19:11 +01:00
|
|
|
if (not skip_while_reverse(begin, buffer.begin(), is_short_enough_word) and
|
2017-06-09 15:21:18 +02:00
|
|
|
begin < end) // (begin might == end if end == buffer.begin())
|
2016-04-08 14:58:33 +02:00
|
|
|
++begin;
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2016-04-08 14:58:33 +02:00
|
|
|
if (i == sels.main_index())
|
|
|
|
{
|
|
|
|
word_begin = begin.base().coord();
|
2017-02-20 20:19:26 +01:00
|
|
|
prefix = buffer.substr(word_begin, end.base().coord());
|
2016-04-08 14:58:33 +02:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2020-11-07 00:19:11 +01:00
|
|
|
skip_while(end, buffer.end(), is_short_enough_word);
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2020-11-07 00:19:11 +01:00
|
|
|
if (len <= WordDB::max_word_len)
|
|
|
|
{
|
|
|
|
StringView word = buffer.substr(begin.base().coord(), end.base().coord());
|
|
|
|
++sel_word_counts[word];
|
|
|
|
}
|
2016-04-08 14:58:33 +02:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2015-10-22 20:49:08 +02:00
|
|
|
struct RankedMatchAndBuffer : RankedMatch
|
2015-10-18 17:55:21 +02:00
|
|
|
{
|
2017-01-08 23:30:15 +01:00
|
|
|
RankedMatchAndBuffer(RankedMatch m, const Buffer* b)
|
|
|
|
: RankedMatch{std::move(m)}, buffer{b} {}
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2015-10-30 14:07:48 +01:00
|
|
|
using RankedMatch::operator==;
|
|
|
|
using RankedMatch::operator<;
|
2015-10-27 22:25:18 +01:00
|
|
|
bool operator==(StringView other) const { return candidate() == other; }
|
|
|
|
|
2015-09-02 14:43:07 +02:00
|
|
|
const Buffer* buffer;
|
|
|
|
};
|
|
|
|
|
2017-12-06 07:15:36 +01:00
|
|
|
auto& word_db = get_word_db(buffer);
|
2019-10-16 23:15:12 +02:00
|
|
|
Vector<RankedMatchAndBuffer> matches = word_db.find_matching(prefix)
|
|
|
|
| transform([&](auto& m) { return RankedMatchAndBuffer{m, &buffer}; })
|
|
|
|
| gather<Vector>();
|
2016-04-08 14:58:33 +02:00
|
|
|
// Remove words that are being edited
|
|
|
|
for (auto& word_count : sel_word_counts)
|
|
|
|
{
|
2017-12-06 07:15:36 +01:00
|
|
|
if (word_db.get_word_occurences(word_count.key) <= word_count.value)
|
2017-03-07 01:30:54 +01:00
|
|
|
unordered_erase(matches, word_count.key);
|
2016-04-08 14:58:33 +02:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
|
|
|
|
if (other_buffers)
|
|
|
|
{
|
|
|
|
for (const auto& buf : BufferManager::instance())
|
|
|
|
{
|
2016-03-12 17:44:55 +01:00
|
|
|
if (buf.get() == &buffer or buf->flags() & Buffer::Flags::Debug)
|
2014-04-28 22:54:00 +02:00
|
|
|
continue;
|
2017-12-06 07:15:36 +01:00
|
|
|
for (auto& m : get_word_db(*buf).find_matching(prefix) |
|
|
|
|
// filter out words that are not considered words for the current buffer
|
2019-07-09 11:29:19 +02:00
|
|
|
filter([&](auto& rm) {
|
|
|
|
auto&& c = rm.candidate();
|
|
|
|
return std::all_of(utf8::iterator{c.begin(), c},
|
|
|
|
utf8::iterator{c.end(), c},
|
|
|
|
is_word_pred); }))
|
2017-12-06 07:15:36 +01:00
|
|
|
matches.push_back({ m, buf.get() });
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-27 09:49:15 +01:00
|
|
|
|
2016-03-14 14:38:46 +01:00
|
|
|
using StaticWords = Vector<String, MemoryDomain::Options>;
|
|
|
|
for (auto& word : options["static_words"].get<StaticWords>())
|
|
|
|
if (RankedMatch match{word, prefix})
|
|
|
|
matches.emplace_back(match, nullptr);
|
2015-12-27 09:49:15 +01:00
|
|
|
|
2017-02-20 20:19:26 +01:00
|
|
|
unordered_erase(matches, prefix);
|
2017-06-07 14:36:47 +02:00
|
|
|
const auto longest = accumulate(matches, 0_char,
|
|
|
|
[](const CharCount& lhs, const RankedMatchAndBuffer& rhs)
|
|
|
|
{ return std::max(lhs, rhs.candidate().char_length()); });
|
2015-09-02 14:43:07 +02:00
|
|
|
|
2019-10-16 23:15:33 +02:00
|
|
|
auto limit = [](StringView s, ColumnCount l) { return s.column_length() <= l ? s.str() : "…" + s.substr(s.column_length() - (l + 1)); };
|
2017-12-21 02:55:29 +01:00
|
|
|
constexpr size_t max_count = 100;
|
|
|
|
// Gather best max_count matches
|
2015-09-02 14:43:07 +02:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2017-12-21 02:55:29 +01:00
|
|
|
candidates.reserve(std::min(matches.size(), max_count));
|
2018-04-15 05:20:13 +02:00
|
|
|
|
|
|
|
for_n_best(matches, max_count, [](auto& lhs, auto& rhs) { return rhs < lhs; },
|
|
|
|
[&](RankedMatchAndBuffer& m) {
|
|
|
|
if (not candidates.empty() and candidates.back().completion == m.candidate())
|
|
|
|
return false;
|
|
|
|
DisplayLine menu_entry;
|
|
|
|
if (other_buffers && m.buffer)
|
2015-09-11 14:03:06 +02:00
|
|
|
{
|
2018-04-15 05:20:13 +02:00
|
|
|
const auto pad_len = longest + 1 - m.candidate().char_length();
|
2018-04-29 12:33:47 +02:00
|
|
|
menu_entry.push_back({ m.candidate().str(), {} });
|
|
|
|
menu_entry.push_back({ String{' ', pad_len}, {} });
|
2019-10-16 23:15:33 +02:00
|
|
|
menu_entry.push_back({ limit(m.buffer->display_name(), 20), faces["MenuInfo"] });
|
2017-12-21 02:55:29 +01:00
|
|
|
}
|
2018-04-15 05:20:13 +02:00
|
|
|
else
|
2018-04-29 12:33:47 +02:00
|
|
|
menu_entry.push_back({ m.candidate().str(), {} });
|
2018-04-15 05:20:13 +02:00
|
|
|
|
|
|
|
candidates.push_back({m.candidate().str(), "", std::move(menu_entry)});
|
|
|
|
return true;
|
|
|
|
});
|
2015-09-02 14:43:07 +02:00
|
|
|
|
2017-02-27 21:29:53 +01:00
|
|
|
return { std::move(candidates), word_begin, cursor_pos, buffer.timestamp() };
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<bool require_slash>
|
2016-04-08 14:50:06 +02:00
|
|
|
InsertCompletion complete_filename(const SelectionList& sels,
|
2018-04-07 07:36:39 +02:00
|
|
|
const OptionManager& options,
|
|
|
|
const FaceRegistry&)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2016-04-08 14:50:06 +02:00
|
|
|
const Buffer& buffer = sels.buffer();
|
|
|
|
auto pos = buffer.iterator_at(sels.main().cursor());
|
2014-04-28 22:54:00 +02:00
|
|
|
auto begin = pos;
|
|
|
|
|
|
|
|
auto is_filename = [](char c)
|
|
|
|
{
|
|
|
|
return isalnum(c) or c == '/' or c == '.' or c == '_' or c == '-';
|
|
|
|
};
|
|
|
|
while (begin != buffer.begin() and is_filename(*(begin-1)))
|
|
|
|
--begin;
|
|
|
|
|
2016-03-14 14:59:54 +01:00
|
|
|
if (begin != buffer.begin() and *begin == '/' and *(begin-1) == '~')
|
|
|
|
--begin;
|
|
|
|
|
2017-02-20 20:19:26 +01:00
|
|
|
StringView prefix = buffer.substr(begin.coord(), pos.coord());
|
2014-04-28 22:54:00 +02:00
|
|
|
if (require_slash and not contains(prefix, '/'))
|
|
|
|
return {};
|
|
|
|
|
2016-05-19 22:46:27 +02:00
|
|
|
// Do not try to complete in that case as its unlikely to be a filename,
|
|
|
|
// and triggers network host search of cygwin.
|
|
|
|
if (prefix.substr(0_byte, 2_byte) == "//")
|
|
|
|
return {};
|
|
|
|
|
2015-09-02 14:43:07 +02:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2016-03-14 14:59:54 +01:00
|
|
|
if (prefix.front() == '/' or prefix.front() == '~')
|
2014-11-07 14:54:43 +01:00
|
|
|
{
|
2017-12-09 12:03:19 +01:00
|
|
|
for (auto& filename : Kakoune::complete_filename(prefix,
|
|
|
|
options["ignored_files"].get<Regex>()))
|
2018-04-29 12:33:47 +02:00
|
|
|
candidates.push_back({ filename, "", {filename, {}} });
|
2014-11-07 14:54:43 +01:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
else
|
|
|
|
{
|
2018-03-25 11:28:26 +02:00
|
|
|
Vector<String> visited_dirs;
|
2014-04-28 22:54:00 +02:00
|
|
|
for (auto dir : options["path"].get<StringList>())
|
|
|
|
{
|
2018-03-25 11:28:26 +02:00
|
|
|
dir = real_path(parse_filename(dir, (buffer.flags() & Buffer::Flags::File) ?
|
|
|
|
split_path(buffer.name()).first : StringView{}));
|
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
if (not dir.empty() and dir.back() != '/')
|
|
|
|
dir += '/';
|
2018-03-25 11:28:26 +02:00
|
|
|
|
|
|
|
if (contains(visited_dirs, dir))
|
|
|
|
continue;
|
2018-03-22 22:22:34 +01:00
|
|
|
|
2017-12-09 12:03:19 +01:00
|
|
|
for (auto& filename : Kakoune::complete_filename(dir + prefix,
|
|
|
|
options["ignored_files"].get<Regex>()))
|
2015-10-05 02:48:00 +02:00
|
|
|
{
|
|
|
|
StringView candidate = filename.substr(dir.length());
|
2018-04-29 12:33:47 +02:00
|
|
|
candidates.push_back({ candidate.str(), "", {candidate.str(), {}} });
|
2015-10-05 02:48:00 +02:00
|
|
|
}
|
2018-03-25 11:28:26 +02:00
|
|
|
|
|
|
|
visited_dirs.push_back(std::move(dir));
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 14:43:07 +02:00
|
|
|
if (candidates.empty())
|
2014-04-28 22:54:00 +02:00
|
|
|
return {};
|
2017-02-27 21:29:53 +01:00
|
|
|
return { std::move(candidates), begin.coord(), pos.coord(), buffer.timestamp() };
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 14:50:06 +02:00
|
|
|
InsertCompletion complete_option(const SelectionList& sels,
|
|
|
|
const OptionManager& options,
|
2018-04-07 07:36:39 +02:00
|
|
|
const FaceRegistry& faces,
|
2016-04-08 14:50:06 +02:00
|
|
|
StringView option_name)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2016-04-08 14:50:06 +02:00
|
|
|
const Buffer& buffer = sels.buffer();
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord cursor_pos = sels.main().cursor();
|
2016-04-08 14:50:06 +02:00
|
|
|
|
2016-04-01 02:27:23 +02:00
|
|
|
const CompletionList& opt = options[option_name].get<CompletionList>();
|
|
|
|
if (opt.list.empty())
|
2014-04-28 22:54:00 +02:00
|
|
|
return {};
|
|
|
|
|
2016-04-01 02:27:23 +02:00
|
|
|
auto& desc = opt.prefix;
|
2014-04-28 22:54:00 +02:00
|
|
|
static const Regex re(R"((\d+)\.(\d+)(?:\+(\d+))?@(\d+))");
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<String::const_iterator> match;
|
2020-10-12 03:41:52 +02:00
|
|
|
if (not regex_match(desc.begin(), desc.end(), match, re))
|
|
|
|
return {};
|
2014-11-17 21:15:54 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
BufferCoord coord{str_to_int({match[1].first, match[1].second}) - 1,
|
|
|
|
str_to_int({match[2].first, match[2].second}) - 1};
|
|
|
|
if (not buffer.is_valid(coord))
|
|
|
|
return {};
|
|
|
|
size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second});
|
|
|
|
auto changes = buffer.changes_since(timestamp);
|
|
|
|
if (any_of(changes, [&](auto&& change) { return change.begin < coord; }))
|
|
|
|
return {};
|
2015-10-05 02:48:00 +02:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
if (cursor_pos.line != coord.line or cursor_pos.column < coord.column)
|
|
|
|
return {};
|
2015-10-29 14:57:50 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
const ColumnCount tabstop = options["tabstop"].get<int>();
|
|
|
|
const ColumnCount column = get_column(buffer, tabstop, cursor_pos);
|
2015-10-29 14:57:50 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
struct RankedMatchAndInfo : RankedMatch
|
|
|
|
{
|
|
|
|
using RankedMatch::RankedMatch;
|
|
|
|
using RankedMatch::operator==;
|
|
|
|
using RankedMatch::operator<;
|
2015-10-29 14:57:50 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
StringView on_select;
|
|
|
|
DisplayLine menu_entry;
|
|
|
|
};
|
2017-12-21 02:55:29 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
StringView query = buffer.substr(coord, cursor_pos);
|
|
|
|
Vector<RankedMatchAndInfo> matches;
|
2015-10-29 14:57:50 +01:00
|
|
|
|
2020-10-12 03:41:52 +02:00
|
|
|
for (auto& candidate : opt.list)
|
|
|
|
{
|
|
|
|
if (RankedMatchAndInfo match{std::get<0>(candidate), query})
|
|
|
|
{
|
|
|
|
match.on_select = std::get<1>(candidate);
|
|
|
|
auto& menu = std::get<2>(candidate);
|
|
|
|
match.menu_entry = not menu.empty() ?
|
|
|
|
parse_display_line(expand_tabs(menu, tabstop, column), faces)
|
|
|
|
: DisplayLine{String{}, {}};
|
|
|
|
|
|
|
|
matches.push_back(std::move(match));
|
2014-11-07 14:54:43 +01:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
2020-10-12 03:41:52 +02:00
|
|
|
|
|
|
|
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())
|
|
|
|
candidates.push_back({ first->candidate().str(), first->on_select.str(),
|
|
|
|
std::move(first->menu_entry) });
|
|
|
|
std::pop_heap(first, last--, greater);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto end = cursor_pos;
|
|
|
|
if (match[3].matched)
|
|
|
|
{
|
|
|
|
ByteCount len = str_to_int({match[3].first, match[3].second});
|
|
|
|
end = buffer.advance(coord, len);
|
|
|
|
}
|
|
|
|
return { std::move(candidates), coord, end, timestamp };
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:04:28 +02:00
|
|
|
template<bool other_buffers>
|
2018-04-07 07:36:39 +02:00
|
|
|
InsertCompletion complete_line(const SelectionList& sels,
|
|
|
|
const OptionManager& options,
|
|
|
|
const FaceRegistry&)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2016-04-08 14:50:06 +02:00
|
|
|
const Buffer& buffer = sels.buffer();
|
2016-09-22 21:36:26 +02:00
|
|
|
BufferCoord cursor_pos = sels.main().cursor();
|
2016-04-08 14:50:06 +02:00
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
const ColumnCount tabstop = options["tabstop"].get<int>();
|
|
|
|
const ColumnCount column = get_column(buffer, tabstop, cursor_pos);
|
2015-10-05 02:48:00 +02:00
|
|
|
|
2021-12-10 23:27:51 +01:00
|
|
|
auto trim_leading_whitespaces = [](StringView s) {
|
|
|
|
utf8::iterator it{s.begin(), s};
|
|
|
|
while (it != s.end() and is_horizontal_blank(*it))
|
|
|
|
++it;
|
|
|
|
return StringView{it.base(), s.end()};
|
|
|
|
};
|
|
|
|
|
|
|
|
StringView prefix = trim_leading_whitespaces(buffer[cursor_pos.line].substr(0_byte, cursor_pos.column));
|
2020-11-16 17:02:13 +01:00
|
|
|
BufferCoord replace_begin = buffer.advance(cursor_pos, -prefix.length());
|
2015-09-02 14:43:07 +02:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2017-10-26 13:04:28 +02:00
|
|
|
|
|
|
|
auto add_candidates = [&](const Buffer& buf) {
|
|
|
|
for (LineCount l = 0_line; l < buf.line_count(); ++l)
|
|
|
|
{
|
|
|
|
if (buf.name() == buffer.name() && l == cursor_pos.line)
|
|
|
|
continue;
|
2020-11-16 17:02:13 +01:00
|
|
|
|
2021-12-10 23:27:51 +01:00
|
|
|
StringView line = buf[l];
|
|
|
|
StringView candidate = trim_leading_whitespaces(line.substr(0_byte, line.length()-1));
|
2020-11-16 17:02:13 +01:00
|
|
|
|
2021-12-10 23:27:51 +01:00
|
|
|
if (candidate.length() == 0)
|
2020-11-16 17:02:13 +01:00
|
|
|
continue;
|
|
|
|
|
2021-12-10 23:27:51 +01:00
|
|
|
if (prefix == candidate.substr(0_byte, prefix.length()))
|
2017-10-26 13:04:28 +02:00
|
|
|
{
|
2021-12-10 23:27:51 +01:00
|
|
|
candidates.push_back({candidate.str(), "", {expand_tabs(candidate, tabstop, column), {}} });
|
2018-02-28 05:40:42 +01:00
|
|
|
// perf: it's unlikely the user intends to search among >10 candidates anyway
|
|
|
|
if (candidates.size() == 100)
|
|
|
|
break;
|
2017-10-26 13:04:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add_candidates(buffer);
|
|
|
|
|
|
|
|
if (other_buffers)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2017-10-26 13:04:28 +02:00
|
|
|
for (const auto& buf : BufferManager::instance())
|
2015-10-05 02:48:00 +02:00
|
|
|
{
|
2018-02-28 05:40:42 +01:00
|
|
|
if (buf.get() != &buffer and not (buf->flags() & Buffer::Flags::Debug))
|
|
|
|
add_candidates(*buf);
|
2015-10-05 02:48:00 +02:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
2017-10-26 13:04:28 +02:00
|
|
|
|
2015-09-02 14:43:07 +02:00
|
|
|
if (candidates.empty())
|
2014-04-28 22:54:00 +02:00
|
|
|
return {};
|
2015-09-02 14:43:07 +02:00
|
|
|
std::sort(candidates.begin(), candidates.end());
|
|
|
|
candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end());
|
2020-11-16 17:02:13 +01:00
|
|
|
return { std::move(candidates), replace_begin, cursor_pos, buffer.timestamp() };
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-21 14:38:34 +02:00
|
|
|
InsertCompleter::InsertCompleter(Context& context)
|
2018-04-07 07:36:39 +02:00
|
|
|
: m_context(context), m_options(context.options()), m_faces(context.faces())
|
2014-11-02 17:04:24 +01:00
|
|
|
{
|
2014-11-05 00:51:41 +01:00
|
|
|
m_options.register_watcher(*this);
|
2014-11-02 17:04:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
InsertCompleter::~InsertCompleter()
|
|
|
|
{
|
2014-11-05 00:51:41 +01:00
|
|
|
m_options.unregister_watcher(*this);
|
2014-11-02 17:04:24 +01:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2018-05-03 14:22:12 +02:00
|
|
|
void InsertCompleter::select(int index, bool relative, Vector<Key>& keystrokes)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
|
|
|
if (not setup_ifn())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto& buffer = m_context.buffer();
|
2018-05-03 14:22:12 +02:00
|
|
|
m_current_candidate = (relative ? m_current_candidate + index : index) % (int)m_completions.candidates.size();
|
2014-04-28 22:54:00 +02:00
|
|
|
if (m_current_candidate < 0)
|
2015-12-28 01:18:38 +01:00
|
|
|
m_current_candidate += m_completions.candidates.size();
|
|
|
|
const InsertCompletion::Candidate& candidate = m_completions.candidates[m_current_candidate];
|
2014-05-14 01:59:36 +02:00
|
|
|
auto& selections = m_context.selections();
|
|
|
|
const auto& cursor_pos = selections.main().cursor();
|
2014-04-28 22:54:00 +02:00
|
|
|
const auto prefix_len = buffer.distance(m_completions.begin, cursor_pos);
|
|
|
|
const auto suffix_len = std::max(0_byte, buffer.distance(cursor_pos, m_completions.end));
|
|
|
|
|
|
|
|
auto ref = buffer.string(m_completions.begin, m_completions.end);
|
2020-06-27 05:33:50 +02:00
|
|
|
Vector<BufferRange> ranges;
|
2014-05-14 01:59:36 +02:00
|
|
|
for (auto& sel : selections)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2020-06-27 05:33:50 +02:00
|
|
|
auto pos = buffer.iterator_at(sel.cursor());
|
|
|
|
if (pos.coord().column >= prefix_len and (pos + suffix_len) != buffer.end() and std::equal(ref.begin(), ref.end(), pos - prefix_len))
|
|
|
|
ranges.push_back({(pos - prefix_len).coord(), (pos + suffix_len).coord()});
|
2017-06-09 14:50:00 +02:00
|
|
|
}
|
2020-06-27 05:33:50 +02:00
|
|
|
replace(buffer, ranges, candidate.completion);
|
2017-06-09 14:50:00 +02:00
|
|
|
|
|
|
|
selections.update();
|
2014-05-31 19:18:29 +02:00
|
|
|
m_completions.end = cursor_pos;
|
2015-08-30 12:05:48 +02:00
|
|
|
m_completions.begin = buffer.advance(cursor_pos, -candidate.completion.length());
|
2014-04-28 22:54:00 +02:00
|
|
|
m_completions.timestamp = buffer.timestamp();
|
2020-06-27 05:33:50 +02:00
|
|
|
m_inserted_ranges = std::move(ranges);
|
|
|
|
|
2016-02-27 18:23:13 +01:00
|
|
|
if (m_context.has_client())
|
2014-11-07 14:54:43 +01:00
|
|
|
{
|
2016-02-27 18:23:13 +01:00
|
|
|
m_context.client().menu_select(m_current_candidate);
|
2014-11-12 00:31:06 +01:00
|
|
|
}
|
2015-03-15 19:44:11 +01:00
|
|
|
|
|
|
|
for (auto i = 0_byte; i < prefix_len; ++i)
|
2017-01-08 23:30:15 +01:00
|
|
|
keystrokes.emplace_back(Key::Backspace);
|
2015-03-15 19:44:11 +01:00
|
|
|
for (auto i = 0_byte; i < suffix_len; ++i)
|
2017-01-08 23:30:15 +01:00
|
|
|
keystrokes.emplace_back(Key::Delete);
|
2015-08-30 12:05:48 +02:00
|
|
|
for (auto& c : candidate.completion)
|
2017-01-08 23:30:15 +01:00
|
|
|
keystrokes.emplace_back(c);
|
2017-11-25 06:47:04 +01:00
|
|
|
|
2019-04-15 17:42:58 +02:00
|
|
|
if (not candidate.on_select.empty())
|
|
|
|
CommandManager::instance().execute(candidate.on_select, m_context);
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 03:46:02 +02:00
|
|
|
void InsertCompleter::update(bool allow_implicit)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2015-10-19 14:43:23 +02:00
|
|
|
if (m_explicit_completer and try_complete(m_explicit_completer))
|
|
|
|
return;
|
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
reset();
|
2018-07-15 03:46:02 +02:00
|
|
|
if (allow_implicit)
|
|
|
|
setup_ifn();
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 05:33:50 +02:00
|
|
|
auto& get_first(BufferRange& range) { return range.begin; }
|
|
|
|
auto& get_last(BufferRange& range) { return range.end; }
|
2020-01-08 10:25:27 +01:00
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
void InsertCompleter::reset()
|
|
|
|
{
|
2017-01-30 11:32:29 +01:00
|
|
|
if (m_explicit_completer or m_completions.is_valid())
|
2014-11-12 00:31:06 +01:00
|
|
|
{
|
2019-12-01 11:03:50 +01:00
|
|
|
String hook_param;
|
|
|
|
if (m_context.has_client() and m_current_candidate >= 0 and m_current_candidate < m_completions.candidates.size() - 1)
|
|
|
|
{
|
|
|
|
auto& buffer = m_context.buffer();
|
2020-06-27 05:33:50 +02:00
|
|
|
update_ranges(buffer, m_completions.timestamp, m_inserted_ranges);
|
|
|
|
hook_param = join(m_inserted_ranges | filter([](auto&& r) { return not r.empty(); }) | transform([&](auto&& r) {
|
|
|
|
return selection_to_string(ColumnType::Byte, buffer, {r.begin, buffer.char_prev(r.end)});
|
2019-12-01 11:03:50 +01:00
|
|
|
}), ' ');
|
|
|
|
}
|
2019-04-26 12:49:55 +02:00
|
|
|
|
2017-01-30 11:32:29 +01:00
|
|
|
m_explicit_completer = nullptr;
|
2016-12-04 14:37:33 +01:00
|
|
|
m_completions = InsertCompletion{};
|
2020-08-18 12:35:48 +02:00
|
|
|
m_inserted_ranges.clear();
|
2016-12-04 14:37:33 +01:00
|
|
|
if (m_context.has_client())
|
|
|
|
{
|
|
|
|
m_context.client().menu_hide();
|
|
|
|
m_context.client().info_hide();
|
2019-12-01 11:03:50 +01:00
|
|
|
m_context.hooks().run_hook(Hook::InsertCompletionHide, hook_param, m_context);
|
2016-12-04 14:37:33 +01:00
|
|
|
}
|
2014-11-12 00:31:06 +01:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InsertCompleter::setup_ifn()
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
if (not m_completions.is_valid())
|
|
|
|
{
|
2014-11-05 00:51:41 +01:00
|
|
|
auto& completers = m_options["completers"].get<InsertCompleterDescList>();
|
2014-04-28 22:54:00 +02:00
|
|
|
for (auto& completer : completers)
|
|
|
|
{
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Filename and
|
2015-12-27 09:49:15 +01:00
|
|
|
try_complete(complete_filename<true>))
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option and
|
2018-04-07 07:36:39 +02:00
|
|
|
try_complete([&](const SelectionList& sels,
|
|
|
|
const OptionManager& options,
|
|
|
|
const FaceRegistry& faces) {
|
|
|
|
return complete_option(sels, options, faces, *completer.param);
|
2014-04-28 22:54:00 +02:00
|
|
|
}))
|
|
|
|
return true;
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Word and
|
|
|
|
*completer.param == "buffer" and
|
2015-10-18 17:55:21 +02:00
|
|
|
try_complete(complete_word<false>))
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Word and
|
|
|
|
*completer.param == "all" and
|
2015-10-18 17:55:21 +02:00
|
|
|
try_complete(complete_word<true>))
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
2017-10-02 18:45:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Line and
|
2017-10-26 13:04:28 +02:00
|
|
|
*completer.param == "buffer" and
|
|
|
|
try_complete(complete_line<false>))
|
|
|
|
return true;
|
|
|
|
if (completer.mode == InsertCompleterDesc::Line and
|
|
|
|
*completer.param == "all" and
|
|
|
|
try_complete(complete_line<true>))
|
2017-10-02 18:45:11 +02:00
|
|
|
return true;
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::menu_show()
|
|
|
|
{
|
2016-02-27 18:23:13 +01:00
|
|
|
if (not m_context.has_client())
|
2014-04-28 22:54:00 +02:00
|
|
|
return;
|
|
|
|
|
2015-10-05 02:25:23 +02:00
|
|
|
Vector<DisplayLine> menu_entries;
|
2015-12-28 01:18:38 +01:00
|
|
|
for (auto& candidate : m_completions.candidates)
|
2015-10-05 02:48:00 +02:00
|
|
|
menu_entries.push_back(candidate.menu_entry);
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2016-02-27 18:23:13 +01:00
|
|
|
m_context.client().menu_show(std::move(menu_entries), m_completions.begin,
|
|
|
|
MenuStyle::Inline);
|
|
|
|
m_context.client().menu_select(m_current_candidate);
|
2018-10-22 23:15:53 +02:00
|
|
|
m_context.hooks().run_hook(Hook::InsertCompletionShow, "", m_context);
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::on_option_changed(const Option& opt)
|
|
|
|
{
|
2015-03-13 14:52:23 +01:00
|
|
|
// Do not reset the menu if the user has selected an entry
|
2015-12-28 01:18:38 +01:00
|
|
|
if (not m_completions.candidates.empty() and
|
|
|
|
m_current_candidate != m_completions.candidates.size() - 1)
|
2015-03-13 14:52:23 +01:00
|
|
|
return;
|
|
|
|
|
2019-11-02 21:49:26 +01:00
|
|
|
const auto& completers = m_options["completers"].get<InsertCompleterDescList>();
|
2014-04-28 22:54:00 +02:00
|
|
|
for (auto& completer : completers)
|
|
|
|
{
|
2015-03-13 14:52:23 +01:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option and
|
|
|
|
*completer.param == opt.name())
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
setup_ifn();
|
|
|
|
break;
|
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:49:15 +01:00
|
|
|
template<typename Func>
|
|
|
|
bool InsertCompleter::try_complete(Func complete_func)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2016-04-08 14:50:06 +02:00
|
|
|
auto& sels = m_context.selections();
|
2014-04-28 22:54:00 +02:00
|
|
|
try
|
|
|
|
{
|
2020-08-30 02:35:15 +02:00
|
|
|
reset();
|
2018-04-07 07:36:39 +02:00
|
|
|
m_completions = complete_func(sels, m_options, m_faces);
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
catch (runtime_error& e)
|
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("error while trying to run completer: {}", e.what()));
|
2014-04-28 22:54:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (not m_completions.is_valid())
|
|
|
|
return false;
|
|
|
|
|
2016-04-08 14:50:06 +02:00
|
|
|
kak_assert(m_completions.begin <= sels.main().cursor());
|
2015-12-28 01:18:38 +01:00
|
|
|
m_current_candidate = m_completions.candidates.size();
|
2014-04-28 22:54:00 +02:00
|
|
|
menu_show();
|
2017-01-29 14:49:45 +01:00
|
|
|
m_completions.candidates.push_back({sels.buffer().string(m_completions.begin, m_completions.end), "", {}});
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_file_complete()
|
|
|
|
{
|
2015-12-27 09:49:15 +01:00
|
|
|
try_complete(complete_filename<false>);
|
|
|
|
m_explicit_completer = complete_filename<false>;
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 22:41:13 +02:00
|
|
|
void InsertCompleter::explicit_word_buffer_complete()
|
|
|
|
{
|
|
|
|
try_complete(complete_word<false>);
|
|
|
|
m_explicit_completer = complete_word<false>;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_word_all_complete()
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2015-10-18 17:55:21 +02:00
|
|
|
try_complete(complete_word<true>);
|
2015-10-19 14:43:23 +02:00
|
|
|
m_explicit_completer = complete_word<true>;
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:04:28 +02:00
|
|
|
void InsertCompleter::explicit_line_buffer_complete()
|
|
|
|
{
|
|
|
|
try_complete(complete_line<false>);
|
|
|
|
m_explicit_completer = complete_line<false>;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_line_all_complete()
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2017-10-26 13:04:28 +02:00
|
|
|
try_complete(complete_line<true>);
|
|
|
|
m_explicit_completer = complete_line<true>;
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|