|
| 1 | +--[[ |
| 2 | + file: Otter |
| 3 | + title: LSP Features in Code Cells |
| 4 | + description: Integrates with otter.nvim to get LSP features directly in norg code cells. |
| 5 | + --- |
| 6 | +
|
| 7 | +From the otter README: |
| 8 | +
|
| 9 | +> Otter.nvim provides lsp features and a code completion source for code embedded in other documents. |
| 10 | +
|
| 11 | +This includes: |
| 12 | +- auto completion |
| 13 | +- diagnostics |
| 14 | +- hover |
| 15 | +- rename symbol |
| 16 | +- go to definition |
| 17 | +- go to references |
| 18 | +- go to type definition |
| 19 | +- range formatting (not document formatting) |
| 20 | +
|
| 21 | +## Setup |
| 22 | +You need to install otter.nvim and make sure it's loaded before Neorg, you can configure it yourself |
| 23 | +by reading [the Otter.nvim README](https://github.com/jmbuhr/otter.nvim). You do not need to |
| 24 | +configure `handle_leading_whitespace`, neorg will do that for you. |
| 25 | +
|
| 26 | +If you want auto complete, make sure you add `"otter"` as a source to nvim-cmp: |
| 27 | +
|
| 28 | +```lua |
| 29 | +sources = { |
| 30 | + -- ... other sources |
| 31 | + { name = "otter" }, |
| 32 | + -- ... other sources |
| 33 | +} |
| 34 | +``` |
| 35 | +
|
| 36 | +## Commands |
| 37 | +- `:Neorg otter enable` - enable otter in the current buffer |
| 38 | +- `:Neorg otter disable` - disable otter in the current buffer |
| 39 | +--]] |
| 40 | + |
| 41 | +local neorg = require("neorg.core") |
| 42 | +local module = neorg.modules.create("core.integrations.otter") |
| 43 | + |
| 44 | +module.setup = function() |
| 45 | + return { |
| 46 | + success = pcall(require, "otter"), |
| 47 | + requires = { |
| 48 | + "core.neorgcmd", |
| 49 | + }, |
| 50 | + } |
| 51 | +end |
| 52 | + |
| 53 | +local otter |
| 54 | +module.load = function() |
| 55 | + local ok |
| 56 | + ok, otter = pcall(require, "otter") |
| 57 | + assert(ok, "[Neorg] Failed to load otter.nvim") |
| 58 | + |
| 59 | + -- This will not interfere with other otter.nvim configuration |
| 60 | + otter.setup({ handle_leading_whitespace = true }) |
| 61 | + |
| 62 | + if module.config.public.auto_start then |
| 63 | + local group = vim.api.nvim_create_augroup("neorg.integrations.otter", {}) |
| 64 | + vim.api.nvim_create_autocmd({ "BufReadPost" }, { |
| 65 | + desc = "Activate Otter on Buf Enter", |
| 66 | + pattern = "*.norg", |
| 67 | + group = group, |
| 68 | + callback = function(_) |
| 69 | + module.public.activate() |
| 70 | + end |
| 71 | + }) |
| 72 | + end |
| 73 | + |
| 74 | + module.required["core.neorgcmd"].add_commands_from_table({ |
| 75 | + otter = { |
| 76 | + args = 1, |
| 77 | + name = "otter", |
| 78 | + condition = "norg", |
| 79 | + subcommands = { |
| 80 | + enable = { |
| 81 | + args = 0, |
| 82 | + name = "otter.enable", |
| 83 | + }, |
| 84 | + disable = { |
| 85 | + args = 0, |
| 86 | + name = "otter.disable", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }) |
| 91 | +end |
| 92 | + |
| 93 | +module.events.subscribed = { |
| 94 | + ["core.neorgcmd"] = { |
| 95 | + ["otter.enable"] = true, |
| 96 | + ["otter.disable"] = true, |
| 97 | + }, |
| 98 | +} |
| 99 | + |
| 100 | +module.on_event = function(event) |
| 101 | + if module.private[event.split_type[2]] then |
| 102 | + module.private[event.split_type[2]](event) |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +module.config.public = { |
| 107 | + -- list of languages that otter will try to start a language server for. |
| 108 | + -- `nil` means all languages |
| 109 | + languages = nil, |
| 110 | + |
| 111 | + -- Automatically start Otter when a norg buffer is opened |
| 112 | + auto_start = true, |
| 113 | + |
| 114 | + -- buffer local mappings set when otter is active |
| 115 | + keys = { |
| 116 | + hover = "K", |
| 117 | + definition = "gd", |
| 118 | + type_definition = "gD", |
| 119 | + references = "gr", |
| 120 | + rename = "<leader>rn", |
| 121 | + format = "<leader>gf", |
| 122 | + document_symbols = "gs", |
| 123 | + }, |
| 124 | + |
| 125 | + completion = { |
| 126 | + -- enable/disable autocomplete |
| 127 | + enabled = true, |
| 128 | + }, |
| 129 | + |
| 130 | + diagnostics = { |
| 131 | + -- enable/disable diagnostics |
| 132 | + enabled = true, |
| 133 | + }, |
| 134 | +} |
| 135 | + |
| 136 | +module.private = { |
| 137 | + ["otter.enable"] = function(_) |
| 138 | + module.public.activate() |
| 139 | + end, |
| 140 | + ["otter.disable"] = function(_) |
| 141 | + module.public.deactivate() |
| 142 | + end, |
| 143 | +} |
| 144 | + |
| 145 | +module.public = { |
| 146 | + ---Activate otter in the current buffer, includes setting buffer keymaps |
| 147 | + activate = function() |
| 148 | + otter.activate( |
| 149 | + module.config.public.languages, |
| 150 | + module.config.public.completion.enabled, |
| 151 | + module.config.public.diagnostics.enabled, |
| 152 | + nil -- or a query... |
| 153 | + ) |
| 154 | + |
| 155 | + local b = vim.api.nvim_get_current_buf() |
| 156 | + for func, lhs in pairs(module.config.public.keys) do |
| 157 | + vim.api.nvim_buf_set_keymap( |
| 158 | + b, |
| 159 | + "n", |
| 160 | + lhs, |
| 161 | + (":lua require'otter'.ask_%s()<cr>"):format(func), |
| 162 | + { silent = true, noremap = true } |
| 163 | + ) |
| 164 | + end |
| 165 | + end, |
| 166 | + |
| 167 | + ---Deactivate otter in the current buffer, including unsetting buffer keymaps |
| 168 | + deactivate = function() |
| 169 | + otter.deactivate( |
| 170 | + module.config.public.completion.enabled, |
| 171 | + module.config.public.diagnostics.enabled |
| 172 | + ) |
| 173 | + local b = vim.api.nvim_get_current_buf() |
| 174 | + for _, lhs in pairs(module.config.public.keys) do |
| 175 | + vim.api.nvim_buf_del_keymap(b, "n", lhs) |
| 176 | + end |
| 177 | + end, |
| 178 | +} |
| 179 | + |
| 180 | +return module |
0 commit comments