@@ -9,7 +9,7 @@ import { CancelablePromise, createCancelablePromise, first, timeout } from 'vs/b
99import { CancellationToken } from 'vs/base/common/cancellation' ;
1010import { onUnexpectedError , onUnexpectedExternalError } from 'vs/base/common/errors' ;
1111import { 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' ;
1313import { IActiveCodeEditor , ICodeEditor } from 'vs/editor/browser/editorBrowser' ;
1414import { EditorAction , EditorContributionInstantiation , IActionOptions , registerEditorAction , registerEditorContribution , registerModelAndPositionCommand } from 'vs/editor/browser/editorExtensions' ;
1515import { EditorOption } from 'vs/editor/common/config/editorOptions' ;
@@ -29,6 +29,7 @@ import { IWordAtPosition } from 'vs/editor/common/core/wordHelper';
2929import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry' ;
3030import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures' ;
3131import { getHighlightDecorationOptions } from 'vs/editor/contrib/wordHighlighter/browser/highlightDecorations' ;
32+ import { Iterable } from 'vs/base/common/iterator' ;
3233
3334const ctxHasWordHighlights = new RawContextKey < boolean > ( 'hasWordHighlights' , false ) ;
3435
@@ -192,9 +193,12 @@ class WordHighlighter {
192193 private readonly _hasWordHighlights : IContextKey < boolean > ;
193194 private _ignorePositionChangeEvent : boolean ;
194195
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 ) {
196199 this . editor = editor ;
197200 this . providers = providers ;
201+ this . linkedHighlighters = linkedHighlighters ;
198202 this . _hasWordHighlights = ctxHasWordHighlights . bindTo ( contextKeyService ) ;
199203 this . _ignorePositionChangeEvent = false ;
200204 this . occurrencesHighlight = this . editor . getOption ( EditorOption . occurrencesHighlight ) ;
@@ -453,6 +457,14 @@ class WordHighlighter {
453457
454458 this . decorations . set ( decorations ) ;
455459 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 . decorations . set ( decorations ) ;
465+ other . _hasWordHighlights . set ( other . hasDecorations ( ) ) ;
466+ }
467+ }
456468 }
457469
458470 public dispose ( ) : void {
@@ -470,13 +482,15 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
470482 }
471483
472484 private wordHighlighter : WordHighlighter | null ;
485+ private linkedContributions : Set < WordHighlighterContribution > ;
473486
474487 constructor ( editor : ICodeEditor , @IContextKeyService contextKeyService : IContextKeyService , @ILanguageFeaturesService languageFeaturesService : ILanguageFeaturesService ) {
475488 super ( ) ;
476489 this . wordHighlighter = null ;
490+ this . linkedContributions = new Set ( ) ;
477491 const createWordHighlighterIfPossible = ( ) => {
478492 if ( editor . hasModel ( ) ) {
479- this . wordHighlighter = new WordHighlighter ( editor , languageFeaturesService . documentHighlightProvider , contextKeyService ) ;
493+ this . wordHighlighter = new WordHighlighter ( editor , languageFeaturesService . documentHighlightProvider , ( ) => Iterable . map ( this . linkedContributions , c => c . wordHighlighter ) , contextKeyService ) ;
480494 }
481495 } ;
482496 this . _register ( editor . onDidChangeModel ( ( e ) => {
@@ -514,6 +528,19 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
514528 this . wordHighlighter ?. stop ( ) ;
515529 }
516530
531+ public linkWordHighlighters ( editor : ICodeEditor ) : IDisposable {
532+ const other = WordHighlighterContribution . get ( editor ) ;
533+ if ( ! other ) {
534+ return Disposable . None ;
535+ }
536+ this . linkedContributions . add ( other ) ;
537+ other . linkedContributions . add ( this ) ;
538+ return toDisposable ( ( ) => {
539+ this . linkedContributions . delete ( other ) ;
540+ other . linkedContributions . delete ( this ) ;
541+ } ) ;
542+ }
543+
517544 public override dispose ( ) : void {
518545 if ( this . wordHighlighter ) {
519546 this . wordHighlighter . dispose ( ) ;
0 commit comments