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

snippet pasting does not honor the indenting mode #4284

Closed
KakCAT opened this issue Aug 1, 2015 · 17 comments
Closed

snippet pasting does not honor the indenting mode #4284

KakCAT opened this issue Aug 1, 2015 · 17 comments
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Milestone

Comments

@KakCAT
Copy link

KakCAT commented Aug 1, 2015

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 Pilchie added this to the 1.1 milestone Aug 3, 2015
@Pilchie Pilchie modified the milestones: Unknown, 1.1 Aug 3, 2015
@Pilchie
Copy link
Member

Pilchie commented Aug 3, 2015

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
Copy link
Author

KakCAT commented Aug 5, 2015

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
Copy link
Member

Pilchie commented Aug 5, 2015

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 😄.

@KakCAT
Copy link
Author

KakCAT commented Aug 5, 2015

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
Copy link
Member

conniey commented Aug 12, 2015

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
Copy link
Author

KakCAT commented Jan 2, 2016

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
Copy link
Contributor

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

@CyrusNajmabadi
Copy link
Member

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

@CyrusNajmabadi CyrusNajmabadi added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label May 17, 2020
@KirillOsenkov KirillOsenkov added the IDE-CodeStyle Built-in analyzers, fixes, and refactorings label Aug 13, 2020
@KirillOsenkov
Copy link
Member

This issue is potentially related to #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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

@KirillOsenkov
Copy link
Member

#19306

@BluishGreenProductions
Copy link

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

@chrisb2244
Copy link

chrisb2244 commented Apr 5, 2021

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.

@CyrusNajmabadi
Copy link
Member

closing out as we are not investing in legacy snippets.

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Nov 5, 2024
@dotnet dotnet locked and limited conversation to collaborators Nov 5, 2024
@github-project-automation github-project-automation bot moved this from InQueue to Completed in Small Fixes Nov 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it IDE-CodeStyle Built-in analyzers, fixes, and refactorings
Projects
Status: Completed
Development

No branches or pull requests

9 participants