Skip to content

Commit eb6527c

Browse files
authored
Cannot apply code snippet into editor when chat is opened as editor (microsoft#239223)
1 parent 060de16 commit eb6527c

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/vs/workbench/contrib/chat/browser/actions/codeBlockOperations.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { isCancellationError } from '../../../../../base/common/errors.js';
1010
import { isEqual } from '../../../../../base/common/resources.js';
1111
import * as strings from '../../../../../base/common/strings.js';
1212
import { URI } from '../../../../../base/common/uri.js';
13-
import { IActiveCodeEditor, isCodeEditor, isDiffEditor } from '../../../../../editor/browser/editorBrowser.js';
13+
import { getCodeEditor, IActiveCodeEditor } from '../../../../../editor/browser/editorBrowser.js';
1414
import { IBulkEditService, ResourceTextEdit } from '../../../../../editor/browser/services/bulkEditService.js';
1515
import { ICodeEditorService } from '../../../../../editor/browser/services/codeEditorService.js';
1616
import { Range } from '../../../../../editor/common/core/range.js';
@@ -129,11 +129,14 @@ export class ApplyCodeBlockOperation {
129129
if (codemapperUri && !isEqual(activeEditorControl?.getModel().uri, codemapperUri)) {
130130
// reveal the target file
131131
try {
132-
await this.editorService.openEditor({ resource: codemapperUri });
133-
134-
activeEditorControl = getEditableActiveCodeEditor(this.editorService);
135-
if (activeEditorControl) {
136-
this.tryToRevealCodeBlock(activeEditorControl, context.code);
132+
const editorPane = await this.editorService.openEditor({ resource: codemapperUri });
133+
const codeEditor = getCodeEditor(editorPane?.getControl());
134+
if (codeEditor && codeEditor.hasModel()) {
135+
this.tryToRevealCodeBlock(codeEditor, context.code);
136+
activeEditorControl = codeEditor;
137+
} else {
138+
this.notify(localize('applyCodeBlock.errorOpeningFile', "Failed to open {0} in a code editor.", codemapperUri.toString()));
139+
return;
137140
}
138141
} catch (e) {
139142
this.logService.info('[ApplyCodeBlockOperation] error opening code mapper file', codemapperUri, e);
@@ -354,19 +357,20 @@ function getEditableActiveCodeEditor(editorService: IEditorService): IActiveCode
354357
return activeCodeEditorInNotebook;
355358
}
356359

357-
let activeEditorControl = editorService.activeTextEditorControl;
358-
if (isDiffEditor(activeEditorControl)) {
359-
activeEditorControl = activeEditorControl.getOriginalEditor().hasTextFocus() ? activeEditorControl.getOriginalEditor() : activeEditorControl.getModifiedEditor();
360-
}
361-
362-
if (!isCodeEditor(activeEditorControl)) {
363-
return undefined;
360+
let codeEditor = getCodeEditor(editorService.activeTextEditorControl);
361+
if (!codeEditor) {
362+
for (const editor of editorService.visibleTextEditorControls) {
363+
codeEditor = getCodeEditor(editor);
364+
if (codeEditor) {
365+
break;
366+
}
367+
}
364368
}
365369

366-
if (!activeEditorControl.hasModel()) {
370+
if (!codeEditor || !codeEditor.hasModel()) {
367371
return undefined;
368372
}
369-
return activeEditorControl;
373+
return codeEditor;
370374
}
371375

372376
function isReadOnly(model: ITextModel, textFileService: ITextFileService): boolean {

0 commit comments

Comments
 (0)