refactor(IWYU): create {ex_getln,rbuffer,os/fileio}_defs.h (#26338)

This commit is contained in:
zeertzjq 2023-12-01 08:06:37 +08:00 committed by GitHub
parent b32b5b2711
commit 09e93d7c4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 174 additions and 153 deletions

View File

@ -151,7 +151,6 @@ iwyu: build/.ran-cmake
|src/nvim/buffer_defs.h\
|src/nvim/channel.h\
|src/nvim/charset.h\
|src/nvim/cmdexpand.h\
|src/nvim/decoration.h\
|src/nvim/drawline.h\
|src/nvim/eval.h\
@ -172,7 +171,6 @@ iwyu: build/.ran-cmake
|src/nvim/event/wstream.h\
|src/nvim/extmark.h\
|src/nvim/garray.h\
|src/nvim/getchar.h\
|src/nvim/globals.h\
|src/nvim/grid.h\
|src/nvim/highlight.h\
@ -187,7 +185,6 @@ iwyu: build/.ran-cmake
|src/nvim/msgpack_rpc/helpers.h\
|src/nvim/msgpack_rpc/unpacker.h\
|src/nvim/option.h\
|src/nvim/os/fileio.h\
|src/nvim/os/input.h\
|src/nvim/os/pty_conpty_win.h\
|src/nvim/os/pty_process_unix.h\

View File

@ -909,7 +909,6 @@ def CheckIncludes(filename, lines, error):
"src/nvim/buffer_defs.h",
"src/nvim/channel.h",
"src/nvim/charset.h",
"src/nvim/cmdexpand.h",
"src/nvim/decoration.h",
"src/nvim/drawline.h",
"src/nvim/eval.h",
@ -930,7 +929,6 @@ def CheckIncludes(filename, lines, error):
"src/nvim/event/wstream.h",
"src/nvim/extmark.h",
"src/nvim/garray.h",
"src/nvim/getchar.h",
"src/nvim/globals.h",
"src/nvim/grid.h",
"src/nvim/highlight.h",
@ -945,7 +943,6 @@ def CheckIncludes(filename, lines, error):
"src/nvim/msgpack_rpc/helpers.h",
"src/nvim/msgpack_rpc/unpacker.h",
"src/nvim/option.h",
"src/nvim/os/fileio.h",
"src/nvim/os/input.h",
"src/nvim/os/pty_conpty_win.h",
"src/nvim/os/pty_process_unix.h",

View File

@ -2,8 +2,9 @@
#include "nvim/cmdexpand_defs.h" // IWYU pragma: export
#include "nvim/eval/typval_defs.h" // IWYU pragma: keep
#include "nvim/ex_getln.h"
#include "nvim/ex_getln_defs.h" // IWYU pragma: keep
#include "nvim/garray_defs.h" // IWYU pragma: keep
#include "nvim/regexp_defs.h" // IWYU pragma: keep
#include "nvim/types_defs.h" // IWYU pragma: keep
// Values for nextwild() and ExpandOne(). See ExpandOne() for meaning.

View File

@ -13,8 +13,8 @@
#include "nvim/hashtab_defs.h"
#include "nvim/macros_defs.h"
#include "nvim/mbyte_defs.h" // IWYU pragma: keep
#include "nvim/os/fileio.h"
#include "nvim/os/stdpaths_defs.h"
#include "nvim/os/fileio_defs.h" // IWYU pragma: keep
#include "nvim/os/stdpaths_defs.h" // IWYU pragma: keep
#include "nvim/vim_defs.h" // IWYU pragma: keep
#define COPYID_INC 2

View File

@ -6,7 +6,7 @@
#include "nvim/event/loop.h"
#include "nvim/event/multiqueue.h"
#include "nvim/rbuffer.h"
#include "nvim/rbuffer_defs.h"
struct stream;

View File

@ -1,76 +1,13 @@
#pragma once
#include <stdbool.h>
#include <stddef.h> // IWYU pragma: keep
#include "klib/kvec.h"
#include "nvim/cmdexpand_defs.h"
#include "nvim/eval/typval_defs.h"
#include "nvim/eval/typval_defs.h" // IWYU pragma: keep
#include "nvim/ex_cmds_defs.h" // IWYU pragma: keep
#include "nvim/ex_getln_defs.h" // IWYU pragma: export
#include "nvim/option_defs.h" // IWYU pragma: keep
#include "nvim/types_defs.h" // IWYU pragma: keep
/// Command-line colors: one chunk
///
/// Defines a region which has the same highlighting.
typedef struct {
int start; ///< Colored chunk start.
int end; ///< Colored chunk end (exclusive, > start).
int attr; ///< Highlight attr.
} CmdlineColorChunk;
/// Command-line colors
///
/// Holds data about all colors.
typedef kvec_t(CmdlineColorChunk) CmdlineColors;
/// Command-line coloring
///
/// Holds both what are the colors and what have been colored. Latter is used to
/// suppress unnecessary calls to coloring callbacks.
typedef struct {
unsigned prompt_id; ///< ID of the prompt which was colored last.
char *cmdbuff; ///< What exactly was colored last time or NULL.
CmdlineColors colors; ///< Last colors.
} ColoredCmdline;
/// Keeps track how much state must be sent to external ui.
typedef enum {
kCmdRedrawNone,
kCmdRedrawPos,
kCmdRedrawAll,
} CmdRedraw;
/// Variables shared between getcmdline(), redrawcmdline() and others.
/// These need to be saved when using CTRL-R |, that's why they are in a
/// structure.
typedef struct cmdline_info CmdlineInfo;
struct cmdline_info {
char *cmdbuff; ///< pointer to command line buffer
int cmdbufflen; ///< length of cmdbuff
int cmdlen; ///< number of chars in command line
int cmdpos; ///< current cursor position
int cmdspos; ///< cursor column on screen
int cmdfirstc; ///< ':', '/', '?', '=', '>' or NUL
int cmdindent; ///< number of spaces before cmdline
char *cmdprompt; ///< message in front of cmdline
int cmdattr; ///< attributes for prompt
int overstrike; ///< Typing mode on the command line. Shared by
///< getcmdline() and put_on_cmdline().
expand_T *xpc; ///< struct being used for expansion, xp_pattern
///< may point into cmdbuff
int xp_context; ///< type of expansion
char *xp_arg; ///< user-defined expansion arg
int input_fn; ///< when true Invoked for input() function
unsigned prompt_id; ///< Prompt number, used to disable coloring on errors.
Callback highlight_callback; ///< Callback used for coloring user input.
ColoredCmdline last_colors; ///< Last cmdline colors
int level; ///< current cmdline level
CmdlineInfo *prev_ccline; ///< pointer to saved cmdline state
char special_char; ///< last putcmdline char (used for redraws)
bool special_shift; ///< shift of last putcmdline char
CmdRedraw redraw_state; ///< needed redraw for external cmdline
};
/// flags used by vim_strsave_fnameescape()
enum {
VSE_NONE = 0,

68
src/nvim/ex_getln_defs.h Normal file
View File

@ -0,0 +1,68 @@
#pragma once
#include <stdbool.h>
#include "klib/kvec.h"
#include "nvim/cmdexpand_defs.h"
/// Command-line colors: one chunk
///
/// Defines a region which has the same highlighting.
typedef struct {
int start; ///< Colored chunk start.
int end; ///< Colored chunk end (exclusive, > start).
int attr; ///< Highlight attr.
} CmdlineColorChunk;
/// Command-line colors
///
/// Holds data about all colors.
typedef kvec_t(CmdlineColorChunk) CmdlineColors;
/// Command-line coloring
///
/// Holds both what are the colors and what have been colored. Latter is used to
/// suppress unnecessary calls to coloring callbacks.
typedef struct {
unsigned prompt_id; ///< ID of the prompt which was colored last.
char *cmdbuff; ///< What exactly was colored last time or NULL.
CmdlineColors colors; ///< Last colors.
} ColoredCmdline;
/// Keeps track how much state must be sent to external ui.
typedef enum {
kCmdRedrawNone,
kCmdRedrawPos,
kCmdRedrawAll,
} CmdRedraw;
/// Variables shared between getcmdline(), redrawcmdline() and others.
/// These need to be saved when using CTRL-R |, that's why they are in a
/// structure.
typedef struct cmdline_info CmdlineInfo;
struct cmdline_info {
char *cmdbuff; ///< pointer to command line buffer
int cmdbufflen; ///< length of cmdbuff
int cmdlen; ///< number of chars in command line
int cmdpos; ///< current cursor position
int cmdspos; ///< cursor column on screen
int cmdfirstc; ///< ':', '/', '?', '=', '>' or NUL
int cmdindent; ///< number of spaces before cmdline
char *cmdprompt; ///< message in front of cmdline
int cmdattr; ///< attributes for prompt
int overstrike; ///< Typing mode on the command line. Shared by
///< getcmdline() and put_on_cmdline().
expand_T *xpc; ///< struct being used for expansion, xp_pattern
///< may point into cmdbuff
int xp_context; ///< type of expansion
char *xp_arg; ///< user-defined expansion arg
int input_fn; ///< when true Invoked for input() function
unsigned prompt_id; ///< Prompt number, used to disable coloring on errors.
Callback highlight_callback; ///< Callback used for coloring user input.
ColoredCmdline last_colors; ///< Last cmdline colors
int level; ///< current cmdline level
CmdlineInfo *prev_ccline; ///< pointer to saved cmdline state
char special_char; ///< last putcmdline char (used for redraws)
bool special_shift; ///< shift of last putcmdline char
CmdRedraw redraw_state; ///< needed redraw for external cmdline
};

View File

@ -1,11 +1,11 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h> // IWYU pragma: keep
#include <stdint.h> // IWYU pragma: keep
#include "nvim/eval/typval_defs.h" // IWYU pragma: keep
#include "nvim/getchar_defs.h" // IWYU pragma: export
#include "nvim/os/fileio.h"
#include "nvim/os/fileio_defs.h"
#include "nvim/types_defs.h" // IWYU pragma: keep
/// Argument for flush_buffers().

View File

@ -1,20 +1,8 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stddef.h> // IWYU pragma: keep
#include "nvim/func_attr.h"
#include "nvim/rbuffer.h"
/// Structure used to read from/write to file
typedef struct {
int fd; ///< File descriptor.
int _error; ///< Error code for use with RBuffer callbacks or zero.
RBuffer *rv; ///< Read or write buffer.
bool wr; ///< True if file is in write mode.
bool eof; ///< True if end of file was encountered.
bool non_blocking; ///< True if EAGAIN should not restart syscalls.
} FileDescriptor;
#include "nvim/os/fileio_defs.h" // IWYU pragma: export
/// file_open() flags
typedef enum {
@ -37,33 +25,6 @@ typedef enum {
kFileMkDir = 256,
} FileOpenFlags;
static inline bool file_eof(const FileDescriptor *fp)
REAL_FATTR_PURE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
/// Check whether end of file was encountered
///
/// @param[in] fp File to check.
///
/// @return true if it was, false if it was not or read operation was never
/// performed.
static inline bool file_eof(const FileDescriptor *const fp)
{
return fp->eof && rbuffer_size(fp->rv) == 0;
}
static inline int file_fd(const FileDescriptor *fp)
REAL_FATTR_PURE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
/// Return the file descriptor associated with the FileDescriptor structure
///
/// @param[in] fp File to check.
///
/// @return File descriptor.
static inline int file_fd(const FileDescriptor *const fp)
{
return fp->fd;
}
enum {
/// Read or write buffer size
///

43
src/nvim/os/fileio_defs.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <stdbool.h>
#include "nvim/func_attr.h"
#include "nvim/rbuffer_defs.h"
/// Structure used to read from/write to file
typedef struct {
int fd; ///< File descriptor.
int _error; ///< Error code for use with RBuffer callbacks or zero.
RBuffer *rv; ///< Read or write buffer.
bool wr; ///< True if file is in write mode.
bool eof; ///< True if end of file was encountered.
bool non_blocking; ///< True if EAGAIN should not restart syscalls.
} FileDescriptor;
static inline bool file_eof(const FileDescriptor *fp)
REAL_FATTR_ALWAYS_INLINE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
/// Check whether end of file was encountered
///
/// @param[in] fp File to check.
///
/// @return true if it was, false if it was not or read operation was never
/// performed.
static inline bool file_eof(const FileDescriptor *const fp)
{
return fp->eof && rbuffer_size(fp->rv) == 0;
}
static inline int file_fd(const FileDescriptor *fp)
REAL_FATTR_ALWAYS_INLINE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
/// Return the file descriptor associated with the FileDescriptor structure
///
/// @param[in] fp File to check.
///
/// @return File descriptor.
static inline int file_fd(const FileDescriptor *const fp)
{
return fp->fd;
}

View File

@ -29,27 +29,12 @@ RBuffer *rbuffer_new(size_t capacity)
return rv;
}
void rbuffer_free(RBuffer *buf)
void rbuffer_free(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
{
xfree(buf->temp);
xfree(buf);
}
size_t rbuffer_size(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
{
return buf->size;
}
size_t rbuffer_capacity(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
{
return (size_t)(buf->end_ptr - buf->start_ptr);
}
size_t rbuffer_space(RBuffer *buf) FUNC_ATTR_NONNULL_ALL
{
return rbuffer_capacity(buf) - buf->size;
}
/// Return a pointer to a raw buffer containing the first empty slot available
/// for writing. The second argument is a pointer to the maximum number of
/// bytes that could be written.

View File

@ -16,6 +16,8 @@
#include <stddef.h>
#include <stdint.h>
#include "nvim/rbuffer_defs.h" // IWYU pragma: export
// Macros that simplify working with the read/write pointers directly by hiding
// ring buffer wrap logic. Some examples:
//
@ -64,22 +66,6 @@
i-- > 0 ? ((int)(c = *rbuffer_get(buf, i))) || 1 : 0; \
)
typedef struct rbuffer RBuffer;
/// Type of function invoked during certain events:
/// - When the RBuffer switches to the full state
/// - When the RBuffer switches to the non-full state
typedef void (*rbuffer_callback)(RBuffer *buf, void *data);
struct rbuffer {
rbuffer_callback full_cb, nonfull_cb;
void *data;
size_t size;
// helper memory used to by rbuffer_reset if required
char *temp;
char *end_ptr, *read_ptr, *write_ptr;
char start_ptr[];
};
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "rbuffer.h.generated.h"
#endif

45
src/nvim/rbuffer_defs.h Normal file
View File

@ -0,0 +1,45 @@
#pragma once
#include <stddef.h>
#include "nvim/func_attr.h"
typedef struct rbuffer RBuffer;
/// Type of function invoked during certain events:
/// - When the RBuffer switches to the full state
/// - When the RBuffer switches to the non-full state
typedef void (*rbuffer_callback)(RBuffer *buf, void *data);
struct rbuffer {
rbuffer_callback full_cb, nonfull_cb;
void *data;
size_t size;
// helper memory used to by rbuffer_reset if required
char *temp;
char *end_ptr, *read_ptr, *write_ptr;
char start_ptr[];
};
static inline size_t rbuffer_size(RBuffer *buf)
REAL_FATTR_ALWAYS_INLINE REAL_FATTR_NONNULL_ALL;
static inline size_t rbuffer_size(RBuffer *buf)
{
return buf->size;
}
static inline size_t rbuffer_capacity(RBuffer *buf)
REAL_FATTR_ALWAYS_INLINE REAL_FATTR_NONNULL_ALL;
static inline size_t rbuffer_capacity(RBuffer *buf)
{
return (size_t)(buf->end_ptr - buf->start_ptr);
}
static inline size_t rbuffer_space(RBuffer *buf)
REAL_FATTR_ALWAYS_INLINE REAL_FATTR_NONNULL_ALL;
static inline size_t rbuffer_space(RBuffer *buf)
{
return rbuffer_capacity(buf) - buf->size;
}

View File

@ -13,6 +13,7 @@
#include "nvim/memory.h"
#include "nvim/option_vars.h"
#include "nvim/os/os.h"
#include "nvim/rbuffer.h"
#include "nvim/strings.h"
#include "nvim/tui/input.h"
#include "nvim/tui/input_defs.h"

View File

@ -7,7 +7,7 @@
#include "nvim/event/loop.h"
#include "nvim/event/stream.h"
#include "nvim/event/time.h"
#include "nvim/rbuffer.h"
#include "nvim/rbuffer_defs.h"
#include "nvim/tui/input_defs.h" // IWYU pragma: export
#include "nvim/tui/tui.h"
#include "nvim/types_defs.h"