Skip to content

Commit

Permalink
Unable to disable/remap Escape key closing CommentThreadWidget
Browse files Browse the repository at this point in the history
Fixes #231674
  • Loading branch information
alexr00 committed Oct 28, 2024
1 parent a14300e commit d15d213
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IContextMenuService } from '../../../../platform/contextview/browser/co
import { MarshalledId } from '../../../../base/common/marshallingIds.js';
import { StandardMouseEvent } from '../../../../base/browser/mouseEvent.js';
import { MarshalledCommentThread } from '../../../common/comments.js';
import { CommentCommandId } from '../common/commentCommandIds.js';

const collapseIcon = registerIcon('review-comment-collapse', Codicon.chevronUp, nls.localize('collapseIcon', 'Icon to collapse a review comment.'));
const COLLAPSE_ACTION_CLASS = 'expand-review-action ' + ThemeIcon.asClassName(collapseIcon);
Expand Down Expand Up @@ -68,7 +69,7 @@ export class CommentThreadHeader<T = IRange> extends Disposable {
this._register(this._actionbarWidget);

const collapseClass = threadHasComments(this._commentThread.comments) ? COLLAPSE_ACTION_CLASS : DELETE_ACTION_CLASS;
this._collapseAction = new Action('review.expand', nls.localize('label.collapse', "Collapse"), collapseClass, true, () => this._delegate.collapse());
this._collapseAction = new Action(CommentCommandId.Hide, nls.localize('label.collapse', "Collapse"), collapseClass, true, () => this._delegate.collapse());
if (!threadHasComments(this._commentThread.comments)) {
const commentsChanged: MutableDisposable<IDisposable> = this._register(new MutableDisposable());
commentsChanged.value = this._commentThread.onDidChangeComments(() => {
Expand Down
12 changes: 3 additions & 9 deletions src/vs/workbench/contrib/comments/browser/commentThreadWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { AccessibilityVerbositySettingId } from '../../accessibility/browser/acc
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';
import { AccessibilityCommandId } from '../../accessibility/common/accessibilityCommands.js';
import { LayoutableEditor } from './simpleCommentEditor.js';
import { DomEmitter } from '../../../../base/browser/event.js';
import { isCodeEditor } from '../../../../editor/browser/editorBrowser.js';

export const COMMENTEDITOR_DECORATION_KEY = 'commenteditordecoration';
Expand Down Expand Up @@ -157,14 +156,6 @@ export class CommentThreadWidget<T extends IRange | ICellRange = IRange> extends
}

this.currentThreadListeners();
this._register(new DomEmitter(this.container, 'keydown').event(e => {
if (dom.isKeyboardEvent(e) && e.key === 'Escape') {
if (Range.isIRange(this.commentThread.range) && isCodeEditor(this._parentEditor)) {
this._parentEditor.setSelection(this.commentThread.range);
}
this.collapse();
}
}));
}

private _setAriaLabel(): void {
Expand Down Expand Up @@ -384,6 +375,9 @@ export class CommentThreadWidget<T extends IRange | ICellRange = IRange> extends
}

collapse() {
if (Range.isIRange(this.commentThread.range) && isCodeEditor(this._parentEditor)) {
this._parentEditor.setSelection(this.commentThread.range);
}
this._containerDelegate.collapse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ export class ReviewZoneWidget extends ZoneWidget implements ICommentThreadWidget
this._refresh(this._commentThreadWidget.getDimensions());
}

collapseAndFocusRange() {
this._commentThreadWidget.collapse();
}

override hide() {
if (this._isExpanded) {
this._isExpanded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1388,12 +1388,8 @@ export class CommentController implements IEditorContribution {
}
}

public closeWidget(): void {
this._commentWidgets?.forEach(widget => widget.hide());
if (this.editor) {
this.editor.focus();
this.editor.revealRangeInCenter(this.editor.getSelection()!);
}
public collapseAndFocusRange(threadId: string): void {
this._commentWidgets?.find(widget => widget.commentThread.threadId === threadId)?.collapseAndFocusRange();
}

private removeCommentWidgetsAndStoreCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,32 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
weight: KeybindingWeight.EditorContrib,
primary: KeyCode.Escape,
secondary: [KeyMod.Shift | KeyCode.Escape],
when: ctxCommentEditorFocused,
when: ContextKeyExpr.or(ctxCommentEditorFocused, CommentContextKeys.commentFocused),
handler: (accessor, args) => {
const activeCodeEditor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
if (activeCodeEditor instanceof SimpleCommentEditor) {
activeCodeEditor.getParentThread().collapse();
} else if (activeCodeEditor) {
const controller = CommentController.get(activeCodeEditor);
if (!controller) {
return;
}
const notificationService = accessor.get(INotificationService);
const commentService = accessor.get(ICommentService);
let error = false;
try {
const activeComment = commentService.lastActiveCommentcontroller?.activeComment;
if (!activeComment) {
error = true;
} else {
controller.collapseAndFocusRange(activeComment.thread.threadId);
}
} catch (e) {
error = true;
}
if (error) {
notificationService.error(nls.localize('comments.focusCommand.error', "The cursor must be on a line with a comment to focus the comment"));
}
}
}
});
Expand Down

0 comments on commit d15d213

Please sign in to comment.