|
| 1 | +# openapi.nvim |
| 2 | + |
| 3 | + |
| 4 | +[](./LICENSE) |
| 5 | +[](https://github.com/devdammit/openapi.nvim/actions/workflows/ci.yml) |
| 6 | + |
| 7 | + |
| 8 | +Navigate OpenAPI / Swagger YAML specifications directly inside Neovim — no |
| 9 | +Swagger UI, no external tools. List operations, fuzzy-find schemas and |
| 10 | +components, and jump between `$ref` declarations and references just like you |
| 11 | +would with LSP. |
| 12 | + |
| 13 | +Built for [LazyVim](https://www.lazyvim.org/) (Neovim 0.10+), powered by |
| 14 | +[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) for |
| 15 | +parsing and [snacks.nvim](https://github.com/folke/snacks.nvim) for the picker |
| 16 | +UI. |
| 17 | + |
| 18 | +<!-- Record a short clip showing the operations picker, gd on a $ref, and the |
| 19 | + $ref highlight, then drop it at assets/demo.gif --> |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +## Features |
| 24 | + |
| 25 | +- **Operations picker** — browse every `METHOD /path` with its `operationId` |
| 26 | + and `summary`. |
| 27 | +- **Components picker** — fuzzy-search all `components.*` (schemas, parameters, |
| 28 | + responses, requestBodies, headers, examples, links, callbacks, |
| 29 | + securitySchemes, pathItems), optionally filtered by kind. |
| 30 | +- **Go to declaration (`gd`)** — jump from a `$ref` to its definition. |
| 31 | +- **Find references (`gr`)** — from a component definition (or a `$ref`), list |
| 32 | + every place that `$ref`s it. |
| 33 | +- **`$ref` highlight** — the reference target under the cursor is underlined |
| 34 | + with an accent colour so you can see at a glance what you're about to follow. |
| 35 | +- **Native keys, scoped to spec buffers** — `gd`/`gr` are remapped only in |
| 36 | + buffers detected as OpenAPI specs, and fall back to the default LSP behaviour |
| 37 | + when the cursor isn't on a `$ref` or a component definition. |
| 38 | + |
| 39 | +## Requirements |
| 40 | + |
| 41 | +- **Neovim 0.10+**. |
| 42 | +- The `yaml` treesitter parser (`:TSInstall yaml` via nvim-treesitter). |
| 43 | +- `snacks.nvim` (bundled with LazyVim) for the picker UI. Without it the plugin |
| 44 | + falls back to `vim.ui.select`. |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +### lazy.nvim |
| 49 | + |
| 50 | +```lua |
| 51 | +-- ~/.config/nvim/lua/plugins/openapi.lua |
| 52 | +return { |
| 53 | + { |
| 54 | + "devdammit/openapi.nvim", |
| 55 | + ft = "yaml", |
| 56 | + dependencies = { "nvim-treesitter/nvim-treesitter" }, |
| 57 | + opts = {}, |
| 58 | + }, |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Local development |
| 63 | + |
| 64 | +```lua |
| 65 | +return { |
| 66 | + { |
| 67 | + dir = "~/path/to/openapi.nvim", |
| 68 | + ft = "yaml", |
| 69 | + opts = {}, |
| 70 | + }, |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Configuration |
| 75 | + |
| 76 | +Defaults are shown below — pass any subset via `opts`: |
| 77 | + |
| 78 | +```lua |
| 79 | +opts = { |
| 80 | + keys = { |
| 81 | + declaration = "gd", -- go to declaration on $ref |
| 82 | + references = "gr", -- find $ref usages |
| 83 | + operations = "<leader>oo", -- operations picker |
| 84 | + components = "<leader>os", -- components picker |
| 85 | + }, |
| 86 | + -- Component kinds to index ("all" indexes every kind that appears). |
| 87 | + components = "all", |
| 88 | + -- How many leading lines to scan for `openapi:`/`swagger:` when detecting a spec. |
| 89 | + detect_lines = 50, |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Set any key to `false` to disable that mapping. |
| 94 | + |
| 95 | +## Commands |
| 96 | + |
| 97 | +These are created buffer-locally in detected OpenAPI buffers: |
| 98 | + |
| 99 | +| Command | Description | |
| 100 | +| --------------------------- | ------------------------------------------------- | |
| 101 | +| `:OpenapiOperations` | Open the operations picker | |
| 102 | +| `:OpenapiComponents [kind]` | Open the components picker (optional kind filter) | |
| 103 | +| `:OpenapiSchemas` | Components picker filtered to `schemas` | |
| 104 | +| `:OpenapiReferences` | Find references to the target under the cursor | |
| 105 | + |
| 106 | +## Highlights |
| 107 | + |
| 108 | +The `$ref` value under the cursor is highlighted with the `OpenapiRefUnderCursor` |
| 109 | +group (underline + bold + accent colour). It is defined with `default = true`, |
| 110 | +so your colorscheme or config can override it: |
| 111 | + |
| 112 | +```lua |
| 113 | +vim.api.nvim_set_hl(0, "OpenapiRefUnderCursor", { |
| 114 | + underline = true, |
| 115 | + fg = "#7aa2f7", |
| 116 | +}) |
| 117 | +``` |
| 118 | + |
| 119 | +## How detection works |
| 120 | + |
| 121 | +A buffer is treated as an OpenAPI spec when it has `filetype=yaml` and one of |
| 122 | +its first `detect_lines` lines contains a top-level `openapi:` or `swagger:` |
| 123 | +key. Regular YAML files are left untouched. |
| 124 | + |
| 125 | +## Notes & limitations |
| 126 | + |
| 127 | +- **YAML only.** JSON specs are not parsed. |
| 128 | +- **Single-file `$ref` only (for now).** Internal refs (`#/components/...`) are |
| 129 | + fully supported. External / multi-file refs (`./schemas/User.yaml#/...`) are |
| 130 | + detected but navigation across files is not implemented yet — `gd` on an |
| 131 | + external ref reports that it's unsupported rather than guessing. |
| 132 | +- Parsing is resilient to partially-invalid YAML (e.g. while you're mid-edit): |
| 133 | + whatever treesitter can recover is indexed. |
| 134 | + |
| 135 | +## Alternatives |
| 136 | + |
| 137 | +- [armsnyder/openapi-language-server](https://github.com/armsnyder/openapi-language-server) |
| 138 | + — a standalone LSP server providing jump-to-definition / find-references for |
| 139 | + OpenAPI. `openapi.nvim` does this natively in-process via treesitter, with no |
| 140 | + separate server, plus snacks pickers and `$ref` highlighting. |
| 141 | +- [codeasashu/oas.nvim](https://github.com/codeasashu/oas.nvim) — focuses on |
| 142 | + rendering previews (Redoc) rather than in-editor navigation. |
| 143 | + |
| 144 | +## Development |
| 145 | + |
| 146 | +```sh |
| 147 | +# format check |
| 148 | +stylua --check lua/ plugin/ |
| 149 | + |
| 150 | +# lint |
| 151 | +luacheck lua/ plugin/ |
| 152 | +``` |
| 153 | + |
| 154 | +There is no test framework dependency: the CI runs a headless Neovim smoke test |
| 155 | +that parses a sample spec and asserts the model, navigation, and highlight |
| 156 | +behaviour. See `.github/workflows/ci.yml`. |
| 157 | + |
| 158 | +## Architecture |
| 159 | + |
| 160 | +``` |
| 161 | +lua/openapi/ |
| 162 | + init.lua setup() + FileType / LspAttach autocmds |
| 163 | + config.lua defaults + merge |
| 164 | + detect.lua is_openapi_buf() |
| 165 | + attach.lua buffer-local keymaps + commands |
| 166 | + highlight.lua $ref-under-cursor highlight |
| 167 | + cache.lua per-buffer model cache (keyed by changedtick) |
| 168 | + util.lua jump + picker availability |
| 169 | + parser/ |
| 170 | + init.lua parse(bufnr) -> model (cached) |
| 171 | + ts.lua YAML AST traversal helpers |
| 172 | + model.lua operations / components / refs / targets |
| 173 | + ref.lua $ref parsing + JSON-pointer handling |
| 174 | + nav/ |
| 175 | + context.lua resolve target under the cursor |
| 176 | + declaration.lua gd |
| 177 | + reference.lua gr |
| 178 | + pickers/ |
| 179 | + operations.lua |
| 180 | + components.lua |
| 181 | +``` |
| 182 | + |
| 183 | +## License |
| 184 | + |
| 185 | +[MIT](./LICENSE) |
0 commit comments