Skip to content

Commit

Permalink
Only show indent guides for current block (lukas-reineke#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kongsgaard authored and Daniel Kongsgaard committed Oct 10, 2023
1 parent b808248 commit 9e5c282
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
24 changes: 16 additions & 8 deletions doc/indent_blankline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CONTENTS *ibl* *indent-blankline*

==============================================================================
2. FUNCTIONS *ibl.functions*

setup({config}) *ibl.setup()*

Initializes and configures indent-blankline.
Expand Down Expand Up @@ -224,7 +224,7 @@ config.viewport_buffer *ibl.config.viewport_buffer*
Minimum number of lines above and below of what is currently
visible in the window for which indentation guides will
be generated

Default: `30` ~

*ibl.config.viewport_buffer.max*
Expand Down Expand Up @@ -322,6 +322,13 @@ config.indent *ibl.config.indent*

Default: `1` ~

*ibl.config.indent.current_block_only*
• {current_block_only}(number)
Only hightlight the current block of code

Default: `false` ~


Example: ~
>lua
{
Expand All @@ -330,6 +337,7 @@ config.indent *ibl.config.indent*
highlight = { "Function", "Label" },
smart_indent_cap = true,
priority = 2,
current_block_only = true,
}
<

Expand Down Expand Up @@ -379,7 +387,7 @@ config.scope *ibl.config.scope*
def foo();
┋ if True:
┋ a = "foo █ar"
┋ # ↳ cursor here
┋ # ↳ cursor here
┋ print(a)
<
In Rust on the other hand, `if` blocks are a new scope. Variables defined
Expand All @@ -390,7 +398,7 @@ config.scope *ibl.config.scope*
fn foo() {
if true {
┋ let a = "foo █ar";
┋ // ↳ cursor here
┋ // ↳ cursor here
}
print(a);
}
Expand Down Expand Up @@ -873,19 +881,19 @@ IblScope *hl-IblScope*

The MIT Licence
http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2023 Lukas Reineke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down
2 changes: 2 additions & 0 deletions lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ M.default_config = {
highlight = "IblIndent",
smart_indent_cap = true,
priority = 1,
current_block_only = false,
},
whitespace = {
highlight = "IblWhitespace",
Expand Down Expand Up @@ -132,6 +133,7 @@ local validate_config = function(config)
highlight = { config.indent.highlight, { "string", "table" }, true },
smart_indent_cap = { config.indent.smart_indent_cap, "boolean", true },
priority = { config.indent.priority, "number", true },
current_block_only = { config.indent.current_block_only, "boolean", true },
}, config.indent, "ibl.config.indent")
if config.indent.char then
vim.validate {
Expand Down
4 changes: 4 additions & 0 deletions lua/ibl/config.types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
---@field smart_indent_cap boolean?
--- Virtual text priority for the indentation guide
---@field priority number?
--- Only show indentation guide for the current block
---@field current_block_only boolean?

---@class ibl.config.whitespace
--- Highlight group, or list of highlight groups, that get applied to the whitespace
Expand Down Expand Up @@ -154,6 +156,8 @@
---@field smart_indent_cap boolean
--- Virtual text priority for the indentation guide
---@field priority number
--- Only show indentation guide for the current block
---@field current_block_only boolean

---@class ibl.config.full.whitespace: ibl.config.whitespace
--- Highlight group, or list of highlight groups, that get applied to the whitespace
Expand Down
39 changes: 39 additions & 0 deletions lua/ibl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,45 @@ M.refresh = function(bufnr)
end
end

if config.indent.current_block_only then
if not scope then
scope = scp.get(bufnr, config)
end
if scope then
local current_block = scope:root()
local parent_block = scope
if current_block == parent_block then
-- if we are in the root, don't show indents
for i, _ in ipairs(lines) do
line_skipped[i] = true
end
end
while parent_block do
-- go up the tree until we are one step below the root
if parent_block:parent() == current_block then
current_block = parent_block
break
else
parent_block = parent_block:parent()
end
end
local block_start_row, _, block_end_row, _ = current_block:range()
block_start_row, block_end_row = block_start_row + 1, block_end_row + 1
for i, _ in ipairs(lines) do
local row = i + offset
if block_start_row > row or block_end_row < row then
-- skip lines not in the current block
line_skipped[i] = true
end
end
else
-- if we can't get scope, then don't show indents anywhere
for i, _ in ipairs(lines) do
line_skipped[i] = true
end
end
end

for i, line in ipairs(lines) do
local row = i + offset
if line_skipped[i] then
Expand Down

0 comments on commit 9e5c282

Please sign in to comment.