From 2f1388df7bafec0d77b25abc27424c9f14629506 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 14 May 2014 21:19:19 +0100 Subject: [PATCH] replace LineChangeWatcher with a free function compute_line_modifications --- src/highlighters.cc | 13 +++++-------- ...{line_change_watcher.cc => line_modification.cc} | 10 +++------- ...{line_change_watcher.hh => line_modification.hh} | 13 +------------ src/word_db.cc | 10 +++++++--- src/word_db.hh | 4 ++-- 5 files changed, 18 insertions(+), 32 deletions(-) rename src/{line_change_watcher.cc => line_modification.cc} (89%) rename src/{line_change_watcher.hh => line_modification.hh} (67%) diff --git a/src/highlighters.cc b/src/highlighters.cc index cc484b88..f3f2af60 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -5,7 +5,7 @@ #include "color_registry.hh" #include "context.hh" #include "display_buffer.hh" -#include "line_change_watcher.hh" +#include "line_modification.hh" #include "option_types.hh" #include "register_manager.hh" #include "string.hh" @@ -158,7 +158,7 @@ struct BufferSideCache { Value& cache_val = buffer.values()[m_id]; if (not cache_val) - cache_val = Value(T{buffer}); + cache_val = Value(T{}); return cache_val.as(); } private: @@ -195,7 +195,6 @@ public: private: struct Cache { - Cache(const Buffer&){} BufferRange m_range; size_t m_timestamp = 0; std::vector>> m_matches; @@ -671,10 +670,8 @@ private: : lhs.begin < rhs.begin; } - struct Cache : public LineChangeWatcher + struct Cache { - Cache(const Buffer& buffer) : LineChangeWatcher(buffer) {} - size_t timestamp = 0; MatchList begin_matches; MatchList end_matches; @@ -696,7 +693,7 @@ private: } else { - auto modifs = cache.compute_modifications(); + auto modifs = compute_line_modifications(buffer, cache.timestamp); update_matches(buffer, modifs, cache.begin_matches, m_begin); update_matches(buffer, modifs, cache.end_matches, m_end); } @@ -718,7 +715,7 @@ private: beg_it = std::upper_bound(beg_it, cache.begin_matches.end(), *end_it, compare_matches_end); } - cache.timestamp = buffer.timestamp(); + cache.timestamp = buf_timestamp; return cache.regions; } diff --git a/src/line_change_watcher.cc b/src/line_modification.cc similarity index 89% rename from src/line_change_watcher.cc rename to src/line_modification.cc index 1a91f053..5da287fc 100644 --- a/src/line_change_watcher.cc +++ b/src/line_modification.cc @@ -1,4 +1,4 @@ -#include "line_change_watcher.hh" +#include "line_modification.hh" #include "buffer.hh" @@ -42,13 +42,10 @@ struct LineChange } -LineChangeWatcher::LineChangeWatcher(const Buffer& buffer) - : m_buffer(&buffer), m_timestamp(buffer.timestamp()) {} - -std::vector LineChangeWatcher::compute_modifications() +std::vector compute_line_modifications(const Buffer& buffer, size_t timestamp) { std::vector res; - for (auto& buf_change : m_buffer->changes_since(m_timestamp)) + for (auto& buf_change : buffer.changes_since(timestamp)) { const LineChange change(buf_change); @@ -102,7 +99,6 @@ std::vector LineChangeWatcher::compute_modifications() it->new_line -= num_removed; } } - m_timestamp = m_buffer->timestamp(); return res; } diff --git a/src/line_change_watcher.hh b/src/line_modification.hh similarity index 67% rename from src/line_change_watcher.hh rename to src/line_modification.hh index d35167ca..3cd2d669 100644 --- a/src/line_change_watcher.hh +++ b/src/line_modification.hh @@ -19,18 +19,7 @@ struct LineModification LineCount diff() const { return new_line - old_line + num_added - num_removed; } }; -class LineChangeWatcher -{ -public: - LineChangeWatcher (const Buffer& buffer); - - std::vector compute_modifications(); - - const Buffer& buffer() const { return *m_buffer; } -private: - safe_ptr m_buffer; - size_t m_timestamp; -}; +std::vector compute_line_modifications(const Buffer& buffer, size_t timestamp); } diff --git a/src/word_db.cc b/src/word_db.cc index 73f6e1ad..83b362c5 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -1,6 +1,7 @@ #include "word_db.hh" #include "utils.hh" +#include "line_modification.hh" #include "utf8_iterator.hh" namespace Kakoune @@ -49,7 +50,7 @@ static void remove_words(WordDB::WordList& wl, const std::vector& words) } WordDB::WordDB(const Buffer& buffer) - : m_change_watcher{buffer} + : m_buffer{&buffer}, m_timestamp{buffer.timestamp()} { m_line_to_words.reserve((int)buffer.line_count()); for (auto line = 0_line, end = buffer.line_count(); line < end; ++line) @@ -61,11 +62,14 @@ WordDB::WordDB(const Buffer& buffer) void WordDB::update_db() { - auto modifs = m_change_watcher.compute_modifications(); + auto& buffer = *m_buffer; + + auto modifs = compute_line_modifications(buffer, m_timestamp); + m_timestamp = buffer.timestamp(); + if (modifs.empty()) return; - auto& buffer = m_change_watcher.buffer(); LineToWords new_lines; new_lines.reserve((int)buffer.line_count()); diff --git a/src/word_db.hh b/src/word_db.hh index 950cc05d..2349a618 100644 --- a/src/word_db.hh +++ b/src/word_db.hh @@ -2,7 +2,6 @@ #define word_db_hh_INCLUDED #include "buffer.hh" -#include "line_change_watcher.hh" #include @@ -26,7 +25,8 @@ private: void update_db(); - LineChangeWatcher m_change_watcher; + safe_ptr m_buffer; + size_t m_timestamp; WordList m_words; LineToWords m_line_to_words; };