Skip to content

Commit

Permalink
feat: show the file name where the match came from (#10)
Browse files Browse the repository at this point in the history
* feat: show the file name where the match came from

* fixup! feat: show the file name where the match came from
  • Loading branch information
mikavilpas authored Nov 4, 2024
1 parent d79688f commit 0c369d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
18 changes: 13 additions & 5 deletions integration-tests/cypress/e2e/cmp-rg/basic_spec.cy.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
describe("the basics", () => {
it("shows words in other files as suggestions", () => {
cy.visit("http://localhost:5173")
cy.startNeovim().then(() => {
cy.startNeovim().then((dir) => {
// wait until text on the start screen is visible
cy.contains("If you see this text, Neovim is ready!")
cy.typeIntoTerminal(
// clear the current line and enter insert mode
"cc",
)

// this will match "Hippopotamus123" from ../../../test-environment/other-file.txt
// this will match text from ../../../test-environment/other-file.txt
//
// If the plugin works, this text should show up as a suggestion.
cy.typeIntoTerminal("hip")
cy.typeIntoTerminal(
// NOTE: need to break it into parts so that this test file itself does
// not match the search :)
"hip" + "234",
)

cy.contains("Hippopotamus123 (rg)")
cy.contains("Hippopotamus" + "234 (rg)")

// should show documentation with more details about the match
cy.contains("Hippopotamus123 is my password")
//
// should show the text for the matched line
cy.contains("Hippopotamus" + "234 was my previous password")
// should show the file name
cy.contains(dir.contents["other-file.txt"].name)
})
})
})
2 changes: 2 additions & 0 deletions integration-tests/test-environment/test-setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ local plugins = {
documentation = {
auto_show = true,
auto_show_delay_ms = 0,
-- file names need to fit the screen when testing
max_width = 200,
},
},
},
Expand Down
22 changes: 20 additions & 2 deletions lua/blink-cmp-rg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function RgSource:get_completions(context, resolve)
end

local lines = vim.split(result.stdout, "\n")
local cwd = vim.uv.cwd()

---@type table<string, blink.cmp.CompletionItem>
local items = {}
Expand All @@ -84,11 +85,28 @@ function RgSource:get_completions(context, resolve)
"ripgrep output missing item.data.lines.text for item "
.. vim.inspect(item)
)
local documentation = item.data.lines.text or ""
assert(
item.data.path.text,
"ripgrep output missing item.data.path.text for item "
.. vim.inspect(item)
)
---@type string
local path = item.data.path.text
if path:sub(1, #cwd) == cwd then
path = path:sub(#cwd + 2)
end

---@type string[]
local documentation = {
item.data.lines.text,
" ", -- empty lines seem to do nothing, so just have something
path,
}

for _, submatch in ipairs(item.data.submatches) do
---@diagnostic disable-next-line: missing-fields
items[submatch.match.text] = {
documentation = documentation,
documentation = table.concat(documentation, "\n"),
source_id = "blink-cmp-rg",
label = submatch.match.text .. " (rg)",
insertText = submatch.match.text,
Expand Down

0 comments on commit 0c369d8

Please sign in to comment.