diff --git a/src/vs/workbench/contrib/comments/browser/commentsController.ts b/src/vs/workbench/contrib/comments/browser/commentsController.ts index 636de0344f6a2..fddb689229c59 100644 --- a/src/vs/workbench/contrib/comments/browser/commentsController.ts +++ b/src/vs/workbench/contrib/comments/browser/commentsController.ts @@ -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); @@ -260,7 +275,7 @@ class CommentingRangeDecorator { const foundHoverActions = new Map(); 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. @@ -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(); } diff --git a/src/vs/workbench/contrib/comments/browser/commentsEditorContribution.ts b/src/vs/workbench/contrib/comments/browser/commentsEditorContribution.ts index 116782e436446..b12521cf40b53 100644 --- a/src/vs/workbench/contrib/comments/browser/commentsEditorContribution.ts +++ b/src/vs/workbench/contrib/comments/browser/commentsEditorContribution.ts @@ -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() { @@ -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(); @@ -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, {