Skip to content

Commit

Permalink
Add SCOPE_ACTIVE hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-reineke committed Sep 11, 2023
1 parent 80ca794 commit 8e577bc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
15 changes: 15 additions & 0 deletions doc/indent_blankline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ hooks.type *ibl.hooks.type*

Variants: ~
{ACTIVE}
• {SCOPE_ACTIVE}
• {SKIP_LINE}
{WHITESPACE}
• {VIRTUAL_TEXT}
Expand Down Expand Up @@ -531,6 +532,20 @@ hooks.cb.active({bufnr}) *ibl.hooks.cb.active()*
(boolean)


hooks.cb.scope_active({bufnr}) *ibl.hooks.cb.scope_active()*

Callback function for the |ibl.hooks.type|.SCOPE_ACTIVE hook.

Gets called before refreshing indent-blankline for a buffer.
If the callback returns false, |ibl.config.scope| will be disabled.

Parameters: ~
{bufnr} (number) Buffer number

Return: ~
(boolean)


hooks.cb.skip_line({tick}, {bufnr}, {row}, {line}) *ibl.hooks.cb.skip_line()*

Callback function for the |ibl.hooks.type|.SKIP_LINE hook.
Expand Down
7 changes: 7 additions & 0 deletions lua/ibl/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local M = {}
---@enum ibl.hooks.type
M.type = {
ACTIVE = "ACTIVE",
SCOPE_ACTIVE = "SCOPE_ACTIVE",
SKIP_LINE = "SKIP_LINE",
WHITESPACE = "WHITESPACE",
VIRTUAL_TEXT = "VIRTUAL_TEXT",
Expand All @@ -21,6 +22,7 @@ local default_opts = {

local hooks = {
[M.type.ACTIVE] = {},
[M.type.SCOPE_ACTIVE] = {},
[M.type.SKIP_LINE] = {},
[M.type.WHITESPACE] = {},
[M.type.VIRTUAL_TEXT] = {},
Expand All @@ -31,6 +33,7 @@ local hooks = {
local count = 0

---@alias ibl.hooks.cb.active fun(bufnr: number): boolean
---@alias ibl.hooks.cb.scope_active fun(bufnr: number): boolean
---@alias ibl.hooks.cb.skip_line fun(tick: number, bufnr: number, row: number, line: string): boolean
---@alias ibl.hooks.cb.whitespace fun(tick: number, bufnr: number, row: number, whitespace: ibl.indent.whitespace[]): ibl.indent.whitespace[]
---@alias ibl.hooks.cb.virtual_text fun(tick: number, bufnr: number, row: number, virt_text: ibl.virtual_text): ibl.virtual_text
Expand All @@ -44,6 +47,7 @@ local count = 0
---@param cb function
---@param opts ibl.hooks.options
---@overload fun(type: 'ACTIVE', cb: ibl.hooks.cb.active, opts: ibl.hooks.options?): string
---@overload fun(type: 'SCOPE_ACTIVE', cb: ibl.hooks.cb.scope_active, opts: ibl.hooks.options?): string
---@overload fun(type: 'SKIP_LINE', cb: ibl.hooks.cb.skip_line, opts: ibl.hooks.options?): string
---@overload fun(type: 'WHITESPACE', cb: ibl.hooks.cb.whitespace, opts: ibl.hooks.options?): string
---@overload fun(type: 'VIRTUAL_TEXT', cb: ibl.hooks.cb.virtual_text, opts: ibl.hooks.options?): string
Expand Down Expand Up @@ -76,6 +80,7 @@ M.register = function(type, cb, opts)
if not hooks.buffer_scoped[bufnr] then
hooks.buffer_scoped[bufnr] = {
[M.type.ACTIVE] = {},
[M.type.SCOPE_ACTIVE] = {},
[M.type.SKIP_LINE] = {},
[M.type.WHITESPACE] = {},
[M.type.VIRTUAL_TEXT] = {},
Expand Down Expand Up @@ -108,6 +113,7 @@ end
M.clear_all = function()
hooks = {
[M.type.ACTIVE] = {},
[M.type.SCOPE_ACTIVE] = {},
[M.type.SKIP_LINE] = {},
[M.type.WHITESPACE] = {},
[M.type.VIRTUAL_TEXT] = {},
Expand All @@ -122,6 +128,7 @@ end
---@param bufnr number
---@param type ibl.hooks.type
---@overload fun(bufnr: number, type: 'ACTIVE'): ibl.hooks.cb.active[]
---@overload fun(bufnr: number, type: 'SCOPE_ACTIVE'): ibl.hooks.cb.scope_active[]
---@overload fun(bufnr: number, type: 'SKIP_LINE'): ibl.hooks.cb.skip_line[]
---@overload fun(bufnr: number, type: 'WHITESPACE'): ibl.hooks.cb.whitespace[]
---@overload fun(bufnr: number, type: 'VIRTUAL_TEXT'): ibl.hooks.cb.virtual_text[]
Expand Down
13 changes: 12 additions & 1 deletion lua/ibl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,19 @@ M.refresh = function(bufnr)
end

local offset = math.max(top_offset - 1 - config.viewport_buffer.min, 0)

local scope_disabled = false
for _, fn in
pairs(hooks.get(bufnr, hooks.type.SCOPE_ACTIVE) --[[ @as ibl.hooks.cb.scope_active[] ]])
do
if not fn(bufnr) then
scope_disabled = true
break
end
end

local scope
if config.scope.enabled and is_current_buffer then
if not scope_disabled and config.scope.enabled then
scope = scp.get(bufnr, config)
if scope and scope:start() >= 0 then
offset = top_offset - math.min(top_offset - math.min(offset, scope:start()), config.viewport_buffer.max)
Expand Down
4 changes: 3 additions & 1 deletion lua/ibl/inlay_hints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ M.setup = function()
handler = vim.lsp.handlers["textDocument/inlayHint"]

vim.lsp.handlers["textDocument/inlayHint"] = function(err, result, ctx, conf)
handler(err, result, ctx, conf)
if handler then
handler(err, result, ctx, conf)
end
require("ibl").debounced_refresh(ctx.bufnr)
end
end
Expand Down
18 changes: 15 additions & 3 deletions lua/ibl/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ local utils = require "ibl.utils"
local scope_lang = require "ibl.scope_languages"
local M = {}

---@param win number
---@return table<number, number>
M.get_cursor_range = function()
local pos = vim.api.nvim_win_get_cursor(0)
M.get_cursor_range = function(win)
local pos = vim.api.nvim_win_get_cursor(win)
local row, col = pos[1] - 1, pos[2]
return { row, 0, row, col }
end
Expand Down Expand Up @@ -40,7 +41,18 @@ M.get = function(bufnr, config)
return nil
end

local range = M.get_cursor_range()
local win
if bufnr ~= vim.api.nvim_get_current_buf() then
local win_list = vim.fn.win_findbuf(bufnr)
win = win_list and win_list[1]
if not win then
return nil
end
else
win = 0
end

local range = M.get_cursor_range(win)
lang_tree = M.language_for_range(lang_tree, range, config)
if not lang_tree then
return nil
Expand Down

0 comments on commit 8e577bc

Please sign in to comment.