Skip to content

Commit

Permalink
Add nvim-cmp completion source
Browse files Browse the repository at this point in the history
  • Loading branch information
m-pilia committed Oct 26, 2024
1 parent a49f18d commit 272ade9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

- name: Install dependencies
run: |
pip install --user codecov==2.0.15 covimerage==0.2.2 mwclient==0.10.0 flake8==3.7.8 coverage==4.5.4
pip install --user codecov==2.1.13 covimerage==0.2.2 mwclient==0.10.0 flake8==3.7.8 coverage==4.5.4
- name: Run Python tests
shell: bash
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage.xml
python_output
browser_output
codecov_validation
doc/tags
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ provides:
* filetype detection
* improved syntax highlighting
* page preview
* semantic auto-completion of links and templates with a
[coc.nvim](https://github.com/neoclide/coc.nvim) source
* semantic auto-completion of links and templates with
[coc.nvim](https://github.com/neoclide/coc.nvim) and [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) sources
* [UltiSnips](https://github.com/SirVer/ultisnips) snippets
* `<plug>` mappings for text objects
* matching pairs for [matchit.vim](https://github.com/vim/vim/blob/master/runtime/pack/dist/opt/matchit/doc/matchit.txt)
Expand All @@ -28,7 +28,20 @@ Auto-completion

If [coc.nvim](https://github.com/neoclide/coc.nvim) is installed and configured
properly, the completion source should work out-of-the-box for buffers of type
`mediawiki`. Completion is triggered when typing inside double square brackets
`mediawiki`.

For [nvim-cmp](https://github.com/hrsh7th/nvim-cmp), add the `cmp_mediawiki`
source:
```lua
require('vim-mediawiki.cmp_mediawiki')
require('cmp').setup({
sources = {
{name = 'cmp_mediawiki'},
},
})
```

Completion is triggered when typing inside double square brackets
or double braces, and the minimum number of characters to type before
completion can be configured. Multi-word completion is supported, so
completions will be suggested when typing more than one word inside the
Expand Down
47 changes: 47 additions & 0 deletions lua/vim-mediawiki/cmp_mediawiki.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local source = {}

function source:complete(cmp_params, cmp_callback)
local coc_options = {
bufnr = cmp_params.context.bufnr,
line = cmp_params.context.cursor_line,
colnr = cmp_params.context.cursor.col,
}

if vim.fn['coc#source#mediawiki#should_complete'](coc_options) == 0 then
cmp_callback(nil)
return
end

local function coc_callback(data)
local response = {
isIncomplete = false,
items = {}
}

for _, datum in ipairs(data) do
table.insert(response.items, {
insertText = datum.word,
label = datum.menu,
detail = datum.info,
})
end

cmp_callback(response)
end

vim.fn['coc#source#mediawiki#complete'](coc_options, coc_callback)
end

function source:is_available()
return vim.bo.filetype == 'mediawiki'
end

function source:resolve(completion_item, callback)
callback(completion_item)
end

function source:execute(completion_item, callback)
callback(completion_item)
end

require('cmp').register_source('cmp_mediawiki', source)

0 comments on commit 272ade9

Please sign in to comment.