Skip to content

Commit

Permalink
feat: support for editing quickfix
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc-stripe committed Jul 29, 2024
1 parent 5445ae3 commit 42b576a
Show file tree
Hide file tree
Showing 4 changed files with 358 additions and 42 deletions.
33 changes: 25 additions & 8 deletions lua/quicker/config.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
local default_config = {
-- Map of quickfix item type to icon
type_icons = {
E = "󰅚 ",
W = "󰀪 ",
I = "",
N = "",
H = "",
},
-- Local options to set for quickfix
opts = {
buflisted = false,
Expand All @@ -22,6 +14,13 @@ local default_config = {
keys = {},
-- Callback function to run any custom logic or keymaps for the quickfix buffer
on_qf = function(bufnr) end,
edit = {
-- Enable editing the quickfix like a normal buffer
enabled = true,
-- Set to true to write buffers after applying edits.
-- Set to "unmodified" to only write unmodified buffers.
autosave = "unmodified",
},
-- Keep the cursor to the right of the filename and lnum columns
constrain_cursor = true,
-- Maximum width of the filename column
Expand All @@ -34,6 +33,14 @@ local default_config = {
treesitter = true,
lsp = true,
},
-- Map of quickfix item type to icon
type_icons = {
E = "󰅚 ",
W = "󰀪 ",
I = "",
N = "",
H = "",
},
-- Border characters
borders = {
vert = "",
Expand All @@ -55,6 +62,7 @@ local default_config = {
---@field keys any[]
---@field use_default_opts boolean
---@field highlight quicker.HighlightConfig
---@field edit quicker.EditConfig
local M = {}

---@class (exact) quicker.SetupOptions
Expand All @@ -67,6 +75,7 @@ local M = {}
---@field keys? any[]
---@field use_default_opts? boolean Set to false to disable the default options in `opts`
---@field highlight? quicker.SetupHighlightConfig
---@field edit? quicker.SetupEditConfig

local has_setup = false
---@param opts? quicker.SetupOptions
Expand Down Expand Up @@ -110,6 +119,14 @@ end
---@field treesitter? boolean|"fast"
---@field lsp? boolean

---@class (exact) quicker.EditConfig
---@field enabled boolean
---@field autosave boolean|"unmodified"

---@class (exact) quicker.SetupEditConfig
---@field enabled? boolean
---@field autosave? boolean|"unmodified"

return setmetatable(M, {
-- If the user hasn't called setup() yet, make sure we correctly set up the config object so there
-- aren't random crashes.
Expand Down
64 changes: 32 additions & 32 deletions lua/quicker/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,44 +108,44 @@ local function add_qf_highlights(info)
vim.api.nvim_buf_clear_namespace(qf_list.qfbufnr, ns, info.start_idx - 1, -1)
end

-- TODO: make more robust when bufnr is nil
for i = info.start_idx, info.end_idx do
---@type QuickfixItem
local item = qf_list.items[i]
-- TODO this bufload may cause hitches. See if we can spread this out
if not vim.api.nvim_buf_is_loaded(item.bufnr) then
vim.fn.bufload(item.bufnr)
end
local line = lines[i]
local src_line = vim.api.nvim_buf_get_lines(item.bufnr, item.lnum - 1, item.lnum, false)[1]
-- Only add highlights if the text in the quickfix matches the source line
if item.text == src_line then
local offset = line:find(b.vert, 1, true)
offset = line:find(b.vert, offset + b.vert:len(), true) + b.vert:len() - 1
if item.bufnr ~= 0 then
if not vim.api.nvim_buf_is_loaded(item.bufnr) then
vim.fn.bufload(item.bufnr)
end
local line = lines[i]
local src_line = vim.api.nvim_buf_get_lines(item.bufnr, item.lnum - 1, item.lnum, false)[1]
-- Only add highlights if the text in the quickfix matches the source line
if item.text == src_line then
local offset = line:find(b.vert, 1, true)
offset = line:find(b.vert, offset + b.vert:len(), true) + b.vert:len() - 1

-- Add treesitter highlights
if config.highlight.treesitter then
for _, hl in ipairs(highlight.buf_get_ts_highlights(item.bufnr, item.lnum)) do
local start_col, end_col, hl_group = hl[1], hl[2], hl[3]
vim.api.nvim_buf_set_extmark(qf_list.qfbufnr, ns, i - 1, start_col + offset, {
hl_group = hl_group,
end_col = end_col + offset,
priority = 100,
strict = false,
})
-- Add treesitter highlights
if config.highlight.treesitter then
for _, hl in ipairs(highlight.buf_get_ts_highlights(item.bufnr, item.lnum)) do
local start_col, end_col, hl_group = hl[1], hl[2], hl[3]
vim.api.nvim_buf_set_extmark(qf_list.qfbufnr, ns, i - 1, start_col + offset, {
hl_group = hl_group,
end_col = end_col + offset,
priority = 100,
strict = false,
})
end
end
end

-- Add LSP semantic token highlights
if config.highlight.lsp then
for _, hl in ipairs(highlight.buf_get_lsp_highlights(item.bufnr, item.lnum)) do
local start_col, end_col, hl_group, priority = hl[1], hl[2], hl[3], hl[4]
vim.api.nvim_buf_set_extmark(qf_list.qfbufnr, ns, i - 1, start_col + offset, {
hl_group = hl_group,
end_col = end_col + offset,
priority = vim.highlight.priorities.semantic_tokens + priority,
strict = false,
})
-- Add LSP semantic token highlights
if config.highlight.lsp then
for _, hl in ipairs(highlight.buf_get_lsp_highlights(item.bufnr, item.lnum)) do
local start_col, end_col, hl_group, priority = hl[1], hl[2], hl[3], hl[4]
vim.api.nvim_buf_set_extmark(qf_list.qfbufnr, ns, i - 1, start_col + offset, {
hl_group = hl_group,
end_col = end_col + offset,
priority = vim.highlight.priorities.semantic_tokens + priority,
strict = false,
})
end
end
end
end
Expand Down
Loading

0 comments on commit 42b576a

Please sign in to comment.