microsoft / vscode

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

VS Code changes indention of current line #61510

Open garretwilson opened 5 years ago

garretwilson commented 5 years ago

I am using VS Code 1.28.2 on Windows 10. I'm editing an HTML file indented with tabs. But I have HTML <pre> sections which I want indented with spaces.

Let's say I have the following code:

<pre>
  foo //I press <Enter> at the end of this line
</pre>

Yes, when I hit <Enter> after "foo", it not only indents the next line but changes the indention that comes before foo from spaces to tabs.

<pre>
  foo //bug: the two spaces have been converted to a tab!
  //this new line also has a tab, which I don't want but can accept for now
</pre>

VS Code has no business of changing the existing line. I simply hit <Enter> to go to the next time. This is a huge problem, and it has likely corrupted a bunch of lines in this file because I didn't catch on to what it was doing.

Sure, we have auto-indent features to try to automatically insert indent on the following line, but please don't mess with the line I already have.

How can I turn this off?

garretwilson commented 5 years ago

And to make things worse, I can't press <Ctrl+Z> to undo the changes, because that removes the newline altogether. I have to manually go back and change all the indents. If it were just for the new line, I could just erase to the beginning of the line and add the indents I want. But now I have to go to previous lines and change them back to what they should be. Very irritating and really gets in the way of work.

vscodebot[bot] commented 5 years ago

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

garretwilson commented 5 years ago

No, the bug I'm filing is not covered by those existing tickets.

I'm filing a bug where VS Code changes the existing indentation of an existing line. The issue is not about what default indention to use on a new line (although I could quibble about that, too—but that's a separate issue).

garretwilson commented 5 years ago

I had added more information, but I've decided that this is a new bug, so I filed a separate ticket #67200.

garretwilson commented 5 years ago

Please raise the priority of this. VS Code should never reformat the indent of existing lines simply because I add a new line.

rebornix commented 5 years ago

You can now turn off editor.autoIndent to workaround for now.

sohrabsaran commented 3 years ago

You can now turn off editor.autoIndent to workaround for now.

Please clarify the exact steps to do this? Please see also issue #109382 I tried setting the value to 'none' under workspace settings, restarted vs code and and it does not work (on pressing Ctrl+S (save shortcut?) the indentation is corrected, which is not what I expect). (If #109382 is a separate issue, please reopen it).

aiday-mar commented 6 months ago

Hi @garretwilson thank you for posting the issue. I am trying to reproduce the behavior on the latest insiders. In the GIF below, the whitespaces are rendered in the editor to better understand what is happening. Spaces are rendered as dots and tabs are rendered as arrows. When I press Enter after the end of the second line, the spaces don't seem to be changed to tabs, and spaces are inserted at the beginning of the new line (not tabs). Granted there is one more indentation level on the next line, which I will look into. It only seems to happen when the // are used however.

https://github.com/microsoft/vscode/assets/61460952/fbb9ab78-90a3-4327-a3eb-fc53f35fa549

garretwilson commented 4 months ago

@aiday-mar , one thing you seem to have missed in my report is this:

I'm editing an HTML file indented with tabs.

I can't see the bottom of your screen to know for sure, but I'll bet you have the file set up so that VS Code thinks you are indenting with tabs. Tell VS Code that you are using tabs for indention.

Better yet, create an entire HTML file (you can find gajillions on the web) that is indented using tabs, or convert it to use tabs. (I can see you're only using three lines, not an entire HTML file.) Then close and open the file so that VS Code detects you're using tabs. Then type those lines from the example, but use spaces do indent.

(Why do I want to use spaces to indent inside <pre>? Because of many reasons. The point of <pre> is that it maintains the formatting, and VS Code should know that when editing in HTML mode.)

As of 2024-06-08, VS Code no longer converts the first line to tabs, so that is an improvement. However it still indents on the next line using tabs, even though the <pre> section is using spaces to indent.

Converting the original line (already indented) from spaces to tabs seems to have been fixed, so this ticket can be closed. (Indenting the following line with tabs when the <pre> indents using spaces is a separate issue.)