diff options
author | pommicket <pommicket@gmail.com> | 2022-12-23 14:55:24 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-23 14:55:24 -0500 |
commit | 214dae3a7b58077436ffd173ae9fe8f2a6d9b97b (patch) | |
tree | 3fda7c1f935eff6ce9e9f647e148296af725e819 | |
parent | dbab768e4d0be378e65399f32f26edb4756533ce (diff) |
wait before showing "Loading..." for autocomplete
-rw-r--r-- | autocomplete.c | 11 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | ted.h | 6 | ||||
-rw-r--r-- | time.c | 7 |
4 files changed, 23 insertions, 3 deletions
diff --git a/autocomplete.c b/autocomplete.c index 020a2e3..adfda6f 100644 --- a/autocomplete.c +++ b/autocomplete.c @@ -189,6 +189,7 @@ static void autocomplete_open(Ted *ted, char32_t trigger_character) { ted->cursor_error_time = 0; ac->last_pos = (BufferPos){0,0}; ac->cursor = 0; + ac->open_time = ted->frame_time; autocomplete_find_completions(ted, trigger_character); switch (arr_len(ac->completions)) { @@ -254,6 +255,14 @@ static void autocomplete_frame(Ted *ted) { menu_height = 200.f; } + if (ac->waiting_for_lsp && ncompletions == 0) { + struct timespec now = ted->frame_time; + if (timespec_sub(now, ac->open_time) < 0.2) { + // don't show "Loading..." unless we've actually been loading for a bit of time + return; + } + } + if (!ac->waiting_for_lsp && ncompletions == 0) { // no completions. close menu. autocomplete_close(ted); @@ -347,7 +356,7 @@ static void autocomplete_frame(Ted *ted) { state.min_x = x + padding; state.min_y = y; state.max_x = x + menu_width - padding; state.max_y = y + menu_height; rgba_u32_to_floats(colors[COLOR_TEXT], state.color); - if (ac->waiting_for_lsp) { + if (ac->waiting_for_lsp && ncompletions == 0) { state.x = x + padding; state.y = y; text_utf8_with_state(font, &state, "Loading..."); } else { @@ -1,6 +1,5 @@ /* @TODO: -- only show "Loading..." if it's taking some time (prevent flash) - dont do completion if provides_completion = false - scroll through completions - LSP setting @@ -691,6 +690,7 @@ int main(int argc, char **argv) { while (!ted->quit) { double frame_start = time_get_seconds(); + ted->frame_time = time_get(); SDL_Event event; Uint8 const *keyboard_state = SDL_GetKeyboardState(NULL); @@ -382,6 +382,10 @@ typedef struct { // which trigger character invoked this (0 if autocomplete was manually invoked) char32_t trigger_char; + // when autocomplete menu was opened + // (this is used to figure out when we should display "Loading...") + struct timespec open_time; + Autocompletion *completions; // dynamic array of all completions u32 *suggested; // dynamic array of completions to be suggested (indices into completions) BufferPos last_pos; // position of cursor last time completions were generated. if this changes, we need to recompute completions. @@ -391,6 +395,8 @@ typedef struct { typedef struct Ted { struct LSP *test_lsp; // @TODO: something better + // current time, as of the start of this frame + struct timespec frame_time; SDL_Window *window; Font *font_bold; @@ -38,7 +38,12 @@ static struct timespec timespec_max(struct timespec a, struct timespec b) { static double timespec_to_seconds(struct timespec ts) { return (double)ts.tv_sec - + (double)ts.tv_nsec * 0.000000001; + + (double)ts.tv_nsec * 1e-9; +} + +static double timespec_sub(struct timespec a, struct timespec b) { + return (double)(a.tv_sec - b.tv_sec) + + (double)(a.tv_nsec - b.tv_nsec) * 1e-9; } static struct timespec time_get(void) { |