@@ -36,7 +36,6 @@ export const DirtyDiffWidgetProps = Symbol('DirtyDiffWidgetProps');
3636export interface DirtyDiffWidgetProps {
3737 readonly editor : MonacoEditor ;
3838 readonly previousRevisionUri : URI ;
39- readonly changes : readonly Change [ ] ;
4039}
4140
4241export const DirtyDiffWidgetFactory = Symbol ( 'DirtyDiffWidgetFactory' ) ;
@@ -50,6 +49,7 @@ export class DirtyDiffWidget implements Disposable {
5049 protected index : number = - 1 ;
5150 private peekView : DirtyDiffPeekView ;
5251 private diffEditorPromise : Promise < MonacoDiffEditor > ;
52+ protected _changes ?: readonly Change [ ] ;
5353
5454 constructor (
5555 @inject ( DirtyDiffWidgetProps ) protected readonly props : DirtyDiffWidgetProps ,
@@ -66,6 +66,14 @@ export class DirtyDiffWidget implements Disposable {
6666 this . diffEditorPromise = this . peekView . create ( ) ;
6767 }
6868
69+ get changes ( ) : readonly Change [ ] {
70+ return this . _changes ?? [ ] ;
71+ }
72+
73+ set changes ( changes : readonly Change [ ] ) {
74+ this . handleChangedChanges ( changes ) ;
75+ }
76+
6977 get editor ( ) : MonacoEditor {
7078 return this . props . editor ;
7179 }
@@ -78,10 +86,6 @@ export class DirtyDiffWidget implements Disposable {
7886 return this . props . previousRevisionUri ;
7987 }
8088
81- get changes ( ) : readonly Change [ ] {
82- return this . props . changes ;
83- }
84-
8589 get currentChange ( ) : Change | undefined {
8690 return this . changes [ this . index ] ;
8791 }
@@ -90,6 +94,16 @@ export class DirtyDiffWidget implements Disposable {
9094 return this . index ;
9195 }
9296
97+ protected handleChangedChanges ( updated : readonly Change [ ] ) : void {
98+ if ( this . currentChange ) {
99+ const { previousRange : { start, end} } = this . currentChange ;
100+ this . index = updated . findIndex ( candidate => candidate . previousRange . start === start && candidate . previousRange . end === end ) ;
101+ } else {
102+ this . index = - 1 ;
103+ }
104+ this . _changes = updated ;
105+ }
106+
93107 async showChange ( index : number ) : Promise < void > {
94108 await this . checkCreated ( ) ;
95109 if ( index >= 0 && index < this . changes . length ) {
@@ -99,23 +113,20 @@ export class DirtyDiffWidget implements Disposable {
99113 }
100114
101115 async showNextChange ( ) : Promise < void > {
102- await this . checkCreated ( ) ;
103- const index = this . index ;
104- const length = this . changes . length ;
105- if ( length > 0 && ( index < 0 || length > 1 ) ) {
106- this . index = index < 0 ? 0 : cycle ( index , 1 , length ) ;
107- this . showCurrentChange ( ) ;
108- }
116+ const editor = await this . checkCreated ( ) ;
117+ editor . diffNavigator . next ( ) ;
118+ this . updateIndex ( editor ) ;
109119 }
110120
111121 async showPreviousChange ( ) : Promise < void > {
112- await this . checkCreated ( ) ;
113- const index = this . index ;
114- const length = this . changes . length ;
115- if ( length > 0 && ( index < 0 || length > 1 ) ) {
116- this . index = index < 0 ? length - 1 : cycle ( index , - 1 , length ) ;
117- this . showCurrentChange ( ) ;
118- }
122+ const editor = await this . checkCreated ( ) ;
123+ editor . diffNavigator . previous ( ) ;
124+ this . updateIndex ( editor ) ;
125+ }
126+
127+ protected updateIndex ( editor : MonacoDiffEditor ) : void {
128+ const line = editor . cursor . line ;
129+ this . index = this . changes . findIndex ( candidate => candidate . currentRange . start <= line && candidate . currentRange . end >= line ) ;
119130 }
120131
121132 async getContentWithSelectedChanges ( predicate : ( change : Change , index : number , changes : readonly Change [ ] ) => boolean ) : Promise < string > {
@@ -174,15 +185,11 @@ export class DirtyDiffWidget implements Disposable {
174185 return Math . min ( changeHeightInLines + /* padding */ 8 , Math . floor ( editorHeightInLines / 3 ) ) ;
175186 }
176187
177- protected async checkCreated ( ) : Promise < void > {
178- await this . diffEditorPromise ;
188+ protected async checkCreated ( ) : Promise < MonacoDiffEditor > {
189+ return this . diffEditorPromise ;
179190 }
180191}
181192
182- function cycle ( index : number , offset : - 1 | 1 , length : number ) : number {
183- return ( index + offset + length ) % length ;
184- }
185-
186193// adapted from https://github.com/microsoft/vscode/blob/823d54f86ee13eb357bc6e8e562e89d793f3c43b/extensions/git/src/staging.ts
187194function applyChanges ( changes : readonly Change [ ] , original : monaco . editor . ITextModel , modified : monaco . editor . ITextModel ) : string {
188195 const result : string [ ] = [ ] ;
0 commit comments