2014-01-16 23:07:42 +01:00
|
|
|
#include "word_db.hh"
|
|
|
|
|
|
|
|
#include "utils.hh"
|
2014-05-26 22:00:26 +02:00
|
|
|
#include "line_modification.hh"
|
2014-01-16 23:07:42 +01:00
|
|
|
#include "utf8_iterator.hh"
|
2015-05-22 14:58:56 +02:00
|
|
|
#include "unit_tests.hh"
|
2014-01-16 23:07:42 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-12-23 20:32:42 +01:00
|
|
|
UsedLetters used_letters(StringView str)
|
2014-10-28 20:23:02 +01:00
|
|
|
{
|
2014-12-23 20:32:42 +01:00
|
|
|
UsedLetters res;
|
2014-10-28 20:23:02 +01:00
|
|
|
for (auto c : str)
|
|
|
|
{
|
|
|
|
if (c >= 'a' and c <= 'z')
|
|
|
|
res.set(c - 'a');
|
|
|
|
else if (c >= 'A' and c <= 'Z')
|
|
|
|
res.set(c - 'A' + 26);
|
|
|
|
else if (c == '_')
|
|
|
|
res.set(53);
|
|
|
|
else if (c == '-')
|
|
|
|
res.set(54);
|
|
|
|
else
|
|
|
|
res.set(63);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
static WordDB::WordList get_words(const SharedString& content)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2015-01-11 20:28:03 +01:00
|
|
|
WordDB::WordList res;
|
2015-09-23 20:39:21 +02:00
|
|
|
using Utf8It = utf8::iterator<const char*, utf8::InvalidPolicy::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;
|
2015-09-23 20:39:21 +02:00
|
|
|
for (Utf8It it{word_start, content}, end{content.end(), content}; it != end; ++it)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
|
|
|
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-10-07 10:15:32 +02:00
|
|
|
const ByteCount start = word_start - content.begin();
|
|
|
|
const ByteCount length = it.base() - word_start;
|
|
|
|
res.push_back(content.acquire_substr(start, length));
|
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
|
|
|
}
|
|
|
|
|
2015-03-10 14:50:25 +01:00
|
|
|
void WordDB::add_words(const SharedString& line)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2015-03-10 14:50:25 +01:00
|
|
|
for (auto& w : get_words(line))
|
2014-10-28 20:23:02 +01:00
|
|
|
{
|
2015-01-15 20:58:08 +01:00
|
|
|
WordDB::WordInfo& info = m_words[intern(w)];
|
2014-10-28 20:23:02 +01:00
|
|
|
++info.refcount;
|
|
|
|
if (info.letters.none())
|
|
|
|
info.letters = used_letters(w);
|
|
|
|
}
|
2014-01-24 01:56:33 +01:00
|
|
|
}
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2015-03-10 14:50:25 +01:00
|
|
|
void WordDB::remove_words(const SharedString& line)
|
2014-01-24 01:56:33 +01:00
|
|
|
{
|
2015-03-10 14:50:25 +01:00
|
|
|
for (auto& w : get_words(line))
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2015-01-15 20:58:08 +01:00
|
|
|
auto it = m_words.find({w, SharedString::NoCopy()});
|
2014-12-23 20:32:42 +01:00
|
|
|
kak_assert(it != m_words.end() and it->second.refcount > 0);
|
2014-10-28 20:23:02 +01:00
|
|
|
if (--it->second.refcount == 0)
|
2014-12-23 20:32:42 +01:00
|
|
|
m_words.erase(it);
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
WordDB::WordDB(const Buffer& buffer)
|
2014-05-14 22:19:19 +02:00
|
|
|
: m_buffer{&buffer}, m_timestamp{buffer.timestamp()}
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2015-01-15 14:58:55 +01:00
|
|
|
m_lines.reserve((int)buffer.line_count());
|
2014-01-24 01:56:33 +01:00
|
|
|
for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
|
2014-01-16 23:07:42 +01:00
|
|
|
{
|
2015-01-27 14:11:32 +01:00
|
|
|
m_lines.push_back(buffer.line_storage(line));
|
2015-03-10 14:50:25 +01:00
|
|
|
add_words(SharedString{m_lines.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-05-14 22:19:19 +02:00
|
|
|
auto& buffer = *m_buffer;
|
|
|
|
|
2014-05-26 22:00:26 +02:00
|
|
|
auto modifs = compute_line_modifications(buffer, m_timestamp);
|
2014-05-14 22:19:19 +02:00
|
|
|
m_timestamp = buffer.timestamp();
|
|
|
|
|
2014-01-24 01:56:33 +01:00
|
|
|
if (modifs.empty())
|
|
|
|
return;
|
2014-01-16 23:07:42 +01:00
|
|
|
|
2015-01-15 14:58:55 +01:00
|
|
|
Lines new_lines;
|
2014-01-24 01:56:33 +01:00
|
|
|
new_lines.reserve((int)buffer.line_count());
|
|
|
|
|
|
|
|
auto old_line = 0_line;
|
|
|
|
for (auto& modif : modifs)
|
|
|
|
{
|
2015-02-04 00:39:04 +01:00
|
|
|
kak_assert(0_line <= modif.new_line and modif.new_line <= buffer.line_count());
|
|
|
|
kak_assert(modif.new_line < buffer.line_count() or modif.num_added == 0);
|
2014-05-26 22:00:26 +02:00
|
|
|
kak_assert(old_line <= modif.old_line);
|
|
|
|
while (old_line < modif.old_line)
|
2015-01-15 14:58:55 +01:00
|
|
|
new_lines.push_back(std::move(m_lines[(int)old_line++]));
|
2014-01-24 01:56:33 +01:00
|
|
|
|
2014-05-26 22:00:26 +02:00
|
|
|
kak_assert((int)new_lines.size() == (int)modif.new_line);
|
|
|
|
|
2015-02-01 00:50:24 +01:00
|
|
|
while (old_line < modif.old_line + modif.num_removed)
|
2014-05-26 22:00:26 +02:00
|
|
|
{
|
2015-01-15 14:58:55 +01:00
|
|
|
kak_assert(old_line < m_lines.size());
|
2015-03-10 14:50:25 +01:00
|
|
|
remove_words(SharedString{m_lines[(int)old_line++]});
|
2014-05-26 22:00:26 +02:00
|
|
|
}
|
2014-01-24 01:56:33 +01:00
|
|
|
|
2015-02-01 00:50:24 +01:00
|
|
|
for (auto l = 0_line; l < modif.num_added; ++l)
|
2014-01-24 01:56:33 +01:00
|
|
|
{
|
2015-01-27 14:11:32 +01:00
|
|
|
new_lines.push_back(buffer.line_storage(modif.new_line + l));
|
2015-03-10 14:50:25 +01:00
|
|
|
add_words(SharedString{new_lines.back()});
|
2014-01-24 01:56:33 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-15 14:58:55 +01:00
|
|
|
while (old_line != (int)m_lines.size())
|
|
|
|
new_lines.push_back(std::move(m_lines[(int)old_line++]));
|
2014-01-24 01:56:33 +01:00
|
|
|
|
2015-01-15 14:58:55 +01:00
|
|
|
m_lines = std::move(new_lines);
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|
|
|
|
|
2014-10-01 01:20:12 +02:00
|
|
|
int WordDB::get_word_occurences(StringView word) const
|
2014-04-22 20:31:31 +02:00
|
|
|
{
|
2015-01-15 20:58:08 +01:00
|
|
|
auto it = m_words.find({word, SharedString::NoCopy()});
|
2014-04-22 20:31:31 +02:00
|
|
|
if (it != m_words.end())
|
2014-10-28 20:23:02 +01:00
|
|
|
return it->second.refcount;
|
2014-04-22 20:31:31 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:58:56 +02:00
|
|
|
UnitTest test_word_db{[]()
|
|
|
|
{
|
|
|
|
Buffer buffer("test", Buffer::Flags::None,
|
|
|
|
{ "tchou mutch\n"_ss,
|
|
|
|
"tchou kanaky tchou\n"_ss,
|
|
|
|
"\n"_ss,
|
|
|
|
"tchaa tchaa\n"_ss,
|
|
|
|
"allo\n"_ss});
|
|
|
|
WordDB word_db(buffer);
|
|
|
|
auto res = word_db.find_matching("", prefix_match);
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
kak_assert(res == WordDB::WordList{ "allo" COMMA "kanaky" COMMA "mutch" COMMA "tchaa" COMMA "tchou" });
|
|
|
|
kak_assert(word_db.get_word_occurences("tchou") == 3);
|
|
|
|
kak_assert(word_db.get_word_occurences("allo") == 1);
|
|
|
|
buffer.erase(buffer.iterator_at({1, 6}), buffer.iterator_at({4, 0}));
|
|
|
|
res = word_db.find_matching("", prefix_match);
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
kak_assert(res == WordDB::WordList{ "allo" COMMA "mutch" COMMA "tchou" });
|
|
|
|
buffer.insert(buffer.iterator_at({1, 0}), "re");
|
|
|
|
res = word_db.find_matching("", subsequence_match);
|
|
|
|
std::sort(res.begin(), res.end());
|
|
|
|
kak_assert(res == WordDB::WordList{ "allo" COMMA "mutch" COMMA "retchou" COMMA "tchou" });
|
|
|
|
}};
|
|
|
|
|
2014-01-16 23:07:42 +01:00
|
|
|
}
|