refactor(terminal): only remove const qualifier when necessary (#26386)

This commit is contained in:
zeertzjq
2023-12-04 20:29:51 +08:00
committed by GitHub
parent cf612c64b0
commit 66f1563c7a
4 changed files with 26 additions and 24 deletions

View File

@ -1017,18 +1017,19 @@ Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err)
} }
} }
TerminalOptions topts;
Channel *chan = channel_alloc(kChannelStreamInternal); Channel *chan = channel_alloc(kChannelStreamInternal);
chan->stream.internal.cb = cb; chan->stream.internal.cb = cb;
chan->stream.internal.closed = false; chan->stream.internal.closed = false;
topts.data = chan; TerminalOptions topts = {
.data = chan,
// NB: overridden in terminal_check_size if a window is already // NB: overridden in terminal_check_size if a window is already
// displaying the buffer // displaying the buffer
topts.width = (uint16_t)MAX(curwin->w_width_inner - win_col_off(curwin), 0); .width = (uint16_t)MAX(curwin->w_width_inner - win_col_off(curwin), 0),
topts.height = (uint16_t)curwin->w_height_inner; .height = (uint16_t)curwin->w_height_inner,
topts.write_cb = term_write; .write_cb = term_write,
topts.resize_cb = term_resize; .resize_cb = term_resize,
topts.close_cb = term_close; .close_cb = term_close,
};
channel_incref(chan); channel_incref(chan);
terminal_open(&chan->term, buf, topts); terminal_open(&chan->term, buf, topts);
if (chan->term != NULL) { if (chan->term != NULL) {
@ -1038,7 +1039,7 @@ Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err)
return (Integer)chan->id; return (Integer)chan->id;
} }
static void term_write(char *buf, size_t size, void *data) // NOLINT(readability-non-const-parameter) static void term_write(const char *buf, size_t size, void *data)
{ {
Channel *chan = data; Channel *chan = data;
LuaRef cb = chan->stream.internal.cb; LuaRef cb = chan->stream.internal.cb;
@ -1048,7 +1049,7 @@ static void term_write(char *buf, size_t size, void *data) // NOLINT(readabilit
MAXSIZE_TEMP_ARRAY(args, 3); MAXSIZE_TEMP_ARRAY(args, 3);
ADD_C(args, INTEGER_OBJ((Integer)chan->id)); ADD_C(args, INTEGER_OBJ((Integer)chan->id));
ADD_C(args, BUFFER_OBJ(terminal_buf(chan->term))); ADD_C(args, BUFFER_OBJ(terminal_buf(chan->term)));
ADD_C(args, STRING_OBJ(((String){ .data = buf, .size = size }))); ADD_C(args, STRING_OBJ(((String){ .data = (char *)buf, .size = size })));
textlock++; textlock++;
nlua_call_ref(cb, "input", args, false, NULL); nlua_call_ref(cb, "input", args, false, NULL);
textlock--; textlock--;

View File

@ -794,19 +794,20 @@ static void channel_callback_call(Channel *chan, CallbackReader *reader)
/// and `buf` is assumed to be a new, unmodified buffer. /// and `buf` is assumed to be a new, unmodified buffer.
void channel_terminal_open(buf_T *buf, Channel *chan) void channel_terminal_open(buf_T *buf, Channel *chan)
{ {
TerminalOptions topts; TerminalOptions topts = {
topts.data = chan; .data = chan,
topts.width = chan->stream.pty.width; .width = chan->stream.pty.width,
topts.height = chan->stream.pty.height; .height = chan->stream.pty.height,
topts.write_cb = term_write; .write_cb = term_write,
topts.resize_cb = term_resize; .resize_cb = term_resize,
topts.close_cb = term_close; .close_cb = term_close,
};
buf->b_p_channel = (OptInt)chan->id; // 'channel' option buf->b_p_channel = (OptInt)chan->id; // 'channel' option
channel_incref(chan); channel_incref(chan);
terminal_open(&chan->term, buf, topts); terminal_open(&chan->term, buf, topts);
} }
static void term_write(char *buf, size_t size, void *data) static void term_write(const char *buf, size_t size, void *data)
{ {
Channel *chan = data; Channel *chan = data;
if (chan->stream.proc.in.closed) { if (chan->stream.proc.in.closed) {

View File

@ -188,7 +188,7 @@ void terminal_teardown(void)
static void term_output_callback(const char *s, size_t len, void *user_data) static void term_output_callback(const char *s, size_t len, void *user_data)
{ {
terminal_send((Terminal *)user_data, (char *)s, len); terminal_send((Terminal *)user_data, s, len);
} }
// public API {{{ // public API {{{
@ -680,7 +680,7 @@ void terminal_destroy(Terminal **termpp)
} }
} }
void terminal_send(Terminal *term, char *data, size_t size) static void terminal_send(Terminal *term, const char *data, size_t size)
{ {
if (term->closed) { if (term->closed) {
return; return;
@ -762,7 +762,7 @@ void terminal_paste(int count, char **y_array, size_t y_size)
vterm_keyboard_end_paste(curbuf->terminal->vt); vterm_keyboard_end_paste(curbuf->terminal->vt);
} }
void terminal_send_key(Terminal *term, int c) static void terminal_send_key(Terminal *term, int c)
{ {
VTermModifier mod = VTERM_MOD_NONE; VTermModifier mod = VTERM_MOD_NONE;
@ -780,7 +780,7 @@ void terminal_send_key(Terminal *term, int c)
} }
} }
void terminal_receive(Terminal *term, char *data, size_t len) void terminal_receive(Terminal *term, const char *data, size_t len)
{ {
if (!data) { if (!data) {
return; return;

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
typedef struct terminal Terminal; typedef struct terminal Terminal;
typedef void (*terminal_write_cb)(char *buffer, size_t size, void *data); typedef void (*terminal_write_cb)(const char *buffer, size_t size, void *data);
typedef void (*terminal_resize_cb)(uint16_t width, uint16_t height, void *data); typedef void (*terminal_resize_cb)(uint16_t width, uint16_t height, void *data);
typedef void (*terminal_close_cb)(void *data); typedef void (*terminal_close_cb)(void *data);