2014-04-29 00:54:00 +04:00
|
|
|
#include "insert_completer.hh"
|
|
|
|
|
|
|
|
#include "buffer_manager.hh"
|
|
|
|
#include "buffer_utils.hh"
|
|
|
|
#include "context.hh"
|
|
|
|
#include "display_buffer.hh"
|
2014-07-11 03:27:04 +04:00
|
|
|
#include "face_registry.hh"
|
2014-04-29 00:54:00 +04:00
|
|
|
#include "file.hh"
|
2014-10-13 16:12:33 +04:00
|
|
|
#include "regex.hh"
|
2014-04-29 00:54:00 +04:00
|
|
|
#include "user_interface.hh"
|
|
|
|
#include "window.hh"
|
|
|
|
#include "word_db.hh"
|
|
|
|
|
2015-09-02 23:15:16 +03:00
|
|
|
#include <numeric>
|
2015-09-02 22:59:45 +03:00
|
|
|
|
2014-04-29 00:54:00 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-01-12 16:24:30 +03:00
|
|
|
using StringList = Vector<String, MemoryDomain::Options>;
|
2014-04-29 00:54:00 +04:00
|
|
|
|
2014-08-19 21:56:11 +04: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-08 00:11:55 +04:00
|
|
|
void option_from_string(StringView str, InsertCompleterDesc& opt)
|
2014-08-19 21:56:11 +04: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;
|
|
|
|
}
|
2015-06-01 23:15:59 +03:00
|
|
|
throw runtime_error(format("invalid completer description: '{}'", str));
|
2014-08-19 21:56:11 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 00:54:00 +04: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>();
|
|
|
|
}
|
|
|
|
|
2015-10-18 18:55:21 +03:00
|
|
|
template<bool other_buffers>
|
2014-05-07 22:51:01 +04:00
|
|
|
InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
|
|
|
auto pos = buffer.iterator_at(cursor_pos);
|
2014-07-03 00:14:01 +04:00
|
|
|
if (pos == buffer.begin() or not is_word(*utf8::previous(pos, buffer.begin())))
|
2014-04-29 00:54:00 +04: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};
|
|
|
|
|
2015-10-22 21:49:08 +03:00
|
|
|
struct RankedMatchAndBuffer : RankedMatch
|
2015-10-18 18:55:21 +03:00
|
|
|
{
|
2015-10-22 21:49:08 +03:00
|
|
|
RankedMatchAndBuffer(StringView w, int r = 0, const Buffer* b = nullptr)
|
|
|
|
: RankedMatch{w, r}, buffer{b} {}
|
2014-04-29 00:54:00 +04:00
|
|
|
|
2015-10-22 21:49:08 +03:00
|
|
|
bool operator==(const RankedMatchAndBuffer& other) const { return word == other.word; }
|
|
|
|
bool operator<(const RankedMatchAndBuffer& other) const { return rank > other.rank; }
|
2015-09-02 15:43:07 +03:00
|
|
|
|
|
|
|
const Buffer* buffer;
|
|
|
|
};
|
2015-10-22 21:49:08 +03:00
|
|
|
Vector<RankedMatchAndBuffer> matches;
|
2015-09-02 15:43:07 +03:00
|
|
|
|
|
|
|
auto add_matches = [&](const Buffer& buf) {
|
|
|
|
auto& word_db = get_word_db(buf);
|
2015-10-18 18:55:21 +03:00
|
|
|
auto bufmatches = word_db.find_matching(prefix);
|
2015-09-02 15:43:07 +03:00
|
|
|
for (auto& m : bufmatches)
|
2015-10-18 18:55:21 +03:00
|
|
|
matches.push_back({ m.word, m.rank, &buf });
|
2015-09-02 15:43:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
add_matches(buffer);
|
|
|
|
|
|
|
|
if (get_word_db(buffer).get_word_occurences(current_word) <= 1)
|
|
|
|
unordered_erase(matches, StringView{current_word});
|
2014-04-29 00:54:00 +04:00
|
|
|
|
|
|
|
if (other_buffers)
|
|
|
|
{
|
|
|
|
for (const auto& buf : BufferManager::instance())
|
|
|
|
{
|
|
|
|
if (buf.get() == &buffer)
|
|
|
|
continue;
|
2015-09-02 15:43:07 +03:00
|
|
|
add_matches(*buf);
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 15:43:07 +03:00
|
|
|
unordered_erase(matches, StringView{prefix});
|
2015-10-20 15:50:55 +03:00
|
|
|
// Sort by word, favoring lowercase
|
2015-10-19 21:39:05 +03:00
|
|
|
std::sort(matches.begin(), matches.end(),
|
2015-10-22 21:49:08 +03:00
|
|
|
[](const RankedMatchAndBuffer& lhs, const RankedMatchAndBuffer& rhs) {
|
2015-10-20 15:50:55 +03:00
|
|
|
return std::lexicographical_compare(
|
|
|
|
lhs.word.begin(), lhs.word.end(), rhs.word.begin(), rhs.word.end(),
|
|
|
|
[](char a, char b) {
|
|
|
|
const bool low_a = islower(a), low_b = islower(b);
|
|
|
|
return low_a == low_b ? a < b : low_a;
|
|
|
|
});
|
2015-10-19 21:39:05 +03:00
|
|
|
});
|
2014-12-10 00:59:47 +03:00
|
|
|
matches.erase(std::unique(matches.begin(), matches.end()), matches.end());
|
2015-10-20 15:50:55 +03:00
|
|
|
// Stable sort by rank to preserve by word sorting
|
|
|
|
std::stable_sort(matches.begin(), matches.end());
|
2014-12-10 00:59:47 +03:00
|
|
|
|
2015-09-02 15:43:07 +03:00
|
|
|
const auto longest = std::accumulate(matches.begin(), matches.end(), 0_char,
|
2015-10-22 21:49:08 +03:00
|
|
|
[](const CharCount& lhs, const RankedMatchAndBuffer& rhs)
|
2015-10-18 18:55:21 +03:00
|
|
|
{ return std::max(lhs, rhs.word.char_length()); });
|
2015-09-02 15:43:07 +03:00
|
|
|
|
|
|
|
InsertCompletion::CandidateList candidates;
|
|
|
|
candidates.reserve(matches.size());
|
2014-12-10 00:59:47 +03:00
|
|
|
for (auto& m : matches)
|
2015-09-02 15:43:07 +03:00
|
|
|
{
|
2015-10-05 03:48:00 +03:00
|
|
|
DisplayLine menu_entry;
|
2015-09-02 15:43:07 +03:00
|
|
|
if (m.buffer)
|
2015-09-11 15:03:06 +03:00
|
|
|
{
|
2015-10-18 18:55:21 +03:00
|
|
|
const auto pad_len = longest + 1 - m.word.char_length();
|
|
|
|
menu_entry.push_back(m.word.str());
|
2015-10-06 15:37:16 +03:00
|
|
|
menu_entry.push_back(String{' ', pad_len});
|
2015-10-05 03:48:00 +03:00
|
|
|
menu_entry.push_back({ m.buffer->display_name(), get_face("MenuInfo") });
|
2015-09-11 15:03:06 +03:00
|
|
|
}
|
2015-10-05 03:48:00 +03:00
|
|
|
else
|
2015-10-18 18:55:21 +03:00
|
|
|
menu_entry.push_back(m.word.str());
|
|
|
|
|
|
|
|
menu_entry.push_back({ " " + to_string(m.rank), get_face("cyan") });
|
2014-12-10 00:59:47 +03:00
|
|
|
|
2015-10-18 18:55:21 +03:00
|
|
|
candidates.push_back({m.word.str(), "", std::move(menu_entry)});
|
2015-09-02 15:43:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return { begin.coord(), cursor_pos, std::move(candidates), buffer.timestamp() };
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<bool require_slash>
|
2014-05-07 22:51:01 +04:00
|
|
|
InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
|
2014-04-29 00:54:00 +04: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 {};
|
|
|
|
|
2015-09-02 15:43:07 +03:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2014-04-29 00:54:00 +04:00
|
|
|
if (prefix.front() == '/')
|
2014-11-07 16:54:43 +03:00
|
|
|
{
|
|
|
|
for (auto& filename : Kakoune::complete_filename(prefix, Regex{}))
|
2015-10-06 15:37:16 +03:00
|
|
|
candidates.push_back({ filename, "", filename });
|
2014-11-07 16:54:43 +03:00
|
|
|
}
|
2014-04-29 00:54:00 +04: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{}))
|
2015-10-05 03:48:00 +03:00
|
|
|
{
|
|
|
|
StringView candidate = filename.substr(dir.length());
|
2015-10-06 15:37:16 +03:00
|
|
|
candidates.push_back({ candidate.str(), "", candidate.str() });
|
2015-10-05 03:48:00 +03:00
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 15:43:07 +03:00
|
|
|
if (candidates.empty())
|
2014-04-29 00:54:00 +04:00
|
|
|
return {};
|
2015-09-02 15:43:07 +03:00
|
|
|
return { begin.coord(), pos.coord(), std::move(candidates), buffer.timestamp() };
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
2014-05-07 22:51:01 +04:00
|
|
|
InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos,
|
2014-06-26 22:01:39 +04:00
|
|
|
OptionManager& options, StringView option_name)
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
2014-10-13 16:12:33 +04:00
|
|
|
const StringList& opt = options[option_name].get<StringList>();
|
2014-04-29 00:54:00 +04:00
|
|
|
if (opt.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto& desc = opt[0];
|
|
|
|
static const Regex re(R"((\d+)\.(\d+)(?:\+(\d+))?@(\d+))");
|
2014-10-13 16:12:33 +04:00
|
|
|
MatchResults<String::const_iterator> match;
|
|
|
|
if (regex_match(desc.begin(), desc.end(), match, re))
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
ByteCoord coord{ str_to_int({match[1].first, match[1].second}) - 1,
|
|
|
|
str_to_int({match[2].first, match[2].second}) - 1 };
|
2014-04-29 00:54:00 +04:00
|
|
|
if (not buffer.is_valid(coord))
|
|
|
|
return {};
|
|
|
|
auto end = coord;
|
|
|
|
if (match[3].matched)
|
|
|
|
{
|
2015-03-10 22:33:46 +03:00
|
|
|
ByteCount len = str_to_int({match[3].first, match[3].second});
|
2014-04-29 00:54:00 +04:00
|
|
|
end = buffer.advance(coord, len);
|
|
|
|
}
|
2015-03-10 22:33:46 +03:00
|
|
|
size_t timestamp = (size_t)str_to_int({match[4].first, match[4].second});
|
2014-11-17 16:53:59 +03:00
|
|
|
auto changes = buffer.changes_since(timestamp);
|
|
|
|
if (find_if(changes, [&](const Buffer::Change& change){
|
|
|
|
return change.begin < coord;
|
|
|
|
}) != changes.end())
|
|
|
|
return {};
|
2014-04-29 00:54:00 +04:00
|
|
|
|
2014-11-17 23:15:54 +03:00
|
|
|
if (cursor_pos.line == coord.line and cursor_pos.column >= coord.column)
|
2014-11-07 16:54:43 +03:00
|
|
|
{
|
2014-11-17 23:15:54 +03:00
|
|
|
StringView prefix = buffer[coord.line].substr(
|
|
|
|
coord.column, cursor_pos.column - coord.column);
|
|
|
|
|
2015-10-05 03:48:00 +03:00
|
|
|
|
|
|
|
const CharCount tabstop = options["tabstop"].get<int>();
|
|
|
|
const CharCount column = get_column(buffer, tabstop, cursor_pos);
|
|
|
|
|
2015-09-02 15:43:07 +03:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2014-11-07 16:54:43 +03:00
|
|
|
for (auto it = opt.begin() + 1; it != opt.end(); ++it)
|
|
|
|
{
|
2014-11-17 23:15:54 +03:00
|
|
|
auto splitted = split(*it, '@');
|
|
|
|
if (not splitted.empty() and prefix_match(splitted[0], prefix))
|
2015-09-02 15:43:07 +03:00
|
|
|
{
|
|
|
|
StringView completion = splitted[0];
|
|
|
|
StringView docstring = splitted.size() > 1 ? splitted[1] : StringView{};
|
|
|
|
|
2015-10-06 15:37:16 +03:00
|
|
|
DisplayLine menu_entry = splitted.size() > 2 ?
|
|
|
|
parse_display_line(expand_tabs(splitted[2], tabstop, column))
|
|
|
|
: DisplayLine{ expand_tabs(splitted[0], tabstop, column) };
|
|
|
|
|
|
|
|
candidates.push_back({completion.str(), docstring.str(), std::move(menu_entry)});
|
2015-09-02 15:43:07 +03:00
|
|
|
}
|
2014-11-07 16:54:43 +03:00
|
|
|
}
|
2015-09-02 15:43:07 +03:00
|
|
|
return { coord, end, std::move(candidates), timestamp };
|
2014-11-07 16:54:43 +03:00
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2015-10-05 03:48:00 +03:00
|
|
|
InsertCompletion complete_line(const Buffer& buffer, OptionManager& options, ByteCoord cursor_pos)
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
2015-10-05 03:48:00 +03:00
|
|
|
const CharCount tabstop = options["tabstop"].get<int>();
|
|
|
|
const CharCount column = get_column(buffer, tabstop, cursor_pos);
|
|
|
|
|
2014-06-26 22:01:39 +04:00
|
|
|
StringView prefix = buffer[cursor_pos.line].substr(0_byte, cursor_pos.column);
|
2015-09-02 15:43:07 +03:00
|
|
|
InsertCompletion::CandidateList candidates;
|
2014-04-29 00:54:00 +04: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()))
|
2015-10-05 03:48:00 +03:00
|
|
|
{
|
|
|
|
StringView candidate = buffer[l].substr(0_byte, len-1);
|
|
|
|
candidates.push_back({candidate.str(), "",
|
2015-10-06 15:37:16 +03:00
|
|
|
expand_tabs(candidate, tabstop, column)});
|
2015-10-05 03:48:00 +03:00
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
2015-09-02 15:43:07 +03:00
|
|
|
if (candidates.empty())
|
2014-04-29 00:54:00 +04:00
|
|
|
return {};
|
2015-09-02 15:43:07 +03:00
|
|
|
std::sort(candidates.begin(), candidates.end());
|
|
|
|
candidates.erase(std::unique(candidates.begin(), candidates.end()), candidates.end());
|
|
|
|
return { cursor_pos.line, cursor_pos, std::move(candidates), buffer.timestamp() };
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertCompleter::InsertCompleter(const Context& context)
|
2014-11-05 02:51:41 +03:00
|
|
|
: m_context(context), m_options(context.options())
|
2014-11-02 19:04:24 +03:00
|
|
|
{
|
2014-11-05 02:51:41 +03:00
|
|
|
m_options.register_watcher(*this);
|
2014-11-02 19:04:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
InsertCompleter::~InsertCompleter()
|
|
|
|
{
|
2014-11-05 02:51:41 +03:00
|
|
|
m_options.unregister_watcher(*this);
|
2014-11-02 19:04:24 +03:00
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
|
2015-03-15 21:44:11 +03:00
|
|
|
void InsertCompleter::select(int offset, Vector<Key>& keystrokes)
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
|
|
|
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();
|
2015-08-30 13:05:48 +03:00
|
|
|
const InsertCompletion::Candidate& candidate = m_matching_candidates[m_current_candidate];
|
2014-05-14 03:59:36 +04:00
|
|
|
auto& selections = m_context.selections();
|
|
|
|
const auto& cursor_pos = selections.main().cursor();
|
2014-04-29 00:54:00 +04: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 03:59:36 +04:00
|
|
|
for (auto& sel : selections)
|
2014-04-29 00:54:00 +04:00
|
|
|
{
|
2014-05-24 20:08:01 +04: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-29 00:54:00 +04:00
|
|
|
std::equal(ref.begin(), ref.end(), pos - prefix_len))
|
|
|
|
{
|
|
|
|
pos = buffer.erase(pos - prefix_len, pos + suffix_len);
|
2015-08-30 13:05:48 +03:00
|
|
|
buffer.insert(pos, candidate.completion);
|
2014-05-14 03:59:36 +04:00
|
|
|
const_cast<SelectionList&>(selections).update();
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 21:18:29 +04:00
|
|
|
m_completions.end = cursor_pos;
|
2015-08-30 13:05:48 +03:00
|
|
|
m_completions.begin = buffer.advance(cursor_pos, -candidate.completion.length());
|
2014-04-29 00:54:00 +04:00
|
|
|
m_completions.timestamp = buffer.timestamp();
|
|
|
|
if (m_context.has_ui())
|
2014-11-07 16:54:43 +03:00
|
|
|
{
|
2014-04-29 00:54:00 +04:00
|
|
|
m_context.ui().menu_select(m_current_candidate);
|
2015-08-30 13:05:48 +03:00
|
|
|
if (not candidate.docstring.empty())
|
|
|
|
m_context.ui().info_show(candidate.completion, candidate.docstring, CharCoord{},
|
2014-11-08 20:59:38 +03:00
|
|
|
get_face("Information"), InfoStyle::MenuDoc);
|
2014-11-12 02:31:06 +03:00
|
|
|
}
|
2015-03-15 21:44:11 +03:00
|
|
|
|
|
|
|
for (auto i = 0_byte; i < prefix_len; ++i)
|
|
|
|
keystrokes.push_back(Key::Backspace);
|
|
|
|
for (auto i = 0_byte; i < suffix_len; ++i)
|
|
|
|
keystrokes.push_back(Key::Delete);
|
2015-08-30 13:05:48 +03:00
|
|
|
for (auto& c : candidate.completion)
|
2015-03-15 21:44:11 +03:00
|
|
|
keystrokes.push_back(c);
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::update()
|
|
|
|
{
|
2015-10-19 15:43:23 +03:00
|
|
|
if (m_explicit_completer and try_complete(m_explicit_completer))
|
|
|
|
return;
|
|
|
|
|
2014-04-29 00:54:00 +04:00
|
|
|
reset();
|
|
|
|
setup_ifn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::reset()
|
|
|
|
{
|
|
|
|
m_completions = InsertCompletion{};
|
2015-10-19 15:43:23 +03:00
|
|
|
m_explicit_completer = nullptr;
|
2014-04-29 00:54:00 +04:00
|
|
|
if (m_context.has_ui())
|
2014-11-12 02:31:06 +03:00
|
|
|
{
|
2014-04-29 00:54:00 +04:00
|
|
|
m_context.ui().menu_hide();
|
2014-11-12 02:31:06 +03:00
|
|
|
m_context.ui().info_hide();
|
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InsertCompleter::setup_ifn()
|
|
|
|
{
|
|
|
|
using namespace std::placeholders;
|
|
|
|
if (not m_completions.is_valid())
|
|
|
|
{
|
2014-11-05 02:51:41 +03:00
|
|
|
auto& completers = m_options["completers"].get<InsertCompleterDescList>();
|
2014-04-29 00:54:00 +04:00
|
|
|
for (auto& completer : completers)
|
|
|
|
{
|
2014-08-19 21:56:11 +04:00
|
|
|
if (completer.mode == InsertCompleterDesc::Filename and
|
2014-05-07 22:51:01 +04:00
|
|
|
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-04-29 00:54:00 +04:00
|
|
|
return complete_filename<true>(buffer, cursor_pos,
|
2014-11-05 02:51:41 +03:00
|
|
|
m_options);
|
2014-04-29 00:54:00 +04:00
|
|
|
}))
|
|
|
|
return true;
|
2014-08-19 21:56:11 +04:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option and
|
2014-05-07 22:51:01 +04:00
|
|
|
try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-04-29 00:54:00 +04:00
|
|
|
return complete_option(buffer, cursor_pos,
|
2014-11-05 02:51:41 +03:00
|
|
|
m_options, *completer.param);
|
2014-04-29 00:54:00 +04:00
|
|
|
}))
|
|
|
|
return true;
|
2014-08-19 21:56:11 +04:00
|
|
|
if (completer.mode == InsertCompleterDesc::Word and
|
|
|
|
*completer.param == "buffer" and
|
2015-10-18 18:55:21 +03:00
|
|
|
try_complete(complete_word<false>))
|
2014-04-29 00:54:00 +04:00
|
|
|
return true;
|
2014-08-19 21:56:11 +04:00
|
|
|
if (completer.mode == InsertCompleterDesc::Word and
|
|
|
|
*completer.param == "all" and
|
2015-10-18 18:55:21 +03:00
|
|
|
try_complete(complete_word<true>))
|
2014-04-29 00:54:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::menu_show()
|
|
|
|
{
|
|
|
|
if (not m_context.has_ui())
|
|
|
|
return;
|
2014-05-07 22:51:01 +04:00
|
|
|
CharCoord menu_pos = m_context.window().display_position(m_completions.begin);
|
2014-04-29 00:54:00 +04:00
|
|
|
|
2015-10-05 03:25:23 +03:00
|
|
|
Vector<DisplayLine> menu_entries;
|
2014-04-29 00:54:00 +04:00
|
|
|
for (auto& candidate : m_matching_candidates)
|
2015-10-05 03:48:00 +03:00
|
|
|
menu_entries.push_back(candidate.menu_entry);
|
2014-04-29 00:54:00 +04:00
|
|
|
|
|
|
|
m_context.ui().menu_show(menu_entries, menu_pos,
|
2014-07-11 03:27:04 +04:00
|
|
|
get_face("MenuForeground"),
|
|
|
|
get_face("MenuBackground"),
|
2014-04-29 00:54:00 +04:00
|
|
|
MenuStyle::Inline);
|
|
|
|
m_context.ui().menu_select(m_current_candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::on_option_changed(const Option& opt)
|
|
|
|
{
|
2015-03-13 16:52:23 +03:00
|
|
|
// Do not reset the menu if the user has selected an entry
|
|
|
|
if (not m_matching_candidates.empty() and
|
|
|
|
m_current_candidate != m_matching_candidates.size() - 1)
|
|
|
|
return;
|
|
|
|
|
2014-11-05 02:51:41 +03:00
|
|
|
auto& completers = m_options["completers"].get<InsertCompleterDescList>();
|
2014-04-29 00:54:00 +04:00
|
|
|
for (auto& completer : completers)
|
|
|
|
{
|
2015-03-13 16:52:23 +03:00
|
|
|
if (completer.mode == InsertCompleterDesc::Option and
|
|
|
|
*completer.param == opt.name())
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
setup_ifn();
|
|
|
|
break;
|
|
|
|
}
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename CompleteFunc>
|
|
|
|
bool InsertCompleter::try_complete(CompleteFunc complete_func)
|
|
|
|
{
|
|
|
|
auto& buffer = m_context.buffer();
|
2014-05-07 22:51:01 +04:00
|
|
|
ByteCoord cursor_pos = m_context.selections().main().cursor();
|
2014-04-29 00:54:00 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_completions = complete_func(buffer, cursor_pos);
|
|
|
|
}
|
|
|
|
catch (runtime_error& e)
|
|
|
|
{
|
2015-06-06 13:54:48 +03:00
|
|
|
write_to_debug_buffer(format("error while trying to run completer: {}", e.what()));
|
2014-04-29 00:54:00 +04:00
|
|
|
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();
|
2015-08-30 13:05:48 +03:00
|
|
|
m_matching_candidates.push_back({buffer.string(m_completions.begin, m_completions.end), ""});
|
2014-04-29 00:54:00 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_file_complete()
|
|
|
|
{
|
2015-10-19 15:43:23 +03:00
|
|
|
auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2014-11-05 02:51:41 +03:00
|
|
|
return complete_filename<false>(buffer, cursor_pos, m_options);
|
2015-10-19 15:43:23 +03:00
|
|
|
};
|
|
|
|
try_complete(func);
|
|
|
|
m_explicit_completer = func;
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_word_complete()
|
|
|
|
{
|
2015-10-18 18:55:21 +03:00
|
|
|
try_complete(complete_word<true>);
|
2015-10-19 15:43:23 +03:00
|
|
|
m_explicit_completer = complete_word<true>;
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void InsertCompleter::explicit_line_complete()
|
|
|
|
{
|
2015-10-19 15:43:23 +03:00
|
|
|
auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) {
|
2015-10-05 03:48:00 +03:00
|
|
|
return complete_line(buffer, m_options, cursor_pos);
|
2015-10-19 15:43:23 +03:00
|
|
|
};
|
|
|
|
try_complete(func);
|
|
|
|
m_explicit_completer = func;
|
2014-04-29 00:54:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|