Repository: microsoft/vscode (TypeScript)
Date: 2020-12-04 15:46:34+00:00
Task type: Bug Fix

Issue:
Multicursor `Enter` issue with indentation (gif attached)
<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- Please search existing issues to avoid creating duplicates. -->
<!-- Also please test using the latest insiders build to make sure your issue has not already been fixed: https://code.visualstudio.com/insiders/ -->

<!-- Use Help > Report Issue to prefill these. -->
- VSCode Version: 1.51.1 / 1.52.0-insider
- OS Version: Ubuntu 20.04 / Windows 10

Steps to Reproduce:

1. insert 4x spaces at the beginning of a line
2. place multiple cursors on that line
3. Press enter

Expected: Insert newline at each of the positions where cursor is present
Actual Behavior: Inserts newline only at the first (NOT the left most) cursor position

_(Let me know the relevant settings values required)_

Addiditional info: 

- I use tabs (width of 4 spaces) for indentation for formatting

![vscode-multicursor-bug](https://user-images.githubusercontent.com/15051871/99904700-c0020e00-2cf2-11eb-99e0-f5b5e2ec90c1.gif)



<!-- Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: Yes



Issue discussion:
@stagefright5 I think this might be caused by one of your installed extensions. Here it is on my machine:

![TO_UPLOAD](https://user-images.githubusercontent.com/5047891/100986459-82e91780-354d-11eb-8504-03c911cdbead.gif)

To validate this, could you please [try the following](https://github.com/microsoft/vscode/wiki/Keybinding-Issues#basic-troubleshooting):

* Run the command Developer: Toggle Keyboard Shortcuts Troubleshooting
  * This will activate logging of dispatched keyboard shortcuts and will open an output panel with the corresponding log file.
* You can then press <kbd>Enter</kbd> and check what command is invoked.

If you are having trouble understanding the output, please feel free to paste here and I can help decipher it for you.
Here is the renderer keyboard troubleshoot log:

[renderer4.log](https://github.com/microsoft/vscode/files/5640664/renderLog4.log)

Also, the settings.json:

[settings.zip](https://github.com/microsoft/vscode/files/5640671/settings.zip)

I have disabled all the extensions. And, now, I can reproduce this with the latest stable too and in Ubuntu.
It is also not exclusive to the multi cursors created using keybinding. I can reproduce this by placing mulitple cursors created using the `Alt + Mouse-Left-Click` too.
@stagefright5 From the keyboard log, I can confirm that nobody is "stealing" Enter via keybindings. But there is another way that extensions can intercept that, via our `type` command. That is the way in which `vim` intercepts Enter. 
Does this reproduce when disabling all extensions? Press `F1` and then choose `Developer: Reload Window with Extensions Disabled`

![Kapture 2020-07-20 at 15 28 23](https://user-images.githubusercontent.com/5047891/87943040-c3b02a80-ca9d-11ea-847d-5f850fae8f05.gif)
Hey @alexdima , In all of my previuos posts I have made sure that I have disabled all the extensions (by running the command that you have mentioned) and also, at the end of each of my posts I have explicitly stated that I had disabled the extensions.
Can it have anything to do with my settings related to tabs?
May be, inspect my settings here: [settings.zip](https://github.com/microsoft/vscode/files/5640671/settings.zip) (which has settings.json) and, try to reproduce the issue by applying the exact same settings (copy paste my settings.json).

Also, @alexdima , Please carefully go through each of the above posts. It seems like you are not reading all the comments. It will save me from repeating the same thing again and again.
Thanks.

Solution patch:
diff --git a/src/vs/editor/common/controller/cursorTypeOperations.ts b/src/vs/editor/common/controller/cursorTypeOperations.ts
--- a/src/vs/editor/common/controller/cursorTypeOperations.ts
+++ b/src/vs/editor/common/controller/cursorTypeOperations.ts
@@ -351,13 +351,6 @@ export class TypeOperations {
 			if (ir) {
 				let oldEndViewColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition());
 				const oldEndColumn = range.endColumn;
-
-				let beforeText = '\n';
-				if (indentation !== config.normalizeIndentation(ir.beforeEnter)) {
-					beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1) + '\n';
-					range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
-				}
-
 				const newLineContent = model.getLineContent(range.endLineNumber);
 				const firstNonWhitespace = strings.firstNonWhitespaceIndex(newLineContent);
 				if (firstNonWhitespace >= 0) {
@@ -367,7 +360,7 @@ export class TypeOperations {
 				}
 
 				if (keepPosition) {
-					return new ReplaceCommandWithoutChangingPosition(range, beforeText + config.normalizeIndentation(ir.afterEnter), true);
+					return new ReplaceCommandWithoutChangingPosition(range, '\n' + config.normalizeIndentation(ir.afterEnter), true);
 				} else {
 					let offset = 0;
 					if (oldEndColumn <= firstNonWhitespace + 1) {
@@ -376,7 +369,7 @@ export class TypeOperations {
 						}
 						offset = Math.min(oldEndViewColumn + 1 - config.normalizeIndentation(ir.afterEnter).length - 1, 0);
 					}
-					return new ReplaceCommandWithOffsetCursorState(range, beforeText + config.normalizeIndentation(ir.afterEnter), 0, offset, true);
+					return new ReplaceCommandWithOffsetCursorState(range, '\n' + config.normalizeIndentation(ir.afterEnter), 0, offset, true);
 				}
 			}
 		}


Test patch:
diff --git a/src/vs/editor/test/browser/controller/cursor.test.ts b/src/vs/editor/test/browser/controller/cursor.test.ts
--- a/src/vs/editor/test/browser/controller/cursor.test.ts
+++ b/src/vs/editor/test/browser/controller/cursor.test.ts
@@ -4169,6 +4169,18 @@ suite('Editor Controller - Indentation Rules', () => {
 		model.dispose();
 		mode.dispose();
 	});
+
+	test('issue #111128: Multicursor `Enter` issue with indentation', () => {
+		const model = createTextModel('    let a, b, c;', { detectIndentation: false, insertSpaces: false, tabSize: 4 }, mode.getLanguageIdentifier());
+		withTestCodeEditor(null, { model: model }, (editor, viewModel) => {
+			editor.setSelections([
+				new Selection(1, 11, 1, 11),
+				new Selection(1, 14, 1, 14),
+			]);
+			viewModel.type('\n', 'keyboard');
+			assert.equal(model.getValue(), '    let a,\n\t b,\n\t c;');
+		});
+	});
 });
 
 interface ICursorOpts {