From 36abfb5a06a1c1bbd6d36f2940181acd0071fcb9 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Fri, 8 Nov 2024 10:24:30 +0200 Subject: [PATCH] refactor: show the relative path of the file in the detail section --- .../e2e/blink-ripgrep/basic_spec.cy.ts | 6 +---- lua/blink-ripgrep/init.lua | 2 +- lua/blink-ripgrep/ripgrep_parser.lua | 23 +++++++++---------- spec/blink-ripgrep/ripgrep_parser_spec.lua | 3 +-- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts b/integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts index 470f1c8..922a6f7 100644 --- a/integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts +++ b/integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts @@ -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) }) }) }) diff --git a/lua/blink-ripgrep/init.lua b/lua/blink-ripgrep/init.lua index 52e536c..8ad7d56 100644 --- a/lua/blink-ripgrep/init.lua +++ b/lua/blink-ripgrep/init.lua @@ -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, diff --git a/lua/blink-ripgrep/ripgrep_parser.lua b/lua/blink-ripgrep/ripgrep_parser.lua index c0822fa..59a39ed 100644 --- a/lua/blink-ripgrep/ripgrep_parser.lua +++ b/lua/blink-ripgrep/ripgrep_parser.lua @@ -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 @@ -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 @@ -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 diff --git a/spec/blink-ripgrep/ripgrep_parser_spec.lua b/spec/blink-ripgrep/ripgrep_parser_spec.lua index e443cda..f1ae825 100644 --- a/spec/blink-ripgrep/ripgrep_parser_spec.lua +++ b/spec/blink-ripgrep/ripgrep_parser_spec.lua @@ -13,6 +13,7 @@ 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) @@ -20,6 +21,4 @@ describe("ripgrep_parser", function() assert.is_not_nil(submatch.match.text) end end) - - -- TODO test that the cwd is stripped from the filename end)