kakoune

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

word_db.hh (1320B)


      1 #ifndef word_db_hh_INCLUDED
      2 #define word_db_hh_INCLUDED
      3 
      4 #include "shared_string.hh"
      5 #include "hash_map.hh"
      6 #include "vector.hh"
      7 #include "ranked_match.hh"
      8 #include "option.hh"
      9 #include "safe_ptr.hh"
     10 
     11 namespace Kakoune
     12 {
     13 
     14 using RankedMatchList = Vector<RankedMatch>;
     15 class Buffer;
     16 
     17 // maintain a database of words available in a buffer
     18 class WordDB : public OptionWatcher
     19 {
     20 public:
     21     WordDB(const Buffer& buffer);
     22     ~WordDB();
     23     WordDB(const WordDB&) = delete;
     24     WordDB(WordDB&&) noexcept;
     25 
     26     RankedMatchList find_matching(StringView str);
     27 
     28     int get_word_occurences(StringView word) const;
     29 private:
     30     void update_db();
     31     void add_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);
     32     void remove_words(StringView line, ConstArrayView<Codepoint> extra_word_chars);
     33 
     34     void rebuild_db();
     35 
     36     void on_option_changed(const Option& option) override;
     37 
     38     struct WordInfo
     39     {
     40         StringDataPtr word;
     41         UsedLetters letters;
     42         int refcount;
     43     };
     44     using WordToInfo = HashMap<StringView, WordInfo, MemoryDomain::WordDB>;
     45     using Lines = Vector<StringDataPtr, MemoryDomain::WordDB>;
     46 
     47     SafePtr<const Buffer> m_buffer;
     48     size_t m_timestamp;
     49     WordToInfo m_words;
     50     Lines m_lines;
     51 };
     52 
     53 WordDB& get_word_db(const Buffer& buffer);
     54 
     55 }
     56 
     57 #endif // word_db_hh_INCLUDED