Skip to content

Commit 5b9d8b2

Browse files
committed
Fixes microsoft#27937: Do not over-type equal auto closing pairs when the character count is even
1 parent 97d76c0 commit 5b9d8b2

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ export class TypeOperations {
410410
return false;
411411
}
412412

413+
const isEqualPair = (ch === config.autoClosingPairsClose[ch]);
414+
413415
for (let i = 0, len = selections.length; i < len; i++) {
414416
const selection = selections[i];
415417

@@ -424,11 +426,28 @@ export class TypeOperations {
424426
if (afterCharacter !== ch) {
425427
return false;
426428
}
429+
430+
if (isEqualPair) {
431+
const lineTextBeforeCursor = lineText.substr(0, position.column - 1);
432+
const chCntBefore = this._countNeedlesInHaystack(lineTextBeforeCursor, ch);
433+
if (chCntBefore % 2 === 0) {
434+
return false;
435+
}
436+
}
427437
}
428438

429439
return true;
430440
}
431441

442+
private static _countNeedlesInHaystack(haystack: string, needle: string): number {
443+
let cnt = 0;
444+
let lastIndex = -1;
445+
while ((lastIndex = haystack.indexOf(needle, lastIndex + 1)) !== -1) {
446+
cnt++;
447+
}
448+
return cnt;
449+
}
450+
432451
private static _runAutoClosingCloseCharType(config: CursorConfiguration, model: ITokenizedModel, selections: Selection[], ch: string): EditOperationResult {
433452
let commands: ICommand[] = [];
434453
for (let i = 0, len = selections.length; i < len; i++) {

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,12 +3493,12 @@ suite('autoClosingPairs', () => {
34933493
let autoClosePositions = [
34943494
'var a =| [|];|',
34953495
'var b =| |`asd`;|',
3496-
'var c =| !\'asd!\';|',
3496+
'var c =| |\'asd!\';|',
34973497
'var d =| |"asd";|',
34983498
'var e =| /*3*/| 3;|',
34993499
'var f =| /**| 3 */3;|',
35003500
'var g =| (3+5);|',
3501-
'var h =| {| a:| !\'value!\'| |};|',
3501+
'var h =| {| a:| |\'value!\'| |};|',
35023502
];
35033503
for (let i = 0, len = autoClosePositions.length; i < len; i++) {
35043504
const lineNumber = i + 1;
@@ -3518,6 +3518,19 @@ suite('autoClosingPairs', () => {
35183518
mode.dispose();
35193519
});
35203520

3521+
test('issue #27937: Trying to add an item to the front of a list is cumbersome', () => {
3522+
let mode = new AutoClosingMode();
3523+
usingCursor({
3524+
text: [
3525+
'var arr = ["b", "c"];'
3526+
],
3527+
languageIdentifier: mode.getLanguageIdentifier()
3528+
}, (model, cursor) => {
3529+
assertType(model, cursor, 1, 12, '"', '""', `does not over type and will auto close`);
3530+
});
3531+
mode.dispose();
3532+
});
3533+
35213534
test('issue #25658 - Do not auto-close single/double quotes after word characters', () => {
35223535
let mode = new AutoClosingMode();
35233536
usingCursor({

0 commit comments

Comments
 (0)