summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-08-04 23:07:27 -0400
committerpommicket <pommicket@gmail.com>2023-08-04 23:07:27 -0400
commitfa4c4fcb7f215dd2d63318c3aef6dd0794a44548 (patch)
tree92c10f429f83af03157c1a426e72de3e2229655a
parent49ab483be3e7af88a3932a43f222aa42cacd3515 (diff)
cache buffer settings index
-rw-r--r--buffer.c19
-rw-r--r--main.c3
-rw-r--r--ted.h5
3 files changed, 16 insertions, 11 deletions
diff --git a/buffer.c b/buffer.c
index 379bda1..05e1f43 100644
--- a/buffer.c
+++ b/buffer.c
@@ -113,6 +113,7 @@ void buffer_create(TextBuffer *buffer, Ted *ted) {
memset(buffer, 0, sizeof *buffer);
buffer->store_undo_events = true;
buffer->ted = ted;
+ buffer->settings_idx = -1;
}
void line_buffer_create(TextBuffer *buffer, Ted *ted) {
@@ -218,7 +219,7 @@ Language buffer_language(TextBuffer *buffer) {
if (buffer->manual_language != LANG_NONE)
return (Language)buffer->manual_language;
- const Settings *settings = buffer->ted->default_settings; // important we don't use buffer_settings here since that would cause a loop!
+ const Settings *settings = buffer->ted->default_settings; // important we don't use buffer_settings here since that would cause infinite recursion!
const char *filename = path_filename(buffer->path);
int match_score = 0;
@@ -292,13 +293,18 @@ LSP *buffer_lsp(TextBuffer *buffer) {
Settings *buffer_settings(TextBuffer *buffer) {
- return ted_get_settings(buffer->ted, buffer->path, buffer_language(buffer));
+ Ted *ted = buffer->ted;
+ if (buffer->settings_idx >= 0 && buffer->settings_idx < (i32)arr_len(ted->all_settings))
+ return &ted->all_settings[buffer->settings_idx];
+
+ Settings *settings = ted_get_settings(ted, buffer->path, buffer_language(buffer));
+ buffer->settings_idx = (i32)(settings - ted->all_settings);
+ assert(buffer->settings_idx >= 0 && buffer->settings_idx < (i32)arr_len(ted->all_settings));
+ return settings;
}
u8 buffer_tab_width(TextBuffer *buffer) {
- if (!buffer->tab_width)
- buffer->tab_width = buffer_settings(buffer)->tab_width;
- return buffer->tab_width;
+ return buffer_settings(buffer)->tab_width;
}
bool buffer_indent_with_spaces(TextBuffer *buffer) {
@@ -2614,6 +2620,7 @@ Status buffer_load_file(TextBuffer *buffer, const char *path) {
if (success) {
// everything is good
buffer_clear(buffer);
+ buffer->settings_idx = -1;
buffer->lines = lines;
buffer->nlines = nlines;
buffer->frame_earliest_line_modified = 0;
@@ -2793,6 +2800,7 @@ bool buffer_save_as(TextBuffer *buffer, const char *new_path) {
LSP *lsp = buffer_lsp(buffer);
char *prev_path = buffer->path;
buffer->path = buffer_strdup(buffer, new_path);
+ buffer->settings_idx = -1; // we might have new settings
if (buffer->path && buffer_save(buffer)) {
buffer->view_only = false;
@@ -2925,7 +2933,6 @@ bool buffer_cursor_move_to_matching_bracket(TextBuffer *buffer) {
void buffer_render(TextBuffer *buffer, Rect r) {
const Settings *settings = buffer_settings(buffer);
- buffer->tab_width = settings->tab_width;
buffer_lsp(buffer); // this will send didOpen/didClose if the buffer's LSP changed
if (r.size.x < 1 || r.size.y < 1) {
diff --git a/main.c b/main.c
index 2b7a051..cbcf708 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,4 @@
/*
-TODO:
-- cache buffer settings
-
FUTURE FEATURES:
- autodetect indentation (tabs vs spaces)
- robust find (results shouldn't move around when you type things)
diff --git a/ted.h b/ted.h
index c872697..4327173 100644
--- a/ted.h
+++ b/ted.h
@@ -413,8 +413,9 @@ typedef struct {
u32 nlines;
/// capacity of \ref lines
u32 lines_capacity;
- /// cached tab width, updated every frame
- u8 tab_width;
+
+ /// cached settings index (into ted->all_settings), or -1 if has not been computed yet
+ i32 settings_idx;
/// which LSP this document is open in
LSPID lsp_opened_in;