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: fall back to regex highlighting when treesitter not available #51

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions integration-tests/MyTestDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export const MyTestDirectorySchema = z.object({
name: z.literal("subproject/"),
type: z.literal("directory"),
contents: z.object({
"example.clj": z.object({
name: z.literal("example.clj"),
type: z.literal("file"),
extension: z.literal("clj"),
stem: z.literal("example."),
}),
"file1.lua": z.object({
name: z.literal("file1.lua"),
type: z.literal("file"),
Expand Down Expand Up @@ -91,6 +97,7 @@ export const testDirectoryFiles = z.enum([
".config",
"initial-file.txt",
"limited/main-project-file.lua",
"limited/subproject/example.clj",
"limited/subproject/file1.lua",
"limited/subproject/file2.lua",
"limited/subproject",
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/cypress/e2e/blink-ripgrep/basic_spec.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ describe("searching inside projects", () => {
)
})
})

describe("syntax highlighting", () => {
it("can highlight file types that don't have a treesitter parser installed", () => {
cy.visit("/")
cy.startNeovim({ filename: "limited/subproject/file1.lua" }).then(() => {
// when opening a file from a subproject, the search should be limited to
// the nearest .git directory (only the files in the same project should
// be searched)
cy.contains("This is text from file1.lua")
createFakeGitDirectoriesToLimitRipgrepScope()

cy.typeIntoTerminal("o")
// match text inside ../../../test-environment/limited/subproject/example.clj
cy.typeIntoTerminal("Subtraction")

// make sure the syntax is highlighted
// (needs https://github.com/Saghen/blink.cmp/pull/462)
cy.contains("defn").should(
"have.css",
"color",
rgbify(flavors.macchiato.colors.pink.rgb),
)
cy.contains("Clojure Calculator").should(
"have.css",
"color",
rgbify(flavors.macchiato.colors.green.rgb),
)
})
})
})
})

function createFakeGitDirectoriesToLimitRipgrepScope() {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/test-environment/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ vim.o.swapfile = false
local plugins = {
{
"saghen/blink.cmp",
dir = "/Users/mikavilpas/git/blink.cmp/",
event = "VeryLazy",
-- use a release tag to download pre-built binaries
version = "v0.*",
Expand Down
23 changes: 23 additions & 0 deletions integration-tests/test-environment/limited/subproject/example.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns calculator.core)

(defn add [a b]
(+ a b))

(defn subtract [a b]
(- a b))

(defn multiply [a b]
(* a b))

(defn divide [a b]
(if (zero? b)
"Cannot divide by zero!"
(/ a b)))

(defn -main []
(println "Welcome to the Clojure Calculator!")
(println "Addition: 10 + 5 =" (add 10 5))
(println "Subtraction: 10 - 5 =" (subtract 10 5))
(println "Multiplication: 10 * 5 =" (multiply 10 5))
(println "Division: 10 / 5 =" (divide 10 5))
(println "Division: 10 / 0 =" (divide 10 0)))
96 changes: 49 additions & 47 deletions lua/blink-ripgrep/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,57 +101,59 @@ function RgSource:get_completions(context, resolve)
local cmd = self.get_command(context, prefix)

vim.system(cmd, nil, function(result)
if result.code ~= 0 then
resolve()
return
end
vim.schedule(function()
if result.code ~= 0 then
resolve()
return
end

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

local parsed = require("blink-ripgrep.ripgrep_parser").parse(
lines,
cwd,
self.options.context_size
)

---@type table<string, blink.cmp.CompletionItem>
local items = {}
for _, file in pairs(parsed.files) do
for _, match in pairs(file.matches) do
local matchkey = match.match.text

-- PERF: only register the match once - right now there is no useful
-- way to display the same match multiple times
if not items[matchkey] then
local label = match.match.text .. " (rg)"
local docstring = ("```" .. file.language .. "\n")
.. table.concat(match.context_preview, "\n")
.. "```"

-- 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[matchkey] = {
documentation = {
kind = "markdown",
value = docstring,
},
detail = file.relative_to_cwd,
source_id = "blink-ripgrep",
label = label,
insertText = matchkey,
}
local lines = vim.split(result.stdout, "\n")
local cwd = vim.uv.cwd() or ""

local parsed = require("blink-ripgrep.ripgrep_parser").parse(
lines,
cwd,
self.options.context_size
)

---@type table<string, blink.cmp.CompletionItem>
local items = {}
for _, file in pairs(parsed.files) do
for _, match in pairs(file.matches) do
local matchkey = match.match.text

-- PERF: only register the match once - right now there is no useful
-- way to display the same match multiple times
if not items[matchkey] then
local label = match.match.text .. " (rg)"
local docstring = ("```" .. file.language .. "\n")
.. table.concat(match.context_preview, "\n")
.. "```"

-- 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[matchkey] = {
documentation = {
kind = "markdown",
value = docstring,
},
detail = file.relative_to_cwd,
source_id = "blink-ripgrep",
label = label,
insertText = matchkey,
}
end
end
end
end

resolve({
is_incomplete_forward = false,
is_incomplete_backward = false,
items = vim.tbl_values(items),
context = context,
})
resolve({
is_incomplete_forward = false,
is_incomplete_backward = false,
items = vim.tbl_values(items),
context = context,
})
end)
end)
end

Expand Down
9 changes: 6 additions & 3 deletions lua/blink-ripgrep/ripgrep_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ function M.parse(ripgrep_output, cwd, context_size)
relative_filename = filename:sub(#cwd + 2)
end

local filetype = vim.fn.fnamemodify(filename, ":e")
local language = vim.treesitter.language.get_lang(filetype or "text")
or "markdown"
local ext = vim.fn.fnamemodify(filename, ":e")

local ft = vim.filetype.match({ filename = filename })
local language = ft
or vim.treesitter.language.get_lang(ext or "text")
or ext

output.files[filename] = {
language = language,
Expand Down
Loading