diff options
| author | Albert Cervin <albert@acervin.com> | 2024-02-16 09:37:32 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2024-02-18 22:58:04 +0100 |
| commit | 69bcee5bbaaaa766ca0e3b8a8949f43b36013995 (patch) | |
| tree | 24f16032ae5fe396a67be250ca4982c655c670ff /src/dged/syntax.c | |
| parent | 0b524a94a5e34148716832f1b6cada02e35369b0 (diff) | |
| download | dged-69bcee5bbaaaa766ca0e3b8a8949f43b36013995.tar.gz dged-69bcee5bbaaaa766ca0e3b8a8949f43b36013995.tar.xz dged-69bcee5bbaaaa766ca0e3b8a8949f43b36013995.zip | |
Fix bytes vs char offset
Tree-sitter uses position in byte coordinates, despite
what it seems to say in the docs.
Diffstat (limited to 'src/dged/syntax.c')
| -rw-r--r-- | src/dged/syntax.c | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/src/dged/syntax.c b/src/dged/syntax.c index 1091123..64178fe 100644 --- a/src/dged/syntax.c +++ b/src/dged/syntax.c @@ -96,12 +96,12 @@ static const char *read_text(void *payload, uint32_t byte_offset, struct text_chunk chunk = text_get_line(text, position.row); // empty lines - if (chunk.nbytes == 0 || position.column >= chunk.nchars) { + if (chunk.nbytes == 0 || position.column >= chunk.nbytes) { *bytes_read = 1; return "\n"; } - uint32_t bytei = text_col_to_byteindex(text, chunk.line, position.column); + uint32_t bytei = position.column; *bytes_read = chunk.nbytes - bytei; return (const char *)chunk.text + bytei; } @@ -379,8 +379,13 @@ static void update_parser(struct buffer *buffer, void *userdata, } buffer_add_text_property( - buffer, (struct location){.line = start.row, .col = start.column}, - (struct location){.line = end.row, .col = end.column - 1}, + buffer, + (struct location){.line = start.row, + .col = text_byteindex_to_col( + buffer->text, start.row, start.column)}, + (struct location){.line = end.row, + .col = text_byteindex_to_col(buffer->text, end.row, + end.column - 1)}, (struct text_property){ .type = TextProperty_Colors, .colors = @@ -399,13 +404,18 @@ static void text_removed(struct buffer *buffer, struct region removed, uint32_t begin_idx, uint32_t end_idx, void *userdata) { struct highlight *h = (struct highlight *)userdata; + TSPoint begin = {.row = removed.begin.line, + .column = text_col_to_byteindex( + buffer->text, removed.begin.line, removed.begin.col)}; + TSPoint new_end = begin; + TSPoint old_end = {.row = removed.end.line, + .column = text_col_to_byteindex( + buffer->text, removed.end.line, removed.end.col)}; + TSInputEdit edit = { - .start_point = - (TSPoint){.row = removed.begin.line, .column = removed.begin.col}, - .old_end_point = - (TSPoint){.row = removed.end.line, .column = removed.end.col}, - .new_end_point = - (TSPoint){.row = removed.begin.line, .column = removed.begin.col}, + .start_point = begin, + .old_end_point = old_end, + .new_end_point = new_end, .start_byte = begin_idx, .old_end_byte = end_idx, .new_end_byte = begin_idx, @@ -446,13 +456,18 @@ static void text_inserted(struct buffer *buffer, struct region inserted, void *userdata) { struct highlight *h = (struct highlight *)userdata; + TSPoint begin = {.row = inserted.begin.line, + .column = text_col_to_byteindex( + buffer->text, inserted.begin.line, inserted.begin.col)}; + TSPoint old_end = begin; + TSPoint new_end = {.row = inserted.end.line, + .column = text_col_to_byteindex( + buffer->text, inserted.end.line, inserted.end.col)}; + TSInputEdit edit = { - .start_point = - (TSPoint){.row = inserted.begin.line, .column = inserted.begin.col}, - .old_end_point = - (TSPoint){.row = inserted.begin.line, .column = inserted.begin.col}, - .new_end_point = - (TSPoint){.row = inserted.end.line, .column = inserted.end.col}, + .start_point = begin, + .old_end_point = old_end, + .new_end_point = new_end, .start_byte = begin_idx, .old_end_byte = begin_idx, .new_end_byte = end_idx, |
