2014-01-16 23:07:42 +01:00
|
|
|
#include "word_db.hh"
|
|
|
|
|
|
|
|
#include "utils.hh"
|
|
|
|
#include "utf8_iterator.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-05-11 13:51:37 +02:00
|
|
|
static std::vector<String> get_words(StringView content)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
std::vector<String> res;
|
2014-05-11 13:51:37 +02:00
|
|
|
using Iterator = utf8::utf8_iterator<const char*,
|
2014-01-16 23:07:42 +01:00
|
|
|
utf8::InvalidBytePolicy::Pass>;
|
2014-05-11 13:51:37 +02:00
|
|
|
const char* word_start = content.begin();
|
2014-01-16 23:07:42 +01:00
|
|
|
bool in_word = false;
|
|
|
|
for (Iterator it{word_start}, end{content.end()}; it != end; ++it)
|
|
|
|
{
|
|
|
|
Codepoint c = *it;
|
|
|
|
const bool word = is_word(c);
|
|
|
|
if (not in_word and word)
|
|
|
|
{
|
|
|
|
word_start = it.base();
|
|
|
|
in_word = true;
|
|
|
|
}
|
|
|
|
else if (in_word and not word)
|
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
res.push_back({word_start, it.base()});
|
2014-01-16 23:07:42 +01:00
|
|
|
in_word = false;
|
|
|
|
}
|
|
|
|
}
|
2014-01-24 01:56:33 +01:00
|
|
|
return res;
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
static void add_words(WordDB::WordList& wl, const std::vector<String>& words)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
for (auto& w : words)
|
|
|
|
++wl[w];
|
|
|
|
}
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
static void remove_words(WordDB::WordList& wl, const std::vector<String>& words)
|
|
|
|
{
|
|
|
|
for (auto& w : words)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
auto it = wl.find(w);
|
|
|
|
kak_assert(it != wl.end() and it->second > 0);
|
|
|
|
if (--it->second == 0)
|
|
|
|
wl.erase(it);
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
WordDB::WordDB(const Buffer& buffer)
|
|
|
|
: m_change_watcher{buffer}
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
m_line_to_words.reserve((int)buffer.line_count());
|
|
|
|
for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
m_line_to_words.push_back(get_words(buffer[line]));
|
|
|
|
add_words(m_words, m_line_to_words.back());
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
void WordDB::update_db()
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
auto modifs = m_change_watcher.compute_modifications();
|
|
|
|
if (modifs.empty())
|
|
|
|
return;
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2014-05-11 14:20:13 +02:00
|
|
|
auto& buffer = m_change_watcher.buffer();
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
LineToWords new_lines;
|
|
|
|
new_lines.reserve((int)buffer.line_count());
|
|
|
|
|
|
|
|
auto old_line = 0_line;
|
|
|
|
for (auto& modif : modifs)
|
|
|
|
{
|
|
|
|
kak_assert(0_line <= modif.new_line and modif.new_line < buffer.line_count());
|
|
|
|
kak_assert(old_line <= modif.old_line);
|
|
|
|
while (old_line < modif.old_line)
|
|
|
|
new_lines.push_back(std::move(m_line_to_words[(int)old_line++]));
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
kak_assert((int)new_lines.size() == (int)modif.new_line);
|
|
|
|
|
|
|
|
while (old_line <= modif.old_line + modif.num_removed)
|
|
|
|
{
|
|
|
|
kak_assert(old_line < m_line_to_words.size());
|
|
|
|
remove_words(m_words, m_line_to_words[(int)old_line++]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto l = 0_line; l <= modif.num_added; ++l)
|
|
|
|
{
|
|
|
|
new_lines.push_back(get_words(buffer[modif.new_line + l]));
|
|
|
|
add_words(m_words, new_lines.back());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (old_line != (int)m_line_to_words.size())
|
|
|
|
new_lines.push_back(std::move(m_line_to_words[(int)old_line++]));
|
|
|
|
|
|
|
|
m_line_to_words = std::move(new_lines);
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
std::vector<String> WordDB::find_prefix(const String& prefix)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-24 01:56:33 +01:00
|
|
|
update_db();
|
|
|
|
|
2014-01-16 23:07:42 +01:00
|
|
|
std::vector<String> res;
|
2014-01-24 01:56:33 +01:00
|
|
|
for (auto it = m_words.lower_bound(prefix); it != m_words.end(); ++it)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2014-01-19 20:43:19 +01:00
|
|
|
if (not prefix_match(it->first, prefix))
|
|
|
|
break;
|
|
|
|
res.push_back(it->first);
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-22 20:31:31 +02:00
|
|
|
int WordDB::get_word_occurences(const String& word) const
|
|
|
|
{
|
|
|
|
auto it = m_words.find(word);
|
|
|
|
if (it != m_words.end())
|
|
|
|
return it->second;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|