Skip to content

Commit

Permalink
Add next/previous commenting range commands+keybindings
Browse files Browse the repository at this point in the history
Part of #192377
  • Loading branch information
alexr00 committed Sep 15, 2023
1 parent bc6b75e commit f9f514a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 5 deletions.
66 changes: 64 additions & 2 deletions src/vs/workbench/contrib/comments/browser/commentsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ class CommentingRangeDecorator {

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

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

Expand Down Expand Up @@ -301,6 +301,46 @@ class CommentingRangeDecorator {
}).map(actions => actions.action);
}

public getNearestCommentingRange(findPosition: Position, reverse?: boolean): Range | undefined {
let findPositionContainedWithin: Range | undefined;
let decorations: CommentingRangeDecoration[];
if (reverse) {
decorations = [];
for (let i = this.commentingRangeDecorations.length - 1; i >= 0; i--) {
decorations.push(this.commentingRangeDecorations[i]);
}
} else {
decorations = this.commentingRangeDecorations;
}
for (const decoration of decorations) {
const range = decoration.getActiveRange();
if (!range) {
continue;
}

if (findPositionContainedWithin && this.areRangesIntersectingOrTouchingByLine(range, findPositionContainedWithin)) {
findPositionContainedWithin = Range.plusRange(findPositionContainedWithin, range);
continue;
}

if (range.startLineNumber <= findPosition.lineNumber && findPosition.lineNumber <= range.endLineNumber) {
findPositionContainedWithin = new Range(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn);
continue;
}

if (!reverse && range.endLineNumber < findPosition.lineNumber) {
continue;
}

if (reverse && range.startLineNumber > findPosition.lineNumber) {
continue;
}

return range;
}
return decorations[0].getActiveRange() ?? undefined;
}

public dispose(): void {
this.commentingRangeDecorations = [];
}
Expand Down Expand Up @@ -669,6 +709,28 @@ export class CommentController implements IEditorContribution {
this._findNearestCommentThread(true);
}

private _findNearestCommentingRange(reverse?: boolean): void {
if (!this.editor?.hasModel()) {
return;
}

const after = this.editor.getSelection().getEndPosition();
const range = this._commentingRangeDecorator.getNearestCommentingRange(after, reverse);
if (range) {
const position = reverse ? range.getEndPosition() : range.getStartPosition();
this.editor.setPosition(position);
this.editor.revealLineInCenterIfOutsideViewport(position.lineNumber);
}
}

public nextCommentingRange(): void {
this._findNearestCommentingRange();
}

public previousCommentingRange(): void {
this._findNearestCommentingRange(true);
}

public dispose(): void {
this.globalToDispose.dispose();
this.localToDispose.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { KeyChord, KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import 'vs/css!./media/review';
import { IActiveCodeEditor, ICodeEditor, isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction, EditorContributionInstantiation, registerEditorAction, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
Expand Down Expand Up @@ -63,11 +63,55 @@ export class PreviousCommentThreadAction extends EditorAction {
}
}


registerEditorContribution(ID, CommentController, EditorContributionInstantiation.AfterFirstRender);
registerEditorAction(NextCommentThreadAction);
registerEditorAction(PreviousCommentThreadAction);

export class NextCommentingRangeAction extends EditorAction {
constructor() {
super({
id: 'editor.action.goToNextCommentingRange',
label: nls.localize('goToNextCommentingRange', "Go to Next Commenting Range"),
alias: 'Go to Next Commenting Range',
precondition: WorkspaceHasCommenting,
kbOpts: {
kbExpr: EditorContextKeys.focus,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.DownArrow),
weight: KeybindingWeight.EditorContrib
}
});
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
const controller = CommentController.get(editor);
controller?.nextCommentingRange();
}
}

export class PreviousCommentingRangeAction extends EditorAction {
constructor() {
super({
id: 'editor.action.goToPreviousCommentingRange',
label: nls.localize('goToPreviousCommentingRange', "Go to Previous Commenting Range"),
alias: 'Go to Next Commenting Range',
precondition: WorkspaceHasCommenting,
kbOpts: {
kbExpr: EditorContextKeys.focus,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.UpArrow),
weight: KeybindingWeight.EditorContrib
}
});
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
const controller = CommentController.get(editor);
controller?.previousCommentingRange();
}
}

registerEditorAction(NextCommentingRangeAction);
registerEditorAction(PreviousCommentingRangeAction);

const TOGGLE_COMMENTING_COMMAND = 'workbench.action.toggleCommenting';
CommandsRegistry.registerCommand({
id: TOGGLE_COMMENTING_COMMAND,
Expand Down Expand Up @@ -111,7 +155,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
}
},
weight: KeybindingWeight.EditorContrib,
primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KeyC,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KeyC),
});

MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
Expand Down

0 comments on commit f9f514a

Please sign in to comment.