From ce56e0a845d68862118f44cb66c5f080e3c1bbed Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 30 Nov 2023 17:16:57 +0800 Subject: [PATCH] refactor(IWYU): move UI and LineFlags to ui_defs.h (#26318) --- Makefile | 8 --- src/clint.py | 12 +--- src/nvim/api/private/dispatch.h | 12 ++-- src/nvim/api/ui.h | 3 +- src/nvim/decoration.h | 10 +-- src/nvim/decoration_defs.h | 2 + src/nvim/highlight.h | 2 +- src/nvim/lua/spell.c | 4 +- src/nvim/tui/tui.c | 2 +- src/nvim/tui/tui.h | 2 +- src/nvim/ui.c | 10 ++- src/nvim/ui.h | 109 ++------------------------------ src/nvim/ui_compositor.h | 3 +- src/nvim/ui_defs.h | 95 ++++++++++++++++++++++++++++ 14 files changed, 134 insertions(+), 140 deletions(-) create mode 100644 src/nvim/ui_defs.h diff --git a/Makefile b/Makefile index 3781d06a6c..e1024009a3 100644 --- a/Makefile +++ b/Makefile @@ -143,14 +143,10 @@ iwyu: build/.ran-cmake cmake --build build > build/iwyu.log iwyu-fix-includes --only_re="src/nvim" --ignore_re="(src/nvim/eval/encode.c|src/nvim/auto/|src/nvim/os/lang.c|src/nvim/map.c\ |src/nvim/api/extmark.h\ - |src/nvim/api/private/dispatch.h\ |src/nvim/api/private/helpers.h\ |src/nvim/api/private/validate.h\ - |src/nvim/api/ui.h\ - |src/nvim/ascii_defs.h\ |src/nvim/assert_defs.h\ |src/nvim/autocmd.h\ - |src/nvim/autocmd_defs.h\ |src/nvim/buffer.h\ |src/nvim/buffer_defs.h\ |src/nvim/channel.h\ @@ -178,7 +174,6 @@ iwyu: build/.ran-cmake |src/nvim/event/time.h\ |src/nvim/event/wstream.h\ |src/nvim/ex_cmds.h\ - |src/nvim/ex_cmds_defs.h\ |src/nvim/ex_docmd.h\ |src/nvim/extmark.h\ |src/nvim/file_search.h\ @@ -217,10 +212,7 @@ iwyu: build/.ran-cmake |src/nvim/syntax.h\ |src/nvim/textobject.h\ |src/nvim/tui/input.h\ - |src/nvim/tui/tui.h\ |src/nvim/ui.h\ - |src/nvim/ui_client.h\ - |src/nvim/ui_compositor.h\ |src/nvim/viml/parser/expressions.h\ |src/nvim/viml/parser/parser.h\ |src/nvim/window.h\ diff --git a/src/clint.py b/src/clint.py index 1f588322f3..cb43ed59c7 100755 --- a/src/clint.py +++ b/src/clint.py @@ -898,14 +898,10 @@ def CheckIncludes(filename, lines, error): # the Makefile. check_includes_ignore = [ "src/nvim/api/extmark.h", - "src/nvim/api/private/dispatch.h", "src/nvim/api/private/helpers.h", "src/nvim/api/private/validate.h", - "src/nvim/api/ui.h", - "src/nvim/ascii_defs.h", "src/nvim/assert_defs.h", "src/nvim/autocmd.h", - "src/nvim/autocmd_defs.h", "src/nvim/buffer.h", "src/nvim/buffer_defs.h", "src/nvim/channel.h", @@ -933,7 +929,6 @@ def CheckIncludes(filename, lines, error): "src/nvim/event/time.h", "src/nvim/event/wstream.h", "src/nvim/ex_cmds.h", - "src/nvim/ex_cmds_defs.h", "src/nvim/ex_docmd.h", "src/nvim/extmark.h", "src/nvim/file_search.h", @@ -972,10 +967,7 @@ def CheckIncludes(filename, lines, error): "src/nvim/syntax.h", "src/nvim/textobject.h", "src/nvim/tui/input.h", - "src/nvim/tui/tui.h", "src/nvim/ui.h", - "src/nvim/ui_client.h", - "src/nvim/ui_compositor.h", "src/nvim/viml/parser/expressions.h", "src/nvim/viml/parser/parser.h", "src/nvim/window.h", @@ -999,8 +991,10 @@ def CheckIncludes(filename, lines, error): if name in skip_headers: continue if (not name.endswith('.h.generated.h') and + not name.endswith('/defs.h') and not name.endswith('_defs.h') and - not name.endswith('/defs.h')): + not name.endswith('_defs.generated.h') and + not name.endswith('_enum.generated.h')): error(filename, i, 'build/include_defs', 5, 'Headers should not include non-"_defs" headers') diff --git a/src/nvim/api/private/dispatch.h b/src/nvim/api/private/dispatch.h index 6a2c9eaf54..88f846f813 100644 --- a/src/nvim/api/private/dispatch.h +++ b/src/nvim/api/private/dispatch.h @@ -14,12 +14,12 @@ typedef Object (*ApiDispatchWrapper)(uint64_t channel_id, Array args, Arena *are struct MsgpackRpcRequestHandler { const char *name; ApiDispatchWrapper fn; - bool fast; // Function is safe to be executed immediately while running the - // uv loop (the loop is run very frequently due to breakcheck). - // If "fast" is false, the function is deferred, i e the call will - // be put in the event queue, for safe handling later. - bool arena_return; // return value is allocated in the arena (or statically) - // and should not be freed as such. + bool fast; ///< Function is safe to be executed immediately while running the + ///< uv loop (the loop is run very frequently due to breakcheck). + ///< If "fast" is false, the function is deferred, i e the call will + ///< be put in the event queue, for safe handling later. + bool arena_return; ///< return value is allocated in the arena (or statically) + ///< and should not be freed as such. }; extern const MsgpackRpcRequestHandler method_handlers[]; diff --git a/src/nvim/api/ui.h b/src/nvim/api/ui.h index 26a91d0dbc..b1f4ff97d9 100644 --- a/src/nvim/api/ui.h +++ b/src/nvim/api/ui.h @@ -4,9 +4,8 @@ #include "nvim/api/private/defs.h" // IWYU pragma: keep #include "nvim/highlight_defs.h" // IWYU pragma: keep -#include "nvim/map_defs.h" #include "nvim/types_defs.h" // IWYU pragma: keep -#include "nvim/ui.h" +#include "nvim/ui_defs.h" // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/ui.h.generated.h" diff --git a/src/nvim/decoration.h b/src/nvim/decoration.h index f5448c051b..e5bac169dc 100644 --- a/src/nvim/decoration.h +++ b/src/nvim/decoration.h @@ -14,9 +14,11 @@ // actual Decor* data is in decoration_defs.h -EXTERN const char *const virt_text_pos_str[] INIT( = { "eol", "overlay", "win_col", "right_align", - "inline" }); +/// Keep in sync with VirtTextPos in decoration_defs.h +EXTERN const char *const virt_text_pos_str[] +INIT( = { "eol", "overlay", "win_col", "right_align", "inline" }); +/// Keep in sync with HlMode in decoration_defs.h EXTERN const char *const hl_mode_str[] INIT( = { "", "replace", "combine", "blend" }); typedef enum { @@ -43,8 +45,8 @@ typedef struct { VirtTextPos pos; } ui; } data; - int attr_id; // cached lookup of inl.hl_id if it was a highlight - bool owned; // ephemeral decoration, free memory immediately + int attr_id; ///< cached lookup of inl.hl_id if it was a highlight + bool owned; ///< ephemeral decoration, free memory immediately DecorPriority priority; DecorRangeKind kind; /// Screen column to draw the virtual text. diff --git a/src/nvim/decoration_defs.h b/src/nvim/decoration_defs.h index dc5d7b9ae4..6e7dc08f80 100644 --- a/src/nvim/decoration_defs.h +++ b/src/nvim/decoration_defs.h @@ -15,6 +15,7 @@ typedef struct { typedef kvec_t(VirtTextChunk) VirtText; #define VIRTTEXT_EMPTY ((VirtText)KV_INITIAL_VALUE) +/// Keep in sync with virt_text_pos_str[] in decoration.h typedef enum { kVPosEndOfLine, kVPosOverlay, @@ -28,6 +29,7 @@ typedef kvec_t(struct virt_line { VirtText line; bool left_col; }) VirtLines; typedef uint16_t DecorPriority; #define DECOR_PRIORITY_BASE 0x1000 +/// Keep in sync with hl_mode_str[] in decoration.h typedef enum { kHlModeUnknown, kHlModeReplace, diff --git a/src/nvim/highlight.h b/src/nvim/highlight.h index e5d3f3d1ca..346c8aa864 100644 --- a/src/nvim/highlight.h +++ b/src/nvim/highlight.h @@ -7,7 +7,7 @@ #include "nvim/buffer_defs.h" // IWYU pragma: keep #include "nvim/highlight_defs.h" // IWYU pragma: export #include "nvim/option_vars.h" -#include "nvim/ui.h" +#include "nvim/ui_defs.h" // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "highlight.h.generated.h" diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index c261c5105e..e6c38ea9cb 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -15,10 +15,11 @@ #include "nvim/spell.h" #ifdef INCLUDE_GENERATED_DECLARATIONS -# include "lua/spell.c.generated.h" // IWYU pragma: export +# include "lua/spell.c.generated.h" #endif int nlua_spell_check(lua_State *lstate) + FUNC_ATTR_NONNULL_ALL { if (lua_gettop(lstate) < 1) { return luaL_error(lstate, "Expected 1 argument"); @@ -99,6 +100,7 @@ static const luaL_Reg spell_functions[] = { }; int luaopen_spell(lua_State *L) + FUNC_ATTR_NONNULL_ALL { lua_newtable(L); luaL_register(L, NULL, spell_functions); diff --git a/src/nvim/tui/tui.c b/src/nvim/tui/tui.c index 78e6cdf32a..c71eb633e9 100644 --- a/src/nvim/tui/tui.c +++ b/src/nvim/tui/tui.c @@ -34,8 +34,8 @@ #include "nvim/tui/tui.h" #include "nvim/types_defs.h" #include "nvim/ugrid.h" -#include "nvim/ui.h" #include "nvim/ui_client.h" +#include "nvim/ui_defs.h" #ifdef MSWIN # include "nvim/os/os_win_console.h" diff --git a/src/nvim/tui/tui.h b/src/nvim/tui/tui.h index 8eb4ac9bd8..34a98004f3 100644 --- a/src/nvim/tui/tui.h +++ b/src/nvim/tui/tui.h @@ -3,7 +3,7 @@ #include "nvim/api/private/defs.h" // IWYU pragma: keep #include "nvim/highlight_defs.h" // IWYU pragma: keep #include "nvim/types_defs.h" // IWYU pragma: keep -#include "nvim/ui.h" +#include "nvim/ui_defs.h" // IWYU pragma: keep typedef struct TUIData TUIData; diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 36f34bc75a..07166d229e 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -37,6 +36,11 @@ #include "nvim/window.h" #include "nvim/winfloat.h" +typedef struct ui_event_callback { + LuaRef cb; + bool ext_widgets[kUIGlobalCount]; +} UIEventCallback; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ui.c.generated.h" #endif @@ -692,7 +696,7 @@ void ui_call_event(char *name, Array args) ui_log(name); } -void ui_cb_update_ext(void) +static void ui_cb_update_ext(void) { memset(ui_cb_ext, 0, ARRAY_SIZE(ui_cb_ext)); @@ -708,7 +712,7 @@ void ui_cb_update_ext(void) } } -void free_ui_event_callback(UIEventCallback *event_cb) +static void free_ui_event_callback(UIEventCallback *event_cb) { api_free_luaref(event_cb->cb); xfree(event_cb); diff --git a/src/nvim/ui.h b/src/nvim/ui.h index 666a869c89..f61398a7a0 100644 --- a/src/nvim/ui.h +++ b/src/nvim/ui.h @@ -1,34 +1,16 @@ #pragma once -#include -#include -#include +#include // IWYU pragma: keep -#include "nvim/api/private/defs.h" +#include "nvim/api/private/defs.h" // IWYU pragma: keep #include "nvim/event/multiqueue.h" -#include "nvim/globals.h" -#include "nvim/highlight_defs.h" +#include "nvim/grid_defs.h" // IWYU pragma: keep +#include "nvim/highlight_defs.h" // IWYU pragma: keep #include "nvim/macros_defs.h" -#include "nvim/memory.h" -#include "nvim/types_defs.h" - -struct ui_t; - -typedef enum { - kUICmdline = 0, - kUIPopupmenu, - kUITabline, - kUIWildmenu, - kUIMessages, -#define kUIGlobalCount kUILinegrid - kUILinegrid, - kUIMultigrid, - kUIHlState, - kUITermColors, - kUIFloatDebug, - kUIExtCount, -} UIExtension; +#include "nvim/types_defs.h" // IWYU pragma: keep +#include "nvim/ui_defs.h" // IWYU pragma: export +/// Keep in sync with UIExtension in ui_defs.h EXTERN const char *ui_ext_names[] INIT( = { "ext_cmdline", "ext_popupmenu", @@ -42,83 +24,6 @@ EXTERN const char *ui_ext_names[] INIT( = { "_debug_float", }); -typedef struct ui_t UI; - -enum { - kLineFlagWrap = 1, - kLineFlagInvalid = 2, -}; - -typedef int LineFlags; - -typedef struct { - uint64_t channel_id; - -#define UI_BUF_SIZE 4096 ///< total buffer size for pending msgpack data. - /// guaranteed size available for each new event (so packing of simple events - /// and the header of grid_line will never fail) -#define EVENT_BUF_SIZE 256 - char buf[UI_BUF_SIZE]; ///< buffer of packed but not yet sent msgpack data - char *buf_wptr; ///< write head of buffer - const char *cur_event; ///< name of current event (might get multiple arglists) - Array call_buf; ///< buffer for constructing a single arg list (max 16 elements!) - - // state for write_cb, while packing a single arglist to msgpack. This - // might fail due to buffer overflow. - size_t pack_totlen; - bool buf_overflow; - char *temp_buf; - - // We start packing the two outermost msgpack arrays before knowing the total - // number of elements. Thus track the location where array size will need - // to be written in the msgpack buffer, once the specific array is finished. - char *nevents_pos; - char *ncalls_pos; - uint32_t nevents; ///< number of distinct events (top-level args to "redraw" - uint32_t ncalls; ///< number of calls made to the current event (plus one for the name!) - bool flushed_events; ///< events where sent to client without "flush" event - - size_t ncells_pending; ///< total number of cells since last buffer flush - - int hl_id; // Current highlight for legacy put event. - Integer cursor_row, cursor_col; // Intended visible cursor position. - - // Position of legacy cursor, used both for drawing and visible user cursor. - Integer client_row, client_col; - bool wildmenu_active; -} UIData; - -struct ui_t { - bool rgb; - bool override; ///< Force highest-requested UI capabilities. - bool composed; - bool ui_ext[kUIExtCount]; ///< Externalized UI capabilities. - int width; - int height; - int pum_nlines; /// actual nr. lines shown in PUM - bool pum_pos; /// UI reports back pum position? - double pum_row; - double pum_col; - double pum_height; - double pum_width; - - // TUI fields. - char *term_name; - char *term_background; ///< Deprecated. No longer needed since background color detection happens - ///< in Lua. To be removed in a future release. - int term_colors; - bool stdin_tty; - bool stdout_tty; - - // TODO(bfredl): integrate into struct! - UIData data[1]; -}; - -typedef struct ui_event_callback { - LuaRef cb; - bool ext_widgets[kUIGlobalCount]; -} UIEventCallback; - // uncrustify:off #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ui.h.generated.h" diff --git a/src/nvim/ui_compositor.h b/src/nvim/ui_compositor.h index f3f5981680..b0dd8e126b 100644 --- a/src/nvim/ui_compositor.h +++ b/src/nvim/ui_compositor.h @@ -1,10 +1,9 @@ #pragma once #include "nvim/api/private/defs.h" // IWYU pragma: keep -#include "nvim/event/defs.h" #include "nvim/grid_defs.h" // IWYU pragma: keep #include "nvim/types_defs.h" // IWYU pragma: keep -#include "nvim/ui.h" +#include "nvim/ui_defs.h" // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ui_compositor.h.generated.h" diff --git a/src/nvim/ui_defs.h b/src/nvim/ui_defs.h new file mode 100644 index 0000000000..c4707d4d7c --- /dev/null +++ b/src/nvim/ui_defs.h @@ -0,0 +1,95 @@ +#pragma once + +#include +#include +#include + +#include "nvim/api/private/defs.h" + +/// Keep in sync with ui_ext_names[] in ui.h +typedef enum { + kUICmdline = 0, + kUIPopupmenu, + kUITabline, + kUIWildmenu, + kUIMessages, +#define kUIGlobalCount kUILinegrid + kUILinegrid, + kUIMultigrid, + kUIHlState, + kUITermColors, + kUIFloatDebug, + kUIExtCount, +} UIExtension; + +enum { + kLineFlagWrap = 1, + kLineFlagInvalid = 2, +}; + +typedef int LineFlags; + +typedef struct ui_t UI; + +typedef struct { + uint64_t channel_id; + +#define UI_BUF_SIZE 4096 ///< total buffer size for pending msgpack data. + /// guaranteed size available for each new event (so packing of simple events + /// and the header of grid_line will never fail) +#define EVENT_BUF_SIZE 256 + char buf[UI_BUF_SIZE]; ///< buffer of packed but not yet sent msgpack data + char *buf_wptr; ///< write head of buffer + const char *cur_event; ///< name of current event (might get multiple arglists) + Array call_buf; ///< buffer for constructing a single arg list (max 16 elements!) + + // state for write_cb, while packing a single arglist to msgpack. This + // might fail due to buffer overflow. + size_t pack_totlen; + bool buf_overflow; + char *temp_buf; + + // We start packing the two outermost msgpack arrays before knowing the total + // number of elements. Thus track the location where array size will need + // to be written in the msgpack buffer, once the specific array is finished. + char *nevents_pos; + char *ncalls_pos; + uint32_t nevents; ///< number of distinct events (top-level args to "redraw" + uint32_t ncalls; ///< number of calls made to the current event (plus one for the name!) + bool flushed_events; ///< events where sent to client without "flush" event + + size_t ncells_pending; ///< total number of cells since last buffer flush + + int hl_id; // Current highlight for legacy put event. + Integer cursor_row, cursor_col; // Intended visible cursor position. + + // Position of legacy cursor, used both for drawing and visible user cursor. + Integer client_row, client_col; + bool wildmenu_active; +} UIData; + +struct ui_t { + bool rgb; + bool override; ///< Force highest-requested UI capabilities. + bool composed; + bool ui_ext[kUIExtCount]; ///< Externalized UI capabilities. + int width; + int height; + int pum_nlines; /// actual nr. lines shown in PUM + bool pum_pos; /// UI reports back pum position? + double pum_row; + double pum_col; + double pum_height; + double pum_width; + + // TUI fields. + char *term_name; + char *term_background; ///< Deprecated. No longer needed since background color detection happens + ///< in Lua. To be removed in a future release. + int term_colors; + bool stdin_tty; + bool stdout_tty; + + // TODO(bfredl): integrate into struct! + UIData data[1]; +};