2014-04-28 22:54:00 +02:00
|
|
|
#include "insert_completer.hh"
|
|
|
|
|
|
|
|
#include "buffer_manager.hh"
|
|
|
|
#include "buffer_utils.hh"
|
|
|
|
#include "context.hh"
|
|
|
|
#include "debug.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 "user_interface.hh"
|
|
|
|
#include "window.hh"
|
|
|
|
#include "word_db.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
using StringList = std::vector<String>;
|
|
|
|
|
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 : "");
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-10-07 22:11:55 +02:00
|
|
|
void option_from_string(StringView str, InsertCompleterDesc& opt)
|
2014-08-19 19:56:11 +02:00
|
|
|
{
|
|
|
|
if (str.substr(0_byte, 7_byte) == "option=")
|
|
|
|
{
|
|
|
|
opt.mode = InsertCompleterDesc::Option;
|
|
|
|
opt.param = str.substr(7_byte).str();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (str.substr(0_byte, 5_byte) == "word=")
|
|
|
|
{
|
|
|
|
auto param = str.substr(5_byte);
|
|
|
|
if (param == "all" or param == "buffer")
|
|
|
|
{
|
|
|
|
opt.mode = InsertCompleterDesc::Word;
|
|
|
|
opt.param = param.str();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (str == "filename")
|
|
|
|
{
|
|
|
|
opt.mode = InsertCompleterDesc::Filename;
|
|
|
|
opt.param = Optional<String>{};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw runtime_error("invalid completer description: " + str);;
|
|
|
|
}
|
|
|
|
|
2014-04-28 22:54:00 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
WordDB& get_word_db(const Buffer& buffer)
|
|
|
|
{
|
|
|
|
static const ValueId word_db_id = ValueId::get_free_id();
|
|
|
|
Value& cache_val = buffer.values()[word_db_id];
|
|
|
|
if (not cache_val)
|
|
|
|
cache_val = Value(WordDB{buffer});
|
|
|
|
return cache_val.as<WordDB>();
|
|
|
|
}
|
|
|
|
|
2014-07-30 20:58:34 +02:00
|
|
|
template<bool other_buffers, bool subseq>
|
2014-05-07 20:51:01 +02:00
|
|
|
InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
|
|
|
auto pos = buffer.iterator_at(cursor_pos);
|
2014-07-02 22:14:01 +02:00
|
|
|
if (pos == buffer.begin() or not is_word(*utf8::previous(pos, buffer.begin())))
|
2014-04-28 22:54:00 +02:00
|
|
|
return {};
|
|
|
|
|
|
|
|
auto end = buffer.iterator_at(cursor_pos);
|
|
|
|
auto begin = end-1;
|
|
|
|
while (begin != buffer.begin() and is_word(*begin))
|
|
|
|
--begin;
|
|
|
|
if (not is_word(*begin))
|
|
|
|
++begin;
|
|
|
|
|
|
|
|
String prefix{begin, end};
|
|
|
|
|
|
|
|
while (end != buffer.end() and is_word(*end))
|
|
|
|
++end;
|
|
|
|
|
|
|
|
String current_word{begin, end};
|
|
|
|
|
|
|
|
auto& word_db = get_word_db(buffer);
|
2014-12-09 22:59:47 +01:00
|
|
|
auto matches = subseq ? word_db.find_subsequence(prefix)
|
|
|
|
: word_db.find_prefix(prefix);
|
2014-04-28 22:54:00 +02:00
|
|
|
|
|
|
|
if (word_db.get_word_occurences(current_word) <= 1)
|
2014-12-09 22:59:47 +01:00
|
|
|
unordered_erase(matches, current_word);
|
2014-04-28 22:54:00 +02:00
|
|
|
|
|
|
|
if (other_buffers)
|
|
|
|
{
|
|
|
|
for (const auto& buf : BufferManager::instance())
|
|
|
|
{
|
|
|
|
if (buf.get() == &buffer)
|
|
|
|
continue;
|
2014-10-28 20:18:33 +01:00
|
|
|
auto& buf_word_db = get_word_db(*buf);
|
2014-12-09 22:59:47 +01:00
|
|
|
auto bufmatches = subseq ? buf_word_db.find_subsequence(prefix)
|
|
|
|
: buf_word_db.find_prefix(prefix);
|
|
|
|
std::move(bufmatches.begin(), bufmatches.end(),
|
|
|
|
std::back_inserter(matches));
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-09 22:59:47 +01:00
|
|
|
unordered_erase(matches, prefix);
|
|
|
|
std::sort(matches.begin(), matches.end());
|
|
|
|
matches.erase(std::unique(matches.begin(), matches.end()), matches.end());
|
|
|
|
|
2014-11-07 14:54:43 +01:00
|
|
|
ComplAndDescList result;
|
2014-12-09 22:59:47 +01:00
|
|
|
result.reserve(matches.size());
|
|
|
|
for (auto& m : matches)
|
|
|
|
result.emplace_back(m, "");
|
|
|
|
|
2014-04-30 00:48:46 +02:00
|
|
|
return { begin.coord(), cursor_pos, std::move(result), buffer.timestamp() };
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<bool require_slash>
|
2014-05-07 20:51:01 +02:00
|
|
|
InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
|
2014-04-28 22:54:00 +02:00
|
|
|
OptionManager& options)
|
|
|
|
{
|
|
|
|
auto pos = buffer.iterator_at(cursor_pos);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (begin == pos)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
String prefix{begin, pos};
|
|
|
|
if (require_slash and not contains(prefix, '/'))
|
|
|
|
return {};
|
|
|
|
|
2014-11-07 14:54:43 +01:00
|
|
|
ComplAndDescList res;
|
2014-04-28 22:54:00 +02:00
|
|
|
if (prefix.front() == '/')
|
2014-11-07 14:54:43 +01:00
|
|
|
{
|
|
|
|
for (auto& filename : Kakoune::complete_filename(prefix, Regex{}))
|
|
|
|
res.emplace_back(std::move(filename), "");
|
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto dir : options["path"].get<StringList>())
|
|
|
|
{
|
|
|
|
if (not dir.empty() and dir.back() != '/')
|
|
|
|
dir += '/';
|
|
|
|
for (auto& filename : Kakoune::complete_filename(dir + prefix, Regex{}))
|
2014-11-07 14:54:43 +01:00
|
|
|
res.emplace_back(filename.substr(dir.length()), "");
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res.empty())
|
|
|
|
return {};
|
|
|
|
return { begin.coord(), pos.coord(), std::move(res), buffer.timestamp() };
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos,
|
2014-06-26 20:01:39 +02:00
|
|
|
OptionManager& options, StringView option_name)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2014-10-13 14:12:33 +02:00
|
|
|
const StringList& opt = options[option_name].get<StringList>();
|
2014-04-28 22:54:00 +02:00
|
|
|
if (opt.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto& desc = opt[0];
|
|
|
|
static const Regex re(R"((\d+)\.(\d+)(?:\+(\d+))?@(\d+))");
|
2014-10-13 14:12:33 +02:00
|
|
|
MatchResults<String::const_iterator> match;
|
|
|
|
if (regex_match(desc.begin(), desc.end(), match, re))
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord coord{ str_to_int(match[1].str()) - 1, str_to_int(match[2].str()) - 1 };
|
2014-04-28 22:54:00 +02:00
|
|
|
if (not buffer.is_valid(coord))
|
|
|
|
return {};
|
|
|
|
auto end = coord;
|
|
|
|
if (match[3].matched)
|
|
|
|
{
|
|
|
|
ByteCount len = str_to_int(match[3].str());
|
|
|
|
end = buffer.advance(coord, len);
|
|
|
|
}
|
|
|
|
size_t timestamp = (size_t)str_to_int(match[4].str());
|
2014-11-17 14:53:59 +01:00
|
|
|
auto changes = buffer.changes_since(timestamp);
|
|
|
|
if (find_if(changes, [&](const Buffer::Change& change){
|
|
|
|
return change.begin < coord;
|
|
|
|
}) != changes.end())
|
|
|
|
return {};
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2014-11-17 21:15:54 +01:00
|
|
|
if (cursor_pos.line == coord.line and cursor_pos.column >= coord.column)
|
2014-11-07 14:54:43 +01:00
|
|
|
{
|
2014-11-17 21:15:54 +01:00
|
|
|
StringView prefix = buffer[coord.line].substr(
|
|
|
|
coord.column, cursor_pos.column - coord.column);
|
|
|
|
|
2014-11-07 14:54:43 +01:00
|
|
|
ComplAndDescList res;
|
|
|
|
for (auto it = opt.begin() + 1; it != opt.end(); ++it)
|
|
|
|
{
|
2014-11-17 21:15:54 +01:00
|
|
|
auto splitted = split(*it, '@');
|
|
|
|
if (not splitted.empty() and prefix_match(splitted[0], prefix))
|
|
|
|
res.emplace_back(splitted[0], splitted.size() > 1 ? splitted[1] : "");
|
2014-11-07 14:54:43 +01:00
|
|
|
}
|
|
|
|
return { coord, end, std::move(res), timestamp };
|
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
InsertCompletion complete_line(const Buffer& buffer, ByteCoord cursor_pos)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2014-06-26 20:01:39 +02:00
|
|
|
StringView prefix = buffer[cursor_pos.line].substr(0_byte, cursor_pos.column);
|
2014-11-07 14:54:43 +01:00
|
|
|
ComplAndDescList res;
|
2014-04-28 22:54:00 +02:00
|
|
|
for (LineCount l = 0_line; l < buffer.line_count(); ++l)
|
|
|
|
{
|
|
|
|
if (l == cursor_pos.line)
|
|
|
|
continue;
|
|
|
|
ByteCount len = buffer[l].length();
|
|
|
|
if (len > cursor_pos.column and std::equal(prefix.begin(), prefix.end(), buffer[l].begin()))
|
2014-11-07 14:54:43 +01:00
|
|
|
res.emplace_back(buffer[l].substr(0_byte, len-1), "");
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
if (res.empty())
|
|
|
|
return {};
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
res.erase(std::unique(res.begin(), res.end()), res.end());
|
|
|
|
return { cursor_pos.line, cursor_pos, std::move(res), buffer.timestamp() };
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertCompleter::InsertCompleter(const Context& context)
|
2014-11-05 00:51:41 +01:00
|
|
|
: m_context(context), m_options(context.options())
|
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
|
|
|
|
|
|
|
void InsertCompleter::select(int offset)
|
|
|
|
{
|
|
|
|
if (not setup_ifn())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto& buffer = m_context.buffer();
|
|
|
|
m_current_candidate = (m_current_candidate + offset) % (int)m_matching_candidates.size();
|
|
|
|
if (m_current_candidate < 0)
|
|
|
|
m_current_candidate += m_matching_candidates.size();
|
2014-11-07 14:54:43 +01:00
|
|
|
const ComplAndDesc& candidate = m_matching_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);
|
2014-05-14 01:59:36 +02:00
|
|
|
for (auto& sel : selections)
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
2014-05-24 18:08:01 +02:00
|
|
|
const auto& cursor = sel.cursor();
|
|
|
|
auto pos = buffer.iterator_at(cursor);
|
|
|
|
if (cursor.column >= prefix_len and (pos + suffix_len) != buffer.end() and
|
2014-04-28 22:54:00 +02:00
|
|
|
std::equal(ref.begin(), ref.end(), pos - prefix_len))
|
|
|
|
{
|
|
|
|
pos = buffer.erase(pos - prefix_len, pos + suffix_len);
|
2014-11-07 14:54:43 +01:00
|
|
|
buffer.insert(pos, candidate.first);
|
2014-05-14 01:59:36 +02:00
|
|
|
const_cast<SelectionList&>(selections).update();
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 19:18:29 +02:00
|
|
|
m_completions.end = cursor_pos;
|
2014-11-07 14:54:43 +01:00
|
|
|
m_completions.begin = buffer.advance(cursor_pos, -candidate.first.length());
|
2014-04-28 22:54:00 +02:00
|
|
|
m_completions.timestamp = buffer.timestamp();
|
|
|
|
if (m_context.has_ui())
|
2014-11-07 14:54:43 +01:00
|
|
|
{
|
2014-04-28 22:54:00 +02:00
|
|
|
m_context.ui().menu_select(m_current_candidate);
|
2014-11-07 14:54:43 +01:00
|
|
|
if (not candidate.second.empty())
|
2014-11-08 18:59:38 +01:00
|
|
|
m_context.ui().info_show(candidate.first, candidate.second, CharCoord{},
|
|
|
|
get_face("Information"), InfoStyle::MenuDoc);
|
2014-11-12 00:31:06 +01:00
|
|
|
}
|
2014-04-28 22:54:00 +02:00
|
|
|
// when we select a match, remove non displayed matches from the candidates
|
|
|
|
// which are considered as invalid with the new completion timestamp
|
|
|
|
m_completions.candidates.clear();
|
|
|
|
std::copy(m_matching_candidates.begin(), m_matching_candidates.end()-1,
|
|
|
|
std::back_inserter(m_completions.candidates));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::update()
|
|
|
|
{
|
|
|
|
if (m_completions.is_valid())
|
|
|
|
{
|
|
|
|
ByteCount longest_completion = 0;
|
|
|
|
for (auto& candidate : m_completions.candidates)
|
2014-11-07 14:54:43 +01:00
|
|
|
longest_completion = std::max(longest_completion, candidate.first.length());
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord cursor = m_context.selections().main().cursor();
|
|
|
|
ByteCoord compl_beg = m_completions.begin;
|
2014-04-28 22:54:00 +02:00
|
|
|
if (cursor.line == compl_beg.line and
|
2014-05-31 19:18:29 +02:00
|
|
|
is_in_range(cursor.column, compl_beg.column,
|
|
|
|
compl_beg.column + longest_completion-1))
|
2014-04-28 22:54:00 +02:00
|
|
|
{
|
|
|
|
String prefix = m_context.buffer().string(compl_beg, cursor);
|
|
|
|
|
|
|
|
if (m_context.buffer().timestamp() == m_completions.timestamp)
|
|
|
|
m_matching_candidates = m_completions.candidates;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_matching_candidates.clear();
|
|
|
|
for (auto& candidate : m_completions.candidates)
|
|
|
|
{
|
2014-11-07 14:54:43 +01:00
|
|
|
if (candidate.first.substr(0, prefix.length()) == prefix)
|
2014-04-28 22:54:00 +02:00
|
|
|
m_matching_candidates.push_back(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (not m_matching_candidates.empty())
|
|
|
|
{
|
|
|
|
m_current_candidate = m_matching_candidates.size();
|
|
|
|
m_completions.end = cursor;
|
|
|
|
menu_show();
|
2014-11-07 14:54:43 +01:00
|
|
|
m_matching_candidates.emplace_back(prefix, "");
|
2014-04-28 22:54:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reset();
|
|
|
|
setup_ifn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::reset()
|
|
|
|
{
|
|
|
|
m_completions = InsertCompletion{};
|
|
|
|
if (m_context.has_ui())
|
2014-11-12 00:31:06 +01:00
|
|
|
{
|
2014-04-28 22:54:00 +02:00
|
|
|
m_context.ui().menu_hide();
|
2014-11-12 00:31:06 +01:00
|
|
|
m_context.ui().info_hide();
|
|
|
|
}
|
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
|
2014-05-07 20:51:01 +02:00
|
|
|
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-04-28 22:54:00 +02:00
|
|
|
return complete_filename<true>(buffer, cursor_pos,
|
2014-11-05 00:51:41 +01:00
|
|
|
m_options);
|
2014-04-28 22:54:00 +02:00
|
|
|
}))
|
|
|
|
return true;
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option and
|
2014-05-07 20:51:01 +02:00
|
|
|
try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-04-28 22:54:00 +02:00
|
|
|
return complete_option(buffer, cursor_pos,
|
2014-11-05 00:51:41 +01:00
|
|
|
m_options, *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
|
2014-07-30 20:58:34 +02:00
|
|
|
(try_complete(complete_word<false, false>) or
|
|
|
|
try_complete(complete_word<false, true>)))
|
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
|
2014-07-30 20:58:34 +02:00
|
|
|
(try_complete(complete_word<true, false>) or
|
|
|
|
try_complete(complete_word<true, true>)))
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::menu_show()
|
|
|
|
{
|
|
|
|
if (not m_context.has_ui())
|
|
|
|
return;
|
2014-05-07 20:51:01 +02:00
|
|
|
CharCoord menu_pos = m_context.window().display_position(m_completions.begin);
|
2014-04-28 22:54:00 +02:00
|
|
|
|
2014-11-05 00:51:41 +01:00
|
|
|
const CharCount tabstop = m_options["tabstop"].get<int>();
|
2014-04-28 22:54:00 +02:00
|
|
|
const CharCount column = get_column(m_context.buffer(), tabstop,
|
|
|
|
m_completions.begin);
|
|
|
|
std::vector<String> menu_entries;
|
|
|
|
for (auto& candidate : m_matching_candidates)
|
2014-11-07 14:54:43 +01:00
|
|
|
menu_entries.push_back(expand_tabs(candidate.first, tabstop, column));
|
2014-04-28 22:54:00 +02:00
|
|
|
|
|
|
|
m_context.ui().menu_show(menu_entries, menu_pos,
|
2014-07-11 01:27:04 +02:00
|
|
|
get_face("MenuForeground"),
|
|
|
|
get_face("MenuBackground"),
|
2014-04-28 22:54:00 +02:00
|
|
|
MenuStyle::Inline);
|
|
|
|
m_context.ui().menu_select(m_current_candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::on_option_changed(const Option& opt)
|
|
|
|
{
|
2014-11-05 00:51:41 +01:00
|
|
|
auto& completers = m_options["completers"].get<InsertCompleterDescList>();
|
2014-08-19 19:56:11 +02:00
|
|
|
std::vector<StringView> option_names;
|
2014-04-28 22:54:00 +02:00
|
|
|
for (auto& completer : completers)
|
|
|
|
{
|
2014-08-19 19:56:11 +02:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option)
|
|
|
|
option_names.emplace_back(*completer.param);
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
if (contains(option_names, opt.name()))
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
setup_ifn();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CompleteFunc>
|
|
|
|
bool InsertCompleter::try_complete(CompleteFunc complete_func)
|
|
|
|
{
|
|
|
|
auto& buffer = m_context.buffer();
|
2014-05-07 20:51:01 +02:00
|
|
|
ByteCoord cursor_pos = m_context.selections().main().cursor();
|
2014-04-28 22:54:00 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_completions = complete_func(buffer, cursor_pos);
|
|
|
|
}
|
|
|
|
catch (runtime_error& e)
|
|
|
|
{
|
|
|
|
write_debug("error while trying to run completer: "_str + e.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (not m_completions.is_valid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
kak_assert(cursor_pos >= m_completions.begin);
|
|
|
|
m_matching_candidates = m_completions.candidates;
|
|
|
|
m_current_candidate = m_matching_candidates.size();
|
|
|
|
menu_show();
|
2014-11-07 14:54:43 +01:00
|
|
|
m_matching_candidates.emplace_back(buffer.string(m_completions.begin, m_completions.end), "");
|
2014-04-28 22:54:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_file_complete()
|
|
|
|
{
|
2014-05-07 20:51:01 +02:00
|
|
|
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-11-05 00:51:41 +01:00
|
|
|
return complete_filename<false>(buffer, cursor_pos, m_options);
|
2014-04-28 22:54:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_word_complete()
|
|
|
|
{
|
2014-07-30 20:58:34 +02:00
|
|
|
try_complete(complete_word<true, true>);
|
2014-04-28 22:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_line_complete()
|
|
|
|
{
|
2014-05-07 20:51:01 +02:00
|
|
|
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-04-28 22:54:00 +02:00
|
|
|
return complete_line(buffer, cursor_pos);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|