Skip to content

Commit 10e4d7a

Browse files
committed
Update code block actions context when model changes
Possible fix for microsoft#252105 I'm having trouble repoing microsoft#252105 but one possible cause is that the text model changes outside of the codeBlockPart getting updated
1 parent 194193e commit 10e4d7a

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/vs/workbench/contrib/chat/browser/codeBlockPart.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ export function parseLocalFileData(text: string) {
132132
}
133133

134134
export interface ICodeBlockActionContext {
135-
code: string;
136-
codemapperUri?: URI;
137-
languageId?: string;
138-
codeBlockIndex: number;
139-
element: unknown;
135+
readonly code: string;
136+
readonly codemapperUri?: URI;
137+
readonly languageId?: string;
138+
readonly codeBlockIndex: number;
139+
readonly element: unknown;
140140

141-
chatSessionId: string | undefined;
141+
readonly chatSessionId: string | undefined;
142142
}
143143

144144
export interface ICodeBlockRenderOptions {
@@ -287,6 +287,14 @@ export class CodeBlockPart extends Disposable {
287287
this.element.classList.add('focused');
288288
WordHighlighterContribution.get(this.editor)?.restoreViewState(true);
289289
}));
290+
this._register(Event.any(
291+
this.editor.onDidChangeModel,
292+
this.editor.onDidChangeModelContent
293+
)(() => {
294+
if (this.currentCodeBlockData) {
295+
this.updateContexts(this.currentCodeBlockData);
296+
}
297+
}));
290298

291299
// Parent list scrolled
292300
if (delegate.onDidScroll) {
@@ -455,15 +463,7 @@ export class CodeBlockPart extends Disposable {
455463
this.editor.revealRangeInCenter(data.range, ScrollType.Immediate);
456464
}
457465

458-
this.toolbar.context = {
459-
code: textModel.getTextBuffer().getValueInRange(data.range ?? textModel.getFullModelRange(), EndOfLinePreference.TextDefined),
460-
codeBlockIndex: data.codeBlockIndex,
461-
element: data.element,
462-
languageId: textModel.getLanguageId(),
463-
codemapperUri: data.codemapperUri,
464-
chatSessionId: data.chatSessionId
465-
} satisfies ICodeBlockActionContext;
466-
this.resourceContextKey.set(textModel.uri);
466+
this.updateContexts(data);
467467
}
468468

469469
private getVulnerabilitiesLabel(): string {
@@ -477,6 +477,23 @@ export class CodeBlockPart extends Disposable {
477477
const icon = (element: IChatResponseViewModel) => element.vulnerabilitiesListExpanded ? Codicon.chevronDown : Codicon.chevronRight;
478478
return `${referencesLabel} $(${icon(this.currentCodeBlockData.element as IChatResponseViewModel).id})`;
479479
}
480+
481+
private updateContexts(data: ICodeBlockData) {
482+
const textModel = this.editor.getModel();
483+
if (!textModel) {
484+
return;
485+
}
486+
487+
this.toolbar.context = {
488+
code: textModel.getTextBuffer().getValueInRange(data.range ?? textModel.getFullModelRange(), EndOfLinePreference.TextDefined),
489+
codeBlockIndex: data.codeBlockIndex,
490+
element: data.element,
491+
languageId: textModel.getLanguageId(),
492+
codemapperUri: data.codemapperUri,
493+
chatSessionId: data.chatSessionId
494+
} satisfies ICodeBlockActionContext;
495+
this.resourceContextKey.set(textModel.uri);
496+
}
480497
}
481498

482499
export class ChatCodeBlockContentProvider extends Disposable implements ITextModelContentProvider {

0 commit comments

Comments
 (0)