@@ -10,18 +10,16 @@ import { ThrottledDelayer, first } from 'vs/base/common/async';
10
10
import { IDisposable , dispose , toDisposable , Disposable , DisposableStore } from 'vs/base/common/lifecycle' ;
11
11
import { Event , Emitter } from 'vs/base/common/event' ;
12
12
import * as ext from 'vs/workbench/common/contributions' ;
13
- import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget' ;
14
13
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
15
14
import { IResolvedTextEditorModel , ITextModelService } from 'vs/editor/common/services/resolverService' ;
16
- import { IEditorService } from 'vs/workbench/services/editor/common/editorService' ;
17
15
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker' ;
18
16
import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
19
17
import { URI } from 'vs/base/common/uri' ;
20
18
import { ISCMService , ISCMRepository , ISCMProvider } from 'vs/workbench/contrib/scm/common/scm' ;
21
19
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel' ;
22
20
import { IColorTheme , themeColorFromId , IThemeService , ThemeIcon } from 'vs/platform/theme/common/themeService' ;
23
21
import { editorErrorForeground , registerColor , transparent } from 'vs/platform/theme/common/colorRegistry' ;
24
- import { ICodeEditor , IEditorMouseEvent , MouseTargetType } from 'vs/editor/browser/editorBrowser' ;
22
+ import { ICodeEditor , IEditorMouseEvent , isCodeEditor , MouseTargetType } from 'vs/editor/browser/editorBrowser' ;
25
23
import { registerEditorAction , registerEditorContribution , ServicesAccessor , EditorAction } from 'vs/editor/browser/editorExtensions' ;
26
24
import { PeekViewWidget , getOuterEditor , peekViewBorder , peekViewTitleBackground , peekViewTitleForeground , peekViewTitleInfoForeground } from 'vs/editor/contrib/peekView/browser/peekView' ;
27
25
import { IContextKeyService , IContextKey , ContextKeyExpr , RawContextKey } from 'vs/platform/contextkey/common/contextkey' ;
@@ -53,6 +51,10 @@ import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/
53
51
import { IChange } from 'vs/editor/common/diff/smartLinesDiffComputer' ;
54
52
import { Color } from 'vs/base/common/color' ;
55
53
import { Iterable } from 'vs/base/common/iterator' ;
54
+ import { ResourceMap } from 'vs/base/common/map' ;
55
+ import { IEditorService } from 'vs/workbench/services/editor/common/editorService' ;
56
+ import { DEFAULT_EDITOR_ASSOCIATION } from 'vs/workbench/common/editor' ;
57
+ import { FILE_EDITOR_INPUT_ID } from 'vs/workbench/contrib/files/common/files' ;
56
58
57
59
class DiffActionRunner extends ActionRunner {
58
60
@@ -66,7 +68,7 @@ class DiffActionRunner extends ActionRunner {
66
68
}
67
69
68
70
export interface IModelRegistry {
69
- getModel ( editorModel : IEditorModel ) : DirtyDiffModel | null ;
71
+ getModel ( editorModel : IEditorModel ) : DirtyDiffModel | undefined ;
70
72
}
71
73
72
74
export const isDirtyDiffVisible = new RawContextKey < boolean > ( 'dirtyDiffVisible' , false ) ;
@@ -1328,7 +1330,10 @@ export class DirtyDiffModel extends Disposable {
1328
1330
1329
1331
class DirtyDiffItem {
1330
1332
1331
- constructor ( readonly model : DirtyDiffModel , readonly decorator : DirtyDiffDecorator ) { }
1333
+ constructor (
1334
+ readonly model : DirtyDiffModel ,
1335
+ readonly decorator : DirtyDiffDecorator
1336
+ ) { }
1332
1337
1333
1338
dispose ( ) : void {
1334
1339
this . decorator . dispose ( ) ;
@@ -1345,7 +1350,7 @@ export class DirtyDiffWorkbenchController extends Disposable implements ext.IWor
1345
1350
1346
1351
private enabled = false ;
1347
1352
private viewState : IViewState = { width : 3 , visibility : 'always' } ;
1348
- private items = new Map < IResolvedTextFileEditorModel , DirtyDiffItem > ( ) ;
1353
+ private items = new ResourceMap < DirtyDiffItem > ( ) ;
1349
1354
private readonly transientDisposables = this . _register ( new DisposableStore ( ) ) ;
1350
1355
private stylesheet : HTMLStyleElement ;
1351
1356
@@ -1425,7 +1430,7 @@ export class DirtyDiffWorkbenchController extends Disposable implements ext.IWor
1425
1430
this . disable ( ) ;
1426
1431
}
1427
1432
1428
- this . transientDisposables . add ( this . editorService . onDidVisibleEditorsChange ( ( ) => this . onEditorsChanged ( ) ) ) ;
1433
+ this . transientDisposables . add ( Event . any ( this . editorService . onDidCloseEditor , this . editorService . onDidVisibleEditorsChange ) ( ( ) => this . onEditorsChanged ( ) ) ) ;
1429
1434
this . onEditorsChanged ( ) ;
1430
1435
this . enabled = true ;
1431
1436
}
@@ -1445,59 +1450,38 @@ export class DirtyDiffWorkbenchController extends Disposable implements ext.IWor
1445
1450
this . enabled = false ;
1446
1451
}
1447
1452
1448
- // HACK: This is the best current way of figuring out whether to draw these decorations
1449
- // or not. Needs context from the editor, to know whether it is a diff editor, in place editor
1450
- // etc.
1451
1453
private onEditorsChanged ( ) : void {
1452
- const models = this . editorService . visibleTextEditorControls
1453
-
1454
- // only interested in code editor widgets
1455
- . filter ( c => c instanceof CodeEditorWidget )
1454
+ for ( const editor of this . editorService . visibleTextEditorControls ) {
1455
+ if ( isCodeEditor ( editor ) ) {
1456
+ const textModel = editor . getModel ( ) ;
1457
+ const controller = DirtyDiffController . get ( editor ) ;
1456
1458
1457
- // set model registry and map to models
1458
- . map ( editor => {
1459
- const codeEditor = editor as CodeEditorWidget ;
1460
- const controller = DirtyDiffController . get ( codeEditor ) ;
1461
1459
if ( controller ) {
1462
1460
controller . modelRegistry = this ;
1463
1461
}
1464
- return codeEditor . getModel ( ) ;
1465
- } )
1466
-
1467
- // remove nulls and duplicates
1468
- . filter ( ( m , i , a ) => ! ! m && ! ! m . uri && a . indexOf ( m , i + 1 ) === - 1 )
1469
-
1470
- // only want resolved text file service models
1471
- . map ( m => this . textFileService . files . get ( m ! . uri ) )
1472
- . filter ( m => m ?. isResolved ( ) ) as IResolvedTextFileEditorModel [ ] ;
1473
-
1474
- const set = new Set ( models ) ;
1475
- const newModels = models . filter ( o => ! this . items . has ( o ) ) ;
1476
- const oldModels = [ ...this . items . keys ( ) ] . filter ( m => ! set . has ( m ) ) ;
1477
-
1478
- oldModels . forEach ( m => this . onModelInvisible ( m ) ) ;
1479
- newModels . forEach ( m => this . onModelVisible ( m ) ) ;
1480
- }
1481
1462
1482
- private onModelVisible ( textFileModel : IResolvedTextFileEditorModel ) : void {
1483
- const model = this . instantiationService . createInstance ( DirtyDiffModel , textFileModel ) ;
1484
- const decorator = new DirtyDiffDecorator ( textFileModel . textEditorModel , model , this . configurationService ) ;
1485
- this . items . set ( textFileModel , new DirtyDiffItem ( model , decorator ) ) ;
1486
- }
1463
+ if ( textModel && ! this . items . has ( textModel . uri ) ) {
1464
+ const textFileModel = this . textFileService . files . get ( textModel . uri ) ;
1487
1465
1488
- private onModelInvisible ( textFileModel : IResolvedTextFileEditorModel ) : void {
1489
- this . items . get ( textFileModel ) ! . dispose ( ) ;
1490
- this . items . delete ( textFileModel ) ;
1491
- }
1466
+ if ( textFileModel ?. isResolved ( ) ) {
1467
+ const dirtyDiffModel = this . instantiationService . createInstance ( DirtyDiffModel , textFileModel ) ;
1468
+ const decorator = new DirtyDiffDecorator ( textFileModel . textEditorModel , dirtyDiffModel , this . configurationService ) ;
1469
+ this . items . set ( textModel . uri , new DirtyDiffItem ( dirtyDiffModel , decorator ) ) ;
1470
+ }
1471
+ }
1472
+ }
1473
+ }
1492
1474
1493
- getModel ( editorModel : ITextModel ) : DirtyDiffModel | null {
1494
- for ( const [ model , diff ] of this . items ) {
1495
- if ( model . textEditorModel . id === editorModel . id ) {
1496
- return diff . model ;
1475
+ for ( const [ uri , item ] of this . items ) {
1476
+ if ( ! this . editorService . isOpened ( { resource : uri , typeId : FILE_EDITOR_INPUT_ID , editorId : DEFAULT_EDITOR_ASSOCIATION . id } ) ) {
1477
+ item . dispose ( ) ;
1478
+ this . items . delete ( uri ) ;
1497
1479
}
1498
1480
}
1481
+ }
1499
1482
1500
- return null ;
1483
+ getModel ( editorModel : ITextModel ) : DirtyDiffModel | undefined {
1484
+ return this . items . get ( editorModel . uri ) ?. model ;
1501
1485
}
1502
1486
1503
1487
override dispose ( ) : void {
0 commit comments