replace LineChangeWatcher with a free function compute_line_modifications

This commit is contained in:
Maxime Coste 2014-05-14 21:19:19 +01:00
parent 0ec0f7c320
commit 2f1388df7b
5 changed files with 18 additions and 32 deletions

View File

@ -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<T>();
}
private:
@ -195,7 +195,6 @@ public:
private:
struct Cache
{
Cache(const Buffer&){}
BufferRange m_range;
size_t m_timestamp = 0;
std::vector<std::vector<std::pair<ByteCoord, ByteCoord>>> 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;
}

View File

@ -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<LineModification> LineChangeWatcher::compute_modifications()
std::vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t timestamp)
{
std::vector<LineModification> 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<LineModification> LineChangeWatcher::compute_modifications()
it->new_line -= num_removed;
}
}
m_timestamp = m_buffer->timestamp();
return res;
}

View File

@ -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<LineModification> compute_modifications();
const Buffer& buffer() const { return *m_buffer; }
private:
safe_ptr<const Buffer> m_buffer;
size_t m_timestamp;
};
std::vector<LineModification> compute_line_modifications(const Buffer& buffer, size_t timestamp);
}

View File

@ -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<String>& 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());

View File

@ -2,7 +2,6 @@
#define word_db_hh_INCLUDED
#include "buffer.hh"
#include "line_change_watcher.hh"
#include <map>
@ -26,7 +25,8 @@ private:
void update_db();
LineChangeWatcher m_change_watcher;
safe_ptr<const Buffer> m_buffer;
size_t m_timestamp;
WordList m_words;
LineToWords m_line_to_words;
};