diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 1f17e888..205b99e8 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -28,7 +28,7 @@ WordDB& get_word_db(const Buffer& buffer) return cache_val.as(); } -template +template InsertCompletion complete_word(const Buffer& buffer, ByteCoord 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); std::unordered_set 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()); 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) 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()); } } @@ -285,14 +288,12 @@ bool InsertCompleter::setup_ifn() })) return true; if (completer == "word=buffer" and - try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_word(buffer, cursor_pos); - })) + (try_complete(complete_word) or + try_complete(complete_word))) return true; if (completer == "word=all" and - try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_word(buffer, cursor_pos); - })) + (try_complete(complete_word) or + try_complete(complete_word))) return true; } return false; @@ -370,9 +371,7 @@ void InsertCompleter::explicit_file_complete() void InsertCompleter::explicit_word_complete() { - try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) { - return complete_word(buffer, cursor_pos); - }); + try_complete(complete_word); } void InsertCompleter::explicit_line_complete() diff --git a/src/word_db.cc b/src/word_db.cc index dc08f102..e3be4245 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -118,6 +118,19 @@ std::vector WordDB::find_prefix(const String& prefix) return res; } +std::vector WordDB::find_subsequence(const String& subsequence) +{ + update_db(); + + std::vector 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 { auto it = m_words.find(word); diff --git a/src/word_db.hh b/src/word_db.hh index 2349a618..043f9960 100644 --- a/src/word_db.hh +++ b/src/word_db.hh @@ -17,6 +17,7 @@ public: WordDB(const Buffer& buffer); std::vector find_prefix(const String& prefix); + std::vector find_subsequence(const String& subsequence); int get_word_occurences(const String& word) const; using WordList = std::map;