Add a static_words str-list option always considered for word completion

Fixes #313
This commit is contained in:
Maxime Coste 2015-12-27 08:49:15 +00:00
parent 978e8d18b9
commit c39a4d5879
5 changed files with 38 additions and 27 deletions

View File

@ -819,6 +819,8 @@ Some options are built in Kakoune, and can be used to control it's behaviour:
element of the list should follow the format: element of the list should follow the format:
_<line>.<column>[+<length>]@<timestamp>_ to define where the completion _<line>.<column>[+<length>]@<timestamp>_ to define where the completion
apply in the buffer, and the other strings are the candidates. apply in the buffer, and the other strings are the candidates.
* `static_words` _str-list_: list of words that are always added to completion
candidates when completing words in insert mode.
* `autoreload` _enum(yes|no|ask)_: auto reload the buffers when an external * `autoreload` _enum(yes|no|ask)_: auto reload the buffers when an external
modification is detected. modification is detected.
* `debug` _flags(hooks|shell|profile)_: dump various debug information in * `debug` _flags(hooks|shell|profile)_: dump various debug information in

View File

@ -123,6 +123,7 @@ hook global WinSetOption filetype=(c|cpp|objc) %[
alias window alt c-family-alternative-file alias window alt c-family-alternative-file
set window formatcmd "astyle" set window formatcmd "astyle"
set window static_words "void:char:short:int:long:signed:unsigned:float:double:size_t:while:for:if:else:do:switch:case:default:goto:asm:break:continue:return:sizeof:const:auto:register:inline:static:volatile:struct:enum:union:typedef:extern:restrict"
] ]
hook global WinSetOption filetype=(?!(c|cpp|objc)$).* %[ hook global WinSetOption filetype=(?!(c|cpp|objc)$).* %[

View File

@ -74,7 +74,7 @@ WordDB& get_word_db(const Buffer& buffer)
} }
template<bool other_buffers> template<bool other_buffers>
InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos) InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos, const OptionManager& options)
{ {
using Utf8It = utf8::iterator<BufferIterator>; using Utf8It = utf8::iterator<BufferIterator>;
Utf8It pos{buffer.iterator_at(cursor_pos), buffer}; Utf8It pos{buffer.iterator_at(cursor_pos), buffer};
@ -129,6 +129,20 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
add_matches(*buf); add_matches(*buf);
} }
} }
{
using StaticWords = Vector<String, MemoryDomain::Options>;
const StaticWords& static_words = options["static_words"].get<StaticWords>();
for (auto& word : static_words)
{
if (prefix.empty())
matches.push_back(RankedMatch{word, prefix});
else if (RankedMatch match{word, prefix})
matches.push_back(match);
}
}
unordered_erase(matches, StringView{prefix}); unordered_erase(matches, StringView{prefix});
std::sort(matches.begin(), matches.end()); std::sort(matches.begin(), matches.end());
matches.erase(std::unique(matches.begin(), matches.end()), matches.end()); matches.erase(std::unique(matches.begin(), matches.end()), matches.end());
@ -160,7 +174,7 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
template<bool require_slash> template<bool require_slash>
InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos, InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
OptionManager& options) const OptionManager& options)
{ {
auto pos = buffer.iterator_at(cursor_pos); auto pos = buffer.iterator_at(cursor_pos);
auto begin = pos; auto begin = pos;
@ -204,7 +218,7 @@ InsertCompletion complete_filename(const Buffer& buffer, ByteCoord cursor_pos,
} }
InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos, InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos,
OptionManager& options, StringView option_name) const OptionManager& options, StringView option_name)
{ {
const StringList& opt = options[option_name].get<StringList>(); const StringList& opt = options[option_name].get<StringList>();
if (opt.empty()) if (opt.empty())
@ -280,7 +294,7 @@ InsertCompletion complete_option(const Buffer& buffer, ByteCoord cursor_pos,
return {}; return {};
} }
InsertCompletion complete_line(const Buffer& buffer, OptionManager& options, ByteCoord cursor_pos) InsertCompletion complete_line(const Buffer& buffer, ByteCoord cursor_pos, const OptionManager& options)
{ {
const CharCount tabstop = options["tabstop"].get<int>(); const CharCount tabstop = options["tabstop"].get<int>();
const CharCount column = get_column(buffer, tabstop, cursor_pos); const CharCount column = get_column(buffer, tabstop, cursor_pos);
@ -395,15 +409,12 @@ bool InsertCompleter::setup_ifn()
for (auto& completer : completers) for (auto& completer : completers)
{ {
if (completer.mode == InsertCompleterDesc::Filename and if (completer.mode == InsertCompleterDesc::Filename and
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete(complete_filename<true>))
return complete_filename<true>(buffer, cursor_pos,
m_options);
}))
return true; return true;
if (completer.mode == InsertCompleterDesc::Option and if (completer.mode == InsertCompleterDesc::Option and
try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete([&,this](const Buffer& buffer, ByteCoord cursor_pos,
return complete_option(buffer, cursor_pos, const OptionManager& options) {
m_options, *completer.param); return complete_option(buffer, cursor_pos, options, *completer.param);
})) }))
return true; return true;
if (completer.mode == InsertCompleterDesc::Word and if (completer.mode == InsertCompleterDesc::Word and
@ -457,14 +468,14 @@ void InsertCompleter::on_option_changed(const Option& opt)
} }
} }
template<typename CompleteFunc> template<typename Func>
bool InsertCompleter::try_complete(CompleteFunc complete_func) bool InsertCompleter::try_complete(Func complete_func)
{ {
auto& buffer = m_context.buffer(); auto& buffer = m_context.buffer();
ByteCoord cursor_pos = m_context.selections().main().cursor(); ByteCoord cursor_pos = m_context.selections().main().cursor();
try try
{ {
m_completions = complete_func(buffer, cursor_pos); m_completions = complete_func(buffer, cursor_pos, m_options);
} }
catch (runtime_error& e) catch (runtime_error& e)
{ {
@ -484,11 +495,8 @@ bool InsertCompleter::try_complete(CompleteFunc complete_func)
void InsertCompleter::explicit_file_complete() void InsertCompleter::explicit_file_complete()
{ {
auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete(complete_filename<false>);
return complete_filename<false>(buffer, cursor_pos, m_options); m_explicit_completer = complete_filename<false>;
};
try_complete(func);
m_explicit_completer = func;
} }
void InsertCompleter::explicit_word_complete() void InsertCompleter::explicit_word_complete()
@ -499,11 +507,8 @@ void InsertCompleter::explicit_word_complete()
void InsertCompleter::explicit_line_complete() void InsertCompleter::explicit_line_complete()
{ {
auto func = [this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete(complete_line);
return complete_line(buffer, m_options, cursor_pos); m_explicit_completer = complete_line;
};
try_complete(func);
m_explicit_completer = func;
} }
} }

View File

@ -76,8 +76,8 @@ public:
private: private:
bool setup_ifn(); bool setup_ifn();
template<typename CompleteFunc> template<typename Func>
bool try_complete(CompleteFunc complete_func); bool try_complete(Func complete_func);
void on_option_changed(const Option& opt) override; void on_option_changed(const Option& opt) override;
void menu_show(); void menu_show();
@ -90,7 +90,8 @@ private:
CandidateList m_matching_candidates; CandidateList m_matching_candidates;
int m_current_candidate = -1; int m_current_candidate = -1;
std::function<InsertCompletion (const Buffer&, ByteCoord)> m_explicit_completer; using CompleteFunc = InsertCompletion (const Buffer&, ByteCoord, const OptionManager& options);
std::function<CompleteFunc> m_explicit_completer;
}; };
} }

View File

@ -220,6 +220,8 @@ void register_options()
InsertCompleterDesc{ InsertCompleterDesc::Filename }, InsertCompleterDesc{ InsertCompleterDesc::Filename },
InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str } InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str }
}), OptionFlags::None); }), OptionFlags::None);
reg.declare_option("static_words", "list of words to always consider for insert word completion",
Vector<String, MemoryDomain::Options>{});
reg.declare_option("autoreload", reg.declare_option("autoreload",
"autoreload buffer when a filesystem modification is detected", "autoreload buffer when a filesystem modification is detected",
Autoreload::Ask); Autoreload::Ask);