Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: show the relative path of the file in the detail section #32

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ describe("the basics", () => {
)

// should show the file name
cy.contains(dir.contents["other-file.lua"].name).should(
"have.css",
"color",
rgbify(flavors.macchiato.colors.maroon.rgb),
)
cy.contains(dir.contents["other-file.lua"].name)
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function RgSource:get_completions(context, resolve)
kind = "markdown",
value = table.concat(file.lines, "\n"),
},
detail = match.match.text .. ".",
detail = file.relative_to_cwd,
source_id = "blink-ripgrep",
label = label,
insertText = match.match.text,
Expand Down
23 changes: 11 additions & 12 deletions lua/blink-ripgrep/ripgrep_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local M = {}
---@class RipgrepFile
---@field lines string[] the context preview for all the matches
---@field submatches RipgrepSubmatch[] the matches
---@field relative_to_cwd string the relative path of the file to the current working directory

---@class RipgrepSubmatch
---@field start number the start column of the match
Expand All @@ -28,7 +29,16 @@ function M.parse(ripgrep_output, cwd)
local filetype = vim.fn.fnamemodify(filename, ":e")
local lang = vim.treesitter.language.get_lang(filetype or "text")
or "markdown"
output.files[filename] = { lines = { "```" .. lang }, submatches = {} }

local relative_filename = filename
if filename:sub(1, #cwd) == cwd then
relative_filename = filename:sub(#cwd + 2)
end
output.files[filename] = {
lines = { "```" .. lang },
submatches = {},
relative_to_cwd = relative_filename,
}
elseif json.type == "context" then
---@type string
local filename = json.data.path.text
Expand All @@ -47,17 +57,6 @@ function M.parse(ripgrep_output, cwd)
end_ = json.data.submatches[1]["end"],
match = { text = json.data.submatches[1].match.text },
}
elseif json.type == "end" then
---@type string
local filename = json.data.path.text
local data = output.files[filename]

if filename:sub(1, #cwd) == cwd then
filename = filename:sub(#cwd + 2)
end
data.lines[#data.lines + 1] = ""
data.lines[#data.lines + 1] = "```"
data.lines[#data.lines + 1] = "> " .. filename
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/blink-ripgrep/ripgrep_parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ describe("ripgrep_parser", function()
assert.is_not_nil(result.files[filename])
assert.is_truthy(#result.files[filename].lines > 19)
assert.same(#result.files[filename].submatches, 3)
assert.same(result.files[filename].relative_to_cwd, filename)

for _, submatch in ipairs(result.files[filename].submatches) do
assert.is_not_nil(submatch.start)
assert.is_not_nil(submatch.end_)
assert.is_not_nil(submatch.match.text)
end
end)

-- TODO test that the cwd is stripped from the filename
end)
Loading