From 272ade9c0efc3bdde899b7d69328587bf22ff622 Mon Sep 17 00:00:00 2001 From: Martino Pilia Date: Sat, 26 Oct 2024 21:51:27 +0200 Subject: [PATCH] Add nvim-cmp completion source --- .github/workflows/checks.yaml | 2 +- .gitignore | 1 + README.md | 19 ++++++++++-- lua/vim-mediawiki/cmp_mediawiki.lua | 47 +++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 lua/vim-mediawiki/cmp_mediawiki.lua diff --git a/.github/workflows/checks.yaml b/.github/workflows/checks.yaml index 1524489..c6d83a7 100644 --- a/.github/workflows/checks.yaml +++ b/.github/workflows/checks.yaml @@ -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 diff --git a/.gitignore b/.gitignore index a8c9606..d769a4d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ coverage.xml python_output browser_output codecov_validation +doc/tags diff --git a/README.md b/README.md index 510e490..5a0c905 100644 --- a/README.md +++ b/README.md @@ -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 * `` mappings for text objects * matching pairs for [matchit.vim](https://github.com/vim/vim/blob/master/runtime/pack/dist/opt/matchit/doc/matchit.txt) @@ -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 diff --git a/lua/vim-mediawiki/cmp_mediawiki.lua b/lua/vim-mediawiki/cmp_mediawiki.lua new file mode 100644 index 0000000..d9610d5 --- /dev/null +++ b/lua/vim-mediawiki/cmp_mediawiki.lua @@ -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)