Skip to content

Commit 790d27f

Browse files
benlubasFurkan Sahin
authored and
Furkan Sahin
committed
feat: add integrations.otter for LSP-like behaviours in code blocks (nvim-neorg#1329)
1 parent 9fe2653 commit 790d27f

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

lua/neorg/modules/core/concealer/module.lua

+3
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ module.public = {
651651
local len = line_lengths[row_0b - row_start_0b + 1]
652652
local mark_col_start_0b = math.max(0, col_start_0b - config.padding.left)
653653
local mark_col_end_0bex = max_len + config.padding.right
654+
local priority = 101
654655
if len >= mark_col_start_0b then
655656
vim.api.nvim_buf_set_extmark(bufid, module.private.ns_icon, row_0b, mark_col_start_0b, {
656657
end_row = row_0b + 1,
@@ -661,6 +662,7 @@ module.public = {
661662
virt_text_pos = "overlay",
662663
virt_text_win_col = len,
663664
spell = config.spell_check,
665+
priority = priority,
664666
})
665667
else
666668
vim.api.nvim_buf_set_extmark(bufid, module.private.ns_icon, row_0b, len, {
@@ -675,6 +677,7 @@ module.public = {
675677
virt_text_pos = "overlay",
676678
virt_text_win_col = len,
677679
spell = config.spell_check,
680+
priority = priority,
678681
})
679682
end
680683
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)