microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.42k stars 29.34k forks source link

Make distinction when removing leading whitespace from trailing whitespace #113235

Open thiagomajesk opened 3 years ago

thiagomajesk commented 3 years ago

Hi, I've noticed that there's only the files.trimTrailingWhitespace without a files.trimLeadingWhitespace counterpart. This has been a problem for editing files that mix markdown documentation with another syntax for a while.

For example, Elixir files can have markdown documentation using the @doc attribute. However, If I enable the trim trailing whitespace option, this removes both trailing and leading whitespace on the lines, affecting markdown line breaks:

@doc """
My function documentation markdown.
This will be part of the first line because the "line break" was removed when saved
"""
def hello(), do: :world

This has been a constant headache because we enforce the removal of unnecessary leading whitespaces from source code, but we can't use the options to remove it all, thus affecting our documentation.

If possible, I'd like to be able to remove only leading or trailing whitespace (for this particular case, removing only leading whitespace so markdown docs don't get butchered).

Update 06/03/2021: After discussing this issue in-depth in this forum thread, I noticed that perhaps the phrasing/ semantics of the issue is not perfect enough. I don't know for sure what would be the correct solution, but I hope that I could make the problem we are facing relatable. So, thanks for everyone that helped so far!

vscodebot[bot] commented 3 years ago

(Experimental duplicate detection) Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

Nicd commented 3 years ago

For me, leading whitespace is not trimmed inside @doc annotation or for that matter anywhere else that I can see. Looking at the forum thread, I cannot see examples of leading whitespace being trimmed. But the trailing whitespace inside @doc should not be trimmed. In fact, this also happens for plain string literals, changing their contents!

ElixirLS extension should use mix format for formatting, but that doesn't trim trailing whitespace inside strings (like the @doc annotation). So I wonder if the extension should mark string literals in a certain way to protect them from have their trailing whitespace trimmed.

But I did a quick test and this also happens for Python with the Python extension by Microsoft. So maybe a more correct issue title should be "files.trimTrailingWhitespace trims trailing whitespace inside string literals."?

Nicd commented 3 years ago

Here it is commented that trailing whitespace trim is language agnostic, so it will trim indiscriminately inside string literals also: https://github.com/microsoft/vscode/issues/52711#issuecomment-400244965