zed icon indicating copy to clipboard operation
zed copied to clipboard

Empty or entirely-whitespace lines break indentation markers

Open charbelnicolas opened this issue 11 months ago • 6 comments

Summary

Due to indentation markers relying on every line in a semantic block having the same indentation level, empty lines at the start or end of a block completely break them, leading to a broken/poor looking appearance. This is especially problematic when combined with the remove_trailing_whitespace_on_save setting, which will remove the indentation from otherwise empty lines that were there to ensure visual consistency.

Examples

Image Image

Zed Version and System Specs

Zed: v0.177.9 (Zed) OS: Linux Wayland arch unknown Memory: 31 GiB Architecture: x86_64 GPU: Intel(R) Graphics (RPL-S) || Intel open-source Mesa driver || Mesa 25.0.1-arch1.2

charbelnicolas avatar Mar 17 '25 22:03 charbelnicolas

A few more examples:

Image

Image

charbelnicolas avatar Mar 17 '25 22:03 charbelnicolas

Hey, although it may seem weird, this is actually the intended behavior. It is due to the fact that the empty lines do not have the same level of indentation as the other lines in the scope. Below is a screenshot where the top empty line isn't indented enough, but the last empty line has the same level of indentation as the line above (each dot is one space, can be enabled by adding "show_whitespaces": "all" to settings).

Image

The root issue here is that the indentation markers are not syntax-aware, i.e. are not inserted based on the {}, rather they are only computed based on the amount of leading whitespace on each line. I recommend making sure the "remove_trailing_whitespace_on_save" setting is set to false for the languages you care about.

If you think we should do something more complex and comprehensive, feel free to open up a discussion and we can go over the pros and cons of such an addition. Thanks!

probably-neb avatar Apr 14 '25 17:04 probably-neb

That is such a shame, no other editor behaves this way... I was actually waiting for this to be fixed to give zed a real try. I'll stick to neovim then. Thank you.

charbelnicolas avatar Apr 14 '25 21:04 charbelnicolas

By that logic I think the indentation lines shouldn't appear at all on empty lines then... it just looks broken...

charbelnicolas avatar Apr 14 '25 21:04 charbelnicolas

Looking back at this, I think I was a bit overeager claiming this was the correct behavior. While I agree Neovim is also a great editor, I think we can do better here because you're totally right - It just looks broken, even if there's a technical explanation for it

probably-neb avatar Apr 18 '25 17:04 probably-neb

This actually shouldn't be too complicated to improve, we just have to keep looking upwards/downwards until we hit a line that is less indented than the current block, and then say the entire region is indented, regardless of the lack of whitespace indentation on otherwise empty lines. Python, as usual will have to be special cased, as in this example, the lines between baz() and if qux: shouldn't have the indent marker for the if foo block. But that shouldn't be difficult to configure on a per-language basis.

if foo:
  bar()
  baz()



if qux:
  pass

probably-neb avatar Apr 18 '25 17:04 probably-neb

I'm afraid this was not properly fixed, I'm using version 191.5 and still seeing this issue.

charbel @ ~ $ zeditor -v
Zed 0.191.5 – /usr/lib/zed/zed-editor

Image

charbelnicolas avatar Jun 19 '25 00:06 charbelnicolas

@bennetbo Sorry for bothering you but should I open a new ticket? Or will you reopen this one? Or this something that will not be fixed anymore?

charbelnicolas avatar Jun 24 '25 03:06 charbelnicolas

Yeah feel free to open a new one. The original PR fixed issues with blank lines, but the case you are seeing is a bit different to what I was originally thinking about. Here the indent guides would need to be language specific (which we do not handle right now). E.g. the indent guides in your screenshot would be correct if this was Python, but when a language is used that is not whitespace dependent the indent guides should look different.

In VSCode you can see that they seem to special case this, e.g.: Language set to Rust

Image

Language set to Python

Image

bennetbo avatar Jun 24 '25 11:06 bennetbo

Alright I will open a new issue even though my initial report specifically stated javascript as the language but it got edited out.

Steps to trigger the problem:

Open any javascript file with a function that has at least an empty line in between the last line of code and a closing }

charbelnicolas avatar Jun 25 '25 18:06 charbelnicolas