Skip to content

Commit 5d4149b

Browse files
authored
Merge pull request microsoft#185351 from microsoft/joh/smoggy-alligator
for embedded diff editors link word highlighters
2 parents aedd803 + 5fbb86a commit 5d4149b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/vs/editor/contrib/wordHighlighter/browser/wordHighlighter.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { CancelablePromise, createCancelablePromise, first, timeout } from 'vs/b
99
import { CancellationToken } from 'vs/base/common/cancellation';
1010
import { onUnexpectedError, onUnexpectedExternalError } from 'vs/base/common/errors';
1111
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';
1313
import { IActiveCodeEditor, ICodeEditor } from 'vs/editor/browser/editorBrowser';
1414
import { EditorAction, EditorContributionInstantiation, IActionOptions, registerEditorAction, registerEditorContribution, registerModelAndPositionCommand } from 'vs/editor/browser/editorExtensions';
1515
import { EditorOption } from 'vs/editor/common/config/editorOptions';
@@ -29,6 +29,7 @@ import { IWordAtPosition } from 'vs/editor/common/core/wordHelper';
2929
import { LanguageFeatureRegistry } from 'vs/editor/common/languageFeatureRegistry';
3030
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
3131
import { getHighlightDecorationOptions } from 'vs/editor/contrib/wordHighlighter/browser/highlightDecorations';
32+
import { Iterable } from 'vs/base/common/iterator';
3233

3334
const 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();

src/vs/workbench/contrib/inlineChat/browser/inlineChatLivePreviewWidget.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { EditOperation } from 'vs/editor/common/core/editOperation';
3333
import { Session } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
3434
import { ILanguageService } from 'vs/editor/common/languages/language';
3535
import { FoldingController } from 'vs/editor/contrib/folding/browser/folding';
36+
import { WordHighlighterContribution } from 'vs/editor/contrib/wordHighlighter/browser/wordHighlighter';
3637

3738
export class InlineChatLivePreviewWidget extends ZoneWidget {
3839

@@ -85,12 +86,18 @@ export class InlineChatLivePreviewWidget extends ZoneWidget {
8586
originalEditor: { contributions: diffContributions },
8687
modifiedEditor: { contributions: diffContributions }
8788
}, editor);
89+
8890
this._disposables.add(this._diffEditor);
8991
this._diffEditor.setModel({ original: this._session.textModel0, modified: editor.getModel() });
9092
this._diffEditor.updateOptions({
9193
lineDecorationsWidth: editor.getLayoutInfo().decorationsWidth
9294
});
9395

96+
const highlighter = WordHighlighterContribution.get(editor);
97+
if (highlighter) {
98+
this._disposables.add(highlighter.linkWordHighlighters(this._diffEditor.getModifiedEditor()));
99+
}
100+
94101
const doStyle = () => {
95102
const theme = themeService.getColorTheme();
96103
const overrides: [target: string, source: string][] = [

0 commit comments

Comments
 (0)