kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

word_db.cc (6063B)


      1 #include "word_db.hh"
      2 
      3 #include "buffer.hh"
      4 #include "line_modification.hh"
      5 #include "unit_tests.hh"
      6 #include "value.hh"
      7 #include "word_splitter.hh"
      8 #include "option_manager.hh"
      9 
     10 namespace Kakoune
     11 {
     12 
     13 WordDB& get_word_db(const Buffer& buffer)
     14 {
     15     static const ValueId word_db_id = get_free_value_id();
     16     Value& cache_val = buffer.values()[word_db_id];
     17     if (not cache_val)
     18         cache_val = Value(WordDB{buffer});
     19     return cache_val.as<WordDB>();
     20 }
     21 
     22 static ConstArrayView<Codepoint> get_extra_word_chars(const Buffer& buffer)
     23 {
     24     return buffer.options()["extra_word_chars"].get<Vector<Codepoint, MemoryDomain::Options>>();
     25 }
     26 
     27 void WordDB::add_words(StringView line, ConstArrayView<Codepoint> extra_word_chars)
     28 {
     29     for (auto&& w : WordSplitter{line, extra_word_chars})
     30     {
     31         auto hash = hash_value(w);
     32         auto index = m_words.find_index(w, hash);
     33         if (index >= 0)
     34             ++m_words.item(index).value.refcount;
     35         else
     36         {
     37             auto word = intern(w, hash);
     38             auto view = word->strview();
     39             m_words.insert({view, {std::move(word), used_letters(view), 1}}, hash);
     40         }
     41     }
     42 }
     43 
     44 void WordDB::remove_words(StringView line, ConstArrayView<Codepoint> extra_word_chars)
     45 {
     46     for (auto&& w : WordSplitter{line, extra_word_chars})
     47     {
     48         auto it = m_words.find(w);
     49         kak_assert(it != m_words.end() and it->value.refcount > 0);
     50         if (--it->value.refcount == 0)
     51             m_words.unordered_remove(it->key);
     52     }
     53 }
     54 
     55 WordDB::WordDB(const Buffer& buffer)
     56     : m_buffer{&buffer}
     57 {
     58     buffer.options().register_watcher(*this);
     59     rebuild_db();
     60 }
     61 
     62 WordDB::WordDB(WordDB&& other) noexcept
     63     : m_buffer{std::move(other.m_buffer)},
     64       m_timestamp{other.m_timestamp},
     65       m_words{std::move(other.m_words)},
     66       m_lines{std::move(other.m_lines)}
     67 {
     68     kak_assert(m_buffer);
     69     m_buffer->options().unregister_watcher(other);
     70     other.m_buffer = nullptr;
     71 
     72     m_buffer->options().register_watcher(*this);
     73 }
     74 
     75 WordDB::~WordDB()
     76 {
     77     if (m_buffer)
     78         m_buffer->options().unregister_watcher(*this);
     79 }
     80 
     81 void WordDB::rebuild_db()
     82 {
     83     auto& buffer = *m_buffer;
     84 
     85     m_words.clear();
     86     m_lines.clear();
     87     m_lines.reserve((int)buffer.line_count());
     88     auto extra_word_chars = get_extra_word_chars(buffer);
     89     for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
     90     {
     91         m_lines.push_back(buffer.line_storage(line));
     92         add_words(m_lines.back()->strview(), extra_word_chars);
     93     }
     94     m_timestamp = buffer.timestamp();
     95 }
     96 
     97 void WordDB::update_db()
     98 {
     99     auto& buffer = *m_buffer;
    100 
    101     auto modifs = compute_line_modifications(buffer, m_timestamp);
    102     m_timestamp = buffer.timestamp();
    103 
    104     if (modifs.empty())
    105         return;
    106 
    107     Lines new_lines;
    108     new_lines.reserve((int)buffer.line_count());
    109 
    110     auto extra_word_chars = get_extra_word_chars(buffer);
    111     auto old_line = 0_line;
    112     for (auto& modif : modifs)
    113     {
    114         kak_assert(0_line <= modif.new_line and modif.new_line <= buffer.line_count());
    115         kak_assert(modif.new_line < buffer.line_count() or modif.num_added == 0);
    116         kak_assert(old_line <= modif.old_line);
    117         while (old_line < modif.old_line)
    118             new_lines.push_back(std::move(m_lines[(int)old_line++]));
    119 
    120         kak_assert((int)new_lines.size() == (int)modif.new_line);
    121 
    122         while (old_line < modif.old_line + modif.num_removed)
    123         {
    124             kak_assert(old_line < m_lines.size());
    125             remove_words(m_lines[(int)old_line++]->strview(), extra_word_chars);
    126         }
    127 
    128         for (auto l = 0_line; l < modif.num_added; ++l)
    129         {
    130             new_lines.push_back(buffer.line_storage(modif.new_line + l));
    131             add_words(new_lines.back()->strview(), extra_word_chars);
    132         }
    133     }
    134     while (old_line != (int)m_lines.size())
    135         new_lines.push_back(std::move(m_lines[(int)old_line++]));
    136 
    137     m_lines = std::move(new_lines);
    138 }
    139 
    140 void WordDB::on_option_changed(const Option& option)
    141 {
    142     if (option.name() == "extra_word_chars")
    143         rebuild_db();
    144 }
    145 
    146 int WordDB::get_word_occurences(StringView word) const
    147 {
    148     auto it = m_words.find(word);
    149     if (it != m_words.end())
    150         return it->value.refcount;
    151     return 0;
    152 }
    153 
    154 RankedMatchList WordDB::find_matching(StringView query)
    155 {
    156     update_db();
    157     const UsedLetters letters = used_letters(query);
    158     RankedMatchList res;
    159     for (auto&& word : m_words)
    160     {
    161         if (RankedMatch match{word.key, word.value.letters, query, letters})
    162             res.push_back(match);
    163     }
    164 
    165     return res;
    166 }
    167 
    168 UnitTest test_word_db{[]()
    169 {
    170     auto cmp_words = [](const RankedMatch& lhs, const RankedMatch& rhs) {
    171         return lhs.candidate() < rhs.candidate();
    172     };
    173 
    174     using WordList = Vector<StringView>;
    175     auto eq = [](ArrayView<const RankedMatch> lhs, const WordList& rhs) {
    176         return lhs.size() == rhs.size() and
    177             std::equal(lhs.begin(), lhs.end(), rhs.begin(),
    178                        [](const RankedMatch& lhs, const StringView& rhs) {
    179                            return lhs.candidate() == rhs;
    180                        });
    181     };
    182 
    183     auto make_lines = [](auto&&... lines) { return BufferLines{StringData::create(lines)...}; };
    184 
    185     Buffer buffer("test", Buffer::Flags::None,
    186                   make_lines("tchou mutch\n", "tchou kanaky tchou\n", "\n", "tchaa tchaa\n", "allo\n"));
    187     WordDB word_db(buffer);
    188     auto res = word_db.find_matching("");
    189     std::sort(res.begin(), res.end(), cmp_words);
    190     kak_assert(eq(res, WordList{ "allo", "kanaky", "mutch", "tchaa", "tchou" }));
    191     kak_assert(word_db.get_word_occurences("tchou") == 3);
    192     kak_assert(word_db.get_word_occurences("allo") == 1);
    193     buffer.erase({1, 6}, {4, 0});
    194     res = word_db.find_matching("");
    195     std::sort(res.begin(), res.end(), cmp_words);
    196     kak_assert(eq(res, WordList{ "allo", "mutch", "tchou" }));
    197     buffer.insert({1, 0}, "re");
    198     res = word_db.find_matching("");
    199     std::sort(res.begin(), res.end(), cmp_words);
    200     kak_assert(eq(res, WordList{ "allo", "mutch", "retchou", "tchou" }));
    201 }};
    202 
    203 }