@@ -9,7 +9,7 @@ import { CancelablePromise, createCancelablePromise, first, timeout } from 'vs/b
9
9
import { CancellationToken } from 'vs/base/common/cancellation' ;
10
10
import { onUnexpectedError , onUnexpectedExternalError } from 'vs/base/common/errors' ;
11
11
import { KeyCode , KeyMod } from 'vs/base/common/keyCodes' ;
12
- import { Disposable , DisposableStore } from 'vs/base/common/lifecycle' ;
12
+ import { Disposable , DisposableStore , IDisposable , toDisposable } from 'vs/base/common/lifecycle' ;
13
13
import { IActiveCodeEditor , ICodeEditor } from 'vs/editor/browser/editorBrowser' ;
14
14
import { EditorAction , EditorContributionInstantiation , IActionOptions , registerEditorAction , registerEditorContribution , registerModelAndPositionCommand } from 'vs/editor/browser/editorExtensions' ;
15
15
import { EditorOption } from 'vs/editor/common/config/editorOptions' ;
@@ -29,6 +29,7 @@ import { IWordAtPosition } from 'vs/editor/common/core/wordHelper';
29
29
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry' ;
30
30
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures' ;
31
31
import { getHighlightDecorationOptions } from 'vs/editor/contrib/wordHighlighter/browser/highlightDecorations' ;
32
+ import { Iterable } from 'vs/base/common/iterator' ;
32
33
33
34
const ctxHasWordHighlights = new RawContextKey < boolean > ( 'hasWordHighlights' , false ) ;
34
35
@@ -192,9 +193,12 @@ class WordHighlighter {
192
193
private readonly _hasWordHighlights : IContextKey < boolean > ;
193
194
private _ignorePositionChangeEvent : boolean ;
194
195
195
- constructor ( editor : IActiveCodeEditor , providers : LanguageFeatureRegistry < DocumentHighlightProvider > , contextKeyService : IContextKeyService ) {
196
+ private readonly linkedHighlighters : ( ) => Iterable < WordHighlighter | null > ;
197
+
198
+ constructor ( editor : IActiveCodeEditor , providers : LanguageFeatureRegistry < DocumentHighlightProvider > , linkedHighlighters : ( ) => Iterable < WordHighlighter | null > , contextKeyService : IContextKeyService ) {
196
199
this . editor = editor ;
197
200
this . providers = providers ;
201
+ this . linkedHighlighters = linkedHighlighters ;
198
202
this . _hasWordHighlights = ctxHasWordHighlights . bindTo ( contextKeyService ) ;
199
203
this . _ignorePositionChangeEvent = false ;
200
204
this . occurrencesHighlight = this . editor . getOption ( EditorOption . occurrencesHighlight ) ;
@@ -453,6 +457,15 @@ class WordHighlighter {
453
457
454
458
this . decorations . set ( decorations ) ;
455
459
this . _hasWordHighlights . set ( this . hasDecorations ( ) ) ;
460
+
461
+ // update decorators of friends
462
+ for ( const other of this . linkedHighlighters ( ) ) {
463
+ if ( other ?. editor . getModel ( ) === this . editor . getModel ( ) ) {
464
+ other . _stopAll ( ) ;
465
+ other . decorations . set ( decorations ) ;
466
+ other . _hasWordHighlights . set ( other . hasDecorations ( ) ) ;
467
+ }
468
+ }
456
469
}
457
470
458
471
public dispose ( ) : void {
@@ -470,13 +483,15 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
470
483
}
471
484
472
485
private wordHighlighter : WordHighlighter | null ;
486
+ private linkedContributions : Set < WordHighlighterContribution > ;
473
487
474
488
constructor ( editor : ICodeEditor , @IContextKeyService contextKeyService : IContextKeyService , @ILanguageFeaturesService languageFeaturesService : ILanguageFeaturesService ) {
475
489
super ( ) ;
476
490
this . wordHighlighter = null ;
491
+ this . linkedContributions = new Set ( ) ;
477
492
const createWordHighlighterIfPossible = ( ) => {
478
493
if ( editor . hasModel ( ) ) {
479
- this . wordHighlighter = new WordHighlighter ( editor , languageFeaturesService . documentHighlightProvider , contextKeyService ) ;
494
+ this . wordHighlighter = new WordHighlighter ( editor , languageFeaturesService . documentHighlightProvider , ( ) => Iterable . map ( this . linkedContributions , c => c . wordHighlighter ) , contextKeyService ) ;
480
495
}
481
496
} ;
482
497
this . _register ( editor . onDidChangeModel ( ( e ) => {
@@ -514,6 +529,19 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
514
529
this . wordHighlighter ?. stop ( ) ;
515
530
}
516
531
532
+ public linkWordHighlighters ( editor : ICodeEditor ) : IDisposable {
533
+ const other = WordHighlighterContribution . get ( editor ) ;
534
+ if ( ! other ) {
535
+ return Disposable . None ;
536
+ }
537
+ this . linkedContributions . add ( other ) ;
538
+ other . linkedContributions . add ( this ) ;
539
+ return toDisposable ( ( ) => {
540
+ this . linkedContributions . delete ( other ) ;
541
+ other . linkedContributions . delete ( this ) ;
542
+ } ) ;
543
+ }
544
+
517
545
public override dispose ( ) : void {
518
546
if ( this . wordHighlighter ) {
519
547
this . wordHighlighter . dispose ( ) ;
0 commit comments