Skip to content

Commit

Permalink
Add a context key for active editor commenting ranges
Browse files Browse the repository at this point in the history
Part of #192377
  • Loading branch information
alexr00 committed Sep 19, 2023
1 parent ef023da commit 9aaf518
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 33 deletions.
11 changes: 3 additions & 8 deletions src/vs/workbench/contrib/comments/browser/commentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import { CommentThreadChangedEvent, CommentInfo, Comment, CommentReaction, CommentingRanges, CommentThread, CommentOptions, PendingCommentThread } from 'vs/editor/common/languages';
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { Event, Emitter } from 'vs/base/common/event';
Expand All @@ -17,16 +16,12 @@ import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { COMMENTS_SECTION, ICommentsConfiguration } from 'vs/workbench/contrib/comments/common/commentsConfiguration';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { CommentContextKeys } from 'vs/workbench/contrib/comments/common/commentContextKeys';

export const ICommentService = createDecorator<ICommentService>('commentService');

export const WorkspaceHasCommenting = new RawContextKey<boolean>('workspaceHasCommenting', false, {
description: nls.localize('hasCommentingProvider', "Whether the open workspace has either comments or commenting ranges."),
type: 'boolean'
});

interface IResourceCommentThreadEvent {
resource: URI;
commentInfos: ICommentInfo[];
Expand Down Expand Up @@ -174,7 +169,7 @@ export class CommentService extends Disposable implements ICommentService {
super();
this._handleConfiguration();
this._handleZenMode();
this._workspaceHasCommenting = WorkspaceHasCommenting.bindTo(contextKeyService);
this._workspaceHasCommenting = CommentContextKeys.WorkspaceHasCommenting.bindTo(contextKeyService);
const storageListener = this._register(new DisposableStore());

storageListener.add(this.storageService.onDidChangeValue(StorageScope.WORKSPACE, CONTINUE_ON_COMMENTS, storageListener)((v) => {
Expand Down
35 changes: 19 additions & 16 deletions src/vs/workbench/contrib/comments/browser/commentsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
import { COMMENTS_SECTION, ICommentsConfiguration } from 'vs/workbench/contrib/comments/common/commentsConfiguration';
import { COMMENTEDITOR_DECORATION_KEY } from 'vs/workbench/contrib/comments/browser/commentReply';
import { Emitter } from 'vs/base/common/event';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { Position } from 'vs/editor/common/core/position';
import { CommentThreadRangeDecorator } from 'vs/workbench/contrib/comments/browser/commentThreadRangeDecorator';
import { ICursorSelectionChangedEvent } from 'vs/editor/common/cursorEvents';
import { CommentsPanel } from 'vs/workbench/contrib/comments/browser/commentsView';
import { status } from 'vs/base/browser/ui/aria/aria';
import { CommentContextKeys } from 'vs/workbench/contrib/comments/common/commentContextKeys';

export const ID = 'editor.contrib.review';

Expand Down Expand Up @@ -347,11 +348,6 @@ class CommentingRangeDecorator {
}
}

export const ActiveCursorHasCommentingRange = new RawContextKey<boolean>('activeCursorHasCommentingRange', false, {
description: nls.localize('hasCommentingRange', "Whether the position at the active cursor has a commenting range"),
type: 'boolean'
});

export class CommentController implements IEditorContribution {
private readonly globalToDispose = new DisposableStore();
private readonly localToDispose = new DisposableStore();
Expand All @@ -371,7 +367,8 @@ export class CommentController implements IEditorContribution {
private _pendingEditsCache: { [key: string]: { [key: string]: { [key: number]: string } } }; // owner -> threadId -> uniqueIdInThread -> pending comment
private _editorDisposables: IDisposable[] = [];
private _activeCursorHasCommentingRange: IContextKey<boolean>;
private _hasProvidedAriaStatus: boolean = false;
private _activeEditorHasCommentingRange: IContextKey<boolean>;
private _hasRespondedToEditorChange: boolean = false;

constructor(
editor: ICodeEditor,
Expand All @@ -390,7 +387,8 @@ export class CommentController implements IEditorContribution {
this._pendingNewCommentCache = {};
this._pendingEditsCache = {};
this._computePromise = null;
this._activeCursorHasCommentingRange = ActiveCursorHasCommentingRange.bindTo(contextKeyService);
this._activeCursorHasCommentingRange = CommentContextKeys.activeCursorHasCommentingRange.bindTo(contextKeyService);
this._activeEditorHasCommentingRange = CommentContextKeys.activeEditorHasCommentingRange.bindTo(contextKeyService);

if (editor instanceof EmbeddedCodeEditorWidget) {
return;
Expand Down Expand Up @@ -419,8 +417,8 @@ export class CommentController implements IEditorContribution {
}
this.beginCompute();
}));
this.globalToDispose.add(this.commentService.onDidSetDataProvider(_ => this.beginComputeAndProvideStatus()));
this.globalToDispose.add(this.commentService.onDidUpdateCommentingRanges(_ => this.beginComputeAndProvideStatus()));
this.globalToDispose.add(this.commentService.onDidSetDataProvider(_ => this.beginComputeAndHandleEditorChange()));
this.globalToDispose.add(this.commentService.onDidUpdateCommentingRanges(_ => this.beginComputeAndHandleEditorChange()));

this.globalToDispose.add(this.commentService.onDidSetResourceCommentInfos(e => {
const editorURI = this.editor && this.editor.hasModel() && this.editor.getModel().uri;
Expand Down Expand Up @@ -750,7 +748,7 @@ export class CommentController implements IEditorContribution {
return;
}

this._hasProvidedAriaStatus = false;
this._hasRespondedToEditorChange = false;

this.localToDispose.add(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
this.localToDispose.add(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
Expand Down Expand Up @@ -841,14 +839,19 @@ export class CommentController implements IEditorContribution {
this._commentThreadRangeDecorator.update(this.editor, commentInfo);
}));

this.beginComputeAndProvideStatus();
this.beginComputeAndHandleEditorChange();
}

private beginComputeAndProvideStatus(): void {
private beginComputeAndHandleEditorChange(): void {
this.beginCompute().then(() => {
if (!this._hasProvidedAriaStatus && this._commentInfos.some(commentInfo => commentInfo.commentingRanges.ranges.length > 0 || commentInfo.commentingRanges.fileComments)) {
this._hasProvidedAriaStatus = true;
status(nls.localize('hasCommentRanges', "Editor has commenting ranges."));
if (!this._hasRespondedToEditorChange) {
if (this._commentInfos.some(commentInfo => commentInfo.commentingRanges.ranges.length > 0 || commentInfo.commentingRanges.fileComments)) {
this._hasRespondedToEditorChange = true;
this._activeEditorHasCommentingRange.set(true);
status(nls.localize('hasCommentRanges', "Editor has commenting ranges."));
} else {
this._activeEditorHasCommentingRange.set(false);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import * as nls from 'vs/nls';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ICommentService, WorkspaceHasCommenting } from 'vs/workbench/contrib/comments/browser/commentService';
import { ICommentService } from 'vs/workbench/contrib/comments/browser/commentService';
import { ctxCommentEditorFocused, SimpleCommentEditor } from 'vs/workbench/contrib/comments/browser/simpleCommentEditor';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
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 { 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';
import { CommentContextKeys } from 'vs/workbench/contrib/comments/common/commentContextKeys';

export class NextCommentThreadAction extends EditorAction {
constructor() {
Expand Down Expand Up @@ -73,7 +74,7 @@ export class NextCommentingRangeAction extends EditorAction {
id: 'editor.action.goToNextCommentingRange',
label: nls.localize('goToNextCommentingRange', "Go to Next Commenting Range"),
alias: 'Go to Next Commenting Range',
precondition: WorkspaceHasCommenting,
precondition: CommentContextKeys.WorkspaceHasCommenting,
kbOpts: {
kbExpr: EditorContextKeys.focus,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.DownArrow),
Expand All @@ -94,7 +95,7 @@ export class PreviousCommentingRangeAction extends EditorAction {
id: 'editor.action.goToPreviousCommentingRange',
label: nls.localize('goToPreviousCommentingRange', "Go to Previous Commenting Range"),
alias: 'Go to Next Commenting Range',
precondition: WorkspaceHasCommenting,
precondition: CommentContextKeys.WorkspaceHasCommenting,
kbOpts: {
kbExpr: EditorContextKeys.focus,
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.UpArrow),
Expand Down Expand Up @@ -128,7 +129,7 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
title: nls.localize('comments.toggleCommenting', "Toggle Editor Commenting"),
category: 'Comments',
},
when: WorkspaceHasCommenting
when: CommentContextKeys.WorkspaceHasCommenting
});

const ADD_COMMENT_COMMAND = 'workbench.action.addComment';
Expand Down Expand Up @@ -164,7 +165,7 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
title: nls.localize('comments.addCommand', "Add Comment on Current Selection"),
category: 'Comments'
},
when: ActiveCursorHasCommentingRange
when: CommentContextKeys.activeCursorHasCommentingRange
});

const COLLAPSE_ALL_COMMENT_COMMAND = 'workbench.action.collapseAllComments';
Expand All @@ -181,7 +182,7 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
title: nls.localize('comments.collapseAll', "Collapse All Comments"),
category: 'Comments'
},
when: WorkspaceHasCommenting
when: CommentContextKeys.WorkspaceHasCommenting
});

const EXPAND_ALL_COMMENT_COMMAND = 'workbench.action.expandAllComments';
Expand All @@ -198,7 +199,7 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
title: nls.localize('comments.expandAll', "Expand All Comments"),
category: 'Comments'
},
when: WorkspaceHasCommenting
when: CommentContextKeys.WorkspaceHasCommenting
});

const EXPAND_UNRESOLVED_COMMENT_COMMAND = 'workbench.action.expandUnresolvedComments';
Expand All @@ -215,7 +216,7 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
title: nls.localize('comments.expandUnresolved', "Expand Unresolved Comments"),
category: 'Comments'
},
when: WorkspaceHasCommenting
when: CommentContextKeys.WorkspaceHasCommenting
});

KeybindingsRegistry.registerCommandAndKeybindingRule({
Expand Down
25 changes: 25 additions & 0 deletions src/vs/workbench/contrib/comments/common/commentContextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';


export namespace CommentContextKeys {

/**
* A context key that is set when the active cursor is in a commenting range.
*/
export const activeCursorHasCommentingRange = new RawContextKey<boolean>('activeCursorHasCommentingRange', false, {
description: nls.localize('hasCommentingRange', "Whether the position at the active cursor has a commenting range"),
type: 'boolean'
});

/**
* A context key that is set when the active editor has commenting ranges.
*/
export const activeEditorHasCommentingRange = new RawContextKey<boolean>('activeEditorHasCommentingRange', false, {
description: nls.localize('editorHasCommentingRange', "Whether the active editor has a commenting range"),
type: 'boolean'
});

/**
* A context key that is set when the workspace has either comments or commenting ranges.
*/
export const WorkspaceHasCommenting = new RawContextKey<boolean>('workspaceHasCommenting', false, {
description: nls.localize('hasCommentingProvider', "Whether the open workspace has either comments or commenting ranges."),
type: 'boolean'
});

/**
* A context key that is set when the comment thread has no comments.
*/
Expand Down

0 comments on commit 9aaf518

Please sign in to comment.