Skip to content

Commit

Permalink
Version 1.3.0
Browse files Browse the repository at this point in the history
* Add context documentation window
* Add `cmp-rg-context_before`
* Add `cmp-rg-context_after`
  • Loading branch information
lukas-reineke committed Nov 26, 2021
1 parent ac729f9 commit 4c54023
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ For more options see `:help cmp-rg`

## Screenshot

![Screenshot](https://user-images.githubusercontent.com/12900252/138992645-7db2f717-be48-44a8-8342-daa01400c45c.png)
![Screenshot](https://user-images.githubusercontent.com/12900252/143555260-8567fb04-eea6-4a73-a1dc-d36d4df8cb64.png)
31 changes: 30 additions & 1 deletion doc/cmp-rg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Author: Lukas Reineke <lukas.reineke@protonmail.com>
Version: 1.2.0
Version: 1.3.0

==============================================================================
CONTENTS *cmp-rg*
Expand Down Expand Up @@ -56,6 +56,30 @@ debounce *cmp-rg-debounce*
{ name = 'rg', option = { debounce = 500 } }
------------------------------------------------------------------------------
context_before *cmp-rg-context_before*

How many lines before the match will be displayed in the documentation
window

Default: 1 ~

Example: >
{ name = 'rg', option = { context_before = 3 } }
------------------------------------------------------------------------------
context_after *cmp-rg-context_after*

How many lines after the match will be displayed in the documentation
window

Default: 3 ~

Example: >
{ name = 'rg', option = { context_after = 5 } }
------------------------------------------------------------------------------
debug *cmp-rg-debug*

Expand All @@ -81,6 +105,11 @@ debug *cmp-rg-debug*
==============================================================================
3. CHANGELOG *cmp-rg-changelog*

1.3.0
* Add context documentation window
* Add |cmp-rg-context_before|
* Add |cmp-rg-context_after|

1.2.0
* update documentation to move from `opts` to `option`

Expand Down
44 changes: 39 additions & 5 deletions lua/cmp-rg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,51 @@ 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 context_before = request.option.context_before or 1
local context_after = request.option.context_after or 3
local quote = "'"
if vim.o.shell == "cmd.exe" then
quote = '"'
end
local seen = {}
local items = {}
local context = {}
local documentation_to_add = 0

local function on_event(job_id, data, event)
if event == "stdout" then
for _, label in ipairs(data) do
if not seen[label] then
table.insert(items, { label = label })
seen[label] = true
for _, entry in ipairs(data) do
if entry ~= "" then
local result = vim.fn.json_decode(entry)
if result.type == "context" then
local documentation = result.data.lines.text:gsub("\n", "")
table.insert(context, documentation)
if documentation_to_add > 0 then
table.insert(items[#items].documentation, documentation)
documentation_to_add = documentation_to_add - 1
if documentation_to_add == 0 then
table.insert(items[#items].documentation, "```")
end
end
elseif result.type == "match" then
local label = result.data.submatches[1].match.text
if label and not seen[label] then
local documentation = { result.data.path.text, "", "```" }
for i = context_before, 0, -1 do
table.insert(documentation, context[i])
end
local match_line = result.data.lines.text:gsub("\n", "") .. " <--"
table.insert(documentation, match_line)
table.insert(items, {
label = label,
documentation = documentation,
})
documentation_to_add = context_after
seen[label] = true
end
elseif result.type == "end" then
context = {}
end
end
end
callback { items = items, isIncomplete = true }
Expand All @@ -48,7 +80,9 @@ source.complete = function(self, request, callback)
vim.fn.jobstop(self.running_job_id)
self.running_job_id = vim.fn.jobstart(
string.format(
"rg --no-filename --no-heading --no-line-number --word-regexp --color never --only-matching %s %s%s%s%s .",
"rg --heading --json --word-regexp -B %d -A %d --color never %s %s%s%s%s .",
context_before,
context_after,
additional_arguments,
quote,
q,
Expand Down

0 comments on commit 4c54023

Please sign in to comment.