Skip to content

Commit

Permalink
feat(treesitter): use ts highlights in main fzf win
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Nov 24, 2024
1 parent ce97847 commit 5faa73d
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion lua/fzf-lua/win.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local path = require "fzf-lua.path"
local utils = require "fzf-lua.utils"
local config = require "fzf-lua.config"
local actions = require "fzf-lua.actions"
Expand Down Expand Up @@ -738,6 +739,45 @@ function FzfWin:set_winleave_autocmd()
self:_nvim_create_autocmd("WinLeave", self.win_leave, [[require('fzf-lua.win').win_leave()]])
end

function FzfWin:treesitter_attach()

local function starts_with_pattern(line)
return line:match("^.-:%d+:") ~= nil
end

-- if self._o.winopts.treesitter then
vim.api.nvim_buf_attach(self.fzf_bufnr, false, {
on_lines = function(_, bufnr, _, first_changed, last_changed, last_updated, bc)
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
local regions = {}
for i = 1, #lines do
local line_idx = i - 1
local line = lines[i]
if starts_with_pattern(line) then
local file = path.entry_to_file(line)
local ft = vim.filetype.match({ filename = file.path })
print(file.path, ":", file.col, ft)

if ft and regions[ft] == nil then
regions[ft] = {}
end

-- if text ~= "" then
-- local first_pos = string.find(line, text, 1, true)
-- if first_pos == nil then
-- return
-- end
-- first_pos = first_pos - 1
-- table.insert(regions[ft], { { line_idx, first_pos, line_idx, line:len() } })
-- TSInjector.attach(args.buf, regions)
-- end
end
end
end
})
-- end
end

function FzfWin:set_tmp_buffer(no_wipe)
if not self:validate() then return end
-- Store the [would be] detached buffer number
Expand All @@ -754,6 +794,8 @@ function FzfWin:set_tmp_buffer(no_wipe)
self:set_winleave_autocmd()
-- automatically resize fzf window
self:set_redraw_autocmd()
-- Use treesitter to highlight results on the main fzf window
self:treesitter_attach()
-- since we have the cursorline workaround from
-- issue #254, resume shows an ugly cursorline.
-- remove it, nvim_win API is better than vim.wo?
Expand Down Expand Up @@ -795,7 +837,7 @@ function FzfWin:create()
-- also recall the user's 'on_create' (#394)
if self.winopts.on_create and
type(self.winopts.on_create) == "function" then
self.winopts.on_create()
self.winopts.on_create({ winid = self.fzf_winid, bufnr = self.fzf_bufnr })
end
-- not sure why but when using a split and reusing the window,
-- fzf will not use all the available width until 'redraw' is
Expand Down Expand Up @@ -842,6 +884,8 @@ function FzfWin:create()
self:set_winleave_autocmd()
-- automatically resize fzf window
self:set_redraw_autocmd()
-- Use treesitter to highlight results on the main fzf window
self:treesitter_attach()

self:reset_win_highlights(self.fzf_winid)

Expand Down

0 comments on commit 5faa73d

Please sign in to comment.