@@ -521,17 +521,26 @@ export class AutoIndentOnPaste implements IEditorContribution {
521
521
}
522
522
} ;
523
523
524
+ let endLineNumber = range . endLineNumber ;
525
+
526
+ // Don't indent the final line if the range ends at the start of it, because it will mess up
527
+ // the indentation of the preexisting code (outside the paste range) on that line.
528
+ // See https://github.com/microsoft/vscode/issues/85781 and https://github.com/microsoft/vscode/issues/147223
529
+ if ( range . endColumn === 1 ) {
530
+ endLineNumber -- ;
531
+ }
532
+
524
533
let startLineNumber = range . startLineNumber ;
525
534
526
- while ( startLineNumber <= range . endLineNumber ) {
535
+ while ( startLineNumber <= endLineNumber ) {
527
536
if ( this . shouldIgnoreLine ( model , startLineNumber ) ) {
528
537
startLineNumber ++ ;
529
538
continue ;
530
539
}
531
540
break ;
532
541
}
533
542
534
- if ( startLineNumber > range . endLineNumber ) {
543
+ if ( startLineNumber > endLineNumber ) {
535
544
return ;
536
545
}
537
546
@@ -568,15 +577,15 @@ export class AutoIndentOnPaste implements IEditorContribution {
568
577
const firstLineNumber = startLineNumber ;
569
578
570
579
// ignore empty or ignored lines
571
- while ( startLineNumber < range . endLineNumber ) {
580
+ while ( startLineNumber < endLineNumber ) {
572
581
if ( ! / \S / . test ( model . getLineContent ( startLineNumber + 1 ) ) ) {
573
582
startLineNumber ++ ;
574
583
continue ;
575
584
}
576
585
break ;
577
586
}
578
587
579
- if ( startLineNumber !== range . endLineNumber ) {
588
+ if ( startLineNumber !== endLineNumber ) {
580
589
const virtualModel = {
581
590
tokenization : {
582
591
getLineTokens : ( lineNumber : number ) => {
@@ -604,7 +613,7 @@ export class AutoIndentOnPaste implements IEditorContribution {
604
613
605
614
if ( newSpaceCntOfSecondLine !== oldSpaceCntOfSecondLine ) {
606
615
const spaceCntOffset = newSpaceCntOfSecondLine - oldSpaceCntOfSecondLine ;
607
- for ( let i = startLineNumber + 1 ; i <= range . endLineNumber ; i ++ ) {
616
+ for ( let i = startLineNumber + 1 ; i <= endLineNumber ; i ++ ) {
608
617
const lineContent = model . getLineContent ( i ) ;
609
618
const originalIndent = strings . getLeadingWhitespace ( lineContent ) ;
610
619
const originalSpacesCnt = indentUtils . getSpaceCnt ( originalIndent , tabSize ) ;
0 commit comments