dev.txt (31477B)
1 *dev.txt* Nvim 2 3 4 NVIM REFERENCE MANUAL 5 6 7 Development of Nvim *development* *dev* 8 9 This reference describes design constraints and guidelines, for developing 10 Nvim applications or Nvim itself. See |dev-arch| for discussion of Nvim's 11 architecture and internal concepts. 12 13 Nvim is free and open source. Everybody is encouraged to contribute. 14 https://github.com/neovim/neovim/blob/master/CONTRIBUTING.md 15 16 Type |gO| to see the table of contents. 17 18 ============================================================================== 19 Design goals *design-goals* 20 21 Most important things come first (roughly). Some items conflict; this is 22 intentional. A balance must be found. 23 24 25 NVIM IS... IMPROVED *design-improved* 26 27 The Neo bits of Nvim should make it a better Vim, without becoming a 28 completely different editor. 29 - In matters of taste, prefer Vim/Unix tradition. If there is no relevant 30 Vim/Unix tradition, consider the "common case". 31 - There is no limit to the features that can be added. Select new features 32 based on (1) what users ask for, (2) how much effort it takes to implement 33 and (3) someone actually implementing it. 34 - Backwards compatibility is a feature. The RPC API in particular should 35 never break. 36 37 38 NVIM IS... WELL DOCUMENTED *design-documented* 39 40 - A feature that isn't documented is a useless feature. A patch for a new 41 feature must include the documentation. 42 - Documentation should be comprehensive and understandable. Use examples. 43 - Don't make the text unnecessarily long. Less documentation means that an 44 item is easier to find. 45 46 47 NVIM IS... FAST AND SMALL *design-speed-size* 48 49 Keep Nvim small and fast. This directly affects versatility and usability. 50 - Computers are becoming faster and bigger each year. Vim can grow too, but 51 no faster than computers are growing. Keep Vim usable on older systems. 52 - Many users start Vim from a shell very often. Startup time must be short. 53 - Commands must work efficiently. The time they consume must be as small as 54 possible. Useful commands may take longer. 55 - Don't forget that some people use Vim over a slow connection. Minimize the 56 communication overhead. 57 - Vim is a component among other components. Don't turn it into a massive 58 application, but have it work well together with other programs 59 ("composability"). 60 61 62 NVIM IS... MAINTAINABLE *design-maintain* 63 64 - The source code should not become a mess. It should be reliable code. 65 - Use comments in a useful way! Quoting the function name and argument names 66 is NOT useful. Do explain what they are for. 67 - Porting to another platform should be made easy, without having to change 68 too much platform-independent code. 69 - Use the object-oriented spirit: Put data and code together. Minimize the 70 knowledge spread to other parts of the code. 71 72 73 NVIM IS... NOT *design-not* 74 75 Nvim is not an operating system; instead it should be composed with other 76 tools or hosted as a component. Marvim once said: "Unlike Emacs, Nvim does not 77 include the kitchen sink... but it's good for plumbing." 78 79 80 ============================================================================== 81 Developer guidelines *dev-guidelines* 82 83 84 PROVIDERS *dev-provider* 85 86 A primary goal of Nvim is to allow extension of the editor without special 87 knowledge in the core. Some core functions are delegated to "providers" 88 implemented as external scripts. 89 90 Examples: 91 92 1. In the Vim source code, clipboard logic accounts for more than 1k lines of 93 C source code (ui.c), to perform two tasks that are now accomplished with 94 shell commands such as xclip or pbcopy/pbpaste. 95 2. Python scripting support: Vim has three files dedicated to embedding the 96 Python interpreter: if_python.c, if_python3.c and if_py_both.h. Together 97 these files sum about 9.5k lines of C source code. In contrast, Nvim Python 98 scripting is performed by an external host process implemented in ~2k lines 99 of Python. 100 101 The provider framework invokes Vimscript from C. It is composed of two 102 functions in eval.c: 103 104 - eval_call_provider({name}, {method}, {arguments}, {discard}): Calls 105 `provider#{name}#Call` with {method} and {arguments}. If {discard} is true, any 106 value returned by the provider will be discarded and empty value will be 107 returned. 108 - eval_has_provider({name}): Checks the `g:loaded_{name}_provider` variable 109 which must be set to 2 by the provider script to indicate that it is 110 "enabled and working". Called by |has()| to check if features are available. 111 112 For example, the Python provider is implemented by the 113 "autoload/provider/python.vim" script, which sets `g:loaded_python_provider` 114 to 2 only if a valid external Python host is found. Then `has("python")` 115 reflects whether Python support is working. 116 117 *provider-reload* 118 Sometimes a GUI or other application may want to force a provider to 119 "reload". To reload a provider, undefine its "loaded" flag, then use 120 |:runtime| to reload it: >vim 121 122 :unlet g:loaded_clipboard_provider 123 :runtime autoload/provider/clipboard.vim 124 125 126 DOCUMENTATION *dev-doc* 127 128 - "Just say it". Avoid mushy, colloquial phrasing in all documentation 129 (docstrings, user manual, website materials, newsletters, …). Don't mince 130 words. Personality and flavor, used sparingly, are welcome--but in general, 131 optimize for the reader's time and energy: be "precise yet concise". 132 - Prefer the active voice: "Foo does X", not "X is done by Foo". 133 - "The words you choose are an essential part of the user experience." 134 https://developer.apple.com/design/human-interface-guidelines/writing 135 - "...without being overly colloquial or frivolous." 136 https://developers.google.com/style/tone 137 - Write docstrings (as opposed to inline comments) with present tense ("Gets"), 138 not imperative ("Get"). This tends to reduce ambiguity and improve clarity 139 by describing "What" instead of "How". > 140 ✅ OK: 141 /// Gets a highlight definition. 142 ❌ NO: 143 /// Get a highlight definition. 144 - Avoid starting docstrings with "The" or "A" unless needed to avoid 145 ambiguity. This is a visual aid and reduces noise. > 146 ✅ OK: 147 /// @param dirname Path fragment before `pend` 148 ❌ NO: 149 /// @param dirname The path fragment before `pend` 150 - Vim differences: 151 - Do not prefix help tags with "nvim-". Use |vim_diff.txt| to catalog 152 differences from Vim; no other distinction is necessary. 153 - If a Vim feature is removed, delete its help section and move its tag to 154 |vim_diff.txt|. 155 - Mention deprecated features in |deprecated.txt| and delete their old doc. 156 - Use consistent language. 157 - "terminal" in a help tag always means "the embedded terminal emulator", 158 not "the user host terminal". 159 - Use "tui-" to prefix help tags related to the host terminal, and "TUI" 160 in prose if possible. 161 - Rough guidelines on where Lua documentation should end up: 162 - Nvim API functions `vim.api.nvim_*` should be in `api.txt`. 163 - If the module is big and not relevant to generic and lower-level Lua 164 functionality, then it's a strong candidate for separation. Example: 165 `treesitter.txt` 166 - Otherwise, add them to `lua.txt` 167 168 Documentation format ~ 169 170 For Nvim-owned docs, use the following strict subset of "vimdoc" to ensure 171 the help doc renders nicely in other formats (such as HTML: 172 https://neovim.io/doc/user ). 173 174 Strict "vimdoc" subset: 175 176 - Use lists (like this!) prefixed with "-" or "•", for adjacent lines that you 177 don't want to auto-wrap. Lists are always rendered with "flow" layout 178 (soft-wrapped) instead of preformatted (hard-wrapped) layout common in 179 legacy :help docs. You can also use numbered lists, e.g. "1." 180 - Separate blocks (paragraphs) of content by a blank line. 181 - Do not use indentation in random places—that prevents the page from using 182 "flow" layout. If you need a preformatted section, put it in 183 a |help-codeblock| starting with ">". 184 - Parameters and fields are documented as `{foo}`. 185 - Optional parameters and fields are documented as `{foo}?`. 186 187 C docstrings ~ 188 189 Nvim API documentation lives in the source code, as docstrings (doc 190 comments) on the function definitions. The |api| :help is generated 191 from the docstrings defined in src/nvim/api/*.c. 192 193 Docstring format: 194 - Lines start with `///` 195 - Special tokens start with `@` followed by the token name: 196 `@note`, `@param`, `@return` 197 - Markdown is supported. 198 - Tags are written as `[tag]()`. 199 - References are written as `[tag]` 200 - Use "```" for code samples. 201 Code samples can be annotated as `vim` or `lua` 202 203 Example: the help for |nvim_open_win()| is generated from a docstring defined 204 in src/nvim/api/win_config.c like this: > 205 206 /// Opens a new window. 207 /// ... 208 /// 209 /// Example (Lua): window-relative float 210 /// 211 /// ```lua 212 /// vim.api.nvim_open_win(0, false, { 213 /// relative='win', 214 /// row=3, 215 /// col=3, 216 /// width=12, 217 /// height=3, 218 /// }) 219 /// ``` 220 /// 221 /// @param buffer Buffer to display 222 /// @param enter Enter the window 223 /// @param config Map defining the window configuration. Keys: 224 /// - relative: Sets the window layout, relative to: 225 /// - "editor" The global editor grid. 226 /// - "win" Window given by the `win` field. 227 /// - "cursor" Cursor position in current window. 228 /// ... 229 /// @param[out] err Error details, if any 230 /// 231 /// @return Window handle, or 0 on error 232 233 234 Lua docstrings ~ 235 *dev-lua-doc* 236 Lua documentation lives in the source code, as docstrings on the function 237 definitions. The |lua-vim| :help is generated from the docstrings. 238 239 Docstring format: 240 - Use LuaCATS annotations: https://luals.github.io/wiki/annotations/ 241 - Markdown is supported. 242 - Tags are written as `[tag]()`. 243 - References are written as `[tag]` 244 - Use "```" for code samples. 245 Code samples can be annotated as `vim` or `lua` 246 - Use `@since <api-level>` to note the |api-level| when the function became 247 "stable". If `<api-level>` is greater than the current stable release (or 248 0), it is marked as "experimental". 249 - See scripts/util.lua for the mapping of api-level to Nvim version. 250 - Use `@nodoc` to prevent documentation generation. 251 - Use `@inlinedoc` to inline `@class` blocks into `@param` blocks. 252 E.g. >lua 253 --- Object with fields: 254 --- @class myOpts 255 --- @inlinedoc 256 --- 257 --- Documentation for some field 258 --- @field somefield? integer 259 260 --- @param opts? myOpts 261 function foo(opts) 262 end 263 < 264 265 Will be rendered as: >vimdoc 266 267 foo({opts}) 268 269 Parameters: 270 - {opts}? (table) Object with the fields: 271 - {somefield}? (integer) Documentation 272 for some field 273 < 274 - Files declared as `@meta` are only used for typing and documentation (similar to "*.d.ts" typescript files). 275 276 Example: the help for |vim.paste()| is generated from a docstring decorating 277 vim.paste in runtime/lua/vim/_core/editor.lua like this: > 278 279 --- Paste handler, invoked by |nvim_paste()| when a conforming UI 280 --- (such as the |TUI|) pastes text into the editor. 281 --- 282 --- Example: To remove ANSI color codes when pasting: 283 --- 284 --- ```lua 285 --- vim.paste = (function() 286 --- local overridden = vim.paste 287 --- ... 288 --- end)() 289 --- ``` 290 --- 291 --- @since 12 292 --- @see |paste| 293 --- 294 --- @param lines ... 295 --- @param phase ... 296 --- @returns false if client should cancel the paste. 297 298 299 STDLIB DESIGN GUIDELINES *dev-lua* 300 301 See also |dev-naming|. 302 303 - Keep the core Lua modules |lua-stdlib| simple. Avoid elaborate OOP or 304 pseudo-OOP designs. Plugin authors just want functions to call, not a big, 305 fancy inheritance hierarchy. 306 - Avoid requiring or returning special objects in the Nvim stdlib. Plain 307 tables or values are easier to serialize, easier to construct from literals, 308 easier to inspect and print, and inherently compatible with all Lua plugins. 309 (This guideline doesn't apply to opaque, non-data objects like `vim.cmd`.) 310 - stdlib functions should follow these common patterns: 311 - Return |lua-result-or-message| (`any|nil,nil|string`) to communicate 312 failure, or choose from |dev-error-patterns| when appropriate. 313 - Accept iterable instead of only table. 314 - Note: in some cases iterable doesn't make sense, e.g. spair() sorts the 315 input by definition, so there is no reason for it to accept an iterable, 316 because the input needs to be "reified"; it can't operate on a "stream". 317 - Return an iterable (generator) instead of table, if possible. 318 - Mimic the pairs() or ipairs() interface if the function is intended for 319 use in a |for-in| loop. 320 321 *dev-error-patterns* 322 To communicate failure to a consumer, choose from these patterns (in order of 323 preference): 324 1. `retval, errmsg` 325 - When failure is normal, or when it is practical for the consumer to 326 continue (fallback) in some other way. See |lua-result-or-message|. 327 2. optional result, no errormsg 328 - Special case of 1. When there is only a single case of "doesn't exist" 329 (e.g. cache lookup, dict lookup). 330 3. `error("no luck")` 331 - For invalid state ("must not happen"), when failure is exceptional, or at 332 a low level where the consumers are unlikely to handle it in a meaningful 333 way. Advantage is that propagation happens for free and it's harder to 334 accidentally swallow errors. (E.g. using 335 `uv_handle/pipe:write()` without checking return values is common.) 336 4. `on_error` callback 337 - For async and "visitors" traversing a graph, where many errors may be 338 collected while work continues. 339 5. `vim.notify` (sometimes with optional `opts.silent` (async, visitors)) 340 - High-level / application-level messages. End-user invokes these directly. 341 342 *dev-patterns* 343 Interface conventions ~ 344 345 Where possible, these patterns apply to _both_ Lua and the API: 346 347 - When accepting a buffer id, etc., 0 means "current buffer", nil means "all 348 buffers". Likewise for window id, tabpage id, etc. 349 - Examples: |vim.lsp.codelens.clear()| |vim.diagnostic.enable()| 350 - Any function signature that accepts a callback (example: |table.foreach()|) 351 should place it as the LAST parameter (after opts), if possible (or ALWAYS 352 for |continuation|s——functions called exactly once). 353 - Improves readability by placing the less "noisy" arguments near the start. 354 - Consistent with luv. 355 - Useful for future async lib which transforms functions of the form 356 `function(<args>, cb(<ret)>))` => `function(<args>) -> <ret>`. 357 - Example: >lua 358 -- ✅ OK: 359 filter(…, opts, function() … end) 360 -- ❌ NO: 361 filter(function() … end, …, opts) 362 -- ❌ NO: 363 filter(…, function() … end, opts) 364 - Expose a `config(opts)` function if the module accepts user configuration. 365 If `opts` is omitted (or `nil`) it returns the current configuration. 366 - Example: See |vim.diagnostic.config()|. 367 - "Enable" ("toggle") interface and behavior: 368 - `enable(…, nil)` and `enable(…, {buf=nil})` are synonyms and control 369 the "global" enablement of a feature. 370 - `is_enabled(nil)` and `is_enabled({buf=nil})`, likewise, query the 371 global state of the feature. 372 - `enable(…, {buf: number})` sets a buffer-local "enable" flag. 373 - `is_enabled({buf: number})`, likewise, queries the buffer-local state of 374 the feature. 375 - See |vim.lsp.inlay_hint.enable()| and |vim.lsp.inlay_hint.is_enabled()| 376 for a reference implementation of these "best practices". 377 - NOTE: open questions: https://github.com/neovim/neovim/issues/28603 378 - Transformation functions should also have "filter" functionality (when 379 appropriate): when the function returns a nil value it excludes (filters 380 out) its input, else the transformed value is used. 381 - Example: See the format() field of |vim.diagnostic.Opts.Float|. 382 383 API DESIGN GUIDELINES *dev-api* 384 385 See also |dev-naming|. 386 387 - When adding an API, check the following: 388 - What precedents did you draw from? How does your solution compare to them? 389 - Does your new API allow future expansion? How? Or why not? 390 - Is the new API similar to existing APIs? Do we need to deprecate the old ones? 391 - Did you cross-reference related concepts in the docs? 392 - Avoid "mutually exclusive" parameters--via constraints or limitations, if 393 necessary. For example nvim_create_autocmd() has mutually exclusive 394 "callback" and "command" args; but the "command" arg could be eliminated by 395 simply not supporting Vimscript function names, and treating a string 396 "callback" arg as an Ex command (which can call Vimscript functions). The 397 "buffer" arg could also be eliminated by treating a number "pattern" as 398 a buffer number. 399 - Avoid functions that depend on cursor position, current buffer, etc. Instead 400 the function should take a position parameter, buffer parameter, etc. 401 402 Where things go ~ 403 404 - API (libnvim/RPC): exposes low-level internals, or fundamental things (such 405 as `nvim_exec_lua()`) needed by clients or C consumers. 406 - Lua stdlib = high-level functionality that builds on top of the API. 407 408 NAMING GUIDELINES *dev-naming* 409 410 Naming is exceedingly important: the name of a thing is the primary interface 411 for uses it, discusses it, searches for it, shares it... Consistent 412 naming in the stdlib, API, and UI helps both users and developers discover and 413 intuitively understand related concepts ("families"), and reduces cognitive 414 burden. Discoverability encourages code re-use and likewise avoids redundant, 415 overlapping mechanisms, which reduces code surface-area, and thereby minimizes 416 bugs... 417 418 Naming conventions ~ 419 420 In general, look for precedent when choosing a name, that is, look at existing 421 (non-deprecated) functions. In particular, see below... 422 423 *dev-name-common* 424 Use existing common {verb} names (actions) if possible: 425 - add: Appends or inserts into a collection 426 - attach: Listens to something to get events from it (TODO: rename to "on"?) 427 - call: Calls a function 428 - callback |continuation|: Function parameter (NOT a field) that 429 returns the result of an async function. Use the "on_…" 430 naming-convention for all other callbacks and event handlers. 431 - cancel: Cancels or dismisses an event or interaction, typically 432 user-initiated and without error. (Compare "abort", which 433 cancels and signals error/failure.) 434 - clear: Clears state but does not destroy the container 435 - create: Creates a new (non-trivial) thing (TODO: rename to "def"?) 436 - del: Deletes a thing (or group of things) 437 - detach: Dispose attached listener (TODO: rename to "un"?) 438 - enable: Enables/disables functionality. Signature should be 439 `enable(enable?:boolean, filter?:table)`. 440 - eval: Evaluates an expression 441 - exec: Executes code, may return a result 442 - fmt: Formats 443 - get: Gets things. Two variants (overloads): 444 1. `get<T>(id: int): T` returns one item. 445 2. `get<T>(filter: dict): T[]` returns a list. 446 - has: Checks for the presence of an item, feature, etc. 447 - inspect: Presents a high-level, often interactive, view 448 - is_enabled: Checks if functionality is enabled. 449 - on_…: Handles events or async results, or registers such 450 a handler. |dev-name-events| 451 - open: Opens something (a buffer, window, …) 452 - parse: Parses something into a structured form 453 - request: Calls a remote (network, RPC) operation. 454 - send: Writes data or a message to a channel. 455 - set: Sets a thing (or group of things) 456 - start: Spin up a long-lived process. Prefer "enable" except when 457 "start" is obviously more appropriate. 458 - stop: Inverse of "start". Teardown a long-lived process. 459 - try_{verb}: Best-effort operation, failure returns null or error obj 460 461 Do NOT use these deprecated verbs: 462 - contains: Prefer "has". 463 - disable: Prefer `enable(enable: boolean)`. 464 - exit: Prefer "cancel" (or "stop" if appropriate). 465 - is_disabled: Prefer `!is_enabled()`. 466 - list: Redundant with "get" 467 - notify: Redundant with "print", "echo" 468 - show: Redundant with "print", "echo" 469 - toggle: Prefer `enable(not is_enabled())`. 470 471 Use consistent names for {topic} in API functions: buffer is called "buf" 472 everywhere, not "buffer" in some places and "buf" in others. 473 - buf: Buffer 474 - chan: |channel| 475 - cmd: Command 476 - cmdline: Command-line UI or input 477 - dir: Directory 478 - fn: Function 479 - hl: Highlight 480 - pos: Position 481 - proc: System process 482 - tabpage: Tabpage 483 - win: Window 484 485 Do NOT use these deprecated nouns: 486 - buffer Use "buf" instead 487 - command Use "cmd" instead 488 - window Use "win" instead 489 490 *dev-name-events* 491 Use the "on_" prefix to name event-handling callbacks and also the interface for 492 "registering" such handlers (on_key). The dual nature is acceptable to avoid 493 a confused collection of naming conventions for these related concepts. 494 495 Editor |events| (autocommands) are historically named like: > 496 {Noun}{Event} 497 498 Use this format to name API (RPC) events: > 499 nvim_{noun}_{event-name}_event 500 501 Example: > 502 nvim_buf_changedtick_event 503 < 504 *continuation* 505 A "continuation" is implemented as a callback function that returns the result 506 of an async function and is called exactly once. Often accepts `err` as the 507 first parameter, to avoid erroring on the main thread. Example: > 508 foo(…, callback: fun(err, …)) 509 Use the name `callback` only for a continuation. All other callbacks and event 510 handlers should be named with the |dev-name-events| "on_…" convention. 511 512 *dev-api-name* 513 Use this format to name new RPC |API| functions: > 514 nvim_{topic}_{verb}_{arbitrary-qualifiers} 515 516 Do not add new nvim_buf/nvim_win/nvim_tabpage APIs, unless you are certain the 517 concept will NEVER be applied to more than one "scope". That is, {topic} 518 should be the TOPIC ("ns", "extmark", "option", …) that acts on the scope(s) 519 (buf/win/tabpage/global), it should NOT be the scope. Instead the scope should 520 be a parameter (typically manifest as mutually-exclusive buf/win/… flags like 521 |nvim_get_option_value()|, or less commonly as a `scope: string` field like 522 |nvim_get_option_info2()|). 523 524 - Example: `nvim_get_keymap('v')` operates in a global context (first 525 parameter is not a Buffer). The "get" verb indicates that it gets anything 526 matching the given filter parameter. A "list" verb is unnecessary because 527 `nvim_get_keymap('')` (empty filter) returns all items. 528 - Example: `nvim_buf_del_mark` acts on a `Buffer` object (the first parameter) 529 and uses the "del" {verb}. 530 531 *dev-namespace-name* 532 Use namespace names like `nvim.foo.bar`: > 533 vim.api.nvim_create_namespace('nvim.lsp.codelens') 534 < 535 536 *dev-augroup-name* 537 Use autocommand group names like `nvim.foo.bar`: > 538 vim.api.nvim_create_augroup('nvim.treesitter.dev') 539 < 540 541 INTERFACE PATTERNS *dev-api-patterns* 542 543 Prefer adding a single `nvim_{topic}_{verb}_…` interface for a given topic. 544 545 Example: > 546 547 nvim_ns_add( 548 ns_id: int, 549 filter: { 550 handle: integer (buf/win/tabpage id) 551 scope: "global" | "win" | "buf" | "tabpage" 552 } 553 ): { ok: boolean } 554 555 nvim_ns_get( 556 ns_id: int, 557 filter: { 558 handle: integer (buf/win/tabpage id) 559 scope: "global" | "win" | "buf" | "tabpage" 560 } 561 ): { ids: int[] } 562 563 nvim_ns_del( 564 ns_id: int, 565 filter: { 566 handle: integer (buf/win/tabpage id) 567 scope: "global" | "win" | "buf" | "tabpage" 568 } 569 ): { ok: boolean } 570 571 572 Anti-Example: 573 574 Creating separate `nvim_xx`, `nvim_buf_xx`, `nvim_win_xx`, and 575 `nvim_tabpage_xx`, functions all for the same `xx` topic, requires 4x the 576 amount of documentation, tests, boilerplate, and interfaces, which users must 577 comprehend, maintainers must maintain, etc. Thus the following is NOT 578 recommended (compare these 12(!) functions to the above 3 functions): > 579 580 nvim_add_ns(…) 581 nvim_buf_add_ns(…) 582 nvim_win_add_ns(…) 583 nvim_tabpage_add_ns(…) 584 nvim_del_ns(…) 585 nvim_buf_del_ns(…) 586 nvim_win_del_ns(…) 587 nvim_tabpage_del_ns(…) 588 nvim_get_ns(…) 589 nvim_buf_get_ns(…) 590 nvim_win_get_ns(…) 591 nvim_tabpage_get_ns(…) 592 593 API-CLIENT *dev-api-client* 594 595 *api-client* 596 API clients wrap the Nvim |API| to provide idiomatic "SDKs" for their 597 respective platforms (see |jargon|). You can build a new API client for your 598 favorite platform or programming language. 599 600 List of API clients: 601 https://github.com/neovim/neovim/wiki/Related-projects#api-clients 602 603 *node-client* *pynvim* 604 These clients can be considered the "reference implementation" for API clients: 605 - https://github.com/neovim/node-client 606 - https://github.com/neovim/pynvim 607 608 Standard Features ~ 609 610 - API clients exist to hide msgpack-rpc details. The wrappers can be 611 automatically generated by reading the |api-metadata| from Nvim. |api-mapping| 612 - Clients should call |nvim_set_client_info()| after connecting, so users and 613 plugins can detect the client by handling the |ChanInfo| event. This avoids 614 the need for special variables or other client hints. 615 - Clients should handle |nvim_error_event| notifications, which will be sent 616 if an async request to nvim was rejected or caused an error. 617 618 Package Naming ~ 619 620 API client packages should NOT be named something ambiguous like "neovim" or 621 "python-client". Use "nvim" as a prefix/suffix to some other identifier 622 following ecosystem conventions. 623 624 For example, Python packages tend to have "py" in the name, so "pynvim" is 625 a good name: it's idiomatic and unambiguous. If the package is named "neovim", 626 it confuses users, and complicates documentation and discussions. 627 628 Examples of API-client package names: 629 - ✅ OK: nvim-racket 630 - ✅ OK: pynvim 631 - ❌ NO: python-client 632 - ❌ NO: neovim_ 633 634 API client implementation guidelines ~ 635 636 - Separate the transport layer from the rest of the library. |rpc-connecting| 637 - Use a MessagePack library that implements at least version 5 of the 638 MessagePack spec, which supports the BIN and EXT types used by Nvim. 639 - Use a single-threaded event loop library/pattern. 640 - Use a fiber/coroutine library for the language being used for implementing 641 a client. These greatly simplify concurrency and allow the library to 642 expose a blocking API on top of a non-blocking event loop without the 643 complexity that comes with preemptive multitasking. 644 - Don't assume anything about the order of responses to RPC requests. 645 - Clients should expect requests, which must be handled immediately because 646 Nvim is blocked while waiting for the client response. 647 - Clients should expect notifications, but these can be handled "ASAP" (rather 648 than immediately) because they won't block Nvim. 649 - For C/C++ projects, consider libmpack instead of the msgpack.org library. 650 https://github.com/libmpack/libmpack/ 651 libmpack is small (no dependencies, can inline into your C/C++ project) and 652 efficient (no allocations). It also implements msgpack-RPC, the protocol 653 required by Nvim. 654 https://github.com/msgpack-rpc/msgpack-rpc 655 656 657 EXTERNAL UI *dev-ui* 658 659 External UIs should be aware of the |api-contract|. In particular, future 660 versions of Nvim may add new items to existing events. The API is strongly 661 backwards-compatible, but clients must not break if new (optional) fields are 662 added to existing events. 663 664 Standard Features ~ 665 666 External UIs are expected to implement these common features: 667 668 - Call |nvim_set_client_info()| after connecting, so users and plugins can 669 detect the UI by handling the |ChanInfo| event. This avoids the need for 670 special variables and UI-specific config files (gvimrc, macvimrc, …). 671 - Cursor style (shape, color) should conform to the 'guicursor' properties 672 delivered with the mode_info_set UI event. 673 - Send the ALT/META ("Option" on macOS) key as a |<M-| chord. 674 - Send the "super" key (Windows key, Apple key) as a |<D-| chord. 675 - Avoid mappings that conflict with the Nvim keymap-space; GUIs have many new 676 chords (<C-,> <C-Enter> <C-S-x> <D-x>) and patterns ("shift shift") that do 677 not potentially conflict with Nvim defaults, plugins, etc. 678 - Consider the "option_set" |ui-global| event as a hint for other GUI 679 behaviors. Various UI-related options ('guifont', 'ambiwidth', …) are 680 published in this event. See also "mouse_on", "mouse_off". 681 - UIs generally should NOT set |$NVIM_APPNAME| (unless explicitly requested by 682 the user). 683 - Support the text decorations/attributes given by |ui-event-hl_attr_define|. 684 The "url" attr should be presented as a clickable hyperlink. 685 - Handle the "restart" UI event so that |:restart| works. 686 - Detect capslock and show an indicator if capslock is active. 687 688 Multigrid UI ~ 689 *dev-ui-multigrid* 690 - A multigrid UI should display floating windows using one of the following 691 methods, using the `win_float_pos` |ui-multigrid| event. Different methods 692 can be selected for each window as needed. 693 694 1. Let Nvim determine the position and z-order of the windows. This can be 695 achieved by using the `compindex`, `screen_row`, and `screen_col` information from 696 the `win_float_pos` UI event. To allow Nvim to automatically determine the grid, 697 the UI should call `nvim_input_mouse` with 0 as the `grid` parameter. 698 2. Use the `anchor`, `anchor_grid`, `anchor_row`, `anchor_col`, and `zindex` as 699 references for positioning the windows. If windows are outside the screen, 700 it is the UI's responsibility to reposition and/or resize them. Since Nvim 701 lacks information about actual window placement in this case, the UI must 702 call `nvim_input_mouse` with the actual grid id, factoring in `mouse_enabled`. 703 Note: Some API functions may return unexpected results for these windows due 704 to the missing information. 705 - For external windows, the grid id should also be passed to `nvim_input_mouse`. 706 707 vim:tw=78:ts=8:sw=4:et:ft=help:norl: