summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-25 16:04:46 -0500
committerpommicket <pommicket@gmail.com>2022-12-25 16:04:46 -0500
commit66a151badc6fb11b772c814ffcf2434e308923f2 (patch)
treede8c2f7825c96aeb591c2e6b37f33e81edb140a8 /buffer.c
parent9ee67ef5097451705b4ae9cb28895f726843cdf3 (diff)
fix problem with jdtls by appending \n to empty files
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/buffer.c b/buffer.c
index 239dda3..298e8b6 100644
--- a/buffer.c
+++ b/buffer.c
@@ -2142,12 +2142,18 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) {
buffer_seterr(buffer, "File too big (size: %zu).", file_size);
success = false;
} else {
- u8 *file_contents = buffer_calloc(buffer, 1, file_size + 1);
+ u8 *file_contents = buffer_calloc(buffer, 1, file_size + 2);
lines_capacity = 4;
lines = buffer_calloc(buffer, lines_capacity, sizeof *buffer->lines); // initial lines
nlines = 1;
size_t bytes_read = fread(file_contents, 1, file_size, fp);
if (bytes_read == file_size) {
+ // append a newline if there's no newline
+ if (file_contents[file_size - 1] != '\n') {
+ file_contents[file_size] = '\n';
+ ++file_size;
+ }
+
char32_t c = 0;
for (u8 *p = file_contents, *end = p + file_size; p != end; ) {
if (*p == '\r' && p != end-1 && p[1] == '\n') {
@@ -2178,8 +2184,7 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) {
buffer_line_append_char(buffer, line, c);
}
}
- }
- if (ferror(fp)) {
+ } else {
buffer_seterr(buffer, "Error reading from file.");
success = false;
}