roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Edit → Format document replaces tabs with spaces when `indent_style = tab`

Open Youssef1313 opened this issue 2 years ago • 4 comments

Version Used:

17.7 Preview 1

Steps to Reproduce:

Create a project with indent_style = tab in .editorconfig. Note that Edit → Format document changes tabs to spaces. This looks like a recent regression.

The formatting analyzer then correctly issues a diagnostic that the code isn't correctly formatted. So "Format document" and "Formatting analyzer" are going against each other.

Youssef1313 avatar Jun 21 '23 07:06 Youssef1313

This is a transient/intermittent issue. I no longer reproduce after re-starting VS.

Closing for now. If it happened again, I'll re-open.

Youssef1313 avatar Jun 21 '23 09:06 Youssef1313

I have seen this again. But no repro steps since it's random with very low rate.

Youssef1313 avatar Jul 15 '23 15:07 Youssef1313

Hit it again today as well.

There was a similar issue but with a different feature previously https://github.com/dotnet/roslyn/issues/57323, which also reproduced randomly. Not sure if that would be related.

Youssef1313 avatar Sep 18 '23 04:09 Youssef1313

Hi, I have found a way to reliably reproduce this.

Steps:

  1. Tools -> Import and Export Settings -> Reset All Settings -> Pick Visual C#
  2. Edit -> Advanced -> Enable "View Whitespace"
  3. Tools -> Options -> Text Editor -> C# -> Tabs
  • Tab size - set to 8
  • Indent size - set to 8
  • Select "Keep Tabs"
  1. Put the following in an .editorconfig
root = true

[*]
# (Please don't specify an indent_size here; that has too many unintended consequences.)
charset = utf-8-bom

# Code files
[*.{cs,csx,vb,vbx}]
indent_size = 4
indent_style = tab
  1. Put the following in Program.cs
namespace Foobar;

internal class Program
{
	private enum FooBars
	{
		Foo,
		Bar,
	}

	static void Main(string[] args)
	{
		FooBars switchThis = FooBars.Foo;

		string returningThis = switchThis switch
		{
			FooBars.Foo => "Foo",
		};
	}
}
  1. Right click on "switch" and select "Add Both"
  2. The initial switch item and the added ones will all use spaces.

Notes: Removing indent_size = 4 from the editorconfig results in tabs being used. Having Tab size and Indent size in the settings as 4 results in tabs being used.

thanks Mike

MrMikeJJ avatar Aug 27 '24 18:08 MrMikeJJ