This commit is contained in:
glepnir 2024-09-16 19:38:28 +08:00 committed by GitHub
commit cd2672ef58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -35,6 +35,7 @@
#include "nvim/highlight_defs.h" #include "nvim/highlight_defs.h"
#include "nvim/insexpand.h" #include "nvim/insexpand.h"
#include "nvim/keycodes.h" #include "nvim/keycodes.h"
#include "nvim/mark.h"
#include "nvim/mbyte.h" #include "nvim/mbyte.h"
#include "nvim/memline.h" #include "nvim/memline.h"
#include "nvim/memory.h" #include "nvim/memory.h"
@ -784,36 +785,34 @@ void pum_redraw(void)
} }
} }
/// set info text to preview buffer.
static void pum_preview_set_text(buf_T *buf, char *info, linenr_T *lnum, int *max_width) static void pum_preview_set_text(buf_T *buf, char *info, linenr_T *lnum, int *max_width)
{ {
bcount_t inserted_bytes = 0; Error err = ERROR_INIT;
for (char *p = info; *p != NUL;) { Arena arena = ARENA_EMPTY;
int text_width = 0; Array replacement = ARRAY_DICT_INIT;
char *e = vim_strchr(p, '\n'); char *token = NULL;
if (e == NULL) { char *line = os_strtok(info, "\n", &token);
ml_append_buf(buf, (*lnum)++, p, 0, false); buf->b_p_ma = true;
text_width = (int)mb_string2cells(p); while (line != NULL) {
if (text_width > *max_width) { ADD(replacement, STRING_OBJ(cstr_to_string(line)));
*max_width = text_width; (*lnum)++;
} (*max_width) = MAX(*max_width, (int)mb_string2cells(line));
break; line = os_strtok(NULL, "\n", &token);
}
*e = NUL;
ml_append_buf(buf, (*lnum)++, p, (int)(e - p + 1), false);
inserted_bytes += (bcount_t)strlen(p) + 1;
text_width = (int)mb_string2cells(p);
if (text_width > *max_width) {
*max_width = text_width;
}
*e = '\n';
p = e + 1;
} }
// delete the empty last line
ml_delete_buf(buf, buf->b_ml.ml_line_count, false); int original_textlock = textlock;
if (get_cot_flags() & COT_POPUP) { if (textlock > 0) {
extmark_splice(buf, 1, 0, 1, 0, 0, buf->b_ml.ml_line_count, 0, inserted_bytes, kExtmarkNoUndo); textlock = 0;
} }
nvim_buf_set_lines(0, buf->handle, 0, -1, false, replacement, &arena, &err);
if (ERROR_SET(&err)) {
emsg(err.msg);
api_clear_error(&err);
}
textlock = original_textlock;
arena_mem_free(arena_finish(&arena));
api_free_array(replacement);
buf->b_p_ma = false;
} }
/// adjust floating info preview window position /// adjust floating info preview window position
@ -863,14 +862,6 @@ win_T *pum_set_info(int selected, char *info)
if (!wp) { if (!wp) {
return NULL; return NULL;
} }
} else {
// clean exist buffer
linenr_T count = wp->w_buffer->b_ml.ml_line_count;
while (!buf_is_empty(wp->w_buffer)) {
ml_delete_buf(wp->w_buffer, 1, false);
}
bcount_t deleted_bytes = get_region_bytecount(wp->w_buffer, 1, count, 0, 0);
extmark_splice(wp->w_buffer, 1, 0, count, 0, deleted_bytes, 1, 0, 0, kExtmarkNoUndo);
} }
linenr_T lnum = 0; linenr_T lnum = 0;
int max_info_width = 0; int max_info_width = 0;
@ -1017,7 +1008,8 @@ static bool pum_set_selected(int n, int repeat)
&& (curbuf->b_nwindows == 1) && (curbuf->b_nwindows == 1)
&& (curbuf->b_fname == NULL) && (curbuf->b_fname == NULL)
&& bt_nofile(curbuf) && bt_nofile(curbuf)
&& (curbuf->b_p_bh[0] == 'w')) { && (curbuf->b_p_bh[0] == 'w')
&& !use_float) {
// Already a "wipeout" buffer, make it empty. // Already a "wipeout" buffer, make it empty.
while (!buf_is_empty(curbuf)) { while (!buf_is_empty(curbuf)) {
ml_delete(1, false); ml_delete(1, false);