1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-24 07:53:41 +03:00

Move line parsing and to Buffer.cc directly

This commit is contained in:
Maxime Coste 2015-10-16 13:52:14 +01:00
parent 3795ff735a
commit fe704b9b84
7 changed files with 89 additions and 104 deletions

View File

@ -17,7 +17,46 @@
namespace Kakoune namespace Kakoune
{ {
Buffer::Buffer(String name, Flags flags, BufferLines lines, struct ParsedLines { BufferLines lines; bool bom, crlf; };
static ParsedLines parse_lines(StringView data)
{
bool bom = false, crlf = false;
const char* pos = data.begin();
if (data.substr(0, 3_byte) == "\xEF\xBB\xBF")
{
bom = true;
pos = data.begin() + 3;
}
BufferLines lines;
while (pos < data.end())
{
const char* line_end = pos;
while (line_end < data.end() and *line_end != '\r' and *line_end != '\n')
++line_end;
lines.emplace_back(StringData::create({pos, line_end}, '\n'));
if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n')
{
crlf = true;
pos = line_end + 2;
}
else
pos = line_end + 1;
}
return { std::move(lines), bom, crlf };
}
static void apply_options(OptionManager& options, const ParsedLines& parsed_lines)
{
options.get_local_option("eolformat").set<String>(parsed_lines.crlf ? "crlf" : "lf");
options.get_local_option("BOM").set<String>(parsed_lines.bom ? "utf-8" : "no");
}
Buffer::Buffer(String name, Flags flags, StringView data,
timespec fs_timestamp) timespec fs_timestamp)
: Scope(GlobalScope::instance()), : Scope(GlobalScope::instance()),
m_name((flags & Flags::File) ? real_path(parse_filename(name)) : std::move(name)), m_name((flags & Flags::File) ? real_path(parse_filename(name)) : std::move(name)),
@ -30,18 +69,22 @@ Buffer::Buffer(String name, Flags flags, BufferLines lines,
BufferManager::instance().register_buffer(*this); BufferManager::instance().register_buffer(*this);
options().register_watcher(*this); options().register_watcher(*this);
if (lines.empty()) ParsedLines parsed_lines = parse_lines(data);
lines.emplace_back(StringData::create("\n"));
if (parsed_lines.lines.empty())
parsed_lines.lines.emplace_back(StringData::create("\n"));
#ifdef KAK_DEBUG #ifdef KAK_DEBUG
for (auto& line : lines) for (auto& line : parsed_lines.lines)
kak_assert(not (line->length == 0) and kak_assert(not (line->length == 0) and
line->data()[line->length-1] == '\n'); line->data()[line->length-1] == '\n');
#endif #endif
static_cast<BufferLines&>(m_lines) = std::move(lines); static_cast<BufferLines&>(m_lines) = std::move(parsed_lines.lines);
m_changes.push_back({ Change::Insert, true, {0,0}, line_count() }); m_changes.push_back({ Change::Insert, true, {0,0}, line_count() });
apply_options(options(), parsed_lines);
if (flags & Flags::File) if (flags & Flags::File)
{ {
if (flags & Flags::New) if (flags & Flags::New)
@ -160,10 +203,12 @@ struct Buffer::Modification
} }
}; };
void Buffer::reload(BufferLines lines, timespec fs_timestamp) void Buffer::reload(StringView data, timespec fs_timestamp)
{ {
if (lines.empty()) ParsedLines parsed_lines = parse_lines(data);
lines.emplace_back(StringData::create("\n"));
if (parsed_lines.lines.empty())
parsed_lines.lines.emplace_back(StringData::create("\n"));
const bool record_undo = not (m_flags & Flags::NoUndo); const bool record_undo = not (m_flags & Flags::NoUndo);
@ -173,14 +218,14 @@ void Buffer::reload(BufferLines lines, timespec fs_timestamp)
{ {
m_changes.push_back({ Change::Erase, true, {0,0}, line_count() }); m_changes.push_back({ Change::Erase, true, {0,0}, line_count() });
static_cast<BufferLines&>(m_lines) = std::move(lines); static_cast<BufferLines&>(m_lines) = std::move(parsed_lines.lines);
m_changes.push_back({ Change::Insert, true, {0,0}, line_count() }); m_changes.push_back({ Change::Insert, true, {0,0}, line_count() });
} }
else else
{ {
auto diff = find_diff(m_lines.begin(), m_lines.size(), auto diff = find_diff(m_lines.begin(), m_lines.size(),
lines.begin(), (int)lines.size(), parsed_lines.lines.begin(), (int)parsed_lines.lines.size(),
[](const StringDataPtr& lhs, const StringDataPtr& rhs) [](const StringDataPtr& lhs, const StringDataPtr& rhs)
{ return lhs->hash == rhs->hash and lhs->strview() == rhs->strview(); }); { return lhs->hash == rhs->hash and lhs->strview() == rhs->strview(); });
@ -196,10 +241,10 @@ void Buffer::reload(BufferLines lines, timespec fs_timestamp)
for (LineCount line = 0; line < d.len; ++line) for (LineCount line = 0; line < d.len; ++line)
m_current_undo_group.emplace_back( m_current_undo_group.emplace_back(
Modification::Insert, cur_line + line, Modification::Insert, cur_line + line,
SharedString{lines[(int)(d.posB + line)]}); SharedString{parsed_lines.lines[(int)(d.posB + line)]});
m_changes.push_back({ Change::Insert, it == m_lines.end(), cur_line, cur_line + d.len }); m_changes.push_back({ Change::Insert, it == m_lines.end(), cur_line, cur_line + d.len });
m_lines.insert(it, &lines[d.posB], &lines[d.posB + d.len]); m_lines.insert(it, &parsed_lines.lines[d.posB], &parsed_lines.lines[d.posB + d.len]);
it = m_lines.begin() + (int)(cur_line + d.len); it = m_lines.begin() + (int)(cur_line + d.len);
} }
else if (d.mode == Diff::Remove) else if (d.mode == Diff::Remove)
@ -219,6 +264,8 @@ void Buffer::reload(BufferLines lines, timespec fs_timestamp)
commit_undo_group(); commit_undo_group();
apply_options(options(), parsed_lines);
m_last_save_undo_index = m_history_cursor - m_history.begin(); m_last_save_undo_index = m_history_cursor - m_history.begin();
m_fs_timestamp = fs_timestamp; m_fs_timestamp = fs_timestamp;
} }
@ -586,7 +633,7 @@ UnitTest test_buffer{[]()
{ {
Buffer empty_buffer("empty", Buffer::Flags::None, {}); Buffer empty_buffer("empty", Buffer::Flags::None, {});
Buffer buffer("test", Buffer::Flags::None, { "allo ?\n"_ss, "mais que fais la police\n"_ss, " hein ?\n"_ss, " youpi\n"_ss }); Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n");
kak_assert(buffer.line_count() == 4); kak_assert(buffer.line_count() == 4);
BufferIterator pos = buffer.begin(); BufferIterator pos = buffer.begin();
@ -629,8 +676,7 @@ UnitTest test_buffer{[]()
UnitTest test_undo{[]() UnitTest test_undo{[]()
{ {
BufferLines lines = { "allo ?\n"_ss, "mais que fais la police\n"_ss, " hein ?\n"_ss, " youpi\n"_ss }; Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais que fais la police\n hein ?\n youpi\n");
Buffer buffer("test", Buffer::Flags::None, lines);
auto pos = buffer.insert(buffer.end(), "kanaky\n"); auto pos = buffer.insert(buffer.end(), "kanaky\n");
buffer.erase(pos, buffer.end()); buffer.erase(pos, buffer.end());
buffer.insert(buffer.iterator_at(2_line), "tchou\n"); buffer.insert(buffer.iterator_at(2_line), "tchou\n");
@ -642,9 +688,11 @@ UnitTest test_undo{[]()
buffer.redo(); buffer.redo();
buffer.undo(); buffer.undo();
kak_assert((int)buffer.line_count() == lines.size()); kak_assert((int)buffer.line_count() == 4);
for (size_t i = 0; i < lines.size(); ++i) kak_assert(buffer[0_line] == "allo ?\n");
kak_assert(SharedString{lines[i]} == buffer[LineCount((int)i)]); kak_assert(buffer[1_line] == "mais que fais la police\n");
kak_assert(buffer[2_line] == " hein ?\n");
kak_assert(buffer[3_line] == " youpi\n");
}}; }};
} }

View File

@ -78,7 +78,7 @@ public:
NoUndo = 8, NoUndo = 8,
}; };
Buffer(String name, Flags flags, BufferLines lines = {}, Buffer(String name, Flags flags, StringView data = {},
timespec fs_timestamp = InvalidTime); timespec fs_timestamp = InvalidTime);
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;
Buffer& operator= (const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete;
@ -152,7 +152,7 @@ public:
void run_hook_in_own_context(StringView hook_name, StringView param); void run_hook_in_own_context(StringView hook_name, StringView param);
void reload(BufferLines lines, timespec fs_timestamp = InvalidTime); void reload(StringView data, timespec fs_timestamp = InvalidTime);
void check_invariant() const; void check_invariant() const;

View File

@ -47,53 +47,12 @@ ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, CharCoord
return (int)(it - line.begin()); return (int)(it - line.begin());
} }
struct BufferData
{
BufferLines lines;
bool bom = false;
bool crlf = false;
BufferData(StringView data)
{
const char* pos = data.begin();
if (data.length() >= 3 and
data[0_byte] == '\xEF' and data[1_byte] == '\xBB' and data[2_byte] == '\xBF')
{
bom = true;
pos = data.begin() + 3;
}
while (pos < data.end())
{
const char* line_end = pos;
while (line_end < data.end() and *line_end != '\r' and *line_end != '\n')
++line_end;
lines.emplace_back(StringData::create({pos, line_end}, '\n'));
if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n')
{
crlf = true;
pos = line_end + 2;
}
else
pos = line_end + 1;
}
}
void apply_options(Buffer& buffer) const
{
OptionManager& options = buffer.options();
options.get_local_option("eolformat").set<String>(crlf ? "crlf" : "lf");
options.get_local_option("BOM").set<String>(bom ? "utf-8" : "no");
}
};
Buffer* create_file_buffer(StringView filename) Buffer* create_file_buffer(StringView filename)
{ {
if (MappedFile file_data{filename}) if (MappedFile file_data{filename})
return create_buffer({ file_data.data, (int)file_data.st.st_size }, filename, return new Buffer(filename.str(), Buffer::Flags::File,
Buffer::Flags::File, file_data.st.st_mtim); { file_data.data, (int)file_data.st.st_size },
file_data.st.st_mtim);
return nullptr; return nullptr;
} }
@ -102,28 +61,12 @@ bool reload_file_buffer(Buffer& buffer)
kak_assert(buffer.flags() & Buffer::Flags::File); kak_assert(buffer.flags() & Buffer::Flags::File);
if (MappedFile file_data{buffer.name()}) if (MappedFile file_data{buffer.name()})
{ {
reload_buffer(buffer, { file_data.data, (int)file_data.st.st_size }, file_data.st.st_mtim); buffer.reload({ file_data.data, (int)file_data.st.st_size }, file_data.st.st_mtim);
return true; return true;
} }
return false; return false;
} }
Buffer* create_buffer(StringView data, StringView name, Buffer::Flags flags,
timespec fs_timestamp)
{
BufferData buf_data(data);
Buffer* buffer = new Buffer{name.str(), flags, std::move(buf_data.lines), fs_timestamp};
buf_data.apply_options(*buffer);
return buffer;
}
void reload_buffer(Buffer& buffer, StringView data, timespec fs_timestamp)
{
BufferData buf_data(data);
buffer.reload(std::move(buf_data.lines), fs_timestamp);
buf_data.apply_options(buffer);
}
Buffer* create_fifo_buffer(String name, int fd, bool scroll) Buffer* create_fifo_buffer(String name, int fd, bool scroll)
{ {
static ValueId s_fifo_watcher_id = ValueId::get_free_id(); static ValueId s_fifo_watcher_id = ValueId::get_free_id();
@ -132,7 +75,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll)
if (buffer) if (buffer)
{ {
buffer->flags() |= Buffer::Flags::NoUndo; buffer->flags() |= Buffer::Flags::NoUndo;
buffer->reload({"\n"_ss}, InvalidTime); buffer->reload({}, InvalidTime);
} }
else else
buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo); buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
@ -213,13 +156,13 @@ void write_to_debug_buffer(StringView str)
return; return;
} }
const StringView debug_buffer_name = "*debug*"; constexpr StringView debug_buffer_name = "*debug*";
if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name)) if (Buffer* buffer = BufferManager::instance().get_buffer_ifp(debug_buffer_name))
buffer->insert(buffer->end(), str); buffer->insert(buffer->end(), str);
else else
{ {
String line = str + ((str.empty() or str.back() != '\n') ? "\n" : ""); String line = str + ((str.empty() or str.back() != '\n') ? "\n" : "");
create_buffer(line, debug_buffer_name, Buffer::Flags::NoUndo, InvalidTime); new Buffer(debug_buffer_name.str(), Buffer::Flags::NoUndo, line, InvalidTime);
} }
} }

View File

@ -30,10 +30,6 @@ CharCount get_column(const Buffer& buffer,
ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop, ByteCount get_byte_to_column(const Buffer& buffer, CharCount tabstop,
CharCoord coord); CharCoord coord);
Buffer* create_buffer(StringView data, StringView name,
Buffer::Flags flags, timespec fs_timestamp);
void reload_buffer(Buffer& buffer, StringView data, timespec fs_timestamp);
Buffer* create_fifo_buffer(String name, int fd, bool scroll = false); Buffer* create_fifo_buffer(String name, int fd, bool scroll = false);
Buffer* create_file_buffer(StringView filename); Buffer* create_file_buffer(StringView filename);
bool reload_file_buffer(Buffer& buffer); bool reload_file_buffer(Buffer& buffer);

View File

@ -105,7 +105,7 @@ bool operator==(const LineModification& lhs, const LineModification& rhs)
UnitTest test_line_modifications{[]() UnitTest test_line_modifications{[]()
{ {
{ {
Buffer buffer("test", Buffer::Flags::None, { "line 1\n"_ss, "line 2\n"_ss }); Buffer buffer("test", Buffer::Flags::None, "line 1\nline 2\n");
auto ts = buffer.timestamp(); auto ts = buffer.timestamp();
buffer.erase(buffer.iterator_at({1, 0}), buffer.iterator_at({2, 0})); buffer.erase(buffer.iterator_at({1, 0}), buffer.iterator_at({2, 0}));
@ -114,7 +114,7 @@ UnitTest test_line_modifications{[]()
} }
{ {
Buffer buffer("test", Buffer::Flags::None, { "line 1\n"_ss, "line 2\n"_ss }); Buffer buffer("test", Buffer::Flags::None, "line 1\nline 2\n");
auto ts = buffer.timestamp(); auto ts = buffer.timestamp();
buffer.insert(buffer.iterator_at({1, 7}), "line 3"); buffer.insert(buffer.iterator_at({1, 7}), "line 3");
@ -123,8 +123,7 @@ UnitTest test_line_modifications{[]()
} }
{ {
Buffer buffer("test", Buffer::Flags::None, Buffer buffer("test", Buffer::Flags::None, "line 1\nline 2\nline 3\n");
{ "line 1\n"_ss, "line 2\n"_ss, "line 3\n"_ss });
auto ts = buffer.timestamp(); auto ts = buffer.timestamp();
buffer.insert(buffer.iterator_at({1, 4}), "hoho\nhehe"); buffer.insert(buffer.iterator_at({1, 4}), "hoho\nhehe");
@ -135,8 +134,7 @@ UnitTest test_line_modifications{[]()
} }
{ {
Buffer buffer("test", Buffer::Flags::None, Buffer buffer("test", Buffer::Flags::None, "line 1\nline 2\nline 3\nline 4\n");
{ "line 1\n"_ss, "line 2\n"_ss, "line 3\n"_ss, "line 4\n"_ss });
auto ts = buffer.timestamp(); auto ts = buffer.timestamp();
buffer.erase(buffer.iterator_at({0,0}), buffer.iterator_at({3,0})); buffer.erase(buffer.iterator_at({0,0}), buffer.iterator_at({3,0}));
@ -155,7 +153,7 @@ UnitTest test_line_modifications{[]()
} }
{ {
Buffer buffer("test", Buffer::Flags::None, { "line 1\n"_ss }); Buffer buffer("test", Buffer::Flags::None, "line 1\n");
auto ts = buffer.timestamp(); auto ts = buffer.timestamp();
buffer.insert(buffer.iterator_at({0,0}), "n"); buffer.insert(buffer.iterator_at({0,0}), "n");
buffer.insert(buffer.iterator_at({0,1}), "e"); buffer.insert(buffer.iterator_at({0,1}), "e");

View File

@ -457,14 +457,14 @@ int run_server(StringView session, StringView init_command,
FaceRegistry face_registry; FaceRegistry face_registry;
ClientManager client_manager; ClientManager client_manager;
UnitTest::run_all_tests();
register_options(); register_options();
register_env_vars(); register_env_vars();
register_registers(); register_registers();
register_commands(); register_commands();
register_highlighters(); register_highlighters();
UnitTest::run_all_tests();
write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***"); write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***");
Server server(session.empty() ? to_string(getpid()) : session.str()); Server server(session.empty() ? to_string(getpid()) : session.str());
@ -601,8 +601,8 @@ int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet)
} }
if (not isatty(0)) if (not isatty(0))
{ {
Buffer* buffer = create_buffer(read_fd(0), "*stdin*", Buffer* buffer = new Buffer("*stdin*", Buffer::Flags::None,
Buffer::Flags::None, InvalidTime); read_fd(0), InvalidTime);
apply_keys_to_buffer(*buffer); apply_keys_to_buffer(*buffer);
write_buffer_to_fd(*buffer, 1); write_buffer_to_fd(*buffer, 1);
buffer_manager.delete_buffer(*buffer); buffer_manager.delete_buffer(*buffer);

View File

@ -139,11 +139,11 @@ int WordDB::get_word_occurences(StringView word) const
UnitTest test_word_db{[]() UnitTest test_word_db{[]()
{ {
Buffer buffer("test", Buffer::Flags::None, Buffer buffer("test", Buffer::Flags::None,
{ "tchou mutch\n"_ss, "tchou mutch\n"
"tchou kanaky tchou\n"_ss, "tchou kanaky tchou\n"
"\n"_ss, "\n"
"tchaa tchaa\n"_ss, "tchaa tchaa\n"
"allo\n"_ss}); "allo\n");
WordDB word_db(buffer); WordDB word_db(buffer);
auto res = word_db.find_matching("", prefix_match); auto res = word_db.find_matching("", prefix_match);
std::sort(res.begin(), res.end()); std::sort(res.begin(), res.end());