Skip to content

Commit

Permalink
fix: replacement characters (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored May 26, 2024
1 parent 166af88 commit b40c353
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ local function build_virt_line(marks, line_len)
return {}
end
local virt_line = {}
local line = string.rep(" ", line_len)
local line_table = utils.create_pad_array(line_len, " ")

for mark, loc in pairs(marks) do
local hint = config.hints[mark].text or mark
local prio = config.hints[mark].prio or 0
local col = loc

if col ~= 0 and prio > 0 then
local existing = line:sub(col, col)
local existing = line_table[col]
if existing == " " and existing ~= hint then
line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
line_table[col] = hint
else -- if the character is not a space, then we need to check the prio
local existingKey
for key, value in pairs(config.hints) do
Expand All @@ -135,11 +135,13 @@ local function build_virt_line(marks, line_len)
end
end
if existing ~= " " and config.hints[mark].prio > config.hints[existingKey].prio then
line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
line_table[col] = hint
end
end
end
end

local line = table.concat(line_table)
if line:match("^%s+$") then
return {}
end
Expand Down
11 changes: 11 additions & 0 deletions lua/precognition/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ function M.is_blacklisted_buffer(bufnr)
return false
end

---@param len integer
---@param str string
---@return string[]
function M.create_pad_array(len, str)
local pad_array = {}
for i = 1, len do
pad_array[i] = str
end
return pad_array
end

return M
14 changes: 14 additions & 0 deletions tests/precognition/char_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ describe("big_word classing", function()
eq(utils.char_class(".", true), 1)
end)
end)

describe("pad arrays", function()
it("are created with the right length", function()
local pad = utils.create_pad_array(10, " ")
eq(10, #pad)
end)

it("have the right pad char", function()
local pad = utils.create_pad_array(10, " ")
for _, v in ipairs(pad) do
eq(" ", v)
end
end)
end)
83 changes: 83 additions & 0 deletions tests/precognition/virtline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,86 @@ describe("Priority", function()
eq(1, #virtual_line[1][1])
end)
end)

describe("replacment charcters", function()
it("regular replacement chars", function()
precognition.setup({
---@diagnostic disable-next-line: missing-fields
hints = {
Caret = {
prio = 100,
text = "x",
},
},
})

local marks = {
Caret = 1,
}

local virtual_line = precognition.build_virt_line(marks, 1)
eq("x", virtual_line[1][1])
eq(1, #virtual_line[1][1])
end)

it("extended alphabet chars", function()
precognition.setup({
---@diagnostic disable-next-line: missing-fields
hints = {
Caret = {
prio = 100,
text = "â",
},
},
})

local marks = {
Caret = 1,
}

local virtual_line = precognition.build_virt_line(marks, 1)
eq("â", virtual_line[1][1])
eq(2, #virtual_line[1][1])
end)

it("adjacent alphabet chars", function()
precognition.setup({})
-- hello

local marks = {
Zero = 1,
Caret = 2,
e = 3,
w = 5,
Dollar = 8,
}

local virtual_line = precognition.build_virt_line(marks, 8)
eq("0^e w $", virtual_line[1][1])
end)

it("adjacent extended chars", function()
precognition.setup({
---@diagnostic disable-next-line: missing-fields
hints = {
Caret = {
prio = 100,
text = "â",
-- text = "t",
},
},
})
-- hello

local marks = {
Zero = 1,
Caret = 2,
e = 3,
w = 5,
Dollar = 8,
}

local virtual_line = precognition.build_virt_line(marks, 8)
eq("0âe w $", virtual_line[1][1])
end)
end)

0 comments on commit b40c353

Please sign in to comment.