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

feat!: highlight the match context with treesitter #22

Merged
merged 2 commits into from
Nov 6, 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
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
neovim_version: ["nightly", "stable"]
# neovim_version: ["nightly", "stable"]
# TODO stable does not seem to support treesitter
neovim_version: ["nightly"]

steps:
- uses: actions/checkout@v4.2.2
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/MyTestDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const MyTestDirectorySchema = z.object({
extension: z.literal("txt"),
stem: z.literal("initial-file."),
}),
"other-file.txt": z.object({
name: z.literal("other-file.txt"),
"other-file.lua": z.object({
name: z.literal("other-file.lua"),
type: z.literal("file"),
extension: z.literal("txt"),
extension: z.literal("lua"),
stem: z.literal("other-file."),
}),
"test-setup.lua": z.object({
Expand All @@ -45,7 +45,7 @@ export type MyTestDirectory = MyTestDirectoryContentsSchemaType["contents"]

export const testDirectoryFiles = z.enum([
"initial-file.txt",
"other-file.txt",
"other-file.lua",
"test-setup.lua",
".",
])
Expand Down
23 changes: 21 additions & 2 deletions integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { flavors } from "@catppuccin/palette"

const theme = flavors.macchiato.colors

export function rgbify(color: (typeof theme)["surface0"]["rgb"]): string {
return `rgb(${color.r.toString()}, ${color.g.toString()}, ${color.b.toString()})`
}

describe("the basics", () => {
it("shows words in other files as suggestions", () => {
cy.visit("http://localhost:5173")
Expand All @@ -23,9 +31,20 @@ describe("the basics", () => {
// should show documentation with more details about the match
//
// should show the text for the matched line
cy.contains("Hippopotamus" + "234 was my previous password")
//
// the text should also be syntax highlighted
cy.contains("Hippopotamus" + "234 was my previous password").should(
"have.css",
"color",
rgbify(flavors.macchiato.colors.green.rgb),
)

// should show the file name
cy.contains(dir.contents["other-file.txt"].name)
cy.contains(dir.contents["other-file.lua"].name).should(
"have.css",
"color",
rgbify(flavors.macchiato.colors.maroon.rgb),
)
})
})
})
3 changes: 3 additions & 0 deletions integration-tests/test-environment/other-file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file contains some text that can be matched in integration-tests.
local foo = "Hippopotamus123 is my password."
local bar = "Hippopotamus234 was my previous password."
3 changes: 0 additions & 3 deletions integration-tests/test-environment/other-file.txt

This file was deleted.

11 changes: 9 additions & 2 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,18 @@ function RgSource:get_completions(context, resolve)
local items = {}
for _, file in pairs(parsed.files) do
for _, match in ipairs(file.submatches) do
local label = match.match.text .. " (rg)"
-- the implementation for render_detail_and_documentation:
-- ../../integration-tests/test-environment/.repro/data/nvim/lazy/blink.cmp/lua/blink/cmp/windows/lib/docs.lua
---@diagnostic disable-next-line: missing-fields
items[match.match.text] = {
documentation = table.concat(file.lines, "\n"),
documentation = {
kind = "markdown",
value = table.concat(file.lines, "\n"),
},
detail = match.match.text .. ".",
source_id = "blink-ripgrep",
label = match.match.text .. " (rg)",
label = label,
insertText = match.match.text,
}
end
Expand Down
8 changes: 6 additions & 2 deletions lua/blink-ripgrep/ripgrep_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ function M.parse(ripgrep_output, cwd)
---@type string
local filename = json.data.path.text

output.files[filename] = { lines = {}, submatches = {} }
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 = {} }
elseif json.type == "context" then
---@type string
local filename = json.data.path.text
Expand All @@ -52,7 +55,8 @@ function M.parse(ripgrep_output, cwd)
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] = "```"
data.lines[#data.lines + 1] = "> " .. filename
end
end
Expand Down
Loading