6

So i upgraded to VS 2022. and anytime i add a semicolon. the whole code block gets backtabbed. does anyone know how to turn this off? getting tired of hitting ctrl z repeatedly..

Before.

Before semicolon

After

enter image description here

Mitch Storrie
  • 131
  • 1
  • 10
  • Does it help you ? https://stackoverflow.com/questions/737222/turn-off-auto-formatting-in-visual-studio – Keyvan Soleimani Jan 11 '22 at 06:41
  • The red squiggle indicates where a semicolon *should* be added. The issue here is that adding the semicolon will *auto-indent the entire block*. (This is default auto-ident behavior occurs even when “indented correctly” per some styling rules.) – user2864740 Jan 11 '22 at 06:46
  • Can you confirm this is newly observed behavior after updating to VS2 022 — as opposed to 2019? — and *not* caused by an extension (ie. ReSharper). – user2864740 Jan 11 '22 at 06:57
  • @CaiusJard i checked this and this is not the issue. also that is simply the await operator due to it being async. not a red squiggly. – Mitch Storrie Jan 11 '22 at 07:16
  • 1
    @user2864740 i checked this. no issue in 2019. issue only falls in VS 2022. – Mitch Storrie Jan 11 '22 at 07:29
  • 1
    Does this happen in all your pages or just this one? – Caius Jard Jan 11 '22 at 07:32
  • 1
    @CaiusJard seems when i try on a different page the issue does not arise so this may be something to do with an error causing the block indenting mentioned earlier. – Mitch Storrie Jan 11 '22 at 07:43
  • Yep, that's why I asked (in the now-deleted earlier comment) if the page was certainly error free; Blazor dev can sometimes be quite frustrating when some minor mistake in the markup somewhere completely clowns the code structure of the rest of the page, and e.g. C# thinks there are no fields or methods defined etc.. – Caius Jard Jan 11 '22 at 17:59

6 Answers6

3

I'm not sure if you ever found the correct answer to your question, but I was having the same problem, and reading the other two answers led me to figuring it out. The problem is with the adaptive formatting option being turned on. To turn it off, go to

Tools -> Options -> Text Editor -> Advanced

You should see enter image description here

Uncheck 'Use adaptive formatting'

Save, and restart Visual Studio

This should do the trick for you.

1
  1. Go to VS2022 Tools - Options Menu
  2. Type indent in search
  3. Look for Indentation under formatting option which appears under

Text Editor - C# - Code Style - Formatting Indentation

check / uncheck boxes to see how your code will look and save when happy

dbCoder
  • 114
  • 5
1

After consolidating code blocks to one block on the razor page... this seems to have corrected the issue. the indenting I believe may have been caused by some bug where the indenting fails due to attempting to address other formatted code. this is not really a solid answer but i did find that moving things around prevented this from occuring and this was nothing to do with any indenting settings within the tools text editor options.

Mitch Storrie
  • 131
  • 1
  • 10
  • Multiple @code blocks are supported, I believe, but I don't recall seeing more than one. For places where the code is getting so big and unwieldy, it makes a lot of sense to split the page into a markup and a partial codebehind. It also makes certain operations easier; you don't tend to get some minor syntax error spiralling out into hundreds of bizarre code errors (I see "doesn't define a constructor with this many arguments" a lot) and, in 2019 at least, performing some refactoring like renaming a property/method is more likely to work/not trash the markup.. Splitting is easy.. – Caius Jard Jan 11 '22 at 18:02
  • Just make a `YourPageName.razor.cs` and a `partial class YourPageName` inside it and move the code.. VS will help out with anything else like missing usings - there might also be bits like moving @inject directives to an [Inject] attribute on a prop etc – Caius Jard Jan 11 '22 at 18:04
0

In visual studio 2022 community:

  • Go to Tools -> Options -> "Generate .editorconfig file from settings"
  • Open .editorconfig in some text editor
  • Search for "indent_size" and "indent_style" and adapt them to your needs

effects are reflected without even restarting visual studio

mcExchange
  • 6,154
  • 12
  • 57
  • 103
0

I have had this problem since the beginning even in 17.1 turning off adaptive formatting did not work however what did work was turning off ALL "Automatic Format" under tools->options->Text Editor->C#->Code Style->Formating

I turned off everything in that section!! because I can do my own formatting.

If you do want the editor to do some formatting you can customize it in the "General" "Indentation", "New Lines", "Spacing" and "Wrapping" sub branches of the Formatting section

X86Inc
  • 1
  • 1
-1

There is another option under Tools - Options

Text Editor - All Languages - Tabs.

Try to specify Tab size and indent size

dbCoder
  • 114
  • 5