refactor(api): make typed dicts appear as types in the source code

problem: can we have Serde?
solution: we have Serde at home

This by itself is just a change of notation, that could be quickly
merged to avoid messy merge conflicts, but upcoming changes are planned:

- keysets no longer need to be defined in one single file. `keysets.h` is
  just the initial automatic conversion of the previous `keysets.lua`.
  keysets just used in a single api/{scope}.h can be moved to that file, later on.

- Typed dicts will have more specific types than Object. this will
  enable most of the existing manual typechecking boilerplate to be eliminated.
  We will need some annotation for missing value, i e a boolean will
  need to be represented as a TriState (none/false/true) in some cases.

- Eventually: optional parameters in form of a `Dict opts` final
  parameter will get added in some form to metadata. this will require
  a discussion/desicion about type forward compatibility.
This commit is contained in:
bfredl
2023-04-03 15:21:24 +02:00
parent 04933b1ea9
commit efb0896f21
23 changed files with 401 additions and 371 deletions

View File

@@ -3,6 +3,7 @@
#include <stdint.h>
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@@ -3,6 +3,7 @@
#include <lauxlib.h>
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#include "nvim/buffer_defs.h"

View File

@@ -10,6 +10,7 @@
#include "lauxlib.h"
#include "nvim/api/command.h"
#include "nvim/api/private/defs.h"
#include "nvim/api/private/dispatch.h"
#include "nvim/api/private/helpers.h"
#include "nvim/api/private/validate.h"
#include "nvim/ascii.h"

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_API_COMMAND_H
#define NVIM_API_COMMAND_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#include "nvim/decoration.h"
#include "nvim/ex_cmds.h"

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_API_EXTMARK_H
#define NVIM_API_EXTMARK_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#include "nvim/decoration.h"
#include "nvim/macros.h"

270
src/nvim/api/keysets.h Normal file
View File

@@ -0,0 +1,270 @@
#ifndef NVIM_API_KEYSETS_H
#define NVIM_API_KEYSETS_H
#include "nvim/api/private/defs.h"
typedef struct {
Object types;
} Dict(context);
typedef struct {
Object on_start;
Object on_buf;
Object on_win;
Object on_line;
Object on_end;
Object _on_hl_def;
Object _on_spell_nav;
} Dict(set_decoration_provider);
typedef struct {
Object id;
Object end_line;
Object end_row;
Object end_col;
Object hl_group;
Object virt_text;
Object virt_text_pos;
Object virt_text_win_col;
Object virt_text_hide;
Object hl_eol;
Object hl_mode;
Object ephemeral;
Object priority;
Object right_gravity;
Object end_right_gravity;
Object virt_lines;
Object virt_lines_above;
Object virt_lines_leftcol;
Object strict;
Object sign_text;
Object sign_hl_group;
Object number_hl_group;
Object line_hl_group;
Object cursorline_hl_group;
Object conceal;
Object spell;
Object ui_watched;
} Dict(set_extmark);
typedef struct {
Object noremap;
Object nowait;
Object silent;
Object script;
Object expr;
Object unique;
Object callback;
Object desc;
Object replace_keycodes;
} Dict(keymap);
typedef struct {
Object builtin;
} Dict(get_commands);
typedef struct {
Object addr;
Object bang;
Object bar;
Object complete;
Object count;
Object desc;
Object force;
Object keepscript;
Object nargs;
Object preview;
Object range;
Object register_;
} Dict(user_command);
typedef struct {
Object row;
Object col;
Object width;
Object height;
Object anchor;
Object relative;
Object win;
Object bufpos;
Object external;
Object focusable;
Object zindex;
Object border;
Object title;
Object title_pos;
Object style;
Object noautocmd;
} Dict(float_config);
typedef struct {
Object is_lua;
Object do_source;
} Dict(runtime);
typedef struct {
Object winid;
Object maxwidth;
Object fillchar;
Object highlights;
Object use_winbar;
Object use_tabline;
Object use_statuscol_lnum;
} Dict(eval_statusline);
typedef struct {
Object scope;
Object win;
Object buf;
Object filetype;
} Dict(option);
typedef struct {
Object bold;
Object standout;
Object strikethrough;
Object underline;
Object undercurl;
Object underdouble;
Object underdotted;
Object underdashed;
Object italic;
Object reverse;
Object altfont;
Object nocombine;
Object default_;
Object cterm;
Object foreground;
Object fg;
Object background;
Object bg;
Object ctermfg;
Object ctermbg;
Object special;
Object sp;
Object link;
Object global_link;
Object fallback;
Object blend;
Object fg_indexed;
Object bg_indexed;
} Dict(highlight);
typedef struct {
Object bold;
Object standout;
Object strikethrough;
Object underline;
Object undercurl;
Object underdouble;
Object underdotted;
Object underdashed;
Object italic;
Object reverse;
Object altfont;
Object nocombine;
} Dict(highlight_cterm);
typedef struct {
Object id;
Object name;
Object link;
} Dict(get_highlight);
typedef struct {
Object buffer;
Object event;
Object group;
Object pattern;
} Dict(clear_autocmds);
typedef struct {
Object buffer;
Object callback;
Object command;
Object desc;
Object group;
Object nested;
Object once;
Object pattern;
} Dict(create_autocmd);
typedef struct {
Object buffer;
Object group;
Object modeline;
Object pattern;
Object data;
} Dict(exec_autocmds);
typedef struct {
Object event;
Object group;
Object pattern;
Object buffer;
} Dict(get_autocmds);
typedef struct {
Object clear;
} Dict(create_augroup);
typedef struct {
Object cmd;
Object range;
Object count;
Object reg;
Object bang;
Object args;
Object magic;
Object mods;
Object nargs;
Object addr;
Object nextcmd;
} Dict(cmd);
typedef struct {
Object file;
Object bar;
} Dict(cmd_magic);
typedef struct {
Object silent;
Object emsg_silent;
Object unsilent;
Object filter;
Object sandbox;
Object noautocmd;
Object browse;
Object confirm;
Object hide;
Object horizontal;
Object keepalt;
Object keepjumps;
Object keepmarks;
Object keeppatterns;
Object lockmarks;
Object noswapfile;
Object tab;
Object verbose;
Object vertical;
Object split;
} Dict(cmd_mods);
typedef struct {
Object pattern;
Object force;
} Dict(cmd_mods_filter);
typedef struct {
Object output;
} Dict(cmd_opts);
typedef struct {
Object verbose;
} Dict(echo_opts);
typedef struct {
Object output;
} Dict(exec_opts);
#endif // NVIM_API_KEYSETS_H

View File

@@ -1,239 +0,0 @@
return {
{ 'context', {
"types";
}};
{ 'set_decoration_provider', {
"on_start";
"on_buf";
"on_win";
"on_line";
"on_end";
"_on_hl_def";
"_on_spell_nav";
}};
{ 'set_extmark', {
"id";
"end_line";
"end_row";
"end_col";
"hl_group";
"virt_text";
"virt_text_pos";
"virt_text_win_col";
"virt_text_hide";
"hl_eol";
"hl_mode";
"ephemeral";
"priority";
"right_gravity";
"end_right_gravity";
"virt_lines";
"virt_lines_above";
"virt_lines_leftcol";
"strict";
"sign_text";
"sign_hl_group";
"number_hl_group";
"line_hl_group";
"cursorline_hl_group";
"conceal";
"spell";
"ui_watched";
}};
{ 'keymap', {
"noremap";
"nowait";
"silent";
"script";
"expr";
"unique";
"callback";
"desc";
"replace_keycodes";
}};
{ 'get_commands', {
"builtin";
}};
{ 'user_command', {
"addr";
"bang";
"bar";
"complete";
"count";
"desc";
"force";
"keepscript";
"nargs";
"preview";
"range";
"register";
}};
{ 'float_config', {
"row";
"col";
"width";
"height";
"anchor";
"relative";
"win";
"bufpos";
"external";
"focusable";
"zindex";
"border";
"title";
"title_pos";
"style";
"noautocmd";
}};
{ 'runtime', {
"is_lua";
"do_source";
}};
{ 'eval_statusline', {
"winid";
"maxwidth";
"fillchar";
"highlights";
"use_winbar";
"use_tabline";
"use_statuscol_lnum";
}};
{ 'option', {
"scope";
"win";
"buf";
"filetype";
}};
{ 'highlight', {
"bold";
"standout";
"strikethrough";
"underline";
"undercurl";
"underdouble";
"underdotted";
"underdashed";
"italic";
"reverse";
"altfont";
"nocombine";
"default";
"cterm";
"foreground"; "fg";
"background"; "bg";
"ctermfg";
"ctermbg";
"special"; "sp";
"link";
"global_link";
"fallback";
"blend";
"fg_indexed";
"bg_indexed";
}};
{ 'highlight_cterm', {
"bold";
"standout";
"strikethrough";
"underline";
"undercurl";
"underdouble";
"underdotted";
"underdashed";
"italic";
"reverse";
"altfont";
"nocombine";
}};
{ 'get_highlight', {
"id";
"name";
"link";
}};
-- Autocmds
{ 'clear_autocmds', {
"buffer";
"event";
"group";
"pattern";
}};
{ 'create_autocmd', {
"buffer";
"callback";
"command";
"desc";
"group";
"nested";
"once";
"pattern";
}};
{ 'exec_autocmds', {
"buffer";
"group";
"modeline";
"pattern";
"data";
}};
{ 'get_autocmds', {
"event";
"group";
"pattern";
"buffer";
}};
{ 'create_augroup', {
"clear";
}};
{ 'cmd', {
"cmd";
"range";
"count";
"reg";
"bang";
"args";
"magic";
"mods";
"nargs";
"addr";
"nextcmd";
}};
{ 'cmd_magic', {
"file";
"bar";
}};
{ 'cmd_mods', {
"silent";
"emsg_silent";
"unsilent";
"filter";
"sandbox";
"noautocmd";
"browse";
"confirm";
"hide";
"horizontal";
"keepalt";
"keepjumps";
"keepmarks";
"keeppatterns";
"lockmarks";
"noswapfile";
"tab";
"verbose";
"vertical";
"split";
}};
{ 'cmd_mods_filter', {
"pattern";
"force";
}};
{ 'cmd_opts', {
"output";
}};
{ 'echo_opts', {
"verbose";
}};
{ 'exec_opts', {
"output";
}};
}

View File

@@ -1,7 +1,9 @@
#ifndef NVIM_API_OPTIONS_H
#define NVIM_API_OPTIONS_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/options.h.generated.h"
#endif

View File

@@ -130,8 +130,4 @@ typedef struct {
size_t ptr_off;
} KeySetLink;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "keysets_defs.generated.h"
#endif
#endif // NVIM_API_PRIVATE_DEFS_H

View File

@@ -28,6 +28,7 @@ extern const MsgpackRpcRequestHandler method_handlers[];
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/private/dispatch.h.generated.h"
# include "api/private/dispatch_wrappers.h.generated.h"
# include "keysets_defs.generated.h"
#endif
#endif // NVIM_API_PRIVATE_DISPATCH_H

View File

@@ -181,7 +181,6 @@ typedef struct {
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/private/helpers.h.generated.h"
# include "keysets.h.generated.h"
#endif
#define WITH_SCRIPT_CONTEXT(channel_id, code) \

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_API_VIM_H
#define NVIM_API_VIM_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_API_VIMSCRIPT_H
#define NVIM_API_VIMSCRIPT_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_API_WIN_CONFIG_H
#define NVIM_API_WIN_CONFIG_H
#include "nvim/api/keysets.h"
#include "nvim/api/private/defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS