Insert word completion: Use subsequence matching if prefix failed

This commit is contained in:
Maxime Coste 2014-07-30 19:58:34 +01:00
parent 4c31449a65
commit 5fc063c520
3 changed files with 25 additions and 12 deletions

View File

@ -28,7 +28,7 @@ WordDB& get_word_db(const Buffer& buffer)
return cache_val.as<WordDB>(); return cache_val.as<WordDB>();
} }
template<bool other_buffers> template<bool other_buffers, bool subseq>
InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos) InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
{ {
auto pos = buffer.iterator_at(cursor_pos); auto pos = buffer.iterator_at(cursor_pos);
@ -51,7 +51,8 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
auto& word_db = get_word_db(buffer); auto& word_db = get_word_db(buffer);
std::unordered_set<String> matches; std::unordered_set<String> matches;
auto bufmatches = word_db.find_prefix(prefix); auto bufmatches = subseq ? word_db.find_subsequence(prefix)
: word_db.find_prefix(prefix);
matches.insert(bufmatches.begin(), bufmatches.end()); matches.insert(bufmatches.begin(), bufmatches.end());
if (word_db.get_word_occurences(current_word) <= 1) if (word_db.get_word_occurences(current_word) <= 1)
@ -63,7 +64,9 @@ InsertCompletion complete_word(const Buffer& buffer, ByteCoord cursor_pos)
{ {
if (buf.get() == &buffer) if (buf.get() == &buffer)
continue; continue;
bufmatches = get_word_db(*buf).find_prefix(prefix); auto buf_word_db = get_word_db(*buf);
bufmatches = subseq ? buf_word_db.find_subsequence(prefix)
: buf_word_db.find_prefix(prefix);
matches.insert(bufmatches.begin(), bufmatches.end()); matches.insert(bufmatches.begin(), bufmatches.end());
} }
} }
@ -285,14 +288,12 @@ bool InsertCompleter::setup_ifn()
})) }))
return true; return true;
if (completer == "word=buffer" and if (completer == "word=buffer" and
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { (try_complete(complete_word<false, false>) or
return complete_word<false>(buffer, cursor_pos); try_complete(complete_word<false, true>)))
}))
return true; return true;
if (completer == "word=all" and if (completer == "word=all" and
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { (try_complete(complete_word<true, false>) or
return complete_word<true>(buffer, cursor_pos); try_complete(complete_word<true, true>)))
}))
return true; return true;
} }
return false; return false;
@ -370,9 +371,7 @@ void InsertCompleter::explicit_file_complete()
void InsertCompleter::explicit_word_complete() void InsertCompleter::explicit_word_complete()
{ {
try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { try_complete(complete_word<true, true>);
return complete_word<true>(buffer, cursor_pos);
});
} }
void InsertCompleter::explicit_line_complete() void InsertCompleter::explicit_line_complete()

View File

@ -118,6 +118,19 @@ std::vector<String> WordDB::find_prefix(const String& prefix)
return res; return res;
} }
std::vector<String> WordDB::find_subsequence(const String& subsequence)
{
update_db();
std::vector<String> res;
for (auto it = m_words.begin(); it != m_words.end(); ++it)
{
if (subsequence_match(it->first, subsequence))
res.push_back(it->first);
}
return res;
}
int WordDB::get_word_occurences(const String& word) const int WordDB::get_word_occurences(const String& word) const
{ {
auto it = m_words.find(word); auto it = m_words.find(word);

View File

@ -17,6 +17,7 @@ public:
WordDB(const Buffer& buffer); WordDB(const Buffer& buffer);
std::vector<String> find_prefix(const String& prefix); std::vector<String> find_prefix(const String& prefix);
std::vector<String> find_subsequence(const String& subsequence);
int get_word_occurences(const String& word) const; int get_word_occurences(const String& word) const;
using WordList = std::map<String, int>; using WordList = std::map<String, int>;