Skip to content

Commit

Permalink
Merge branch 'main' into count_motions
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored Jul 31, 2024
2 parents 4f640d4 + 6772d3a commit a2f6d11
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 40 deletions.
1 change: 1 addition & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"describe",
"it",
"before_each",
"after_each",
]
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ return {
-- PrevParagraph = { text = "{", prio = 8 },
-- NextParagraph = { text = "}", prio = 8 },
-- },
-- disabled_fts = {
-- "startify",
-- },
},
}
```
Expand All @@ -58,6 +61,8 @@ return {
1. As a table containing a link property pointing to an existing highlight group (see `:highlight` for valid options).
2. As a table specifying custom highlight values, such as foreground and background colors. ([more info](<https://neovim.io/doc/user/api.html#nvim_set_hl()>))

- `disabled_fts` can be used to disable `precognition` on specific filetypes.

### Hint priorities

Any hints that could appear in the same place as others should have unique priorities to avoid conflicts.
Expand All @@ -80,6 +85,16 @@ or
require("precognition").toggle()
```

The return value indicating the visible state can be used to produce a notification.

```lua
if require("precognition").toggle() then
vim.notify("precognition on")
else
vim.notify("precognition off")
end
```

The subcommands and functions `show` and `hide` are also available.

### Peeking
Expand Down
10 changes: 10 additions & 0 deletions lua/precognition/compat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local M = {}

function M.inlay_hints_enabled(t)
if vim.lsp and vim.lsp.inlay_hint and vim.lsp.inlay_hint.is_enabled then
return vim.lsp.inlay_hint.is_enabled(t)
end
return false
end

return M
6 changes: 5 additions & 1 deletion lua/precognition/horizontal_motions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ function M.end_of_word(str, cursorcol, linelen, big_word)
if c_class == cc.whitespace or next_char_class == cc.whitespace then
local next_word_start = M.next_word_boundary(str, cursorcol, linelen, big_word)
if next_word_start then
c_class = utils.char_class(vim.fn.strcharpart(str, next_word_start - 1, 1), big_word)
next_char_class = utils.char_class(vim.fn.strcharpart(str, (next_word_start - 1) + 1, 1), big_word)
--next word is single char
if next_char_class == cc.whitespace then
--next word is single char
rev_offset = next_word_start
elseif c_class == cc.punctuation and next_char_class ~= cc.punctuation then
--next word starts with punctuation
rev_offset = next_word_start
else
rev_offset = M.end_of_word(str, next_word_start, linelen, big_word)
Expand Down
32 changes: 28 additions & 4 deletions lua/precognition/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local compat = require("precognition.compat")

local M = {}

---@class Precognition.HintOpts
Expand Down Expand Up @@ -27,6 +29,7 @@ local M = {}
---@field highlightColor vim.api.keyset.highlight
---@field hints Precognition.HintConfig
---@field gutterHints Precognition.GutterHintConfig
---@field disabled_fts string[]

---@class Precognition.PartialConfig
---@field startVisible? boolean
Expand Down Expand Up @@ -80,6 +83,9 @@ local default = {
PrevParagraph = { text = "{", prio = 8 },
NextParagraph = { text = "}", prio = 8 },
},
disabled_fts = {
"startify",
},
}

---@type Precognition.Config
Expand Down Expand Up @@ -107,17 +113,18 @@ local showcmd

---@param marks Precognition.VirtLine
---@param line_len integer
---@param extra_padding Precognition.ExtraPadding
---@param extra_padding Precognition.ExtraPadding[]
---@return table
local function build_virt_line(marks, line_len, extra_padding)
local utils = require("precognition.utils")
if not marks then
return {}
end
if line_len == 0 then
return {}
end
local virt_line = {}
local line_table = require("precognition.utils").create_pad_array(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
Expand Down Expand Up @@ -267,9 +274,23 @@ local function display_marks()
Zero = 1,
}

if compat.inlay_hints_enabled({ bufnr = 0 }) then
local inlays_hints = vim.lsp.inlay_hint.get({
bufnr = 0,
range = {
start = { line = cursorline - 1, character = 0 },
["end"] = { line = cursorline - 1, character = line_len - 1 },
},
})

for _, hint in ipairs(inlays_hints) do
local length, ws_offset = utils.calc_ws_offset(hint, tab_width, vim.api.nvim_get_current_line())
table.insert(extra_padding, { start = ws_offset, length = length })
end
end
--multicharacter padding

require("precognition.utils").add_multibyte_padding(cur_line, extra_padding, line_len)
utils.add_multibyte_padding(cur_line, extra_padding, line_len)

local virt_line = build_virt_line(virtual_line_marks, line_len, extra_padding)

Expand Down Expand Up @@ -415,17 +436,20 @@ function M.hide()
end

--- Toggle automatic showing of hints
--- with return value indicating the visible state
function M.toggle()
if visible then
M.hide()
else
M.show()
end
return visible
end

---@param opts Precognition.PartialConfig
function M.setup(opts)
config = vim.tbl_deep_extend("force", default, opts or {})
opts = opts or {}
config = vim.tbl_deep_extend("force", default, opts)
if opts.highlightColor then
config.highlightColor = opts.highlightColor
end
Expand Down
29 changes: 28 additions & 1 deletion lua/precognition/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ function M.char_class(char, big_word)
end

---@param bufnr? integer
---@param disabled_fts? string[]
---@return boolean
function M.is_blacklisted_buffer(bufnr)
function M.is_blacklisted_buffer(bufnr, disabled_fts)
bufnr = bufnr or vim.api.nvim_get_current_buf()
if vim.api.nvim_get_option_value("buftype", { buf = bufnr }) ~= "" then
return true
end

if disabled_fts == nil then
return false
end

for _, ft in ipairs(disabled_fts) do
if vim.api.nvim_get_option_value("filetype", { buf = bufnr }) == ft then
return true
end
end
return false
end

Expand Down Expand Up @@ -110,6 +121,22 @@ function M.create_pad_array(len, str)
return pad_array
end

---calculates the white space offset of a partial string
---@param hint vim.lsp.inlay_hint.get.ret
---@param tab_width integer
---@param current_line string
---@return integer
---@return integer
function M.calc_ws_offset(hint, tab_width, current_line)
-- + 1 here because of trailing padding
local length = #hint.inlay_hint.label[1].value + 1
local start = hint.inlay_hint.position.character
local prefix = vim.fn.strcharpart(current_line, 0, start)
local expanded = string.gsub(prefix, "\t", string.rep(" ", tab_width))
local ws_offset = vim.fn.strcharlen(expanded)
return length, ws_offset
end

---Add extra padding for multi byte character characters
---@param cur_line string
---@param extra_padding Precognition.ExtraPadding[]
Expand Down
7 changes: 7 additions & 0 deletions tests/precognition/blacklist_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ describe("blacklist buffers", function()
vim.api.nvim_open_term(test_buffer, {})
eq(utils.is_blacklisted_buffer(test_buffer), true)
end)

it("blacklisted buffer by filetype", function()
local test_buffer = vim.api.nvim_create_buf(true, false)
local test_fts = { "startify" }
vim.api.nvim_set_option_value("filetype", "startify", { buf = test_buffer })
eq(utils.is_blacklisted_buffer(test_buffer, test_fts), true)
end)
end)
42 changes: 9 additions & 33 deletions tests/precognition/e2e_spec.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
local precognition = require("precognition")
local tu = require("tests.precognition.utils.utils")
---@diagnostic disable-next-line: undefined-field
local eq = assert.are.same

local function get_gutter_extmarks(buffer)
local gutter_extmarks = {}
for _, extmark in
pairs(vim.api.nvim_buf_get_extmarks(buffer, -1, 0, -1, {
details = true,
}))
do
if extmark[4] and extmark[4].sign_name and extmark[4].sign_name:match(precognition.gutter_group) then
table.insert(gutter_extmarks, extmark)
end
end
return gutter_extmarks
end

local function hex2dec(hex)
hex = hex:gsub("#", "")
local r = tonumber("0x" .. hex:sub(1, 2))
local g = tonumber("0x" .. hex:sub(3, 4))
local b = tonumber("0x" .. hex:sub(5, 6))

local dec = (r * 256 ^ 2) + (g * 256) + b

return dec
end

describe("e2e tests", function()
before_each(function()
precognition.setup({})
Expand Down Expand Up @@ -67,7 +43,7 @@ describe("e2e tests", function()
details = true,
})

local gutter_extmarks = get_gutter_extmarks(buffer)
local gutter_extmarks = tu.get_gutter_extmarks(buffer)

for _, extmark in pairs(gutter_extmarks) do
if extmark[4].sign_text == "G " then
Expand Down Expand Up @@ -126,7 +102,7 @@ describe("e2e tests", function()
vim.api.nvim_win_set_cursor(0, { 2, 1 })
precognition.on_cursor_moved()

gutter_extmarks = get_gutter_extmarks(buffer)
gutter_extmarks = tu.get_gutter_extmarks(buffer)

extmarks = vim.api.nvim_buf_get_extmark_by_id(buffer, precognition.ns, precognition.extmark, {
details = true,
Expand All @@ -151,7 +127,7 @@ describe("e2e tests", function()

vim.api.nvim_win_set_cursor(0, { 4, 1 })
precognition.on_cursor_moved()
gutter_extmarks = get_gutter_extmarks(buffer)
gutter_extmarks = tu.get_gutter_extmarks(buffer)

for _, extmark in pairs(gutter_extmarks) do
if extmark[4].sign_text == "G " then
Expand Down Expand Up @@ -187,7 +163,7 @@ describe("e2e tests", function()
details = true,
})

local gutter_extmarks = get_gutter_extmarks(buffer)
local gutter_extmarks = tu.get_gutter_extmarks(buffer)

for _, extmark in pairs(gutter_extmarks) do
if extmark[4].sign_text == "G " then
Expand Down Expand Up @@ -221,7 +197,7 @@ describe("e2e tests", function()
local background = "#00ff00"
local foreground = "#ff0000"
local customColor = { foreground = foreground, background = background }
local customMark = { fg = hex2dec(foreground), bg = hex2dec(background) }
local customMark = { fg = tu.hex2dec(foreground), bg = tu.hex2dec(background) }
precognition.setup({ highlightColor = customColor })
local buffer = vim.api.nvim_create_buf(true, false)
vim.api.nvim_set_current_buf(buffer)
Expand All @@ -240,7 +216,7 @@ describe("e2e tests", function()
details = true,
})

local gutter_extmarks = get_gutter_extmarks(buffer)
local gutter_extmarks = tu.get_gutter_extmarks(buffer)

for _, extmark in pairs(gutter_extmarks) do
if extmark[4].sign_text == "G " then
Expand Down Expand Up @@ -297,7 +273,7 @@ describe("Gutter Priority", function()

precognition.on_cursor_moved()

local gutter_extmarks = get_gutter_extmarks(testBuf)
local gutter_extmarks = tu.get_gutter_extmarks(testBuf)

for _, extmark in pairs(gutter_extmarks) do
eq(true, extmark[4].sign_text ~= "G ")
Expand Down Expand Up @@ -326,7 +302,7 @@ describe("Gutter Priority", function()

precognition.on_cursor_moved()

local gutter_extmarks = get_gutter_extmarks(testBuf)
local gutter_extmarks = tu.get_gutter_extmarks(testBuf)

eq(1, vim.tbl_count(gutter_extmarks))
eq("gg", gutter_extmarks[1][4].sign_text)
Expand Down
8 changes: 8 additions & 0 deletions tests/precognition/horizontal_motions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,12 @@ describe("edge case", function()
eq(3, hm.prev_word_boundary(str, 18, len, true))
eq(3, hm.prev_word_boundary(str, 5, len, false))
end)

it("quoted strings", function()
local str = 'this = "that"'
eq(8, hm.end_of_word(str, 6, #str, false))

str = 'b = "^", c = 2 },'
eq(8, hm.end_of_word(str, 3, #str, false))
end)
end)
Loading

0 comments on commit a2f6d11

Please sign in to comment.