Skip to content

Commit

Permalink
chore: wider formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 committed May 3, 2024
1 parent d6a4d91 commit 62df2df
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .stylua.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
column_width = 80
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
Expand Down
11 changes: 3 additions & 8 deletions lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,14 @@ function M.end_of_word(str, cursorcol, _linelen)
local offset = cursorcol
local char = vim.fn.strcharpart(str, offset - 1, 1)
local c_class = utils.char_class(char)
local next_char_class =
utils.char_class(vim.fn.strcharpart(str, (offset - 1) + 1, 1))
local next_char_class = utils.char_class(vim.fn.strcharpart(str, (offset - 1) + 1, 1))
local rev_offset

if
(c_class == 1 and next_char_class ~= 1)
or (next_char_class == 1 and c_class ~= 1)
then
if (c_class == 1 and next_char_class ~= 1) or (next_char_class == 1 and c_class ~= 1) then
offset = offset + 1
char = vim.fn.strcharpart(str, offset - 1, 1)
c_class = utils.char_class(char)
next_char_class =
utils.char_class(vim.fn.strcharpart(str, (offset - 1) + 1, 1))
next_char_class = utils.char_class(vim.fn.strcharpart(str, (offset - 1) + 1, 1))
end

if c_class ~= 0 and next_char_class ~= 0 then
Expand Down
38 changes: 8 additions & 30 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ local function build_virt_line(marks, line_len)
if existing == " " and existing ~= hint then
line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
else -- if the character is not a space, then we need to check the prio
if
existing ~= ""
and config.hints[mark].prio
> config.hints[existing].prio
then
if existing ~= "" and config.hints[mark].prio > config.hints[existing].prio then
line = line:sub(1, col - 1) .. hint .. line:sub(col + 1)
end
end
Expand Down Expand Up @@ -115,36 +111,24 @@ local function apply_gutter_hints(gutter_hints, buf)
for hint, loc in pairs(gutter_hints) do
if config.gutterHints[hint] and loc ~= 0 and loc ~= nil then
if gutter_signs_cache[hint] then
vim.fn.sign_unplace(
gutter_group,
{ id = gutter_signs_cache[hint].id }
)
vim.fn.sign_unplace(gutter_group, { id = gutter_signs_cache[hint].id })
gutter_signs_cache[hint] = nil
end
vim.fn.sign_define(gutter_name_prefix .. hint, {
text = config.gutterHints[hint].text,
texthl = "Comment",
})
local ok, res = pcall(
vim.fn.sign_place,
0,
gutter_group,
gutter_name_prefix .. config.gutterHints[hint].text,
buf,
{
local ok, res =
pcall(vim.fn.sign_place, 0, gutter_group, gutter_name_prefix .. config.gutterHints[hint].text, buf, {
lnum = loc,
priority = 100,
}
)
})
if ok then
gutter_signs_cache[hint] = { line = loc, id = res }
end
if not ok and loc ~= 0 then
vim.notify_once(
"Failed to place sign: "
.. config.gutterHints[hint].text
.. " at line "
.. loc,
"Failed to place sign: " .. config.gutterHints[hint].text .. " at line " .. loc,
vim.log.levels.WARN
)
end
Expand All @@ -160,8 +144,7 @@ local function on_cursor_hold()
end

local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop
local cur_line =
vim.api.nvim_get_current_line():gsub("\t", string.rep(" ", tab_width))
local cur_line = vim.api.nvim_get_current_line():gsub("\t", string.rep(" ", tab_width))
local line_len = vim.fn.strcharlen(cur_line)
-- local after_cursor = vim.fn.strcharpart(cur_line, cursorcol + 1)
-- local before_cursor = vim.fn.strcharpart(cur_line, 0, cursorcol - 1)
Expand All @@ -182,12 +165,7 @@ local function on_cursor_hold()
-- TODO: can we add indent lines to the virt line to match indent-blankline or similar (if installed)?

-- create (or overwrite) the extmark
if
vim.api.nvim_get_option_value(
"buftype",
{ buf = vim.api.nvim_get_current_buf() }
) == ""
then
if vim.api.nvim_get_option_value("buftype", { buf = vim.api.nvim_get_current_buf() }) == "" then
extmark = vim.api.nvim_buf_set_extmark(0, ns, cursorline - 1, 0, {
id = extmark, -- reuse the same extmark if it exists
virt_lines = { virt_line },
Expand Down
56 changes: 25 additions & 31 deletions tests/precognition/gutter_hints_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,31 @@ describe("Gutter hints table", function()
}, hints)
end)

it(
"should return a table with the correct keys when the buffer is empty",
function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {})
vim.api.nvim_set_current_buf(testBuf)

local hints = precognition.build_gutter_hints(testBuf)

eq({
["gg"] = 1,
["G"] = 1,
}, hints)
end
)

it(
"should return a table with the correct keys when the buffer is a single line",
function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, { "ABC" })
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 1, 1 })

local hints = precognition.build_gutter_hints(testBuf)
eq({
["gg"] = 1,
["G"] = 1,
}, hints)
end
)
it("should return a table with the correct keys when the buffer is empty", function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {})
vim.api.nvim_set_current_buf(testBuf)

local hints = precognition.build_gutter_hints(testBuf)

eq({
["gg"] = 1,
["G"] = 1,
}, hints)
end)

it("should return a table with the correct keys when the buffer is a single line", function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, { "ABC" })
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 1, 1 })

local hints = precognition.build_gutter_hints(testBuf)
eq({
["gg"] = 1,
["G"] = 1,
}, hints)
end)

it("moving the cursor will update the hints table", function()
local testBuf = vim.api.nvim_create_buf(true, true)
Expand Down
15 changes: 6 additions & 9 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,10 @@ describe("edge case", function()
eq(10, hm.end_of_word(str, 4, #str))
end)

it(
"can handle strings with multiple consecutive special characters",
function()
local str = "this || that"
eq(9, hm.next_word_boundary(str, 6, #str))
eq(1, hm.prev_word_boundary(str, 6, #str))
eq(7, hm.end_of_word(str, 6, #str))
end
)
it("can handle strings with multiple consecutive special characters", function()
local str = "this || that"
eq(9, hm.next_word_boundary(str, 6, #str))
eq(1, hm.prev_word_boundary(str, 6, #str))
eq(7, hm.end_of_word(str, 6, #str))
end)
end)
98 changes: 46 additions & 52 deletions tests/precognition/vertical_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,56 +101,50 @@ describe("gutter motion locations", function()
eq(5, prev_line)
end)

it(
"can find the prev paragraph in a file with multiple consecutive blank lines",
function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {
"ABC",
"DEF",
"",
"",
"GHI",
"",
"JKL",
"",
"",
"",
"MNO",
})
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 10, 0 })

local prev_line = vm.prev_paragraph_line(testBuf)
-- TODO: This is a bug, it should be 5
-- If there are multiple consecutive blank lines, it will currentlu return the next blankline
-- not the next blank line preceding content
eq(9, prev_line)
end
)

it(
"can find the next paragraph in a file with multiple consecutive blank lines",
function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {
"ABC",
"DEF",
"",
"",
"",
"",
"GHI",
"",
"JKL",
})
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 3, 0 })

local next_line = vm.next_paragraph_line(testBuf)
-- TODO: This is a bug, it should be 8
-- Same description as above
eq(4, next_line)
end
)
it("can find the prev paragraph in a file with multiple consecutive blank lines", function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {
"ABC",
"DEF",
"",
"",
"GHI",
"",
"JKL",
"",
"",
"",
"MNO",
})
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 10, 0 })

local prev_line = vm.prev_paragraph_line(testBuf)
-- TODO: This is a bug, it should be 5
-- If there are multiple consecutive blank lines, it will currentlu return the next blankline
-- not the next blank line preceding content
eq(9, prev_line)
end)

it("can find the next paragraph in a file with multiple consecutive blank lines", function()
local testBuf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_lines(testBuf, 0, -1, false, {
"ABC",
"DEF",
"",
"",
"",
"",
"GHI",
"",
"JKL",
})
vim.api.nvim_set_current_buf(testBuf)
vim.api.nvim_win_set_cursor(0, { 3, 0 })

local next_line = vm.next_paragraph_line(testBuf)
-- TODO: This is a bug, it should be 8
-- Same description as above
eq(4, next_line)
end)
end)
27 changes: 11 additions & 16 deletions tests/precognition/virtline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ describe("Build Virtual Line", function()
eq(10, #virtual_line[1][1])
end)

it(
"can build a virtual line with a single mark at the beginning",
function()
---@type Precognition.VirtLine
local marks = {
["^"] = 1,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq("^ ", virtual_line[1][1])
eq(10, #virtual_line[1][1])
end
)
it("can build a virtual line with a single mark at the beginning", function()
---@type Precognition.VirtLine
local marks = {
["^"] = 1,
}
local virtual_line = precognition.build_virt_line(marks, 10)
eq("^ ", virtual_line[1][1])
eq(10, #virtual_line[1][1])
end)

it("can build a complex virtual line", function()
---@type Precognition.VirtLine
Expand Down Expand Up @@ -77,8 +74,7 @@ describe("Build Virtual Line", function()
it("example virtual line", function()
local line = "abcdef ghijkl mnopqr stuvwx yz"
local cursorcol = 2
local tab_width = vim.bo.expandtab and vim.bo.shiftwidth
or vim.bo.tabstop
local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop
local cur_line = line:gsub("\t", string.rep(" ", tab_width))
local line_len = vim.fn.strcharlen(cur_line)

Expand All @@ -98,8 +94,7 @@ describe("Build Virtual Line", function()
local line = " abc def"
-- abc def
local cursorcol = 5
local tab_width = vim.bo.expandtab and vim.bo.shiftwidth
or vim.bo.tabstop
local tab_width = vim.bo.expandtab and vim.bo.shiftwidth or vim.bo.tabstop
local cur_line = line:gsub("\t", string.rep(" ", tab_width))
local line_len = vim.fn.strcharlen(cur_line)

Expand Down

0 comments on commit 62df2df

Please sign in to comment.