Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commenting ranges issue #199011

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 43 additions & 17 deletions src/vs/workbench/contrib/comments/browser/commentsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ interface CommentRangeAction {
commentingRangesInfo: languages.CommentingRanges;
}

interface MergedCommentRangeActions {
range?: Range;
action: CommentRangeAction;
}

class CommentingRangeDecoration implements IModelDeltaDecoration {
private _decorationId: string | undefined;
private _startLineNumber: number;
Expand Down Expand Up @@ -262,16 +267,18 @@ class CommentingRangeDecorator {
return true;
}

public getMatchedCommentAction(commentRange: Range | undefined): CommentRangeAction[] {
public getMatchedCommentAction(commentRange: Range | undefined): MergedCommentRangeActions[] {
if (commentRange === undefined) {
const foundInfos = this._infos?.filter(info => info.commentingRanges.fileComments);
if (foundInfos) {
return foundInfos.map(foundInfo => {
return {
ownerId: foundInfo.owner,
extensionId: foundInfo.extensionId,
label: foundInfo.label,
commentingRangesInfo: foundInfo.commentingRanges
action: {
ownerId: foundInfo.owner,
extensionId: foundInfo.extensionId,
label: foundInfo.label,
commentingRangesInfo: foundInfo.commentingRanges
}
};
});
}
Expand Down Expand Up @@ -303,9 +310,15 @@ class CommentingRangeDecorator {
}
}

const seenOwners = new Set<string>();
return Array.from(foundHoverActions.values()).filter(action => {
return (action.range.startLineNumber <= commentRange.startLineNumber) && (commentRange.endLineNumber <= action.range.endLineNumber);
}).map(actions => actions.action);
if (seenOwners.has(action.action.ownerId)) {
return false;
} else {
seenOwners.add(action.action.ownerId);
return true;
}
});
}

public getNearestCommentingRange(findPosition: Position, reverse?: boolean): Range | undefined {
Expand Down Expand Up @@ -1028,6 +1041,16 @@ export class CommentController implements IEditorContribution {
}
}

private clipUserRangeToCommentRange(userRange: Range, commentRange: Range): Range {
if (userRange.startLineNumber < commentRange.startLineNumber) {
userRange = new Range(commentRange.startLineNumber, commentRange.startColumn, userRange.endLineNumber, userRange.endColumn);
}
if (userRange.endLineNumber > commentRange.endLineNumber) {
userRange = new Range(userRange.startLineNumber, userRange.startColumn, commentRange.endLineNumber, commentRange.endColumn);
}
return userRange;
}

public addCommentAtLine(range: Range | undefined, e: IEditorMouseEvent | undefined): Promise<void> {
const newCommentInfos = this._commentingRangeDecorator.getMatchedCommentAction(range);
if (!newCommentInfos.length || !this.editor?.hasModel()) {
Expand Down Expand Up @@ -1055,27 +1078,29 @@ export class CommentController implements IEditorContribution {
return;
}

const commentInfos = newCommentInfos.filter(info => info.ownerId === pick.id);
const commentInfos = newCommentInfos.filter(info => info.action.ownerId === pick.id);

if (commentInfos.length) {
const { ownerId } = commentInfos[0];
this.addCommentAtLine2(range, ownerId);
const { ownerId } = commentInfos[0].action;
const clippedRange = range && commentInfos[0].range ? this.clipUserRangeToCommentRange(range, commentInfos[0].range) : range;
this.addCommentAtLine2(clippedRange, ownerId);
}
}).then(() => {
this._addInProgress = false;
});
}
} else {
const { ownerId } = newCommentInfos[0]!;
this.addCommentAtLine2(range, ownerId);
const { ownerId } = newCommentInfos[0]!.action;
const clippedRange = range && newCommentInfos[0].range ? this.clipUserRangeToCommentRange(range, newCommentInfos[0].range) : range;
this.addCommentAtLine2(clippedRange, ownerId);
}

return Promise.resolve();
}

private getCommentProvidersQuickPicks(commentInfos: { ownerId: string; extensionId: string | undefined; label: string | undefined; commentingRangesInfo: languages.CommentingRanges | undefined }[]) {
private getCommentProvidersQuickPicks(commentInfos: MergedCommentRangeActions[]) {
const picks: QuickPickInput[] = commentInfos.map((commentInfo) => {
const { ownerId, extensionId, label } = commentInfo;
const { ownerId, extensionId, label } = commentInfo.action;

return <IQuickPickItem>{
label: label || extensionId,
Expand All @@ -1086,19 +1111,20 @@ export class CommentController implements IEditorContribution {
return picks;
}

private getContextMenuActions(commentInfos: { ownerId: string; extensionId: string | undefined; label: string | undefined; commentingRangesInfo: languages.CommentingRanges }[], commentRange: Range): IAction[] {
private getContextMenuActions(commentInfos: MergedCommentRangeActions[], commentRange: Range): IAction[] {
const actions: IAction[] = [];

commentInfos.forEach(commentInfo => {
const { ownerId, extensionId, label } = commentInfo;
const { ownerId, extensionId, label } = commentInfo.action;

actions.push(new Action(
'addCommentThread',
`${label || extensionId}`,
undefined,
true,
() => {
this.addCommentAtLine2(commentRange, ownerId);
const clippedRange = commentInfo.range ? this.clipUserRangeToCommentRange(commentRange, commentInfo.range) : commentRange;
this.addCommentAtLine2(clippedRange, ownerId);
return Promise.resolve();
}
));
Expand Down