@@ -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,15 @@ 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 . _stopAll ( ) ;
465+ other . decorations . set ( decorations ) ;
466+ other . _hasWordHighlights . set ( other . hasDecorations ( ) ) ;
467+ }
468+ }
456469 }
457470
458471 public dispose ( ) : void {
@@ -470,13 +483,15 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
470483 }
471484
472485 private wordHighlighter : WordHighlighter | null ;
486+ private linkedContributions : Set < WordHighlighterContribution > ;
473487
474488 constructor ( editor : ICodeEditor , @IContextKeyService contextKeyService : IContextKeyService , @ILanguageFeaturesService languageFeaturesService : ILanguageFeaturesService ) {
475489 super ( ) ;
476490 this . wordHighlighter = null ;
491+ this . linkedContributions = new Set ( ) ;
477492 const createWordHighlighterIfPossible = ( ) => {
478493 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 ) ;
480495 }
481496 } ;
482497 this . _register ( editor . onDidChangeModel ( ( e ) => {
@@ -514,6 +529,19 @@ export class WordHighlighterContribution extends Disposable implements IEditorCo
514529 this . wordHighlighter ?. stop ( ) ;
515530 }
516531
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+
517545 public override dispose ( ) : void {
518546 if ( this . wordHighlighter ) {
519547 this . wordHighlighter . dispose ( ) ;
0 commit comments