Skip to content

Commit

Permalink
refactor: use virtual text to display filename and line numbers (#32)
Browse files Browse the repository at this point in the history
* refactor: use virtual text to display filename and line numbers

* feat: API method for jumping to quickfix item by filename

* perf: small optimization when adding virtual text to buffer

* fix: guard against incomplete quickfix buffer

* fix: restore the syntax file for better default highlighting

* cleanup: don't need to convert border space characters anymore

* fix: highlight out of range issue

* refactor: put the filename back into the real buffer text

* cleanup: remove find_file method

* fix: highlight and line numbers for truncated filenames

* feat: more minimal display when no items have a filename

* fix: remove now-unnecessary trailing space
  • Loading branch information
stevearc authored Dec 24, 2024
1 parent 049d665 commit 9782132
Show file tree
Hide file tree
Showing 33 changed files with 483 additions and 396 deletions.
12 changes: 2 additions & 10 deletions lua/quicker/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ local default_config = {

---@class quicker.Config
---@field on_qf fun(bufnr: number)
---@field constrain_cursor boolean
---@field opts table<string, any>
---@field keys quicker.Keymap[]
---@field use_default_opts boolean
---@field constrain_cursor boolean
---@field highlight quicker.HighlightConfig
---@field edit quicker.EditConfig
---@field type_icons table<string, string>
Expand All @@ -82,10 +82,10 @@ local M = {}

---@class (exact) quicker.SetupOptions
---@field on_qf? fun(bufnr: number) Callback function to run any custom logic or keymaps for the quickfix buffer
---@field constrain_cursor? boolean Keep the cursor to the right of the filename and lnum columns
---@field opts? table<string, any> Local options to set for quickfix
---@field keys? quicker.Keymap[] Keymaps to set for the quickfix buffer
---@field use_default_opts? boolean Set to false to disable the default options in `opts`
---@field constrain_cursor? boolean Keep the cursor to the right of the filename and lnum columns
---@field highlight? quicker.SetupHighlightConfig Configure syntax highlighting
---@field edit? quicker.SetupEditConfig
---@field type_icons? table<string, string> Map of quickfix item type to icon
Expand All @@ -104,14 +104,6 @@ M.setup = function(opts)
M[k] = v
end

-- Transparently convert a space into an em quad https://unicode-explorer.com/c/2001
-- This is to keep it somewhat unique so it can still be used as an identifiable separator
for _, key in ipairs({ "vert", "strong_header", "soft_header" }) do
if M.borders[key] == " " then
M.borders[key] = ""
end
end

-- Remove the default opts values if use_default_opts is false
if not new_conf.use_default_opts then
M.opts = opts.opts or {}
Expand Down
27 changes: 18 additions & 9 deletions lua/quicker/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function M.expand(opts)
ctx.quicker = quicker_ctx
end
local curpos = vim.api.nvim_win_get_cursor(winid)[1]
local cur_item = qf_list.items[curpos]
local newpos

-- calculate the number of lines to show before and after the current line
Expand Down Expand Up @@ -118,12 +119,6 @@ function M.expand(opts)
end
end

-- Insert the header
if prev_item and not overlaps_previous then
local filename = vim.fs.basename(vim.api.nvim_buf_get_name(item.bufnr))
table.insert(items, { text = filename, valid = 0, user_data = { header = header_type } })
end

local high = item.lnum + num_after
local next_item = get_next_item(i)
if next_item then
Expand All @@ -132,14 +127,12 @@ function M.expand(opts)
end
end

local item_start_idx = #items
local lines = vim.api.nvim_buf_get_lines(item.bufnr, low, high, false)
for j, line in ipairs(lines) do
if j + low == item.lnum then
update_item_text_keep_diagnostics(item, line)
table.insert(items, item)
if i == curpos then
newpos = #items
end
else
table.insert(items, {
bufnr = item.bufnr,
Expand All @@ -149,6 +142,18 @@ function M.expand(opts)
user_data = { lnum = low + j },
})
end
if cur_item.bufnr == item.bufnr and cur_item.lnum == low + j then
newpos = #items
end
end

-- Add the header to the first item in this sequence, if one is needed
if prev_item and not overlaps_previous then
local first_item = items[item_start_idx + 1]
if first_item then
first_item.user_data = first_item.user_data or {}
first_item.user_data.header = header_type
end
end

prev_item = item
Expand Down Expand Up @@ -192,6 +197,10 @@ function M.collapse(opts)
local last_item
for i, item in ipairs(qf_list.items) do
if item.valid == 1 then
if item.user_data then
-- Clear the header, if present
item.user_data.header = nil
end
table.insert(items, item)
if i <= curpos then
last_item = #items
Expand Down
11 changes: 3 additions & 8 deletions lua/quicker/cursor.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
local config = require("quicker.config")
local M = {}

local function constrain_cursor()
local b = config.borders
local display = require("quicker.display")
local cur = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_buf_get_lines(0, cur[1] - 1, cur[1], true)[1]
local idx = line:find(b.vert, 1, true)
local idx = line:find(display.EM_QUAD, 1, true)
if not idx then
return
end
idx = line:find(b.vert, idx + b.vert:len(), true)
if not idx then
return
end
local min_col = idx + b.vert:len() - 1
local min_col = idx + display.EM_QUAD_LEN - 1
if cur[2] < min_col then
vim.api.nvim_win_set_cursor(0, { cur[1], min_col })
end
Expand Down
Loading

0 comments on commit 9782132

Please sign in to comment.