Skip to content

Commit

Permalink
refactor: mark table config
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed Apr 29, 2024
1 parent 79f6d8b commit 7575fee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
43 changes: 16 additions & 27 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ local M = {}

---@class Precognition.Config
---@field startVisible boolean
---@field hints table<SupportedHints, string>
---@field hints { SupportedHints: string }

---@class Precognition.PartialConfig
---@field startVisible? boolean
---@field hints? table<SupportedHints, string>
---@field hints? { SupportedHints: string }

---@alias Precognition.VirtLine table<SupportedHints, integer>
---@alias Precognition.VirtLine { [ SupportedHints]: integer | nil }

---@type Precognition.Config
local default = {
Expand Down Expand Up @@ -165,11 +165,13 @@ local function build_virt_line(marks, line_len)
local virt_line = {}
local line = string.rep(" ", line_len)

for _, mark in ipairs(marks) do
local hint = config.hints[mark[1]] or mark[1]
local col = mark[2] or 0
for mark, loc in pairs(marks) do
local hint = config.hints[mark] or mark
local col = loc

line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
if col ~= nil then
line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
end
end
table.insert(virt_line, { line, "Comment" })

Expand Down Expand Up @@ -203,26 +205,13 @@ local function on_cursor_hold()

local motion_b = prev_word_boundary(cur_line, cursorcol)

-- create the list of hints to show in { hint, column } format
-- TODO: extract this into a function, add hints for other motions
---@type Precognition.VirtLine
local marks = {}
table.insert(marks, { "^", math.max(0, line_start) })
table.insert(marks, { "$", line_end })
if motion_w then
table.insert(marks, { "w", motion_w })
end
if motion_e then
table.insert(marks, { "e", motion_e })
end
if motion_b then
table.insert(marks, { "b", motion_b })
end
table.sort(marks, function(a, b)
return a[2] < b[2]
end)

local virt_line = build_virt_line(marks, line_len)
local virt_line = build_virt_line({
w = motion_w,
e = motion_e,
b = motion_b,
["^"] = line_start,
["$"] = line_end,
}, line_len)

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

Expand Down
18 changes: 9 additions & 9 deletions tests/precognition/virtline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe("Build Virtual Line", function()
it("can build a simple virtual line", function()
---@type Precognition.VirtLine
local marks = {
{ "^", 4 },
{ "$", 10 },
["^"] = 4,
["$"] = 10,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq(" ^ $", virtual_line[1][1])
Expand All @@ -16,7 +16,7 @@ describe("Build Virtual Line", function()
it("can build a virtual line with a single mark", function()
---@type Precognition.VirtLine
local marks = {
{ "^", 4 },
["^"] = 4,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq(" ^ ", virtual_line[1][1])
Expand All @@ -26,7 +26,7 @@ describe("Build Virtual Line", function()
it("can build a virtual line with a single mark at the end", function()
---@type Precognition.VirtLine
local marks = {
{ "$", 10 },
["$"] = 10,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq(" $", virtual_line[1][1])
Expand All @@ -38,7 +38,7 @@ describe("Build Virtual Line", function()
function()
---@type Precognition.VirtLine
local marks = {
{ "^", 1 },
["^"] = 1,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq("^ ", virtual_line[1][1])
Expand All @@ -49,10 +49,10 @@ describe("Build Virtual Line", function()
it("can build a complex virtual line", function()
---@type Precognition.VirtLine
local marks = {
{ "^", 1 },
{ "b", 4 },
{ "w", 10 },
{ "$", 50 },
["^"] = 1,
["b"] = 4,
["w"] = 10,
["$"] = 50,
}
local virtual_line = precognition.build_virt_line(marks, 50)
local line_num = 1
Expand Down

0 comments on commit 7575fee

Please sign in to comment.