Skip to content

Commit a2e7a49

Browse files
Add support for nvim-cmp completion.
1 parent 4e50412 commit a2e7a49

File tree

10 files changed

+162
-81
lines changed

10 files changed

+162
-81
lines changed

DOCS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,15 @@ If you use [nvim-compe](https://github.com/hrsh7th/nvim-compe) add this to compe
616616
})
617617
```
618618

619+
For [nvim-cmp](https://github.com/hrsh7th/nvim-cmp), add `orgmode` to list of sources:
620+
```lua
621+
require'cmp'.setup({
622+
sources = {
623+
{ name = 'orgmode' }
624+
}
625+
})
626+
```
627+
619628
For [completion.nvim](https://github.com/nvim-lua/completion-nvim), just add `omni` mode to chain complete list and add additional keyword chars:
620629
```lua
621630
vim.g.completion_chain_complete_list = {

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ require'compe'.setup({
8686
})
8787
```
8888

89+
For [nvim-cmp](https://github.com/hrsh7th/nvim-cmp), add `orgmode` to list of sources:
90+
```lua
91+
require'cmp'.setup({
92+
sources = {
93+
{ name = 'orgmode' }
94+
}
95+
})
96+
```
97+
8998
For [completion.nvim](https://github.com/nvim-lua/completion-nvim), just add `omni` mode to chain complete list and add additional keyword chars:
9099
```lua
91100
vim.g.completion_chain_complete_list = {

doc/orgmode.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,15 @@ If you use nvim-compe (https://github.com/hrsh7th/nvim-compe) add this to compe
838838
})
839839
<
840840

841+
For nvim-cmp (https://github.com/hrsh7th/nvim-cmp), add `orgmode` to list of sources:
842+
>
843+
require'cmp'.setup({
844+
sources = {
845+
{ name = 'orgmode' }
846+
}
847+
})
848+
<
849+
841850
For completion.nvim (https://github.com/nvim-lua/completion-nvim), just add `omni` mode to chain complete list and add additional keyword chars:
842851
>
843852
vim.g.completion_chain_complete_list = {

ftplugin/org.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
lua require('orgmode.config'):setup_mappings('org')
22
function OrgmodeOmni(findstart, base)
3-
return luaeval('require("orgmode.org.autocompletion").omni(_A[1], _A[2])', [a:findstart, a:base])
3+
return luaeval('require("orgmode.org.autocompletion.omni")(_A[1], _A[2])', [a:findstart, a:base])
44
endfunction
55

66
setlocal omnifunc=OrgmodeOmni

lua/orgmode/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local instance = nil
33

44
---@class Org
55
---@field initialized boolean
6-
---@field files OrgFiles
6+
---@field files Files
77
---@field agenda Agenda
88
---@field capture Capture
99
---@field notifications Notifications
@@ -28,7 +28,7 @@ function Org:init()
2828
capture = self.capture,
2929
agenda = self.agenda,
3030
})
31-
require('orgmode.org.autocompletion') -- Needed for registering compe source
31+
require('orgmode.org.autocompletion').register()
3232
self.initialized = true
3333
end
3434

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local has_cmp, cmp = pcall(require, 'cmp')
2+
if not has_cmp then
3+
return
4+
end
5+
6+
local OrgmodeOmniCompletion = require('orgmode.org.autocompletion.omni')
7+
8+
local Source = {}
9+
10+
Source.new = function()
11+
local self = setmetatable({}, { __index = Source })
12+
return self
13+
end
14+
15+
Source.get_debug_name = function()
16+
return 'orgmode'
17+
end
18+
19+
function Source:is_available()
20+
return true
21+
end
22+
23+
function Source:get_trigger_characters(_)
24+
return { '#', '+', ':', '*' }
25+
end
26+
27+
function Source:complete(params, callback)
28+
local offset = OrgmodeOmniCompletion(1, '') + 1
29+
local input = string.sub(params.context.cursor_before_line, offset)
30+
print(vim.inspect(input))
31+
local results = OrgmodeOmniCompletion(0, input)
32+
local items = {}
33+
for _, item in ipairs(results) do
34+
table.insert(items, {
35+
label = item.word,
36+
})
37+
end
38+
39+
callback({
40+
items = items,
41+
isIncomplete = true,
42+
})
43+
end
44+
45+
cmp.register_source('orgmode', Source.new())
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local has_compe, compe = pcall(require, 'compe')
2+
if not has_compe then
3+
return
4+
end
5+
6+
local OrgmodeOmniCompletion = require('orgmode.org.autocompletion.omni')
7+
8+
local CompeSource = {}
9+
10+
function CompeSource.new()
11+
return setmetatable({}, { __index = CompeSource })
12+
end
13+
14+
function CompeSource.get_metadata()
15+
return {
16+
priority = 999,
17+
sort = false,
18+
dup = 0,
19+
filetypes = { 'org' },
20+
menu = '[Org]',
21+
}
22+
end
23+
24+
function CompeSource.determine(_, context)
25+
local offset = OrgmodeOmniCompletion(1, '') + 1
26+
if offset > 0 then
27+
return {
28+
keyword_pattern_offset = offset,
29+
trigger_character_offset = vim.tbl_contains({ '#', '+', ':', '*' }, context.before_char) and context.col or 0,
30+
}
31+
end
32+
end
33+
34+
function CompeSource.complete(_, context)
35+
local items = OrgmodeOmniCompletion(0, context.input)
36+
context.callback({
37+
items = items,
38+
incomplete = true,
39+
})
40+
end
41+
42+
compe.register_source('orgmode', CompeSource)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local function register()
2+
require('orgmode.org.autocompletion.compe')
3+
require('orgmode.org.autocompletion.cmp')
4+
end
5+
6+
return {
7+
register = register,
8+
}

lua/orgmode/org/autocompletion.lua renamed to lua/orgmode/org/autocompletion/omni.lua

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ local data = {
99
metadata = { 'DEADLINE:', 'SCHEDULED:', 'CLOSED:' },
1010
}
1111

12-
local Autocompletion = {}
13-
1412
local directives = { rgx = vim.regex([[^\#+\?\w*$]]), line_rgx = vim.regex([[^\#\?+\?\w*$]]), list = data.directives }
1513
local begin_blocks = {
1614
rgx = vim.regex([[\(^\s*\)\@<=\#+\?\w*$]]),
@@ -70,7 +68,7 @@ local headline_contexts = {
7068
todo_keywords,
7169
}
7270

73-
function Autocompletion.omni(findstart, base)
71+
local function omni(findstart, base)
7472
local line = vim.api.nvim_get_current_line():sub(1, vim.api.nvim_call_function('col', { '.' }) - 1)
7573
local is_headline = line:match('^%*+%s+')
7674
local ctx = is_headline and headline_contexts or contexts
@@ -107,43 +105,4 @@ function Autocompletion.omni(findstart, base)
107105
return results
108106
end
109107

110-
local CompeSource = {}
111-
112-
function CompeSource.new()
113-
return setmetatable({}, { __index = CompeSource })
114-
end
115-
116-
function CompeSource.get_metadata()
117-
return {
118-
priority = 999,
119-
sort = false,
120-
dup = 0,
121-
filetypes = { 'org' },
122-
menu = '[Org]',
123-
}
124-
end
125-
126-
function CompeSource.determine(_, context)
127-
local offset = Autocompletion.omni(1, '') + 1
128-
if offset > 0 then
129-
return {
130-
keyword_pattern_offset = offset,
131-
trigger_character_offset = vim.tbl_contains({ '#', '+', ':', '*' }, context.before_char) and context.col or 0,
132-
}
133-
end
134-
end
135-
136-
function CompeSource.complete(_, context)
137-
local items = Autocompletion.omni(0, context.input)
138-
context.callback({
139-
items = items,
140-
incomplete = true,
141-
})
142-
end
143-
144-
local has_compe, compe = pcall(require, 'compe')
145-
if has_compe then
146-
compe.register_source('orgmode', CompeSource)
147-
end
148-
149-
return Autocompletion
108+
return omni

0 commit comments

Comments
 (0)