Skip to content

Commit

Permalink
[vscode] introduction of the CommentingRange type
Browse files Browse the repository at this point in the history
also fix creation of comments, with arguments passed to the command being the one expected by argument processor

fix eclipse-theia#14941

Contributed on behalf of STMicroelectronics

Signed-off-by: Remi Schnekenburger <[email protected]>
  • Loading branch information
rschnekenbu committed Feb 24, 2025
1 parent acb6170 commit 332d154
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 33 deletions.
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/common/plugin-api-rpc-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ export interface CommentThread {
extensionId?: string;
threadId: string;
resource: string | null;
range: Range;
range: Range | undefined;
label: string | undefined;
contextValue: string | undefined;
comments: Comment[] | undefined;
Expand All @@ -738,7 +738,7 @@ export interface CommentThread {
state?: CommentThreadState;
input?: CommentInput;
onDidChangeInput: TheiaEvent<CommentInput | undefined>;
onDidChangeRange: TheiaEvent<Range>;
onDidChangeRange: TheiaEvent<Range | undefined>;
onDidChangeLabel: TheiaEvent<string | undefined>;
onDidChangeState: TheiaEvent<CommentThreadState | undefined>;
onDidChangeCollapsibleState: TheiaEvent<CommentThreadCollapsibleState | undefined>;
Expand Down Expand Up @@ -771,6 +771,7 @@ export interface CommentThreadChangedEvent {
export interface CommentingRanges {
readonly resource: URI;
ranges: Range[];
fileComments: boolean;
}

export interface CommentInfo {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2069,10 +2069,10 @@ export interface ClipboardMain {
}

export interface CommentsExt {
$createCommentThreadTemplate(commentControllerHandle: number, uriComponents: UriComponents, range: Range): void;
$createCommentThreadTemplate(commentControllerHandle: number, uriComponents: UriComponents, range: Range | undefined): void;
$updateCommentThreadTemplate(commentControllerHandle: number, threadHandle: number, range: Range): Promise<void>;
$deleteCommentThread(commentControllerHandle: number, commentThreadHandle: number): Promise<void>;
$provideCommentingRanges(commentControllerHandle: number, uriComponents: UriComponents, token: CancellationToken): Promise<Range[] | undefined>;
$provideCommentingRanges(commentControllerHandle: number, uriComponents: UriComponents, token: CancellationToken): Promise<{ ranges: Range[]; fileComments: boolean } | undefined>;
}

export interface CommentProviderFeatures {
Expand All @@ -2093,7 +2093,7 @@ export interface CommentsMain {
$registerCommentController(handle: number, id: string, label: string): void;
$unregisterCommentController(handle: number): void;
$updateCommentControllerFeatures(handle: number, features: CommentProviderFeatures): void;
$createCommentThread(handle: number, commentThreadHandle: number, threadId: string, resource: UriComponents, range: Range, extensionId: string): CommentThread | undefined;
$createCommentThread(handle: number, commentThreadHandle: number, threadId: string, resource: UriComponents, range: Range | undefined, extensionId: string): CommentThread | undefined;
$updateCommentThread(handle: number, commentThreadHandle: number, threadId: string, resource: UriComponents, changes: CommentThreadChanges): void;
$deleteCommentThread(handle: number, commentThreadHandle: number): void;
$onDidCommentThreadsChange(handle: number, event: CommentThreadChangedEvent): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class CommentThreadWidget extends BaseWidget {
this.toDispose.push(this.commentGlyphWidget = new CommentGlyphWidget(editor));
this.toDispose.push(this._commentThread.onDidChangeCollapsibleState(state => {
if (state === CommentThreadCollapsibleState.Expanded && !this.isExpanded) {
const lineNumber = this._commentThread.range.startLineNumber;
const lineNumber = this._commentThread.range?.startLineNumber ?? 0;

this.display({ afterLineNumber: lineNumber, afterColumn: 1, heightInLines: 2 });
return;
Expand Down Expand Up @@ -256,8 +256,8 @@ export class CommentThreadWidget extends BaseWidget {
const frameThickness = Math.round(lineHeight / 9) * 2;
const body = this.zoneWidget.containerNode.getElementsByClassName('body')[0];

const computedLinesNumber = Math.ceil((headHeight + body.clientHeight + arrowHeight + frameThickness + 8 /** margin bottom to avoid margin collapse */) / lineHeight);
this.zoneWidget.show({ afterLineNumber: this._commentThread.range.startLineNumber, heightInLines: computedLinesNumber });
const computedLinesNumber = Math.ceil((headHeight + body?.clientHeight + arrowHeight + frameThickness + 8 /** margin bottom to avoid margin collapse */) / lineHeight);
this.zoneWidget.show({ afterLineNumber: this._commentThread.range?.startLineNumber ?? 0, heightInLines: computedLinesNumber });
}

protected render(): void {
Expand Down Expand Up @@ -588,7 +588,8 @@ export class CommentEditContainer extends React.Component<CommentEditContainer.P
{menus.getMenu(COMMENT_CONTEXT).children.map((node, index) => {
const onClick = () => {
commands.executeCommand(node.id, {
thread: commentThread,
commentControlHandle: commentThread.controllerHandle,
commentThreadHandle: commentThread.commentThreadHandle,
commentUniqueId: comment.uniqueIdInThread,
text: this.inputRef.current ? this.inputRef.current.value : ''
});
Expand Down Expand Up @@ -623,7 +624,8 @@ export class CommentsInlineAction extends React.Component<CommentsInlineAction.P
title={node.label}
onClick={() => {
commands.executeCommand(node.id, {
thread: commentThread,
commentControlHandle: commentThread.controllerHandle,
commentThreadHandle: commentThread.commentThreadHandle,
commentUniqueId
});
}} />
Expand Down Expand Up @@ -651,8 +653,10 @@ export class CommentActions extends React.Component<CommentActions.Props> {
commands={commands}
node={node}
onClick={() => {
console.log('CommentActions.onClick: ' + commentThread);
commands.executeCommand(node.id, {
thread: commentThread,
commentControlHandle: commentThread.controllerHandle,
commentThreadHandle: commentThread.commentThreadHandle,
text: getInput()
});
clearInput();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class CommentsContribution {
this.commentsContextKeyService.commentController.set(provider.id);
}
const zoneWidget = new CommentThreadWidget(editor, owner, thread, this.commentService, this.menus, this.commentsContextKeyService, this.commands);
zoneWidget.display({ afterLineNumber: thread.range.startLineNumber, heightInLines: 5 });
zoneWidget.display({ afterLineNumber: thread.range?.startLineNumber ?? 0, heightInLines: 5 }); // messages with no range are put on top of the editor
const currentEditor = this.getCurrentEditor();
if (currentEditor) {
currentEditor.onDispose(() => zoneWidget.dispose());
Expand Down
21 changes: 11 additions & 10 deletions packages/plugin-ext/src/main/browser/comments/comments-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ export class CommentThreadImpl implements CommentThread, Disposable {
private readonly onDidChangeCommentsEmitter = new Emitter<Comment[] | undefined>();
get onDidChangeComments(): Event<Comment[] | undefined> { return this.onDidChangeCommentsEmitter.event; }

set range(range: Range) {
set range(range: Range | undefined) {
this._range = range;
this.onDidChangeRangeEmitter.fire(this._range);
}

get range(): Range {
get range(): Range | undefined {
return this._range;
}

private readonly onDidChangeRangeEmitter = new Emitter<Range>();
private readonly onDidChangeRangeEmitter = new Emitter<Range | undefined>();
public onDidChangeRange = this.onDidChangeRangeEmitter.event;

private _collapsibleState: CommentThreadCollapsibleState | undefined;
Expand Down Expand Up @@ -164,7 +164,7 @@ export class CommentThreadImpl implements CommentThread, Disposable {
public extensionId: string,
public threadId: string,
public resource: string,
private _range: Range
private _range: Range | undefined
) {
this._isDisposed = false;
}
Expand All @@ -173,7 +173,7 @@ export class CommentThreadImpl implements CommentThread, Disposable {
const modified = (value: keyof CommentThreadChanges): boolean =>
Object.prototype.hasOwnProperty.call(changes, value);

if (modified('range')) { this._range = changes.range!; }
if (modified('range')) { this._range = changes.range; }
if (modified('label')) { this._label = changes.label; }
if (modified('contextValue')) { this._contextValue = changes.contextValue; }
if (modified('comments')) { this._comments = changes.comments; }
Expand Down Expand Up @@ -244,7 +244,7 @@ export class CommentController {
commentThreadHandle: number,
threadId: string,
resource: UriComponents,
range: Range,
range: Range | undefined,
): CommentThread {
const thread = new CommentThreadImpl(
commentThreadHandle,
Expand Down Expand Up @@ -336,14 +336,15 @@ export class CommentController {
threads: ret,
commentingRanges: {
resource: resource,
ranges: commentingRanges || []
ranges: commentingRanges?.ranges || [],
fileComments: !!commentingRanges?.fileComments
}
};
}

async getCommentingRanges(resource: URI, token: CancellationToken): Promise<Range[]> {
async getCommentingRanges(resource: URI, token: CancellationToken): Promise<{ ranges: Range[]; fileComments: boolean } | undefined> {
const commentingRanges = await this._proxy.$provideCommentingRanges(this.handle, resource, token);
return commentingRanges || [];
return commentingRanges;
}

getAllComments(): CommentThread[] {
Expand Down Expand Up @@ -425,7 +426,7 @@ export class CommentsMainImp implements CommentsMain {
commentThreadHandle: number,
threadId: string,
resource: UriComponents,
range: Range,
range: Range | undefined,
extensionId: string
): CommentThread | undefined {
const provider = this.commentControllers.get(handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,17 @@ export class PluginCommentService implements CommentsService {
}

async getCommentingRanges(resource: URI): Promise<Range[]> {
const commentControlResult: Promise<Range[]>[] = [];
const commentControlResult: Promise<{ ranges: Range[]; fileComments: boolean } | undefined>[] = [];

this.commentControls.forEach(control => {
commentControlResult.push(control.getCommentingRanges(resource, CancellationToken.None));
});

const ret = await Promise.all(commentControlResult);
return ret.reduce((prev, curr) => {
prev.push(...curr);
return ret.reduce<Range[]>((prev, curr) => {
if (curr) {
prev.push(...curr.ranges);
}
return prev;
}, []);
}
Expand Down
35 changes: 30 additions & 5 deletions packages/plugin-ext/src/plugin/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
Plugin as InternalPlugin,
PLUGIN_RPC_CONTEXT
} from '../common/plugin-api-rpc';
import { isArray } from '../common/types';

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
Expand Down Expand Up @@ -163,7 +164,11 @@ export class CommentsExtImpl implements CommentsExt {
}
}

async $provideCommentingRanges(commentControllerHandle: number, uriComponents: UriComponents, token: theia.CancellationToken): Promise<Range[] | undefined> {
async $provideCommentingRanges(
commentControllerHandle: number,
uriComponents: UriComponents,
token: theia.CancellationToken
): Promise<{ ranges: Range[]; fileComments: boolean } | undefined> {
const commentController = this.commentControllers.get(commentControllerHandle);

if (!commentController || !commentController.commentingRangeProvider) {
Expand All @@ -172,11 +177,30 @@ export class CommentsExtImpl implements CommentsExt {

const documentData = this._documents.getDocumentData(URI.revive(uriComponents));
if (documentData) {
const ranges: theia.Range[] | undefined | null = await commentController.commentingRangeProvider!.provideCommentingRanges(documentData.document, token);
if (ranges) {
return ranges.map(x => fromRange(x));
const commentingRanges:
| theia.Range[]
| theia.CommentingRanges
| undefined
| null =
await commentController.commentingRangeProvider!.provideCommentingRanges(
documentData.document,
token
);
if (isArray(commentingRanges)) {
return {
ranges: commentingRanges.map(x => fromRange(x)),
fileComments: false
};
} else if (commentingRanges) {
return {
ranges: commentingRanges.ranges?.map(x => fromRange(x)) || [],
fileComments: commentingRanges.enableFileComments
};
} else {
return commentingRanges ?? undefined;
}
}
return undefined;
}
}

Expand Down Expand Up @@ -221,7 +245,8 @@ export class ExtHostCommentThread implements theia.CommentThread, theia.Disposab
readonly onDidUpdateCommentThread = this._onDidUpdateCommentThread.event;

set range(range: theia.Range) {
if (!range.isEqual(this._range)) {
if ( ((range === undefined) !== (this._range === undefined))
|| (range && !range.isEqual(this._range))) {
this._range = range;
this.modifications.range = range;
this._onDidUpdateCommentThread.fire();
Expand Down
22 changes: 19 additions & 3 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13744,9 +13744,10 @@ export module '@theia/plugin' {

/**
* The range the comment thread is located within the document. The thread icon will be shown
* at the first line of the range.
* at the last line of the range. When set to undefined, the comment will be associated with the
* file, and not a specific range.
*/
range: Range;
range: Range | undefined;

/**
* The ordered comments of the thread.
Expand Down Expand Up @@ -13921,14 +13922,29 @@ export module '@theia/plugin' {
text: string;
}

/**
* The ranges a CommentingRangeProvider enables commenting on.
*/
export interface CommentingRanges {
/**
* Enables comments to be added to a file without a specific range.
*/
enableFileComments: boolean;

/**
* The ranges which allow new comment threads creation.
*/
ranges?: Range[];
}

/**
* Commenting range provider for a {@link CommentController comment controller}.
*/
export interface CommentingRangeProvider {
/**
* Provide a list of ranges which allow new comment threads creation or null for a given document
*/
provideCommentingRanges(document: TextDocument, token: CancellationToken): ProviderResult<Range[]>;
provideCommentingRanges(document: TextDocument, token: CancellationToken): ProviderResult<Range[] | CommentingRanges>;
}

/**
Expand Down

0 comments on commit 332d154

Please sign in to comment.