diff options
| author | Albert Cervin <albert@acervin.com> | 2024-02-05 15:00:35 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2024-02-12 09:46:04 +0100 |
| commit | 84a29094d497cf56c4efd5505efb044b822b89cd (patch) | |
| tree | c3e17ef67cafc58ed59e576eaa34e4b515038321 | |
| parent | 9be0d9bddd6189ce82ea3775571b5b94d6e168ca (diff) | |
| download | dged-84a29094d497cf56c4efd5505efb044b822b89cd.tar.gz dged-84a29094d497cf56c4efd5505efb044b822b89cd.tar.xz dged-84a29094d497cf56c4efd5505efb044b822b89cd.zip | |
Fix final newline displaying
| -rw-r--r-- | src/dged/buffer.c | 14 | ||||
| -rw-r--r-- | src/dged/lang.c | 3 | ||||
| -rw-r--r-- | src/dged/text.c | 28 | ||||
| -rw-r--r-- | test/text.c | 19 |
4 files changed, 35 insertions, 29 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c index 0120cb0..a512c60 100644 --- a/src/dged/buffer.c +++ b/src/dged/buffer.c @@ -215,6 +215,13 @@ static bool moveh(struct buffer *buffer, int64_t coldelta, return true; } +static void strip_final_newline(struct buffer *b) { + uint32_t nlines = text_num_lines(b->text); + if (nlines > 0 && text_line_length(b->text, nlines - 1) == 0) { + text_delete(b->text, nlines - 1, 0, nlines - 1, 1); + } +} + static void buffer_read_from_file(struct buffer *b) { struct stat sb; char *fullname = to_abspath(b->filename); @@ -245,6 +252,9 @@ static void buffer_read_from_file(struct buffer *b) { fclose(file); b->last_write = sb.st_mtim; + + // if last line is empty, remove it + strip_final_newline(b); } else { minibuffer_echo("Error opening %s: %s", b->filename, strerror(errno)); free(fullname); @@ -510,6 +520,10 @@ struct location buffer_set_text(struct buffer *buffer, uint8_t *text, text_clear(buffer->text); text_append(buffer->text, text, nbytes, &lines, &cols); + + // if last line is empty, remove it + strip_final_newline(buffer); + return buffer_clamp(buffer, lines, cols); } diff --git a/src/dged/lang.c b/src/dged/lang.c index 562f162..a287d59 100644 --- a/src/dged/lang.c +++ b/src/dged/lang.c @@ -51,10 +51,11 @@ static struct language g_fundamental = { void languages_init(bool register_default) { if (register_default) { + define_lang("Bash", "bash", ".*\\.bash", 4, NULL); define_lang("C", "c", ".*\\.(c|h)", 2, "clangd"); define_lang("C++", "cxx", ".*\\.(cpp|cxx|cc|c++|hh|h)", 2, "clangd"); define_lang("Rust", "rs", ".*\\.rs", 4, "rust-analyzer"); - define_lang("Nix", "nix", ".*\\.nix", 4, "rnix-lsp"); + define_lang("Nix", "nix", ".*\\.nix", 2, "rnix-lsp"); define_lang("Make", "make", ".*(Makefile|\\.mk)", 4, NULL); define_lang("Python", "python", ".*\\.py", 4, NULL); define_lang("Git Commit Message", "gitcommit", "COMMIT_EDITMSG", 4, NULL); diff --git a/src/dged/text.c b/src/dged/text.c index 30036a0..3942efc 100644 --- a/src/dged/text.c +++ b/src/dged/text.c @@ -104,7 +104,13 @@ uint32_t text_byteindex_to_col(struct text *text, uint32_t line, uint32_t text_global_idx(struct text *text, uint32_t line, uint32_t col) { uint32_t byteoff = 0; uint32_t nlines = text_num_lines(text); + + if (nlines == 0) { + return 0; + } + for (uint32_t l = 0; l < line && l < nlines; ++l) { + // +1 for newline byteoff += text_line_size(text, l) + 1; } @@ -288,8 +294,7 @@ void delete_line(struct text *text, uint32_t line) { void text_insert_at_inner(struct text *text, uint32_t line, uint32_t col, uint8_t *bytes, uint32_t nbytes, - uint32_t *lines_added, uint32_t *cols_added, - bool force_newline) { + uint32_t *lines_added, uint32_t *cols_added) { uint32_t linelen = 0, start_line = line; *cols_added = 0; @@ -302,12 +307,7 @@ void text_insert_at_inner(struct text *text, uint32_t line, uint32_t col, insert_at(text, line, col, line_data, linelen, nchars); col += nchars; - - // only insert a newline if we have to - if (force_newline || linelen == 0 || col < text_line_length(text, line) || - line + 1 < text->nlines) { - new_line_at(text, line, col); - } + new_line_at(text, line, col); ++line; linelen = 0; @@ -333,15 +333,13 @@ void text_append(struct text *text, uint8_t *bytes, uint32_t nbytes, uint32_t line = text->nlines > 0 ? text->nlines - 1 : 0; uint32_t col = text_line_length(text, line); - text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added, - true); + text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added); } void text_insert_at(struct text *text, uint32_t line, uint32_t col, uint8_t *bytes, uint32_t nbytes, uint32_t *lines_added, uint32_t *cols_added) { - text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added, - false); + text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added); } void text_delete(struct text *text, uint32_t start_line, uint32_t start_col, @@ -353,12 +351,8 @@ void text_delete(struct text *text, uint32_t start_line, uint32_t start_col, uint32_t maxline = text->nlines > 0 ? text->nlines - 1 : 0; - // make sure we stay inside if (start_line > maxline) { - start_line = maxline; - start_col = text->lines[start_line].nchars > 0 - ? text->lines[start_line].nchars - 1 - : 0; + return; } if (end_line > maxline) { diff --git a/test/text.c b/test/text.c index 3092dcc..9faa663 100644 --- a/test/text.c +++ b/test/text.c @@ -22,9 +22,6 @@ void test_add_text() { const char *txt = "This is line 1\n"; text_insert_at(t, 0, 0, (uint8_t *)txt, strlen(txt), &lines_added, &cols_added); - ASSERT( - text_num_lines(t) == 1, - "Expected text to have one line after insertion, since line 2 is empty"); ASSERT(text_line_size(t, 0) == 14 && text_line_length(t, 0) == 14, "Expected line 1 to have 14 chars and 14 bytes"); @@ -34,8 +31,8 @@ void test_add_text() { const char *txt2 = "This is line 2\n"; text_insert_at(t, 1, 0, (uint8_t *)txt2, strlen(txt2), &lines_added, &cols_added); - ASSERT(text_num_lines(t) == 2, - "Expected text to have two lines after second insertion"); + ASSERT(text_num_lines(t) == 3, + "Expected text to have three lines after second insertion"); assert_line_eq(text_get_line(t, 1), "This is line 2", "Expected line 2 to be line 2"); @@ -43,8 +40,8 @@ void test_add_text() { const char *txt3 = " "; text_insert_at(t, 0, 0, (uint8_t *)txt3, strlen(txt3), &lines_added, &cols_added); - ASSERT(text_num_lines(t) == 2, - "Expected text to have two lines after second insertion"); + ASSERT(text_num_lines(t) == 3, + "Expected text to have three lines after second insertion"); assert_line_eq(text_get_line(t, 0), " This is line 1", "Expected line 1 to be indented"); assert_line_eq(text_get_line(t, 1), "This is line 2", @@ -52,8 +49,8 @@ void test_add_text() { // insert newline in middle of line text_insert_at(t, 1, 4, (uint8_t *)"\n", 1, &lines_added, &cols_added); - ASSERT(text_num_lines(t) == 3, - "Expected text to have three lines after inserting a new line"); + ASSERT(text_num_lines(t) == 4, + "Expected text to have four lines after inserting a new line"); assert_line_eq(text_get_line(t, 1), "This", "Expected line 2 to be split"); assert_line_eq(text_get_line(t, 2), " is line 2", "Expected line 2 to be split"); @@ -61,8 +58,8 @@ void test_add_text() { // insert newline before line 1 text_insert_at(t, 1, 0, (uint8_t *)"\n", 1, &lines_added, &cols_added); ASSERT( - text_num_lines(t) == 4, - "Expected to have four lines after adding an empty line in the middle"); + text_num_lines(t) == 5, + "Expected to have five lines after adding an empty line in the middle"); ASSERT(text_line_length(t, 1) == 0, "Expected line 2 to be empty"); assert_line_eq(text_get_line(t, 2), "This", "Expected line 3 to be previous line 2"); |
