Skip to content

Commit

Permalink
feat: inlay hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed May 24, 2024
1 parent 83d452d commit 3c6118d
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local hm = require("precognition.horizontal_motions")

local vm = require("precognition.vertical_motions")
local utils = require("precognition.utils")

Expand Down Expand Up @@ -89,13 +90,13 @@ local config = default
---@type integer?
local extmark -- the active extmark in the current buffer
---@type boolean
local dirty -- whether a redraw is needed
local dirty -- whether a redraw is needed
---@type boolean
local visible = false
---@type string
local gutter_name_prefix = "precognition_gutter_" -- prefix for gutter signs object naame
---@type {SupportedGutterHints: { line: integer, id: integer }} -- cache for gutter signs
local gutter_signs_cache = {} -- cache for gutter signs
local gutter_signs_cache = {} -- cache for gutter signs

---@type integer
local au = vim.api.nvim_create_augroup("precognition", { clear = true })
Expand All @@ -105,9 +106,10 @@ local ns = vim.api.nvim_create_namespace("precognition")
local gutter_group = "precognition_gutter"

---@param marks Precognition.VirtLine
---@param line_num integer
---@param line_len integer
---@return table
local function build_virt_line(marks, line_len)
local function build_virt_line(marks, line_num, line_len)
if not marks then
return {}
end
Expand Down Expand Up @@ -140,9 +142,40 @@ local function build_virt_line(marks, line_len)
end
end
end

if line:match("^%s+$") then
return {}
end

if vim.lsp.inlay_hint.is_enabled({ bufnr = 0 }) then
local inlays_hints = vim.lsp.inlay_hint.get({
bufnr = 0,
range = {
start = { line = line_num - 1, character = 0 },
["end"] = { line = line_num - 1, character = line_len - 1 },
},
})
-- sort the hints by start character
table.sort(inlays_hints, function(a, b)
return a.inlay_hint.label[1].location.range.start.character
< b.inlay_hint.label[1].location.range.start.character
end)

local total_added = 0
local i = 0
for _, hint in ipairs(inlays_hints) do
local length = #hint.inlay_hint.label[1].value + 1
local start = hint.inlay_hint.label[1].location.range.start.character + 1
-- Pad characters to the left of the hint to avoid overlapping with the inlay hint
local padstr = string.rep(" ", length)
local before = string.sub(line, 1, start + total_added + 1)
local after = string.sub(line, start + total_added + 2)
line = before .. padstr .. after
total_added = total_added + length
i = i + 1
vim.print("Added " .. total_added .. " spaces over " .. i .. "occurrences")
end
end
table.insert(virt_line, { line, "PrecognitionHighlight" })
return virt_line
end
Expand Down Expand Up @@ -230,7 +263,7 @@ local function display_marks()
Zero = 1,
}

local virt_line = build_virt_line(virtual_line_marks, line_len)
local virt_line = build_virt_line(virtual_line_marks, cursorline, line_len)

-- TODO: can we add indent lines to the virt line to match indent-blankline or similar (if installed)?

Expand Down

0 comments on commit 3c6118d

Please sign in to comment.