@@ -22,6 +22,7 @@ import { IRange, Range } from 'vs/editor/common/core/range';
22
22
import { Selection } from 'vs/editor/common/core/selection' ;
23
23
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer' ;
24
24
import { IEditorContribution , IEditorDecorationsCollection , ScrollType } from 'vs/editor/common/editorCommon' ;
25
+ import { TextEdit } from 'vs/editor/common/languages' ;
25
26
import { ICursorStateComputer , IModelDecorationOptions , IModelDeltaDecoration , IValidEditOperation } from 'vs/editor/common/model' ;
26
27
import { ModelDecorationOptions , createTextBufferFactoryFromSnapshot } from 'vs/editor/common/model/textModel' ;
27
28
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker' ;
@@ -582,6 +583,12 @@ export class InteractiveEditorController implements IEditorContribution {
582
583
}
583
584
}
584
585
586
+ acceptLast ( ) : void {
587
+ if ( this . _activeSession ?. lastExchange ?. response instanceof EditResponse && this . _strategy ) {
588
+ this . _activeSession . lastExchange . accepted = true ;
589
+ }
590
+ }
591
+
585
592
async applyChanges ( ) : Promise < EditResponse | void > {
586
593
if ( this . _activeSession ?. lastExchange ?. response instanceof EditResponse && this . _strategy ) {
587
594
const strategy = this . _strategy ;
@@ -757,11 +764,26 @@ class LiveStrategy extends EditModeStrategy {
757
764
}
758
765
759
766
async cancel ( ) {
760
- const { modelN, model0 } = this . _session ;
767
+ const { modelN, model0, exchanges , lastExchange } = this . _session ;
761
768
if ( modelN . isDisposed ( ) || model0 . isDisposed ( ) ) {
762
769
return ;
763
770
}
764
- const edits = await this . _editorWorkerService . computeMoreMinimalEdits ( modelN . uri , [ { range : modelN . getFullModelRange ( ) , text : model0 . getValue ( ) } ] ) ;
771
+
772
+ if ( lastExchange ?. accepted ) {
773
+ return ;
774
+ }
775
+
776
+ // find the last exchange which is accepted
777
+ const lastAcceptedExchange = exchanges . reverse ( ) . find ( exchange => exchange . accepted ) ;
778
+ let edits : TextEdit [ ] | undefined = undefined ;
779
+ if ( lastAcceptedExchange && lastAcceptedExchange . response instanceof EditResponse ) {
780
+ // apply change on model0 with the last accepted exchange, and then compute minimal edits to apply on modelN
781
+ model0 . applyEdits ( lastAcceptedExchange . response . localEdits ) ;
782
+ edits = await this . _editorWorkerService . computeMoreMinimalEdits ( modelN . uri , [ { range : modelN . getFullModelRange ( ) , text : model0 . getValue ( ) } ] ) ;
783
+ } else {
784
+ edits = await this . _editorWorkerService . computeMoreMinimalEdits ( modelN . uri , [ { range : modelN . getFullModelRange ( ) , text : model0 . getValue ( ) } ] ) ;
785
+ }
786
+
765
787
if ( edits ) {
766
788
const operations = edits . map ( e => EditOperation . replace ( Range . lift ( e . range ) , e . text ) ) ;
767
789
modelN . pushEditOperations ( null , operations , ( ) => null ) ;
0 commit comments