From 4ab3631aadcd27df708a16f6bf22953e0ff9b1cf Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 13:55:53 -0400 Subject: internalize Line structure --- buffer.c | 14 ++++++++++++++ build.c | 8 ++++---- ide-document-link.c | 7 +++++++ ide-usages.c | 3 +-- tags.c | 4 ++-- ted.h | 38 ++++++++++++++++++++------------------ 6 files changed, 48 insertions(+), 26 deletions(-) diff --git a/buffer.c b/buffer.c index 2ce9fdf..3f32585 100644 --- a/buffer.c +++ b/buffer.c @@ -8,6 +8,12 @@ #define BUFFER_UNTITLED "Untitled" // what to call untitled buffers +struct Line { + SyntaxState syntax; + u32 len; + char32_t *str; +}; + // this is a macro so we get -Wformat warnings #define buffer_error(buffer, ...) \ snprintf(buffer->error, sizeof buffer->error - 1, __VA_ARGS__) @@ -331,6 +337,10 @@ bool buffer_indent_with_spaces(TextBuffer *buffer) { return buffer_settings(buffer)->indent_with_spaces; } +u32 buffer_get_num_lines(TextBuffer *buffer) { + return buffer->nlines; +} + String32 buffer_get_line(TextBuffer *buffer, u32 line_number) { if (line_number >= buffer->nlines) { return str32(NULL, 0); @@ -341,6 +351,10 @@ String32 buffer_get_line(TextBuffer *buffer, u32 line_number) { }; } +char *buffer_get_line_utf8(TextBuffer *buffer, u32 line_number) { + return str32_to_utf8_cstr(buffer_get_line(buffer, line_number)); +} + // Returns a simple checksum of the buffer. // This is only used for testing, and shouldn't be relied on. static u64 buffer_checksum(TextBuffer *buffer) { diff --git a/build.c b/build.c index d4804b2..5e95b32 100644 --- a/build.c +++ b/build.c @@ -204,8 +204,8 @@ void build_check_for_errors(Ted *ted) { TextBuffer *buffer = &ted->build_buffer; arr_clear(ted->build_errors); for (u32 line_idx = 0; line_idx < buffer->nlines; ++line_idx) { - Line *line = &buffer->lines[line_idx]; - if (line->len < 3) { + String32 line = buffer_get_line(buffer, line_idx); + if (line.len < 3) { continue; } bool is_error = true; @@ -218,7 +218,7 @@ void build_check_for_errors(Ted *ted) { // all this is to say that columns_per_tab is currently always 1, // but might be useful at some point. u8 columns_per_tab = 1; - char32_t *p = line->str, *end = p + line->len; + char32_t *p = line.str, *end = p + line.len; { // rust errors look like: @@ -242,7 +242,7 @@ void build_check_for_errors(Ted *ted) { char32_t *filename_start = p; while (p != end) { if ((*p == ':' || *p == '(') - && p != line->str + 1) // don't catch "C:\thing\whatever.c" as "filename: C, line number: \thing\whatever.c" + && p != line.str + 1) // don't catch "C:\thing\whatever.c" as "filename: C, line number: \thing\whatever.c" break; if (!is_source_path(*p)) { is_error = false; diff --git a/ide-document-link.c b/ide-document-link.c index 731089c..7f5e476 100644 --- a/ide-document-link.c +++ b/ide-document-link.c @@ -1,5 +1,12 @@ #include "ted.h" +struct DocumentLink { + char *target; + char *tooltip; + BufferPos start; + BufferPos end; +}; + void document_link_clear(Ted *ted) { DocumentLinks *dl = &ted->document_links; arr_foreach_ptr(dl->links, DocumentLink, l) { diff --git a/ide-usages.c b/ide-usages.c index a3b9194..b9c2085 100644 --- a/ide-usages.c +++ b/ide-usages.c @@ -65,8 +65,7 @@ void usages_process_lsp_response(Ted *ted, const LSPResponse *response) { if (last_buffer) { // read the line from the buffer if (line < last_buffer->nlines) { - BufferPos pos = {.line = line, .index = 0}; - line_text = buffer_get_utf8_text_at_pos(last_buffer, pos, last_buffer->lines[line].len); + line_text = buffer_get_line_utf8(last_buffer, line); } } else if (last_file) { // read the line from the file diff --git a/tags.c b/tags.c index 16c005a..95f5e5b 100644 --- a/tags.c +++ b/tags.c @@ -325,8 +325,8 @@ top:; pcre2_match_data *match_data = pcre2_match_data_create(10, NULL); if (match_data) { for (u32 line_idx = 0; line_idx < buffer->nlines; ++line_idx) { - Line *line = &buffer->lines[line_idx]; - int n = pcre2_match(code, line->str, line->len, 0, PCRE2_NOTEMPTY, + String32 line = buffer_get_line(buffer, line_idx); + int n = pcre2_match(code, line.str, line.len, 0, PCRE2_NOTEMPTY, match_data, NULL); if (n == 1) { // found it! diff --git a/ted.h b/ted.h index 27be1b0..a8decf2 100644 --- a/ted.h +++ b/ted.h @@ -42,6 +42,8 @@ extern "C" { #define TEXT_SIZE_MAX 70 /// max number of LSPs running at once #define TED_LSP_MAX 200 +/// max number of macros +#define TED_MACRO_MAX 256 // If you are adding new languages, DO NOT change the constant values // of the previous languages. It will mess up config files which use :set-language! @@ -150,6 +152,8 @@ typedef struct { /// for markdown #define SYNTAX_LINK SYNTAX_CONSTANT +typedef struct Settings Settings; + /// special keycodes for mouse X1 & X2 buttons. enum { KEYCODE_X1 = 1<<20, @@ -242,7 +246,7 @@ typedef struct { /// /// NOTE: to add more options to ted, add fields here, /// and change the settings_ global constant near the top of config.c -typedef struct { +struct Settings { SettingsContext context; u32 colors[COLOR_COUNT]; float cursor_blink_time_on, cursor_blink_time_off; @@ -311,7 +315,7 @@ typedef struct { LanguageExtension *language_extensions; /// dynamic array, sorted by KEY_COMBO(modifier, key) KeyAction *key_actions; -} Settings; +}; /// A position in the buffer typedef struct { @@ -324,11 +328,7 @@ typedef struct { } BufferPos; /// A single line in a buffer -typedef struct { - SyntaxState syntax; - u32 len; - char32_t *str; -} Line; +typedef struct Line Line; /// Sections of `ted.cfg` typedef enum { @@ -670,12 +670,7 @@ typedef struct { Signature signatures[SIGNATURE_HELP_MAX]; } SignatureHelp; -typedef struct { - char *target; - char *tooltip; - BufferPos start; - BufferPos end; -} DocumentLink; +typedef struct DocumentLink DocumentLink; /// "document link" information (LSP) typedef struct { @@ -746,9 +741,9 @@ typedef struct { /// more severe message types should have higher numbers. /// they will override less severe messages. typedef enum { - MESSAGE_INFO, - MESSAGE_WARNING, - MESSAGE_ERROR + MESSAGE_INFO = 0x10000, + MESSAGE_WARNING = 0x20000, + MESSAGE_ERROR = 0x30000, } MessageType; typedef struct { @@ -766,8 +761,6 @@ typedef struct { Font *font; } LoadedFont; -#define TED_MACRO_MAX 256 - typedef struct { vec2 pos; u8 times; @@ -1010,10 +1003,19 @@ Settings *buffer_settings(TextBuffer *buffer); u8 buffer_tab_width(TextBuffer *buffer); /// Get whether or not to indent with spaces for this buffer. bool buffer_indent_with_spaces(TextBuffer *buffer); +/// returns the number of lines in the buffer. +u32 buffer_get_num_lines(TextBuffer *buffer); +/// get line contents. does not include a newline character. /// NOTE: this string will be invalidated when the line is edited!!! /// only use it briefly!! +/// /// returns an empty string if `line_number` is out of range. String32 buffer_get_line(TextBuffer *buffer, u32 line_number); +/// get line contents. does not include a newline character. +/// +/// string must be freed by caller. +/// returns an empty string if `line_number` is out of range. +char *buffer_get_line_utf8(TextBuffer *buffer, u32 line_number); /// get at most `nchars` characters starting from position `pos`. /// returns the number of characters actually available. /// you can pass NULL for text if you just want to know how many -- cgit v1.2.3