summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-07-30 19:58:34 +0100
committerMaxime Coste <frrrwww@gmail.com>2014-07-30 19:58:34 +0100
commit5fc063c520afea4cdc8593eefa690f1ea804f1f4 (patch)
tree3890081a9beab05cb7740834455a3267f74ad266 /src
parent4c31449a6505f802fe990014cc9aa095e3fa2f60 (diff)
Insert word completion: Use subsequence matching if prefix failed
Diffstat (limited to 'src')
-rw-r--r--src/insert_completer.cc23
-rw-r--r--src/word_db.cc13
-rw-r--r--src/word_db.hh1
3 files changed, 25 insertions, 12 deletions
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<WordDB>();
}
-template<bool other_buffers>
+template<bool other_buffers, bool subseq>
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<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());
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<false>(buffer, cursor_pos);
- }))
+ (try_complete(complete_word<false, false>) or
+ try_complete(complete_word<false, true>)))
return true;
if (completer == "word=all" and
- try_complete([this](const Buffer& buffer, ByteCoord cursor_pos) {
- return complete_word<true>(buffer, cursor_pos);
- }))
+ (try_complete(complete_word<true, false>) or
+ try_complete(complete_word<true, true>)))
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<true>(buffer, cursor_pos);
- });
+ try_complete(complete_word<true, true>);
}
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<String> WordDB::find_prefix(const String& prefix)
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
{
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<String> find_prefix(const String& prefix);
+ std::vector<String> find_subsequence(const String& subsequence);
int get_word_occurences(const String& word) const;
using WordList = std::map<String, int>;