summaryrefslogtreecommitdiff
path: root/src/word_db.hh
blob: dab65d4cfe1ccdf25b42609fefa94c5c8d9b2aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef word_db_hh_INCLUDED
#define word_db_hh_INCLUDED

#include "shared_string.hh"
#include "hash_map.hh"
#include "vector.hh"
#include "ranked_match.hh"
#include "option_manager.hh"
#include "safe_ptr.hh"

namespace Kakoune
{

using RankedMatchList = Vector<RankedMatch>;
class Buffer;

// maintain a database of words available in a buffer
class WordDB : public OptionManagerWatcher
{
public:
    WordDB(const Buffer& buffer);
    ~WordDB();
    WordDB(const WordDB&) = delete;
    WordDB(WordDB&&) noexcept;

    RankedMatchList find_matching(StringView str);

    int get_word_occurences(StringView word) const;
private:
    void update_db();
    void add_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);
    void remove_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);

    void rebuild_db();

    void on_option_changed(const Option& option) override;

    struct WordInfo
    {
        StringDataPtr word;
        UsedLetters letters;
        int refcount;
    };
    using WordToInfo = HashMap<StringView, WordInfo, MemoryDomain::WordDB>;
    using Lines = Vector<StringDataPtr, MemoryDomain::WordDB>;

    SafePtr<const Buffer> m_buffer;
    size_t m_timestamp;
    WordToInfo m_words;
    Lines m_lines;
};

WordDB& get_word_db(const Buffer& buffer);

}

#endif // word_db_hh_INCLUDED