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

Bad indentation of scope for multi-line function definition #752

Closed
sfc-gh-mjendruk opened this issue Oct 23, 2023 · 9 comments · Fixed by #795
Closed

Bad indentation of scope for multi-line function definition #752

sfc-gh-mjendruk opened this issue Oct 23, 2023 · 9 comments · Fixed by #795
Labels
bug Something isn't working

Comments

@sfc-gh-mjendruk
Copy link

Problem

I noticed an indentation issue of the scope highlighting when viewing C++ files. However I saw the same issue also in Java files so I assume it's a general issue.

My project puts the opening brace { in the same line as the if-clause, function parameters, etc. If the parameter list spans multiple lines then it uses the wrong indentation to display the scope (see images).

image image

Steps to reproduce

  • This is using indent-blankline v3.
  • Scopes must be enabled.
  • Create a C++ file and a function with a multi-line parameter list as shown in the images.
  • Move the cursor inside the function

Expected behavior

It should instead highlight the leftmost line in case of the function scope and in case of the if-clause, it should highlight the 2nd line, aligning with the indentation of the if.

Neovim version (nvim -v)

0.9.1

@sfc-gh-mjendruk sfc-gh-mjendruk added the bug Something isn't working label Oct 23, 2023
@Danielkonge
Copy link
Contributor

If you want to keep the meaning of scope, I don't think it should just highlight the first line in the first picture or the second line in the second picture (assuming the scope is within the curly braces?). I guess you would want it to highlight lines 216-219 in the first picture and all but the top row of the second line in the second picture. I don't think this would look very good though.

I think your formatting style just doesn't really work well together with scope, and it might be better to just use current indent for those files when it is ready.

@lukas-reineke
Copy link
Owner

Yeah, your formatting is just very weird. The scope is correct, it starts with the curly braces. It just doesn't look correct because of your formatting.

There isn't really anything I can do.

@sfc-gh-mjendruk
Copy link
Author

Hello, thank you for your responses.

I attached two more images that visualize in red how where I think the scope highlighting should be placed. You're right that the scope is identified correctly, it is just the placement which is not ideal.

277366300-cd0faf7f-7965-49dd-90ba-64e4f76b34cb 277368726-141e237f-c8a1-4470-8193-2086f35e2765

The code formatting actually doesn't really matter as along as the function parameters are placed on separate lines, which is really really common for functions with long parameter names if you want to adhere to any kind of maximum line length. In the example this is of course not really necessary because the parameter types are short. Putting the opening brace on the same line is also really common in C++, Java, and probably other languages too. Nothing weird about that.

Here is a different parameter formatting which shows the same issue:
image

Here is where I think it should be placed.
277614435-e4bdcb8f-13de-4833-b438-32f88b986ed9

Here is a Java example showing the same issue:

image

To me it looks like the scope indentation is simply based on the line of the opening brace. My idea for a possible fix would be to determine the indentation based on the closing brace instead? This could be configured using an additional option. I think this would work for most C-like languages and formatting styles.

@lukas-reineke
Copy link
Owner

Fair, basing the scope indent on the end of the scope would make this look better.

But the way the scope is currently implemented, this is not possible. It would require a lot of refactoring.
I don't think I will work on this, PRs are always welcome.

@Danielkonge
Copy link
Contributor

I have run into what seems to be the same problem. It seems that the problem is caused by the newline in the function or if statement (+ the way the C language tree works). Here are some pictures showing how different formatting doesn't give the problem, but a newline in the if statement and { at the end of the line doesn't work.

Screenshot 2023-11-08 at 16 57 23 Screenshot 2023-11-08 at 16 54 25 Screenshot 2023-11-08 at 16 57 07

I think I have an idea for how to solve this after #743 is committed, so I can look at it at that time.

@Danielkonge
Copy link
Contributor

@sfc-gh-mjendruk #795 fixes this for my test cases (above pictures). Could you check if it highlights as you wanted too? (I think the red line goes up one more line than your pictures, because it starts highlighting on the line with { which is what starts the scope [this can be seen by showing the exact scope start].)

@sfc-gh-mjendruk
Copy link
Author

Hello, thanks for your work. It got a little better, but it's still not entirely as I would expect it:

There is now no visible scope line in the following case. Previously, the scope line would be aligned with int var3) {

image

If I add something whose indentation level is higher than int var3) { then the scope line appears.

image

@Danielkonge
Copy link
Contributor

I cannot reproduce the highlighting you get:

Screenshot 2023-12-21 at 17 03 47

Can you give me a few more details. Are you indenting with spaces or tabs? The above is c++ right? What is your setup for indent-blankline?

@sfc-gh-mjendruk
Copy link
Author

Hi, I tried out v3.5.1 again, and now it looks like in your screenshot. Not sure what was wrong before, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants