Add an experimental configurable input completion fonction

The completions option (a std::vector<String>) is used for
completions candidates, if it's first element matches the current
cursor position and buffer timestamp.
This commit is contained in:
Maxime Coste 2013-03-09 14:23:19 +01:00
parent 4db6e3e917
commit ddc894ccfe
2 changed files with 29 additions and 1 deletions

View File

@ -482,6 +482,28 @@ static std::pair<CandidateList, BufferIterator> complete_word(const BufferIterat
return { std::move(result), begin }; return { std::move(result), begin };
} }
static std::pair<CandidateList, BufferIterator> complete_opt(const BufferIterator& pos, OptionManager& options)
{
using StringList = std::vector<String>;
const StringList& opt = options["completions"].get<StringList>();
if (opt.empty())
return { {}, pos };
auto& desc = opt[0];
Regex re(R"((\d+):(\d+)@(\d+))");
boost::match_results<String::iterator> match;
if (boost::regex_match(desc.begin(), desc.end(), match, re))
{
LineCount line = str_to_int(String(match[1].first, match[1].second)) - 1;
ByteCount column = str_to_int(String(match[2].first, match[2].second)) - 1;
int timestamp = str_to_int(String(match[3].first, match[3].second));
if (timestamp == pos.buffer().timestamp() and line == pos.line() and column == pos.column())
return { { opt.begin() + 1, opt.end() }, pos };
}
return { {}, pos };
}
class BufferCompleter class BufferCompleter
{ {
public: public:
@ -507,7 +529,9 @@ private:
if (not m_position.is_valid()) if (not m_position.is_valid())
{ {
BufferIterator cursor = context.editor().selections().back().last(); BufferIterator cursor = context.editor().selections().back().last();
auto completions = complete_word(cursor); auto completions = complete_opt(cursor, context.options());
if (completions.first.empty())
completions = complete_word(cursor);
m_completions = std::move(completions.first); m_completions = std::move(completions.first);
if (m_completions.empty()) if (m_completions.empty())
return false; return false;

View File

@ -121,6 +121,9 @@ template void Option::set<bool>(const bool&);
template const std::vector<int>& Option::get<std::vector<int>>() const; template const std::vector<int>& Option::get<std::vector<int>>() const;
template void Option::set<std::vector<int>>(const std::vector<int>&); template void Option::set<std::vector<int>>(const std::vector<int>&);
template const std::vector<String>& Option::get<std::vector<String>>() const;
template void Option::set<std::vector<String>>(const std::vector<String>&);
OptionManager::OptionManager(OptionManager& parent) OptionManager::OptionManager(OptionManager& parent)
: m_parent(&parent) : m_parent(&parent)
{ {
@ -235,6 +238,7 @@ GlobalOptions::GlobalOptions()
declare_option<bool>("incsearch", true); declare_option<bool>("incsearch", true);
declare_option<String>("ignored_files", R"(^(\..*|.*\.(o|so|a))$)"); declare_option<String>("ignored_files", R"(^(\..*|.*\.(o|so|a))$)");
declare_option<String>("filetype", ""); declare_option<String>("filetype", "");
declare_option<std::vector<String>>("completions", {});
} }
template<typename T> template<typename T>