summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2024-11-08 15:36:13 +0100
committerAlbert Cervin <albert@acervin.com>2024-11-08 15:36:13 +0100
commit4764653ba333f015bc2f87fd2248aab6b6185868 (patch)
tree58002646ea0cb8fc3793e4e8f9d6e05fffede25e
parentcab171318a4efff57fc7f0ae3f178dc09b9c590d (diff)
downloaddged-4764653ba333f015bc2f87fd2248aab6b6185868.tar.gz
dged-4764653ba333f015bc2f87fd2248aab6b6185868.tar.xz
dged-4764653ba333f015bc2f87fd2248aab6b6185868.zip
Fix col offset again
Was not doing the correct thing when multiple lines was added.
-rw-r--r--src/dged/buffer.c2
-rw-r--r--test/buffer.c9
2 files changed, 10 insertions, 1 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c
index 91c1a14..d4501b7 100644
--- a/src/dged/buffer.c
+++ b/src/dged/buffer.c
@@ -545,7 +545,7 @@ struct location buffer_add(struct buffer *buffer, struct location at,
cols_added += visual_char_width(codepoint, tab_width);
}
final = buffer_clamp(buffer, (int64_t)at.line + lines_added,
- (int64_t)at.col + cols_added);
+ (int64_t)(lines_added > 0 ? 0 : at.col) + cols_added);
struct location final_bytes = buffer_location_to_byte_coords(buffer, final);
undo_push_add(
diff --git a/test/buffer.c b/test/buffer.c
index f40166e..514671b 100644
--- a/test/buffer.c
+++ b/test/buffer.c
@@ -37,6 +37,15 @@ static void test_add(void) {
ASSERT(loc.line == 1 && loc.col == 16,
"Expected to be at end of second line");
+ // test newline
+ buffer_clear(&b);
+ txt = "some chars";
+ loc = buffer_add(&b, (struct location){.line = 0, .col = 0}, (uint8_t *)txt,
+ strlen(txt));
+ loc = buffer_newline(&b, (struct location){.line = 0, .col = 4});
+ ASSERT(loc.line == 1 && loc.col == 0,
+ "Expected to be at start of second line after inserting newline");
+
// test callback
uint32_t hook_id = buffer_add_insert_hook(&b, add_callback, NULL);
buffer_add(&b, (struct location){.line = 0, .col = 0}, (uint8_t *)"hej", 3);