Skip to content

Commit

Permalink
Add keyboard shortcut for Add Comment (#193118)
Browse files Browse the repository at this point in the history
Part of #192377
  • Loading branch information
alexr00 authored Sep 15, 2023
1 parent bccfade commit 6d8c845
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/vs/workbench/contrib/comments/browser/commentsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ class CommentingRangeDecorator {
}
}

private areRangesIntersectingOrTouchingByLine(a: Range, b: Range) {
// Check if `a` is before `b`
if (a.endLineNumber < b.startLineNumber) {
return false;
}

// Check if `b` is before `a`
if (b.endLineNumber < a.startLineNumber) {
return false;
}

// These ranges must intersect
return true;
}

public getMatchedCommentAction(commentRange: Range | undefined): CommentRangeAction[] {
if (commentRange === undefined) {
const foundInfos = this._infos?.filter(info => info.commentingRanges.fileComments);
Expand All @@ -260,7 +275,7 @@ class CommentingRangeDecorator {
const foundHoverActions = new Map<string, { range: Range; action: CommentRangeAction }>();
for (const decoration of this.commentingRangeDecorations) {
const range = decoration.getActiveRange();
if (range && ((range.startLineNumber <= commentRange.startLineNumber) || (commentRange.endLineNumber <= range.endLineNumber))) {
if (range && this.areRangesIntersectingOrTouchingByLine(range, commentRange)) {
// We can have several commenting ranges that match from the same owner because of how
// the line hover and selection decoration is done.
// The ranges must be merged so that we can see if the new commentRange fits within them.
Expand Down Expand Up @@ -863,6 +878,9 @@ export class CommentController implements IEditorContribution {
const newCommentInfos = this._commentingRangeDecorator.getMatchedCommentAction(range);
if (!newCommentInfos.length || !this.editor?.hasModel()) {
this._addInProgress = false;
if (!newCommentInfos.length) {
throw new Error('There are no commenting ranges at the current position.');
}
return Promise.resolve();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ActiveCursorHasCommentingRange, CommentController, ID } from 'vs/workbench/contrib/comments/browser/commentsController';
import { IRange, Range } from 'vs/editor/common/core/range';
import { INotificationService } from 'vs/platform/notification/common/notification';

export class NextCommentThreadAction extends EditorAction {
constructor() {
Expand Down Expand Up @@ -87,9 +88,9 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
});

const ADD_COMMENT_COMMAND = 'workbench.action.addComment';
CommandsRegistry.registerCommand({
KeybindingsRegistry.registerCommandAndKeybindingRule({
id: ADD_COMMENT_COMMAND,
handler: (accessor, args?: { range: IRange; fileComment: boolean }) => {
handler: async (accessor, args?: { range: IRange; fileComment: boolean }) => {
const activeEditor = getActiveEditor(accessor);
if (!activeEditor) {
return Promise.resolve();
Expand All @@ -102,8 +103,15 @@ CommandsRegistry.registerCommand({

const position = args?.range ? new Range(args.range.startLineNumber, args.range.startLineNumber, args.range.endLineNumber, args.range.endColumn)
: (args?.fileComment ? undefined : activeEditor.getSelection());
return controller.addOrToggleCommentAtLine(position, undefined);
}
const notificationService = accessor.get(INotificationService);
try {
await controller.addOrToggleCommentAtLine(position, undefined);
} catch (e) {
notificationService.error(nls.localize('comments.addCommand.error', "The cursor must be within a commenting range to add a comment")); // TODO: Once we have commands to go to next commenting range they should be included as buttons in the error.
}
},
weight: KeybindingWeight.EditorContrib,
primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KeyC,
});

MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
Expand Down

0 comments on commit 6d8c845

Please sign in to comment.