Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: current indent #743

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

feat: current indent #743

wants to merge 2 commits into from

Conversation

lukas-reineke
Copy link
Owner

@Danielkonge I finally had some proper time to go over your PR.

Like I mentioned before, I did some pretty extensive refactor. Your code was good, but there were things I wanted to do a bit differently.

  1. Instead of having a new current_indent_stack, I reused indent_state
  2. I wasn't able to explain this properly, but I change the behavior of show_end for current indent to match scope better
  3. Some bugfixes, for example inlay hints
  4. I added exclude and hooks for current indent

Can you take a look at this? It's very likely I broke something that was working in your version.

@lukas-reineke lukas-reineke self-assigned this Oct 20, 2023
Co-authored-by: Lukas Reineke <[email protected]>
Co-authored-by: Daniel Kongsgaard <[email protected]>
Copy link
Contributor

@Danielkonge Danielkonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have written some comments now. I will try to use this a bit and go through the code more thoroughly to see if notice anything else.

doc/indent_blankline.txt Outdated Show resolved Hide resolved
doc/indent_blankline.txt Outdated Show resolved Hide resolved
doc/indent_blankline.txt Outdated Show resolved Hide resolved
doc/indent_blankline.txt Outdated Show resolved Hide resolved
lua/ibl/init.lua Outdated Show resolved Hide resolved
lua/ibl/utils.lua Outdated Show resolved Hide resolved
lua/ibl/hooks.lua Outdated Show resolved Hide resolved
@Danielkonge
Copy link
Contributor

Danielkonge commented Oct 20, 2023

Screenshot 2023-10-20 at 13 50 53

In my files with spaces for tabs I haven't noticed any problems, but when using actual tabs, current_indent has some problems (see picture).

The problem seems to be when you go back multiple indent levels at once.

@lukas-reineke
Copy link
Owner Author

yeah... no this doesn't work
it's completely broken with tabs or without smart_indent_cap when the indent changes by more than one

I'll try to rewrite it again next week

@lukas-reineke lukas-reineke marked this pull request as draft October 20, 2023 13:06
@Danielkonge
Copy link
Contributor

Just a clarifying note: I noticed that it is actually going multiple indents in at once that is the problem. Not going multiple indents back.

@@ -46,6 +49,9 @@ local setup_builtin_hl_groups = function()
if not_set(get(ibl_scope_hl_name)) then
vim.api.nvim_set_hl(0, ibl_scope_hl_name, line_nr_hl)
end
if not_set(get(ibl_current_indent_hl_name)) then
vim.api.nvim_set_hl(0, ibl_current_indent_hl_name, line_nr_hl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the highlights from "CursorLineNr" (or maybe just white) makes more sense here as the default highlight for current_indent. I know it might not be set for everyone, but we could use this as a backup then instead. This would (when it is set) make it easier to distinguish between scope and current_indent for people trying out both.

Alternatively, it might be a better idea to just add an example with "CursorLineNr" as the highlight group for current_indent in the docs, since I feel like that might be a common highlight choice.

Comment on lines +407 to +410
if indent_state.stack[j].indent < current_indent_col_start_single then
break
end
current_indent.start_row = indent_state.stack[j].row + 1
Copy link
Contributor

@Danielkonge Danielkonge Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if indent_state.stack[j].indent < current_indent_col_start_single then
break
end
current_indent.start_row = indent_state.stack[j].row + 1
current_indent.start_row = indent_state.stack[j].row + 1
if indent_state.stack[j].indent <= current_indent_col_start_single then
break
end

This partially fixes the problem with multiple indents for me (I will make another note to fix the other problem).

and config.scope.priority >= config.current_indent.priority
)
then
vim.api.nvim_buf_set_extmark(bufnr, namespace, row - 1, current_indent_col_start_single, {
Copy link
Contributor

@Danielkonge Danielkonge Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vim.api.nvim_buf_set_extmark(bufnr, namespace, row - 1, current_indent_col_start_single, {
vim.api.nvim_buf_set_extmark(bufnr, namespace, row - 1, #whitespace, {

We need to start highlights after the whitespace here (any highlights before the whitespace ends is already taken care of in the virtual text overlay). [Note: The code before the suggestion doesn't work with tabs since it depends on the whitespace_tbl instead of whitespace.]

Together with the above fix, this fixes the multiple indent level jumps problem for me (based on my testing so far).

@Danielkonge
Copy link
Contributor

Screenshot 2023-10-20 at 15 48 01

The two suggested fixes managed to fix the above problem for me (but I need to test it more).

The scope is *not* the current indentation level!
See |ibl.config.current_indent| if you want to highlight the current
indentation.
Instead, scope it is the indentation level where variables or functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Instead, scope it is the indentation level where variables or functions
Instead, scope is the indentation level where variables or functions

@@ -131,6 +155,8 @@
---@field whitespace ibl.config.full.whitespace: ibl.config.whitespace
--- Configures the scope
---@field scope ibl.config.full.scope: ig.config.scope
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---@field scope ibl.config.full.scope: ig.config.scope
---@field scope ibl.config.full.scope: ibl.config.scope

@Danielkonge
Copy link
Contributor

Danielkonge commented Oct 21, 2023

Another small note: While doing a lot of undos in a larger file, I got the following error:

Error executing lua callback: ...nal/nvim-plugins/indent-blankline.nvim/lua/ibl/utils.lua:264: attempt to perform arithmetic on a nil value

[Update: I figured out a way to reproduce the problem, and it was not with undos. I have submitted a fix below.]

@Danielkonge
Copy link
Contributor

Danielkonge commented Oct 24, 2023

Small note:
I now have this problem, where current_indent.show_start shows the start above an empty line at the end of the file (only if the empty line is the last line of the file). If you haven't looked at this by tomorrow, I will try to get back with a fix for it by then.

Screenshot 2023-10-25 at 00 14 50

Update: See the comment below for a solution.

Copy link
Contributor

@Danielkonge Danielkonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub won't let me put this comment in the code for some reason, but there is a problem at line 328 of init.lua.

            if i == #lines then
                whitespace_tbl = {} 

we need to do something with the indent_state too, otherwise current_indent.show_start will mistakenly highlight the line above the last line of the code (if the last line is empty).

            if i == #lines then
                whitespace_tbl, indent_state = indent.get(whitespace, indent_opts, indent_state, row)

works, but I am not sure if that is the best way to do it. Maybe it is better to set it explicitly. Will the last line ever be visible if it is not the last line of the file? If not, we could just set indent_state.stack = {{ indent = 0, row = row }} in that if statement.

Copy link
Contributor

@Danielkonge Danielkonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a fix to a problem giving the following error.

Error executing lua callback: ...nal/nvim-plugins/indent-blankline.nvim/lua/ibl/utils.lua:264: attempt to perform arithmetic on a nil value

local end_pos_col

if end_line[1] then
end_pos = vim.inspect_pos(bufnr, end_row, end_line[1]:find "%S" - 1, {
Copy link
Contributor

@Danielkonge Danielkonge Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end_pos = vim.inspect_pos(bufnr, end_row, end_line[1]:find "%S" - 1, {
local end_pos_find = #end_line[1] - 1
local try_find = end_line[1]:find "%S"
if try_find then
end_pos_find = try_find - 1
end
end_pos = vim.inspect_pos(bufnr, end_row, end_pos_find, {

The current code causes problems when find returns nil sometimes.

@Danielkonge
Copy link
Contributor

Danielkonge commented Nov 9, 2023

Just a quick update. I have used a fork of this with the fixes I describe above for a couple of weeks now. I have been editing files with both tabs and spaces for indentation, and I haven't noticed any other problems.

@mehalter
Copy link
Contributor

mehalter commented Nov 9, 2023

@Danielkonge could you link the fork you have been using? I would be interested in giving it a try!

@Danielkonge
Copy link
Contributor

@Danielkonge could you link the fork you have been using? I would be interested in giving it a try!

It's this fork: https://github.com/Danielkonge/indent-blankline.nvim/tree/current_indent_final

Note that this doesn't include any of the fixes that has been committed to the master branch in the last couple of weeks. This is mainly to test out current_indent.

Other than some comments the only real difference between the fork and this pull request is on lines: 328, 407-410 and 573 in init.lua and then lines 270-275 in utils.lua.

@mehalter
Copy link
Contributor

mehalter commented Nov 9, 2023

No problem! I am just interested in investigating the current_indent feature. AstroNvim is about to do a major version release and I'm looking to see if we can wait for this PR and drop the need for mini.indentscope for highlighting the current indent level. Thanks so much for all your work for this! I've been following your development and PRs leading up to this :)

Copy link
Contributor

@Danielkonge Danielkonge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one more issue. I think this should fix it.

The previous code had this behavior:
Screenshot 2023-11-12 at 23 08 38

Screenshot 2023-11-12 at 23 08 53

My change gives this behavior:

Screenshot 2023-11-12 at 23 09 23 Screenshot 2023-11-12 at 23 09 41

current_indent_enabled
and config.current_indent.show_end
and is_current_indent_end
and #whitespace_tbl > current_indent_col_start_single
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and #whitespace_tbl > current_indent_col_start_single
and current_indent.start_row <= row
and #whitespace_tbl > current_indent_col_start_single

RayJameson added a commit to RayJameson/astronvim_config that referenced this pull request Dec 4, 2023
RayJameson added a commit to RayJameson/astronvim_config that referenced this pull request Dec 4, 2023
@lukas-reineke
Copy link
Owner Author

Just a quick update, I am still planning on merging this. But it needs more polish, and I am very short on time at the moment.

@mehalter
Copy link
Contributor

mehalter commented Dec 6, 2023

Thanks for the update, looking forward to this improvement! Appreciate all your work on this!

@Danielkonge

This comment was marked as resolved.

@lukas-reineke
Copy link
Owner Author

@Danielkonge Sounds good, but please make it a separate PR.

@mehalter
Copy link
Contributor

mehalter commented Sep 6, 2024

Hey! Just wanted to check in and see if this PR is still planned on getting merged at some point :)

@lukas-reineke
Copy link
Owner Author

It is still planned. Unfortunately, my situation hasn't changed much. This year has been super busy, I just don't have that much time for open source.
New feature development like this will have to wait a bit more.

@jdelgadofrts
Copy link

For whatever is worth, this would be an amazing feature to add. I love the notion of just having guideline essentially highlighted for the current indentation.

It solves a lot of visual noise that can happen, specially around rainbow guidelines.

Open source is pretty ungrateful, so regardless, this is an amazing plugin, and thank you for your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants