Repository: microsoft/vscode (TypeScript) — Bug Fix

Issue:
Pressing Enter creates line with bad indent
Issue Type: <b>Bug</b>

Please look this,

![image](https://user-images.githubusercontent.com/97441570/165941642-8f743bd8-2ede-456b-a7ef-d01afce3a3fd.png)

Line 2: `SP` `SP` `Tab` "Text"
Line 3: `Tab` `SP` `SP`  .... This is auto indent line.

-------
以下のような行で、`Enter`で改行すると自動インデントとなりますが、その時のインデントが不適切ではないでしょうか?
(表示上、`SP`:空白, `Tab`: タブ)

`SP` `SP` `Tab`ABC`Enter` とすると、
`Tab` `SP` `SP`  となる。




Discussion:
I update this issue. But, It is automaticaly closed.  Why?

Please look this updated isuue.
@kato033 What are the steps to reproduce? What is happening versus what you would expect to happen?
First line is, (`<SP>` is white space, `<Tab>` is Tab code)
`<SP>` `<SP>` `<Tab>` "Text" + `Enter`-key 

Actual input is: (The above procedure removes the white space.)
[Tab]-key, "Text", (move coursor position to the beginning of line), [Space][Space], (move coursor position to end of line), [Enter]

The next cursor position is invalid.

Please look the image of issue.

I think, this is not serious but incorrect behavior.

 



Thank you for the extra information. Here are simplified steps:
* have a file with the contents `<space><space><tab>`
* press <kbd>Enter</kbd>
* if indentation is configured with tabs, the new line will contain `<tab><space><space>`
* if indentation is configured with spaces, the new line will contain `<space><space><space><space><space><space>`

Resolving patch:
diff --git a/src/vs/editor/common/core/indentation.ts b/src/vs/editor/common/core/indentation.ts
--- a/src/vs/editor/common/core/indentation.ts
+++ b/src/vs/editor/common/core/indentation.ts
@@ -4,12 +4,13 @@
  *--------------------------------------------------------------------------------------------*/
 
 import * as strings from 'vs/base/common/strings';
+import { CursorColumns } from 'vs/editor/common/core/cursorColumns';
 
 function _normalizeIndentationFromWhitespace(str: string, indentSize: number, insertSpaces: boolean): string {
 	let spacesCnt = 0;
 	for (let i = 0; i < str.length; i++) {
 		if (str.charAt(i) === '\t') {
-			spacesCnt += indentSize;
+			spacesCnt = CursorColumns.nextIndentTabStop(spacesCnt, indentSize);
 		} else {
 			spacesCnt++;
 		}


Test patch:
diff --git a/src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts b/src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts
--- a/src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts
+++ b/src/vs/editor/contrib/snippet/test/browser/snippetSession.test.ts
@@ -63,7 +63,7 @@ suite('SnippetSession', function () {
 		assertNormalized(new Position(1, 1), 'foo\rbar', 'foo\nbar');
 		assertNormalized(new Position(1, 1), 'foo\rbar', 'foo\nbar');
 		assertNormalized(new Position(2, 5), 'foo\r\tbar', 'foo\n        bar');
-		assertNormalized(new Position(2, 3), 'foo\r\tbar', 'foo\n      bar');
+		assertNormalized(new Position(2, 3), 'foo\r\tbar', 'foo\n    bar');
 		assertNormalized(new Position(2, 5), 'foo\r\tbar\nfoo', 'foo\n        bar\n    foo');
 
 		//Indentation issue with choice elements that span multiple lines #46266
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
@@ -3225,6 +3225,33 @@ suite('Editor Controller', () => {
 		});
 	});
 
+	test('issue #148256: Pressing Enter creates line with bad indent with insertSpaces: true', () => {
+		usingCursor({
+			text: [
+				'  \t'
+			],
+		}, (editor, model, viewModel) => {
+			moveTo(editor, viewModel, 1, 4, false);
+			viewModel.type('\n', 'keyboard');
+			assert.strictEqual(model.getValue(), '  \t\n    ');
+		});
+	});
+
+	test('issue #148256: Pressing Enter creates line with bad indent with insertSpaces: false', () => {
+		usingCursor({
+			text: [
+				'  \t'
+			]
+		}, (editor, model, viewModel) => {
+			model.updateOptions({
+				insertSpaces: false
+			});
+			moveTo(editor, viewModel, 1, 4, false);
+			viewModel.type('\n', 'keyboard');
+			assert.strictEqual(model.getValue(), '  \t\n\t');
+		});
+	});
+
 	test('removeAutoWhitespace off', () => {
 		usingCursor({
 			text: [
diff --git a/src/vs/editor/test/common/model/textModel.test.ts b/src/vs/editor/test/common/model/textModel.test.ts
--- a/src/vs/editor/test/common/model/textModel.test.ts
+++ b/src/vs/editor/test/common/model/textModel.test.ts
@@ -915,10 +915,11 @@ suite('Editor Model - TextModel', () => {
 		assert.strictEqual(model.normalizeIndentation('  '), '  ');
 		assert.strictEqual(model.normalizeIndentation(' '), ' ');
 		assert.strictEqual(model.normalizeIndentation(''), '');
-		assert.strictEqual(model.normalizeIndentation(' \t   '), '\t\t');
-		assert.strictEqual(model.normalizeIndentation(' \t  '), '\t   ');
-		assert.strictEqual(model.normalizeIndentation(' \t '), '\t  ');
-		assert.strictEqual(model.normalizeIndentation(' \t'), '\t ');
+		assert.strictEqual(model.normalizeIndentation(' \t    '), '\t\t');
+		assert.strictEqual(model.normalizeIndentation(' \t   '), '\t   ');
+		assert.strictEqual(model.normalizeIndentation(' \t  '), '\t  ');
+		assert.strictEqual(model.normalizeIndentation(' \t '), '\t ');
+		assert.strictEqual(model.normalizeIndentation(' \t'), '\t');
 
 		assert.strictEqual(model.normalizeIndentation('\ta'), '\ta');
 		assert.strictEqual(model.normalizeIndentation('    a'), '\ta');
@@ -926,10 +927,11 @@ suite('Editor Model - TextModel', () => {
 		assert.strictEqual(model.normalizeIndentation('  a'), '  a');
 		assert.strictEqual(model.normalizeIndentation(' a'), ' a');
 		assert.strictEqual(model.normalizeIndentation('a'), 'a');
-		assert.strictEqual(model.normalizeIndentation(' \t   a'), '\t\ta');
-		assert.strictEqual(model.normalizeIndentation(' \t  a'), '\t   a');
-		assert.strictEqual(model.normalizeIndentation(' \t a'), '\t  a');
-		assert.strictEqual(model.normalizeIndentation(' \ta'), '\t a');
+		assert.strictEqual(model.normalizeIndentation(' \t    a'), '\t\ta');
+		assert.strictEqual(model.normalizeIndentation(' \t   a'), '\t   a');
+		assert.strictEqual(model.normalizeIndentation(' \t  a'), '\t  a');
+		assert.strictEqual(model.normalizeIndentation(' \t a'), '\t a');
+		assert.strictEqual(model.normalizeIndentation(' \ta'), '\ta');
 
 		model.dispose();
 	});
@@ -943,10 +945,11 @@ suite('Editor Model - TextModel', () => {
 		assert.strictEqual(model.normalizeIndentation('  a'), '  a');
 		assert.strictEqual(model.normalizeIndentation(' a'), ' a');
 		assert.strictEqual(model.normalizeIndentation('a'), 'a');
-		assert.strictEqual(model.normalizeIndentation(' \t   a'), '        a');
-		assert.strictEqual(model.normalizeIndentation(' \t  a'), '       a');
-		assert.strictEqual(model.normalizeIndentation(' \t a'), '      a');
-		assert.strictEqual(model.normalizeIndentation(' \ta'), '     a');
+		assert.strictEqual(model.normalizeIndentation(' \t    a'), '        a');
+		assert.strictEqual(model.normalizeIndentation(' \t   a'), '       a');
+		assert.strictEqual(model.normalizeIndentation(' \t  a'), '      a');
+		assert.strictEqual(model.normalizeIndentation(' \t a'), '     a');
+		assert.strictEqual(model.normalizeIndentation(' \ta'), '    a');
 
 		model.dispose();
 	});