diff --git a/README.md b/README.md index 8f4821f..906b919 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ return { -- PrevParagraph = { text = "{", prio = 8 }, -- NextParagraph = { text = "}", prio = 8 }, -- }, + -- disabled_fts = { + -- "startify", + -- }, }, } ``` @@ -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]()) +- `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. @@ -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 diff --git a/lua/precognition/horizontal_motions.lua b/lua/precognition/horizontal_motions.lua index 8cb1133..e4e858d 100644 --- a/lua/precognition/horizontal_motions.lua +++ b/lua/precognition/horizontal_motions.lua @@ -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) diff --git a/lua/precognition/init.lua b/lua/precognition/init.lua index 7e6000c..79e31fd 100644 --- a/lua/precognition/init.lua +++ b/lua/precognition/init.lua @@ -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 @@ -82,6 +83,9 @@ local default = { PrevParagraph = { text = "{", prio = 8 }, NextParagraph = { text = "}", prio = 8 }, }, + disabled_fts = { + "startify", + }, } ---@type Precognition.Config @@ -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 diff --git a/lua/precognition/utils.lua b/lua/precognition/utils.lua index 84059ba..26b6505 100644 --- a/lua/precognition/utils.lua +++ b/lua/precognition/utils.lua @@ -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 diff --git a/tests/precognition/blacklist_spec.lua b/tests/precognition/blacklist_spec.lua index 94d4810..7853bf0 100644 --- a/tests/precognition/blacklist_spec.lua +++ b/tests/precognition/blacklist_spec.lua @@ -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) diff --git a/tests/precognition/horizontal_motions_spec.lua b/tests/precognition/horizontal_motions_spec.lua index 42877ff..7d0e0c9 100644 --- a/tests/precognition/horizontal_motions_spec.lua +++ b/tests/precognition/horizontal_motions_spec.lua @@ -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)