From 1ae21ce4136adbb50e93db8fe79116aca09317f0 Mon Sep 17 00:00:00 2001 From: glepnir Date: Wed, 17 Apr 2024 21:42:34 +0800 Subject: [PATCH] fix(float): tail truncate the long title Problem: now we truncate the title from start,This is somewhat inconsistent with the way vim works. Usually we use < to truncate and keep more trailing characters. Solution: keep more trailing characters. --- src/nvim/drawscreen.c | 36 +++++++++++++++++-- test/functional/ui/float_spec.lua | 58 ++++++++++++++++++++++++------- 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/src/nvim/drawscreen.c b/src/nvim/drawscreen.c index a01cbe6223f078..3e8fd6300966b5 100644 --- a/src/nvim/drawscreen.c +++ b/src/nvim/drawscreen.c @@ -92,6 +92,7 @@ #include "nvim/match.h" #include "nvim/mbyte.h" #include "nvim/memline.h" +#include "nvim/memory.h" #include "nvim/message.h" #include "nvim/move.h" #include "nvim/normal.h" @@ -717,6 +718,11 @@ void end_search_hl(void) static void win_redr_bordertext(win_T *wp, VirtText vt, int col) { + int used_col = col; + int width = wp->w_config.width; + bool truncate = false; + char *new_text = NULL; + for (size_t i = 0; i < kv_size(vt);) { int attr = 0; char *text = next_virt_text_chunk(vt, &i, &attr); @@ -724,11 +730,37 @@ static void win_redr_bordertext(win_T *wp, VirtText vt, int col) break; } attr = hl_apply_winblend(wp, attr); - col += grid_line_puts(col, text, -1, attr); + int cell = (int)mb_string2cells(text); + int shift = used_col + cell - width; + + if (shift > 1) { + truncate = true; + if (used_col > col) { + int move_start = col + shift - 1; + int start_pos = col; + for (int j = move_start; j < used_col; j++) { + grid_line_put_schar(start_pos, linebuf_char[j], linebuf_attr[j]); + start_pos++; + } + used_col -= (shift - 1); + } else { + new_text = xmemdupz(text + shift - 1, strlen(text) - (size_t)shift + 1); + } + } + + used_col += grid_line_puts(used_col, new_text ? new_text : text, -1, attr); + if (new_text) { + xfree(new_text); + new_text = NULL; + } + } + + if (truncate) { + grid_line_put_schar(col, schar_from_ascii('<'), linebuf_attr[col]); } } -int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align) +static int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align) { switch (align) { case kAlignLeft: diff --git a/test/functional/ui/float_spec.lua b/test/functional/ui/float_spec.lua index ed66557ee86fd2..f9eb33d3155dd2 100644 --- a/test/functional/ui/float_spec.lua +++ b/test/functional/ui/float_spec.lua @@ -1935,15 +1935,26 @@ describe('float window', function() eq('center', footer_pos) end) - it('center aligned title longer than window width #25746', function() + it('truncates title longer than window width #23602', function() local buf = api.nvim_create_buf(false, false) api.nvim_buf_set_lines(buf, 0, -1, true, {' halloj! ', ' BORDAA '}) - local win = api.nvim_open_win(buf, false, { + local config = { relative='editor', width=9, height=2, row=2, col=5, border="double", title = "abcdefghijklmnopqrstuvwxyz",title_pos = "center", - }) + } + + local expected = [[ + ^ | + {0:~ }| + {0:~ }{5:╔}{11: