Skip to content

Commit

Permalink
Merge branch 'main' into inlay_hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tris203 authored Jul 31, 2024
2 parents b927107 + 2a566f0 commit fbcab75
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
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
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
9 changes: 8 additions & 1 deletion lua/precognition/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,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 @@ -82,6 +83,9 @@ local default = {
PrevParagraph = { text = "{", prio = 8 },
NextParagraph = { text = "}", prio = 8 },
},
disabled_fts = {
"startify",
},
}

---@type Precognition.Config
Expand Down Expand Up @@ -425,17 +429,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
13 changes: 12 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
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)
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)

0 comments on commit fbcab75

Please sign in to comment.