Fix regex and current() system.

This commit is contained in:
2026-09-03 07:42:50 +01:00
parent 33dc9ad507
commit f8975a60d8
5 changed files with 21 additions and 29 deletions
+9 -3
View File
@@ -221,6 +221,7 @@ void GenericBuffer::load(BEd &ctx, vase::Shard *text) {
ctx.prev().start = 1; ctx.prev().start = 1;
ctx.prev().end = text->lines + 1; ctx.prev().end = text->lines + 1;
} }
ctx.current() = {name, lines()};
syntax::release(parse); syntax::release(parse);
parse = syntax::make_parser(root, lines(), ctx.languages["ruby"]); parse = syntax::make_parser(root, lines(), ctx.languages["ruby"]);
} }
@@ -240,6 +241,7 @@ void GenericBuffer::append(BEd &ctx, vase::Shard *text, uint64_t line) {
ctx.prev().buffername = name; ctx.prev().buffername = name;
ctx.prev().start = line + 1; ctx.prev().start = line + 1;
ctx.prev().end = line + text->lines + 1; ctx.prev().end = line + text->lines + 1;
ctx.current() = {name, line + text->lines + 1};
root = vase::insert(&ctx.append, root, text, line); root = vase::insert(&ctx.append, root, text, line);
ctx.marks.insert(name, line, text->lines + 1); ctx.marks.insert(name, line, text->lines + 1);
if (parse.lang) if (parse.lang)
@@ -251,8 +253,10 @@ void GenericBuffer::remove(BEd &ctx, uint64_t start_line, uint64_t end_line) {
snapshot(std::format("Remove lines {} to {}", start_line, end_line)); snapshot(std::format("Remove lines {} to {}", start_line, end_line));
root = vase::erase(root, start_line, end_line); root = vase::erase(root, start_line, end_line);
ctx.prev().buffername = name; ctx.prev().buffername = name;
ctx.prev().start = std::min(start_line, lines()); uint64_t l = std::min(start_line, lines());
ctx.prev().end = std::min(start_line, lines()); ctx.prev().start = l;
ctx.prev().end = l;
ctx.current() = {name, l};
ctx.marks.erase(name, start_line, end_line - start_line + 1); ctx.marks.erase(name, start_line, end_line - start_line + 1);
if (parse.lang) if (parse.lang)
syntax::erase(parse, root, start_line, end_line - start_line + 1); syntax::erase(parse, root, start_line, end_line - start_line + 1);
@@ -268,6 +272,7 @@ void GenericBuffer::replace(BEd &ctx, vase::Shard *text, uint64_t start_line, ui
ctx.prev().buffername = name; ctx.prev().buffername = name;
ctx.prev().start = start_line; ctx.prev().start = start_line;
ctx.prev().end = start_line + text->lines; ctx.prev().end = start_line + text->lines;
ctx.current() = {name, start_line + text->lines};
uint64_t new_count = text->lines + 1; uint64_t new_count = text->lines + 1;
uint64_t old_count = end_line - start_line + 1; uint64_t old_count = end_line - start_line + 1;
root = vase::replace(root, text, start_line, end_line); root = vase::replace(root, text, start_line, end_line);
@@ -293,6 +298,7 @@ void GenericBuffer::join(BEd &ctx, uint64_t start_line, uint64_t end_line) {
ctx.prev().buffername = name; ctx.prev().buffername = name;
ctx.prev().start = start_line; ctx.prev().start = start_line;
ctx.prev().end = start_line; ctx.prev().end = start_line;
ctx.current() = {name, start_line};
ctx.marks.collapse(name, start_line, end_line - start_line); ctx.marks.collapse(name, start_line, end_line - start_line);
if (parse.lang) if (parse.lang)
syntax::erase(parse, root, start_line, end_line - start_line); syntax::erase(parse, root, start_line, end_line - start_line);
@@ -329,11 +335,11 @@ void GenericBuffer::substitute(
if (edit) if (edit)
edit->insert(line, new_lines); edit->insert(line, new_lines);
} }
ctx.current() = {name, line + new_lines};
} }
); );
if (edit) if (edit)
edit->commit(root); edit->commit(root);
ctx.prev().buffername = name;
state = Modified; state = Modified;
} }
} // namespace bed::internal::buffer } // namespace bed::internal::buffer
-1
View File
@@ -93,7 +93,6 @@ void Function::register_extented(BEd &ctx) {
vase::Shard::release(text); vase::Shard::release(text);
throw; throw;
} }
ctx.current() = {arg.buffername, arg.start + addr.end - addr.start + 1};
}, },
} }
); );
+3 -10
View File
@@ -47,7 +47,6 @@ void Function::register_posix(BEd &ctx) {
) { ) {
auto addr = std::get<buffer::Line>(addr_); auto addr = std::get<buffer::Line>(addr_);
ctx.buffer(addr.buffername).append(ctx, text, addr.number); ctx.buffer(addr.buffername).append(ctx, text, addr.number);
ctx.current() = {ctx.prev().buffername, ctx.prev().end};
vase::Shard::release(text); vase::Shard::release(text);
} }
} }
@@ -71,7 +70,6 @@ void Function::register_posix(BEd &ctx) {
) { ) {
auto addr = std::get<buffer::Range>(addr_); auto addr = std::get<buffer::Range>(addr_);
ctx.buffer(addr.buffername).replace(ctx, text, addr.start, addr.end); ctx.buffer(addr.buffername).replace(ctx, text, addr.start, addr.end);
ctx.current() = {ctx.prev().buffername, ctx.prev().end};
vase::Shard::release(text); vase::Shard::release(text);
} }
} }
@@ -95,7 +93,6 @@ void Function::register_posix(BEd &ctx) {
) { ) {
auto addr = std::get<buffer::Range>(addr_); auto addr = std::get<buffer::Range>(addr_);
ctx.buffer(addr.buffername).remove(ctx, addr.start, addr.end); ctx.buffer(addr.buffername).remove(ctx, addr.start, addr.end);
ctx.current() = {addr.buffername, addr.start};
} }
} }
); );
@@ -145,7 +142,6 @@ void Function::register_posix(BEd &ctx) {
throw; throw;
} }
ctx.io.write_line(std::format("{}", buf.bytes())); ctx.io.write_line(std::format("{}", buf.bytes()));
ctx.current() = {addr, buf.lines()};
} }
} }
); );
@@ -191,7 +187,6 @@ void Function::register_posix(BEd &ctx) {
throw; throw;
} }
ctx.io.write_line(std::format("{}", buf.bytes())); ctx.io.write_line(std::format("{}", buf.bytes()));
ctx.current() = {addr, buf.lines()};
} }
} }
); );
@@ -289,7 +284,6 @@ void Function::register_posix(BEd &ctx) {
if (addr.number) if (addr.number)
addr.number--; addr.number--;
ctx.buffer(addr.buffername).append(ctx, text, addr.number); ctx.buffer(addr.buffername).append(ctx, text, addr.number);
ctx.current() = {ctx.prev().buffername, ctx.prev().end};
vase::Shard::release(text); vase::Shard::release(text);
} }
} }
@@ -313,7 +307,6 @@ void Function::register_posix(BEd &ctx) {
) { ) {
auto addr = std::get<buffer::Range>(addr_); auto addr = std::get<buffer::Range>(addr_);
ctx.buffer(addr.buffername).join(ctx, addr.start, addr.end); ctx.buffer(addr.buffername).join(ctx, addr.start, addr.end);
ctx.current() = {addr.buffername, addr.start};
} }
} }
); );
@@ -403,7 +396,6 @@ void Function::register_posix(BEd &ctx) {
vase::Shard::release(text); vase::Shard::release(text);
throw; throw;
} }
ctx.current() = {arg.buffername, arg.number + addr.end - addr.start + 1};
}, },
} }
); );
@@ -567,7 +559,6 @@ void Function::register_posix(BEd &ctx) {
try { try {
buf.append(ctx, s, addr.number); buf.append(ctx, s, addr.number);
ctx.io.write_line(std::format("{}", s ? s->length + 1 : 0)); ctx.io.write_line(std::format("{}", s ? s->length + 1 : 0));
ctx.current() = {addr.buffername, addr.number + (s ? s->lines + 1 : 0)};
vase::Shard::release(s); vase::Shard::release(s);
} catch (...) { } catch (...) {
vase::Shard::release(s); vase::Shard::release(s);
@@ -642,7 +633,6 @@ void Function::register_posix(BEd &ctx) {
vase::Shard::release(text); vase::Shard::release(text);
throw; throw;
} }
ctx.current() = {arg.buffername, arg.number + addr.end - addr.start + 1};
}, },
} }
); );
@@ -746,6 +736,9 @@ void Function::register_posix(BEd &ctx) {
ctx.io.write_line(std::format(":{}:{}", addr.buffername, addr.start)); ctx.io.write_line(std::format(":{}:{}", addr.buffername, addr.start));
else else
ctx.io.write_line(std::format(":{}:{},{}", addr.buffername, addr.start, addr.end)); ctx.io.write_line(std::format(":{}:{},{}", addr.buffername, addr.start, addr.end));
ctx.prev().buffername = addr.buffername;
ctx.prev().start = addr.start;
ctx.prev().end = addr.end;
ctx.current() = {addr.buffername, addr.end}; ctx.current() = {addr.buffername, addr.end};
}, },
} }
+2 -1
View File
@@ -165,12 +165,13 @@ void Parser::operation() {
j++; j++;
} }
std::string replacement(peek_str(j)); std::string replacement(peek_str(j));
std::string options;
if (peek(j) != '\0') { if (peek(j) != '\0') {
advance(j + 1); advance(j + 1);
} else { } else {
advance(j); advance(j);
options = "p";
} }
std::string options;
if (peek() != '\0') { if (peek() != '\0') {
options = std::string(peek_str()); options = std::string(peek_str());
advance(peek_str().size()); advance(peek_str().size());
+4 -11
View File
@@ -90,12 +90,7 @@ Shard *substitute(
if (matches.empty()) if (matches.empty())
return root; return root;
std::vector<ReplacePart> replace_parts = parse_replace(ap, replace); std::vector<ReplacePart> replace_parts = parse_replace(ap, replace);
struct Edit { uint64_t current_line = 1;
uint64_t line;
uint64_t old_lines;
uint64_t new_lines;
};
uint64_t orig_line = start;
int64_t line_delta = 0; int64_t line_delta = 0;
std::vector<Shard *> pieces; std::vector<Shard *> pieces;
pieces.reserve(matches.size() * 2 + 1); pieces.reserve(matches.size() * 2 + 1);
@@ -109,7 +104,7 @@ Shard *substitute(
Shard::release(remaining); Shard::release(remaining);
pieces.push_back(keep); pieces.push_back(keep);
remaining = rest; remaining = rest;
orig_line += keep ? keep->lines : 0; current_line += keep ? keep->lines : 0;
} }
auto [dropped, rest2] = Shard::split(remaining, match.end - match.start); auto [dropped, rest2] = Shard::split(remaining, match.end - match.start);
Shard::release(remaining); Shard::release(remaining);
@@ -152,13 +147,11 @@ Shard *substitute(
} }
} }
Shard::release(dropped); Shard::release(dropped);
if (old_lines || new_lines) { uint64_t report_line = (uint64_t)((int64_t)current_line + line_delta);
uint64_t report_line = (uint64_t)((int64_t)orig_line + line_delta);
if (on_edit) if (on_edit)
on_edit(report_line, old_lines, new_lines); on_edit(report_line, old_lines, new_lines);
line_delta += (int64_t)new_lines - (int64_t)old_lines; line_delta += (int64_t)new_lines - (int64_t)old_lines;
} current_line += old_lines;
orig_line += old_lines;
cursor = match.end; cursor = match.end;
} }
pieces.push_back(remaining); pieces.push_back(remaining);