Skip to content

Commit af1b4eb

Browse files
author
Rachel Macfarlane
committed
Refactoring comment service to not be keyed on handle
1 parent 88bb482 commit af1b4eb

9 files changed

Lines changed: 132 additions & 88 deletions

File tree

src/vs/editor/common/modes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,6 @@ export interface Command {
11301130
* @internal
11311131
*/
11321132
export interface CommentInfo {
1133-
owner: number;
11341133
threads: CommentThread[];
11351134
commentingRanges?: IRange[];
11361135
reply?: Command;
@@ -1187,7 +1186,6 @@ export interface Comment {
11871186
* @internal
11881187
*/
11891188
export interface CommentThreadChangedEvent {
1190-
readonly owner: number;
11911189
/**
11921190
* Added comment threads.
11931191
*/

src/vs/workbench/api/electron-browser/mainThreadComments.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,46 @@ import { COMMENTS_PANEL_ID } from 'vs/workbench/parts/comments/electron-browser/
1616
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
1717
import { URI } from 'vs/base/common/uri';
1818
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
19+
import { generateUuid } from 'vs/base/common/uuid';
1920

21+
export class ExtensionCommentProviderHandler implements modes.DocumentCommentProvider {
22+
private _proxy: ExtHostCommentsShape;
23+
private _handle: number;
24+
25+
constructor(proxy: ExtHostCommentsShape, handle: number) {
26+
this._proxy = proxy;
27+
this._handle = handle;
28+
}
29+
30+
async provideDocumentComments(uri, token) {
31+
return this._proxy.$provideDocumentComments(this._handle, uri);
32+
}
33+
34+
async createNewCommentThread(uri, range, text, token) {
35+
return this._proxy.$createNewCommentThread(this._handle, uri, range, text);
36+
}
37+
38+
async replyToCommentThread(uri, range, thread, text, token) {
39+
return this._proxy.$replyToCommentThread(this._handle, uri, range, thread, text);
40+
}
41+
42+
async editComment(uri, comment, text, token) {
43+
return this._proxy.$editComment(this._handle, uri, comment, text);
44+
}
45+
46+
async deleteComment(uri, comment, token) {
47+
return this._proxy.$deleteComment(this._handle, uri, comment);
48+
}
49+
50+
onDidChangeCommentThreads = null;
51+
}
2052
@extHostNamedCustomer(MainContext.MainThreadComments)
2153
export class MainThreadComments extends Disposable implements MainThreadCommentsShape {
2254
private _disposables: IDisposable[];
2355
private _proxy: ExtHostCommentsShape;
2456
private _documentProviders = new Map<number, IDisposable>();
2557
private _workspaceProviders = new Map<number, IDisposable>();
58+
private _handlers = new Map<number, string>();
2659
private _firstSessionStart: boolean;
2760

2861
constructor(
@@ -40,40 +73,28 @@ export class MainThreadComments extends Disposable implements MainThreadComments
4073

4174
$registerDocumentCommentProvider(handle: number): void {
4275
this._documentProviders.set(handle, undefined);
76+
const handler = new ExtensionCommentProviderHandler(this._proxy, handle);
4377

44-
this._commentService.registerDataProvider(
45-
handle,
46-
{
47-
provideDocumentComments: async (uri, token) => {
48-
return this._proxy.$provideDocumentComments(handle, uri);
49-
},
50-
onDidChangeCommentThreads: null,
51-
createNewCommentThread: async (uri, range, text, token) => {
52-
return this._proxy.$createNewCommentThread(handle, uri, range, text);
53-
},
54-
replyToCommentThread: async (uri, range, thread, text, token) => {
55-
return this._proxy.$replyToCommentThread(handle, uri, range, thread, text);
56-
},
57-
editComment: async (uri, comment, text, token) => {
58-
return this._proxy.$editComment(handle, uri, comment, text);
59-
},
60-
deleteComment: async (uri, comment, token) => {
61-
return this._proxy.$deleteComment(handle, uri, comment);
62-
}
63-
}
64-
);
78+
const providerId = generateUuid();
79+
this._handlers.set(handle, providerId);
80+
81+
this._commentService.registerDataProvider(providerId, handler);
6582
}
6683

6784
$registerWorkspaceCommentProvider(handle: number, extensionId: string): void {
6885
this._workspaceProviders.set(handle, undefined);
86+
87+
const providerId = generateUuid();
88+
this._handlers.set(handle, providerId);
89+
6990
this._panelService.setPanelEnablement(COMMENTS_PANEL_ID, true);
7091
if (this._firstSessionStart) {
7192
this._panelService.openPanel(COMMENTS_PANEL_ID);
7293
this._firstSessionStart = false;
7394
}
7495
this._proxy.$provideWorkspaceComments(handle).then(commentThreads => {
7596
if (commentThreads) {
76-
this._commentService.setWorkspaceComments(handle, commentThreads);
97+
this._commentService.setWorkspaceComments(providerId, commentThreads);
7798
}
7899
});
79100

@@ -89,20 +110,25 @@ export class MainThreadComments extends Disposable implements MainThreadComments
89110

90111
$unregisterDocumentCommentProvider(handle: number): void {
91112
this._documentProviders.delete(handle);
92-
this._commentService.unregisterDataProvider(handle);
113+
const handlerId = this._handlers.get(handle);
114+
this._commentService.unregisterDataProvider(handlerId);
115+
this._handlers.delete(handle);
93116
}
94117

95118
$unregisterWorkspaceCommentProvider(handle: number): void {
96119
this._workspaceProviders.delete(handle);
97120
if (this._workspaceProviders.size === 0) {
98121
this._panelService.setPanelEnablement(COMMENTS_PANEL_ID, false);
99122
}
100-
this._commentService.removeWorkspaceComments(handle);
123+
const handlerId = this._handlers.get(handle);
124+
this._commentService.removeWorkspaceComments(handlerId);
125+
this._handlers.delete(handle);
101126
}
102127

103128
$onDidCommentThreadsChange(handle: number, event: modes.CommentThreadChangedEvent) {
104129
// notify comment service
105-
this._commentService.updateComments(event);
130+
const providerId = this._handlers.get(handle);
131+
this._commentService.updateComments(providerId, event);
106132
}
107133

108134
getVisibleEditors(): ICodeEditor[] {

src/vs/workbench/api/node/extHostComments.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ export class ExtHostComments implements ExtHostCommentsShape {
146146
provider.onDidChangeCommentThreads(event => {
147147

148148
this._proxy.$onDidCommentThreadsChange(handle, {
149-
owner: handle,
150149
changed: event.changed.map(thread => convertToCommentThread(provider, thread, this._commandsConverter)),
151150
added: event.added.map(thread => convertToCommentThread(provider, thread, this._commandsConverter)),
152151
removed: event.removed.map(thread => convertToCommentThread(provider, thread, this._commandsConverter))
@@ -157,7 +156,6 @@ export class ExtHostComments implements ExtHostCommentsShape {
157156

158157
function convertCommentInfo(owner: number, provider: vscode.DocumentCommentProvider, vscodeCommentInfo: vscode.CommentInfo, commandsConverter: CommandsConverter): modes.CommentInfo {
159158
return {
160-
owner: owner,
161159
threads: vscodeCommentInfo.threads.map(x => convertToCommentThread(provider, x, commandsConverter)),
162160
commentingRanges: vscodeCommentInfo.commentingRanges ? vscodeCommentInfo.commentingRanges.map(range => extHostTypeConverter.Range.from(range)) : []
163161
};

src/vs/workbench/parts/comments/common/commentModel.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { groupBy, firstIndex, flatten } from 'vs/base/common/arrays';
1010
import { localize } from 'vs/nls';
1111
import { values } from 'vs/base/common/map';
1212

13+
export interface ICommentThreadChangedEvent extends CommentThreadChangedEvent {
14+
owner: string;
15+
}
16+
1317
export class CommentNode {
1418
threadId: string;
1519
range: IRange;
@@ -53,19 +57,19 @@ export class ResourceWithCommentThreads {
5357

5458
export class CommentsModel {
5559
resourceCommentThreads: ResourceWithCommentThreads[];
56-
commentThreadsMap: Map<number, ResourceWithCommentThreads[]>;
60+
commentThreadsMap: Map<string, ResourceWithCommentThreads[]>;
5761

5862
constructor() {
5963
this.resourceCommentThreads = [];
60-
this.commentThreadsMap = new Map<number, ResourceWithCommentThreads[]>();
64+
this.commentThreadsMap = new Map<string, ResourceWithCommentThreads[]>();
6165
}
6266

63-
public setCommentThreads(owner: number, commentThreads: CommentThread[]): void {
67+
public setCommentThreads(owner: string, commentThreads: CommentThread[]): void {
6468
this.commentThreadsMap.set(owner, this.groupByResource(commentThreads));
6569
this.resourceCommentThreads = flatten(values(this.commentThreadsMap));
6670
}
6771

68-
public updateCommentThreads(event: CommentThreadChangedEvent): boolean {
72+
public updateCommentThreads(event: ICommentThreadChangedEvent): boolean {
6973
const { owner, removed, changed, added } = event;
7074
if (!this.commentThreadsMap.has(owner)) {
7175
return false;

src/vs/workbench/parts/comments/electron-browser/commentNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class CommentNode extends Disposable {
5656

5757
constructor(
5858
public comment: modes.Comment,
59-
private owner: number,
59+
private owner: string,
6060
private resource: URI,
6161
private markdownRenderer: MarkdownRenderer,
6262
private themeService: IThemeService,

src/vs/workbench/parts/comments/electron-browser/commentService.ts

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,44 @@ import { URI } from 'vs/base/common/uri';
1111
import { Range } from 'vs/editor/common/core/range';
1212
import { keys } from 'vs/base/common/map';
1313
import { CancellationToken } from 'vs/base/common/cancellation';
14+
import { ExtensionCommentProviderHandler } from 'vs/workbench/api/electron-browser/mainThreadComments';
15+
import { assign } from 'vs/base/common/objects';
16+
import { ICommentThreadChangedEvent } from 'vs/workbench/parts/comments/common/commentModel';
1417

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

1720
export interface IResourceCommentThreadEvent {
1821
resource: URI;
19-
commentInfos: CommentInfo[];
22+
commentInfos: ICommentInfo[];
23+
}
24+
25+
export interface ICommentInfo extends CommentInfo {
26+
owner: string;
2027
}
2128

2229
export interface IWorkspaceCommentThreadsEvent {
23-
ownerId: number;
30+
ownerId: string;
2431
commentThreads: CommentThread[];
2532
}
2633

2734
export interface ICommentService {
2835
_serviceBrand: any;
2936
readonly onDidSetResourceCommentInfos: Event<IResourceCommentThreadEvent>;
3037
readonly onDidSetAllCommentThreads: Event<IWorkspaceCommentThreadsEvent>;
31-
readonly onDidUpdateCommentThreads: Event<CommentThreadChangedEvent>;
38+
readonly onDidUpdateCommentThreads: Event<ICommentThreadChangedEvent>;
3239
readonly onDidSetDataProvider: Event<void>;
33-
readonly onDidDeleteDataProvider: Event<number>;
34-
setDocumentComments(resource: URI, commentInfos: CommentInfo[]): void;
35-
setWorkspaceComments(owner: number, commentsByResource: CommentThread[]): void;
36-
removeWorkspaceComments(owner: number): void;
37-
registerDataProvider(owner: number, commentProvider: DocumentCommentProvider): void;
38-
unregisterDataProvider(owner: number): void;
39-
updateComments(event: CommentThreadChangedEvent): void;
40-
createNewCommentThread(owner: number, resource: URI, range: Range, text: string): Promise<CommentThread | null>;
41-
replyToCommentThread(owner: number, resource: URI, range: Range, thread: CommentThread, text: string): Promise<CommentThread | null>;
42-
editComment(owner: number, resource: URI, comment: Comment, text: string): Promise<void>;
43-
deleteComment(owner: number, resource: URI, comment: Comment): Promise<boolean>;
44-
getComments(resource: URI): Promise<CommentInfo[]>;
40+
readonly onDidDeleteDataProvider: Event<string>;
41+
setDocumentComments(resource: URI, commentInfos: ICommentInfo[]): void;
42+
setWorkspaceComments(owner: string, commentsByResource: CommentThread[]): void;
43+
removeWorkspaceComments(owner: string): void;
44+
registerDataProvider(owner: string, commentProvider: ExtensionCommentProviderHandler): void;
45+
unregisterDataProvider(owner: string): void;
46+
updateComments(ownerId: string, event: CommentThreadChangedEvent): void;
47+
createNewCommentThread(owner: string, resource: URI, range: Range, text: string): Promise<CommentThread | null>;
48+
replyToCommentThread(owner: string, resource: URI, range: Range, thread: CommentThread, text: string): Promise<CommentThread | null>;
49+
editComment(owner: string, resource: URI, comment: Comment, text: string): Promise<void>;
50+
deleteComment(owner: string, resource: URI, comment: Comment): Promise<boolean>;
51+
getComments(resource: URI): Promise<ICommentInfo[]>;
4552
}
4653

4754
export class CommentService extends Disposable implements ICommentService {
@@ -50,51 +57,52 @@ export class CommentService extends Disposable implements ICommentService {
5057
private readonly _onDidSetDataProvider: Emitter<void> = this._register(new Emitter<void>());
5158
readonly onDidSetDataProvider: Event<void> = this._onDidSetDataProvider.event;
5259

53-
private readonly _onDidDeletetDataProvider: Emitter<number> = this._register(new Emitter<number>());
54-
readonly onDidDeleteDataProvider: Event<number> = this._onDidDeletetDataProvider.event;
60+
private readonly _onDidDeleteDataProvider: Emitter<string> = this._register(new Emitter<string>());
61+
readonly onDidDeleteDataProvider: Event<string> = this._onDidDeleteDataProvider.event;
5562

5663
private readonly _onDidSetResourceCommentInfos: Emitter<IResourceCommentThreadEvent> = this._register(new Emitter<IResourceCommentThreadEvent>());
5764
readonly onDidSetResourceCommentInfos: Event<IResourceCommentThreadEvent> = this._onDidSetResourceCommentInfos.event;
5865

5966
private readonly _onDidSetAllCommentThreads: Emitter<IWorkspaceCommentThreadsEvent> = this._register(new Emitter<IWorkspaceCommentThreadsEvent>());
6067
readonly onDidSetAllCommentThreads: Event<IWorkspaceCommentThreadsEvent> = this._onDidSetAllCommentThreads.event;
6168

62-
private readonly _onDidUpdateCommentThreads: Emitter<CommentThreadChangedEvent> = this._register(new Emitter<CommentThreadChangedEvent>());
63-
readonly onDidUpdateCommentThreads: Event<CommentThreadChangedEvent> = this._onDidUpdateCommentThreads.event;
69+
private readonly _onDidUpdateCommentThreads: Emitter<ICommentThreadChangedEvent> = this._register(new Emitter<ICommentThreadChangedEvent>());
70+
readonly onDidUpdateCommentThreads: Event<ICommentThreadChangedEvent> = this._onDidUpdateCommentThreads.event;
6471

65-
private _commentProviders = new Map<number, DocumentCommentProvider>();
72+
private _commentProviders = new Map<string, DocumentCommentProvider>();
6673

6774
constructor() {
6875
super();
6976
}
7077

71-
setDocumentComments(resource: URI, commentInfos: CommentInfo[]): void {
78+
setDocumentComments(resource: URI, commentInfos: ICommentInfo[]): void {
7279
this._onDidSetResourceCommentInfos.fire({ resource, commentInfos });
7380
}
7481

75-
setWorkspaceComments(owner: number, commentsByResource: CommentThread[]): void {
82+
setWorkspaceComments(owner: string, commentsByResource: CommentThread[]): void {
7683
this._onDidSetAllCommentThreads.fire({ ownerId: owner, commentThreads: commentsByResource });
7784
}
7885

79-
removeWorkspaceComments(owner: number): void {
86+
removeWorkspaceComments(owner: string): void {
8087
this._onDidSetAllCommentThreads.fire({ ownerId: owner, commentThreads: [] });
8188
}
8289

83-
registerDataProvider(owner: number, commentProvider: DocumentCommentProvider) {
90+
registerDataProvider(owner: string, commentProvider: DocumentCommentProvider) {
8491
this._commentProviders.set(owner, commentProvider);
8592
this._onDidSetDataProvider.fire();
8693
}
8794

88-
unregisterDataProvider(owner: number): void {
95+
unregisterDataProvider(owner: string): void {
8996
this._commentProviders.delete(owner);
90-
this._onDidDeletetDataProvider.fire(owner);
97+
this._onDidDeleteDataProvider.fire(owner);
9198
}
9299

93-
updateComments(event: CommentThreadChangedEvent): void {
94-
this._onDidUpdateCommentThreads.fire(event);
100+
updateComments(ownerId: string, event: CommentThreadChangedEvent): void {
101+
const evt = assign({}, event, { ownerId });
102+
this._onDidUpdateCommentThreads.fire(evt);
95103
}
96104

97-
async createNewCommentThread(owner: number, resource: URI, range: Range, text: string): Promise<CommentThread | null> {
105+
async createNewCommentThread(owner: string, resource: URI, range: Range, text: string): Promise<CommentThread | null> {
98106
const commentProvider = this._commentProviders.get(owner);
99107

100108
if (commentProvider) {
@@ -104,7 +112,7 @@ export class CommentService extends Disposable implements ICommentService {
104112
return null;
105113
}
106114

107-
async replyToCommentThread(owner: number, resource: URI, range: Range, thread: CommentThread, text: string): Promise<CommentThread | null> {
115+
async replyToCommentThread(owner: string, resource: URI, range: Range, thread: CommentThread, text: string): Promise<CommentThread | null> {
108116
const commentProvider = this._commentProviders.get(owner);
109117

110118
if (commentProvider) {
@@ -114,7 +122,7 @@ export class CommentService extends Disposable implements ICommentService {
114122
return null;
115123
}
116124

117-
editComment(owner: number, resource: URI, comment: Comment, text: string): Promise<void> {
125+
editComment(owner: string, resource: URI, comment: Comment, text: string): Promise<void> {
118126
const commentProvider = this._commentProviders.get(owner);
119127

120128
if (commentProvider) {
@@ -124,7 +132,7 @@ export class CommentService extends Disposable implements ICommentService {
124132
return Promise.resolve(void 0);
125133
}
126134

127-
deleteComment(owner: number, resource: URI, comment: Comment): Promise<boolean> {
135+
deleteComment(owner: string, resource: URI, comment: Comment): Promise<boolean> {
128136
const commentProvider = this._commentProviders.get(owner);
129137

130138
if (commentProvider) {
@@ -134,12 +142,23 @@ export class CommentService extends Disposable implements ICommentService {
134142
return Promise.resolve(false);
135143
}
136144

137-
getComments(resource: URI): Promise<CommentInfo[]> {
138-
const result: Promise<CommentInfo>[] = [];
139-
for (const handle of keys(this._commentProviders)) {
140-
const provider = this._commentProviders.get(handle);
141-
if ((<DocumentCommentProvider>provider).provideDocumentComments) {
142-
result.push((<DocumentCommentProvider>provider).provideDocumentComments(resource, CancellationToken.None));
145+
getComments(resource: URI): Promise<ICommentInfo[]> {
146+
const result: Promise<ICommentInfo>[] = [];
147+
for (const owner of keys(this._commentProviders)) {
148+
const provider = this._commentProviders.get(owner);
149+
if (provider.provideDocumentComments) {
150+
result.push(provider.provideDocumentComments(resource, CancellationToken.None).then(commentInfo => {
151+
if (commentInfo) {
152+
return <ICommentInfo>{
153+
owner: owner,
154+
threads: commentInfo.threads,
155+
commentingRanges: commentInfo.commentingRanges,
156+
reply: commentInfo.reply
157+
};
158+
} else {
159+
return null;
160+
}
161+
}));
143162
}
144163
}
145164

0 commit comments

Comments
 (0)