microsoft / vscode

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

Auto-indent on paste #25260

Closed pokliu closed 7 years ago

pokliu commented 7 years ago
Extension Author Version
crane HvyIndustries 0.3.6
NodeAssertionSnippets jaymorrow 0.1.0
format-php Kasik96 1.1.2
php-docblocker neilbrayfield 0.4.2
laravel-blade onecentlin 1.8.1
laravel5-snippets onecentlin 1.3.4
upload wuwei 0.1.5 ;

Steps to Reproduce:

1.Create a file named 'test.php', and type some code like this: image

2.When I try to paste these selected multiline code to the first line of function 'test', it shows: image


Why do not auto-indent except the first line of pasted codes?

alexdima commented 7 years ago

fyi @rebornix

CherryDT commented 7 years ago

@poklau123 About the "first line" question: You have copied the first line without the indention, but the second line with the indention, so of course the idention is unequal after pasting. To have it equal, you need to copy both lines with indention (e.g. by using the left border of the code window where the mouse cursor points right).

In general, though, I am also missing a way to auto-indent after paste. There have been various issues in GitHub about this before (see also https://github.com/Microsoft/vscode/issues/13945), and in January an option was added to do "Format on Paste" ("editor.formatOnPaste": true). However, I didn't want it to format on paste (including things other than just indenting), I wanted it only to indent, in a way similar to how Sublime Text does it on Ctrl+Shift+V.

My solution for now is the plugin Paste and Indent. I have changed the key bindings so that Ctrl+V does normal paste while Ctrl+Shift+V invokes pasteAndIndent.action. It does have a slightly different behavior from Sublime Text though (see https://github.com/rubymaniac/vscode-paste-and-indent/issues/2): You have to set the cursor at the indention level which sould be the lowest indention you want for the pasted code, and then paste. It will not automatically determine the correct indention based on the surrounding code as Sublime Text did, and in the meantime I actually found this behavior not so bad because I have more control over how things get pasted.

pokliu commented 7 years ago

@CherryDT Thanks for your advice! It's better for me to set option "editor.formatOnPaste": true , it is enough. TY!

update: I just found out the option "editor.formatOnPaste": true will intend the entire document, so I chose to use plugin Paste and Indent.

druellan commented 7 years ago

@poklau123 I was also experimenting with "editor.formatOnPaste": true. The thing is: if the extension you are using for formatting does not allow a "format-selection" command, it usually ends formatting all the file instead of the pasted code.

I'm also a bit confusing about the paste&format thing. When the discussion stared it was related to paste&indent, but then the paste&format discussion started and took over like a more advanced replacement, but I still believe paste&indent should be part of the editor for cases on where you don't want to format, but also, for languages that have no suitable formatter available, like SCSS.

CherryDT commented 7 years ago

@druellan I agree, that's what I meant in my comment above and that's why I ended up using the Paste and Ident extension.

rebornix commented 7 years ago

Already added this feature in Insider build, thanks!