diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..0307e85 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: [lukas-reineke] diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml new file mode 100644 index 0000000..d820cb5 --- /dev/null +++ b/.github/workflows/pr_check.yml @@ -0,0 +1,22 @@ +name: Pull request check + +on: + pull_request: + +jobs: + format: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: JohnnyMorganz/stylua-action@1.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --check . + + block-fixup: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Block Fixup Commit Merge + uses: 13rac1/block-fixup-merge-action@v2.0.0 diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..f38dc29 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT Licence + +Copyright (c) 2021 Lukas Reineke + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e70ecdc --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# cmp-rg + +[ripgrep](https://github.com/BurntSushi/ripgrep) source for [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) + +## Dependencies + +You need to have `rg` and `sort` installed. + +## Install + +Use your favourite plugin manager to install. + +#### Example with Packer + +[wbthomason/packer.nvim](https://github.com/wbthomason/packer.nvim) + +```lua +-- init.lua +require("packer").startup( + function() + use "lukas-reineke/cmp-rg" + end +) +``` + +#### Example with Plug + +[junegunn/vim-plug](https://github.com/junegunn/vim-plug) + +```vim +" init.vim +call plug#begin('~/.vim/plugged') +Plug 'lukas-reineke/cmp-rg' +call plug#end() +``` + +## Setup + +Add `rg` to your cmp sources + +```lua +require'cmp'.setup { + sources = { + { name = 'rg' } + } +} +``` + +For more options see `:help cmp-rg` diff --git a/after/plugin/cmp-rg.lua b/after/plugin/cmp-rg.lua new file mode 100644 index 0000000..55a8f01 --- /dev/null +++ b/after/plugin/cmp-rg.lua @@ -0,0 +1,5 @@ +local ok, cmp = pcall(require, "cmp") + +if ok then + cmp.register_source("rg", require("cmp-rg").new()) +end diff --git a/doc/cmp-rg.txt b/doc/cmp-rg.txt new file mode 100644 index 0000000..8f9cb1b --- /dev/null +++ b/doc/cmp-rg.txt @@ -0,0 +1,75 @@ +*cmp-rg.txt* ripgrep source for nvim-cmp + + +Author: Lukas Reineke +Version: 1.0.0 + +============================================================================== +CONTENTS *cmp-rg* + + 1. Options |cmp-rg-options| + 2. Highlights |cmp-rg-highlights| + 3. Setup |cmp-rg-setup| + 4. Variables |cmp-rg-variables| + 5. Commands |cmp-rg-commands| + 6. Changelog |cmp-rg-changelog| + 7. License |cmp-rg-license| + +============================================================================== + 1. OPTIONS *cmp-rg-options* + +------------------------------------------------------------------------------ +pattern *cmp-rg-pattern* + +The pattern to search for matches with ripgrep. + +Default: "[a-zA-Z_-]+" ~ + +------------------------------------------------------------------------------ +additional_arguments *cmp-rg-additional_arguments* + +Any additional arguments you want to pass to ripgrep. + +Default: "" ~ + +------------------------------------------------------------------------------ +cwd *cmp-rg-cwd* + +Directory from where ripgrep will search. + +Default: Current working directory ~ + +============================================================================== + 6. CHANGELOG *cmp-rg-changelog* + +1.0.0 + * First release + +============================================================================== + 7. LICENSE *cmp-rg-license* + +The MIT Licence +http://www.opensource.org/licenses/mit-license.php + +Copyright (c) 2021 Lukas Reineke + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +============================================================================== +vim:tw=78:ts=8:ft=help:norl diff --git a/lua/cmp-rg/init.lua b/lua/cmp-rg/init.lua new file mode 100644 index 0000000..df7618e --- /dev/null +++ b/lua/cmp-rg/init.lua @@ -0,0 +1,48 @@ +local source = {} + +source.new = function() + return setmetatable({}, { __index = source }) +end + +source.complete = function(self, request, callback) + local q = string.sub(request.context.cursor_before_line, request.offset) + local pattern = request.option.pattern or "[a-zA-Z_-]+" + local additional_arguments = request.option.additional_arguments or "" + local items = {} + + local function on_event(job_id, data, event) + if event == "stdout" then + for _, label in ipairs(data) do + table.insert(items, { label = label }) + end + callback { items = items, isIncomplete = true } + end + + if event == "stderr" then + vim.cmd "echohl Error" + vim.cmd('echomsg "' .. table.concat(data, "") .. '"') + vim.cmd "echohl None" + end + + if event == "exit" then + callback { items = items, isIncomplete = false } + end + end + + vim.fn.jobstart( + string.format( + "rg --no-filename --no-heading --no-line-number --color never --only-matching %s %s%s . | sort -u", + additional_arguments, + q, + pattern + ), + { + on_stderr = on_event, + on_stdout = on_event, + on_exit = on_event, + cwd = request.option.cwd or vim.fn.getcwd(), + } + ) +end + +return source diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..fa346d0 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,4 @@ +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 4 +no_call_parentheses = true