Skip to content

Commit

Permalink
Add completion-nvim support. Closes nvim-orgmode#5.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Jun 29, 2021
1 parent 9c25432 commit b8c027b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
9 changes: 9 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,15 @@ If you use [nvim-compe](https://github.com/hrsh7th/nvim-compe) add this to compe
})
```

For [completion.nvim](https://github.com/nvim-lua/completion-nvim), add this to your completion nvim config:
```lua
vim.g.completion_chain_complete_list = {
org = {
{ complete_items = {'orgmode'}},
},
}
```

Note that autocompletion is context aware, which means that
for example tags autocompletion will kick in only when cursor is at the end of headline.
Example (`|` marks the cursor):
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ If you use [nvim-compe](https://github.com/hrsh7th/nvim-compe) and want
to enable autocompletion, add this to your compe config:

```lua
require'compe'.setup({
source = {
orgmode = true
}
})
require'compe'.setup({
source = {
orgmode = true
}
})
```

For [completion.nvim](https://github.com/nvim-lua/completion-nvim), add this to your completion nvim config:
```lua
vim.g.completion_chain_complete_list = {
org = {
{ complete_items = {'orgmode'}},
},
}
```

Or just use `omnifunc` via <kbd>\<C-x\>\<C-o\></kbd>

### Gifs
Expand Down
9 changes: 9 additions & 0 deletions doc/orgmode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ If you use nvim-compe (https://github.com/hrsh7th/nvim-compe) add this to compe
})
<

For completion.nvim (https://github.com/nvim-lua/completion-nvim), add this to your completion nvim config:
>
vim.g.completion_chain_complete_list = {
org = {
{ complete_items = {'orgmode'}},
},
}
<

Note that autocompletion is context aware, which means that
for example tags autocompletion will kick in only when cursor is at the end of headline.
Example (`|` marks the cursor):
Expand Down
29 changes: 22 additions & 7 deletions lua/orgmode/org/autocompletion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ function Autocompletion.omni(findstart, base)
return results
end

local Source = {}
local CompeSource = {}

function Source.new()
return setmetatable({}, { __index = Source })
function CompeSource.new()
return setmetatable({}, { __index = CompeSource })
end

function Source.get_metadata()
function CompeSource.get_metadata()
return {
priority = 999,
sort = false,
Expand All @@ -103,7 +103,7 @@ function Source.get_metadata()
}
end

function Source.determine(_, context)
function CompeSource.determine(_, context)
local offset = Autocompletion.omni(1, '') + 1
if offset > 0 then
return {
Expand All @@ -113,7 +113,7 @@ function Source.determine(_, context)
end
end

function Source.complete(_, context)
function CompeSource.complete(_, context)
local items = Autocompletion.omni(0, context.input)
context.callback({
items = items,
Expand All @@ -124,7 +124,22 @@ end

local has_compe, compe = pcall(require, 'compe')
if has_compe then
compe.register_source('orgmode', Source)
compe.register_source('orgmode', CompeSource)
end

local has_completion_nvim, completion_nvim = pcall(require, 'completion')
if has_completion_nvim then
local CompletionNvim = {}
vim.cmd[[set iskeyword+=:,#,+]]
CompletionNvim.item = function(prefix)
local items = Autocompletion.omni(0, prefix)
for _, item in pairs(items) do
item.dup = 0
end

return items
end
completion_nvim.addCompletionSource('orgmode', CompletionNvim)
end

return Autocompletion

0 comments on commit b8c027b

Please sign in to comment.