dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.03k stars 4.03k forks source link

snippet pasting does not honor the indenting mode #4284

Open KakCAT opened 9 years ago

KakCAT commented 9 years ago

Hi,

when pasting a snippet when using VS2015 RTM, the snippet is formatted as if indenting mode would be set smart no matter what indenting mode is selected. (not happenning with snippets which starts with '#', but I suppose it's because of my current configuration)

To reproduce, use block indenting and type this code

using System;

namespace ns { $ }

put cursor at (and delete) $, then type any snippet (i.e. wde )

p.s. sorry if this does not belong to this project, I'm quite lost with visual studio

Pilchie commented 9 years ago

Is this different from VS2013's handling? Can you let us know what options you have set to non-default values, and what your expected behavior is?

KakCAT commented 9 years ago

Hi Pilchie,

configuration: reset to defaults, then select "Indenting: none". Keep tabs, and disable all autoformatting in options/Text Editor/C#/Formatting

Starting from this file setup: vs2013_image1

I press tab once.

The result in VS2013 is this vs2013_image2

The result in VS2015 is this: vs2015_image2

VS2015 apparently skips the setting "indenting=none" and uses "indenting=smart" instead.

EDIT: the "enum" snippet it's the default C# enum snippet provided by VS2013 and VS2015 (no changes made)

Pilchie commented 9 years ago

Thanks. We'll take a look, but as far as we know, you're the only C# developer in the world who uses Indent Style = none :smile:.

KakCAT commented 9 years ago

Hi Pilchie,

no, in fact I don't use Indent Style = none :)

My "real" problem is issue #4351 , VS2015 RTM has made the current smart indenting totally unusable for me.

I'm considering moving to style=block , but with style=block I found this issue. I just put this issue with style=none instead of style=block because it was more obvious. (the problem is present in both modes)

conniey commented 9 years ago

Another customer scenario to verify is that “trivia” (whitespace/comments/preprocessor directives) is not formatted as well.

Expected

    class Test
    {
/* Test Format */
        public int MyProperty { get; set; }
    /* End Format */
    }

Actual:

    class Test
    {
        /* Test Format */
        public int MyProperty { get; set; }
                                           /* End Format */
    }
KakCAT commented 8 years ago

Hi,

any news on this issue? This has not been fixed in update 1. Additionally there's another problem that not only macros expanded have the incorrect indentation, but also reformat the following lines.

Using my snippet for comments: mycomment.snippet.txt

img1

Press tab to expand the snippet

img2

Everything messed up.

Proven that autoformatting is broken unless you adhere to VS2015 forced formatting standards, wouldn't be wise to add an option to totally disable autoformatting? I end up using more time reformatting code in VS2015 than actually typing it, to the point that I'm editing code in VS2012 and switching to VS2015 to compile for windows UWP.

shaggygi commented 4 years ago

I came across this today and appears indentation is still not working with Visual Studio 2019.

CyrusNajmabadi commented 4 years ago

we would likely take a contribution here. But it's unlikely that we would invest in this ourselves in the near future.

KirillOsenkov commented 4 years ago

This issue is potentially related to https://github.com/dotnet/roslyn/issues/37292.

Basically what I think is happening is when the IDE generates some code into the buffer, like during snippet application or pressing enter inside { } (I forgot which feature is responsible for this), but we don't respect the line endings and indentation set in the TextView.

For example if you have a file that uses LF as line endings and you apply a snippet there you will get CRLF where the IDE inserted a line break:

image

KirillOsenkov commented 4 years ago

Ah, I see, it just hardcodes Environment.NewLine instead of getting the line break character from the Editor settings: http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp.EditorFeatures/AutomaticCompletion/Sessions/CurlyBraceCompletionSession.cs,71

KirillOsenkov commented 4 years ago

No, it's not that easy, it does call formatting after that, so needs further investigation on why the formatting doesn't use the right line break and indentation.

KirillOsenkov commented 4 years ago

The interesting stack here is:

  | Microsoft.CodeAnalysis.Workspaces | AnalyzerConfigOptionsExtensions.TryGetEditorConfigOption Line 67
-- | -- | --
  | Microsoft.CodeAnalysis.Workspaces | AnalyzerConfigOptionsExtensions.TryGetEditorConfigOptionOrDefault Line 56
  | Microsoft.CodeAnalysis.Workspaces | AnalyzerConfigOptionsExtensions.GetOptionWithAssertOnFailure Line 43
  | Microsoft.CodeAnalysis.Workspaces | AnalyzerConfigOptionsExtensions.GetOption Line 39
  | Microsoft.CodeAnalysis.Workspaces | AbstractTriviaDataFactory.FormattedWhitespace..ctor Line 26
  | Microsoft.CodeAnalysis.Workspaces | AbstractTriviaDataFactory.ModifiedWhitespace.Format Line 86
  | Microsoft.CodeAnalysis.Workspaces | AbstractFormatEngine.<ApplyTriviaOperations>g__TriviaFormatter\|20_1 Line 323
  | Microsoft.CodeAnalysis.Workspaces | AbstractFormatEngine.ApplyTriviaOperations Line 296
  | Microsoft.CodeAnalysis.Workspaces | AbstractFormatEngine.Format Line 118
  | Microsoft.CodeAnalysis.CSharp.Workspaces | CSharpSyntaxFormattingService.Format Line 61
  | Microsoft.CodeAnalysis.Workspaces | AbstractSyntaxFormattingService.FormatIndividually Line 82
  | Microsoft.CodeAnalysis.Workspaces | AbstractSyntaxFormattingService.Format Line 51
  | Microsoft.CodeAnalysis.Workspaces | Formatter.GetFormattingResult Line 256
  | Microsoft.CodeAnalysis.Workspaces | Formatter.GetFormattedTextChanges Line 296
  | Microsoft.CodeAnalysis.CSharp.Workspaces | CSharpSmartTokenFormatter.<FormatTokenAsync>d__6.MoveNext Line 116
  | mscorlib | AsyncTaskMethodBuilder`1.Start Line 472
  | Microsoft.CodeAnalysis.CSharp.Workspaces | CSharpSmartTokenFormatter.FormatTokenAsync
  | Microsoft.CodeAnalysis.Workspaces | AbstractIndentationService`1.Indenter.TryGetSmartTokenIndentation Line 175
  | Microsoft.CodeAnalysis.Workspaces | AbstractIndentationService`1.GetIndentation Line 43
  | Microsoft.CodeAnalysis.Workspaces | IIndentationServiceExtensions.GetIndentation Line 63
  | Microsoft.CodeAnalysis.EditorFeatures | SmartIndent.GetDesiredIndentation Line 55
  | Microsoft.CodeAnalysis.EditorFeatures | SmartIndent.GetDesiredIndentation Line 29
  | Microsoft.VisualStudio.Platform.VSEditor | SmartIndentationService.GetDesiredIndentation Line 30
  | Microsoft.VisualStudio.Platform.VSEditor | EditorOperations.PositionCaretWithSmartIndent Line 4834
  | Microsoft.VisualStudio.Platform.VSEditor | EditorOperations.<>c__DisplayClass78_0.<InsertNewLine>b__1 Line 1444
  | Microsoft.VisualStudio.Platform.VSEditor | MultiSelectionBroker.PerformActionOnAllSelections Line 967
  | Microsoft.VisualStudio.Platform.VSEditor | EditorOperations.<InsertNewLine>b__78_0 Line 1455
  | Microsoft.VisualStudio.Platform.VSEditor | EditorOperations.ExecuteAction Line 5027
  | Microsoft.VisualStudio.Platform.VSEditor | EditorOperations.InsertNewLine Line 1459
KirillOsenkov commented 4 years ago

This line returns crlf: http://sourceroslyn.io/#Microsoft.CodeAnalysis.Workspaces/AnalyzerConfigOptionsExtensions.cs,71

KirillOsenkov commented 4 years ago

https://github.com/dotnet/roslyn/issues/19306

BluishGreenProductions commented 3 years ago

I come across this issue all the time, would appreciate it if it was fixed. So much time wasted fixing formatting.

chrisb2244 commented 3 years ago

Is this the same issue that causes in a LF/Tabs-set file, the following to create a mixed/mixed file? The example here uses a timer, but I think it's the same with other autocompletion for Events.

System.Timers.Timer newTimer = new System.Timers.Timer(1000);
newTimer.Elapsed += <tab completion here, with predicted name>

Edit: note that this doesn't happen if an .editorconfig file specifies LF/Tabs, so with the editorconfig I'm fine. Seems a little curious that VS detects one but inserts the other, but not a big problem if so easily overridden by the config file.