summaryrefslogtreecommitdiff
path: root/src/dged
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2024-02-05 15:00:35 +0100
committerAlbert Cervin <albert@acervin.com>2024-02-12 09:46:04 +0100
commit84a29094d497cf56c4efd5505efb044b822b89cd (patch)
treec3e17ef67cafc58ed59e576eaa34e4b515038321 /src/dged
parent9be0d9bddd6189ce82ea3775571b5b94d6e168ca (diff)
downloaddged-84a29094d497cf56c4efd5505efb044b822b89cd.tar.gz
dged-84a29094d497cf56c4efd5505efb044b822b89cd.tar.xz
dged-84a29094d497cf56c4efd5505efb044b822b89cd.zip
Fix final newline displaying
Diffstat (limited to 'src/dged')
-rw-r--r--src/dged/buffer.c14
-rw-r--r--src/dged/lang.c3
-rw-r--r--src/dged/text.c28
3 files changed, 27 insertions, 18 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) {