Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InsertCodeBlockAction for diff editors #178632

Merged
merged 1 commit into from Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Codicon } from 'vs/base/common/codicons';
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
import { isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
import { ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { IBulkEditService, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
import { Range } from 'vs/editor/common/core/range';
Expand All @@ -15,6 +15,7 @@ import { INTERACTIVE_SESSION_CATEGORY } from 'vs/workbench/contrib/interactiveSe
import { IInteractiveSessionCopyAction, IInteractiveSessionService, IInteractiveSessionUserActionEvent, InteractiveSessionCopyKind } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionService';
import { IInteractiveResponseViewModel } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionViewModel';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';

export interface IInteractiveSessionCodeBlockActionContext {
code: string;
Expand Down Expand Up @@ -97,28 +98,42 @@ export function registerInteractiveSessionCodeBlockActions() {
const editorService = accessor.get(IEditorService);
const bulkEditService = accessor.get(IBulkEditService);
const interactiveSessionService = accessor.get(IInteractiveSessionService);
const activeEditorControl = editorService.activeTextEditorControl;
if (isCodeEditor(activeEditorControl)) {
const activeModel = activeEditorControl.getModel();
if (!activeModel) {
return;
}
const textFileService = accessor.get(ITextFileService);

let activeEditorControl = editorService.activeTextEditorControl;
if (isDiffEditor(activeEditorControl)) {
activeEditorControl = activeEditorControl.getOriginalEditor().hasTextFocus() ? activeEditorControl.getOriginalEditor() : activeEditorControl.getModifiedEditor();
}

if (!isCodeEditor(activeEditorControl)) {
return;
}

const activeSelection = activeEditorControl.getSelection() ?? new Range(activeModel.getLineCount(), 1, activeModel.getLineCount(), 1);
await bulkEditService.apply([new ResourceTextEdit(activeModel.uri, {
range: activeSelection,
text: context.code,
})]);

interactiveSessionService.notifyUserAction(<IInteractiveSessionUserActionEvent>{
providerId: context.element.providerId,
action: {
kind: 'insert',
responseId: context.element.providerResponseId,
codeBlockIndex: context.codeBlockIndex,
}
});
const activeModel = activeEditorControl.getModel();
if (!activeModel) {
return;
}

const activeTextModel = textFileService.files.get(activeModel.uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand this? Is the point of this just the isReadonly check? And do you want to use activeTextModel instead of activeModel after this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think only ITextFileEditorModel has a readonly property. Not sure if there's a better way to check this without using ITextFileService

if (!activeTextModel || activeTextModel.isReadonly()) {
return;
}

const activeSelection = activeEditorControl.getSelection() ?? new Range(activeModel.getLineCount(), 1, activeModel.getLineCount(), 1);
await bulkEditService.apply([new ResourceTextEdit(activeModel.uri, {
range: activeSelection,
text: context.code,
})]);

interactiveSessionService.notifyUserAction(<IInteractiveSessionUserActionEvent>{
providerId: context.element.providerId,
action: {
kind: 'insert',
responseId: context.element.providerResponseId,
codeBlockIndex: context.codeBlockIndex,
}
});
}

});
}