Skip to content

Commit 0a03c8a

Browse files
committed
Fix microsoft#29285. If users have autoindent turned on, we no longer adjust the indentation of current line on Enter as that should be already taken care of.
1 parent dc0ebb4 commit 0a03c8a

3 files changed

Lines changed: 25 additions & 60 deletions

File tree

src/vs/editor/common/controller/cursorTypeOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class TypeOperations {
306306
normalizeIndentation: (indent) => {
307307
return config.normalizeIndentation(indent);
308308
}
309-
});
309+
}, config.autoIndent);
310310

311311
let lineText = model.getLineContent(range.startLineNumber);
312312
let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);

src/vs/editor/common/modes/languageConfigurationRegistry.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ export class LanguageConfigurationRegistryImpl {
456456
return null;
457457
}
458458

459-
public getIndentForEnter(model: ITokenizedModel, range: Range, indentConverter: IIndentConverter): { beforeEnter: string, afterEnter: string } {
459+
public getIndentForEnter(model: ITokenizedModel, range: Range, indentConverter: IIndentConverter, autoIndent: boolean): { beforeEnter: string, afterEnter: string } {
460460
model.forceTokenization(range.startLineNumber);
461461
let lineTokens = model.getLineTokens(range.startLineNumber);
462462

@@ -487,19 +487,24 @@ export class LanguageConfigurationRegistryImpl {
487487
return null;
488488
}
489489

490-
let beforeEnterIndentAction = this.getInheritIndentForLine(model, range.startLineNumber);
490+
let beforeEnterResult = beforeEnterText;
491491
let beforeEnterIndent = strings.getLeadingWhitespace(beforeEnterText);
492492

493-
if (indentRulesSupport.shouldDecrease(beforeEnterText)) {
494-
if (beforeEnterIndentAction) {
495-
beforeEnterIndent = beforeEnterIndentAction.indentation;
496-
if (beforeEnterIndentAction.action !== IndentAction.Indent) {
497-
beforeEnterIndent = indentConverter.unshiftIndent(beforeEnterIndent);
493+
if (!autoIndent) {
494+
let beforeEnterIndentAction = this.getInheritIndentForLine(model, range.startLineNumber);
495+
let beforeEnterIndent = strings.getLeadingWhitespace(beforeEnterText);
496+
497+
if (indentRulesSupport.shouldDecrease(beforeEnterText)) {
498+
if (beforeEnterIndentAction) {
499+
beforeEnterIndent = beforeEnterIndentAction.indentation;
500+
if (beforeEnterIndentAction.action !== IndentAction.Indent) {
501+
beforeEnterIndent = indentConverter.unshiftIndent(beforeEnterIndent);
502+
}
498503
}
499504
}
500-
}
501505

502-
let beforeEnterResult = beforeEnterIndent + strings.ltrim(strings.ltrim(beforeEnterText, ' '), '\t');
506+
beforeEnterResult = beforeEnterIndent + strings.ltrim(strings.ltrim(beforeEnterText, ' '), '\t');
507+
}
503508

504509
let virtualModel: IVirtualModel = {
505510
getLineTokens: (lineNumber: number) => {

src/vs/editor/test/common/controller/cursor.test.ts

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,20 +2274,20 @@ suite('Editor Controller - Indentation Rules', () => {
22742274
});
22752275
});
22762276

2277-
test('Enter honors decreaseIndentPattern', () => {
2277+
test('Type honors decreaseIndentPattern', () => {
22782278
usingCursor({
22792279
text: [
22802280
'if (true) {',
2281-
'\t}'
2281+
'\t'
22822282
],
22832283
languageIdentifier: mode.getLanguageIdentifier(),
22842284
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
22852285
}, (model, cursor) => {
2286-
moveTo(cursor, 2, 3, false);
2287-
assertCursor(cursor, new Selection(2, 3, 2, 3));
2286+
moveTo(cursor, 2, 2, false);
2287+
assertCursor(cursor, new Selection(2, 2, 2, 2));
22882288

2289-
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2290-
assertCursor(cursor, new Selection(3, 1, 3, 1));
2289+
cursorCommand(cursor, H.Type, { text: '}' }, 'keyboard');
2290+
assertCursor(cursor, new Selection(2, 2, 2, 2));
22912291
assert.equal(model.getLineContent(2), '}', '001');
22922292
});
22932293
});
@@ -2365,46 +2365,6 @@ suite('Editor Controller - Indentation Rules', () => {
23652365
model.dispose();
23662366
});
23672367

2368-
test('Enter adjusts indentation of current line 1', () => {
2369-
usingCursor({
2370-
text: [
2371-
'if (true) {',
2372-
'\tif (true) {',
2373-
'\t\treturn true;',
2374-
'\t\t}}'
2375-
],
2376-
languageIdentifier: mode.getLanguageIdentifier(),
2377-
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
2378-
}, (model, cursor) => {
2379-
moveTo(cursor, 4, 4, false);
2380-
assertCursor(cursor, new Selection(4, 4, 4, 4));
2381-
2382-
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2383-
assertCursor(cursor, new Selection(5, 1, 5, 1));
2384-
assert.equal(model.getLineContent(4), '\t}', '001');
2385-
});
2386-
});
2387-
2388-
test('Enter adjusts indentation of current line 2', () => {
2389-
usingCursor({
2390-
text: [
2391-
'if (true) {',
2392-
'\tif (true) {',
2393-
'\t\treturn true;',
2394-
'}}'
2395-
],
2396-
languageIdentifier: mode.getLanguageIdentifier(),
2397-
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
2398-
}, (model, cursor) => {
2399-
moveTo(cursor, 4, 2, false);
2400-
assertCursor(cursor, new Selection(4, 2, 4, 2));
2401-
2402-
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
2403-
assertCursor(cursor, new Selection(5, 1, 5, 1));
2404-
assert.equal(model.getLineContent(4), '\t}', '001');
2405-
});
2406-
});
2407-
24082368
test('Enter honors intential indent', () => {
24092369
usingCursor({
24102370
text: [
@@ -2431,14 +2391,14 @@ suite('Editor Controller - Indentation Rules', () => {
24312391
'if (true) {',
24322392
'\tif (true) {',
24332393
'\t\treturn true;',
2434-
'\t\t}a}'
2394+
'\t}a}'
24352395
],
24362396
languageIdentifier: mode.getLanguageIdentifier(),
24372397
modelOpts: { insertSpaces: false, tabSize: 4, detectIndentation: false, defaultEOL: DefaultEndOfLine.LF, trimAutoWhitespace: true }
24382398
}, (model, cursor) => {
2439-
moveTo(cursor, 4, 4, false);
2440-
moveTo(cursor, 4, 5, true);
2441-
assertCursor(cursor, new Selection(4, 4, 4, 5));
2399+
moveTo(cursor, 4, 3, false);
2400+
moveTo(cursor, 4, 4, true);
2401+
assertCursor(cursor, new Selection(4, 3, 4, 4));
24422402

24432403
cursorCommand(cursor, H.Type, { text: '\n' }, 'keyboard');
24442404
assertCursor(cursor, new Selection(5, 1, 5, 1));

0 commit comments

Comments
 (0)