Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/common/commentModel.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { URI } from '../../../../base/common/uri.js';
7
import { IRange } from '../../../../editor/common/core/range.js';
8
import { Comment, CommentThread, CommentThreadChangedEvent, CommentThreadApplicability, CommentThreadState } from '../../../../editor/common/languages.js';
9
10
export interface ICommentThreadChangedEvent extends CommentThreadChangedEvent<IRange> {
11
uniqueOwner: string;
12
owner: string;
13
ownerLabel: string;
14
}
15
16
export class CommentNode {
17
isRoot: boolean = false;
18
replies: CommentNode[] = [];
19
public readonly threadId: string;
20
public readonly range: IRange | undefined;
21
public readonly threadState: CommentThreadState | undefined;
22
public readonly threadRelevance: CommentThreadApplicability | undefined;
23
public readonly contextValue: string | undefined;
24
public readonly controllerHandle: number;
25
public readonly threadHandle: number;
26
27
constructor(
28
public readonly uniqueOwner: string,
29
public readonly owner: string,
30
public readonly resource: URI,
31
public readonly comment: Comment,
32
public readonly thread: CommentThread) {
33
this.threadId = thread.threadId;
34
this.range = thread.range;
35
this.threadState = thread.state;
36
this.threadRelevance = thread.applicability;
37
this.contextValue = thread.contextValue;
38
this.controllerHandle = thread.controllerHandle;
39
this.threadHandle = thread.commentThreadHandle;
40
}
41
42
hasReply(): boolean {
43
return this.replies && this.replies.length !== 0;
44
}
45
46
private _lastUpdatedAt: string | undefined;
47
48
get lastUpdatedAt(): string {
49
if (this._lastUpdatedAt === undefined) {
50
let updatedAt = this.comment.timestamp || '';
51
if (this.replies.length) {
52
const reply = this.replies[this.replies.length - 1];
53
const replyUpdatedAt = reply.lastUpdatedAt;
54
if (replyUpdatedAt > updatedAt) {
55
updatedAt = replyUpdatedAt;
56
}
57
}
58
this._lastUpdatedAt = updatedAt;
59
}
60
return this._lastUpdatedAt;
61
}
62
}
63
64
export class ResourceWithCommentThreads {
65
id: string;
66
uniqueOwner: string;
67
owner: string;
68
ownerLabel: string | undefined;
69
commentThreads: CommentNode[]; // The top level comments on the file. Replys are nested under each node.
70
resource: URI;
71
72
constructor(uniqueOwner: string, owner: string, resource: URI, commentThreads: CommentThread[]) {
73
this.uniqueOwner = uniqueOwner;
74
this.owner = owner;
75
this.id = resource.toString();
76
this.resource = resource;
77
this.commentThreads = commentThreads.filter(thread => thread.comments && thread.comments.length).map(thread => ResourceWithCommentThreads.createCommentNode(uniqueOwner, owner, resource, thread));
78
}
79
80
public static createCommentNode(uniqueOwner: string, owner: string, resource: URI, commentThread: CommentThread): CommentNode {
81
const { comments } = commentThread;
82
const commentNodes: CommentNode[] = comments!.map(comment => new CommentNode(uniqueOwner, owner, resource, comment, commentThread));
83
if (commentNodes.length > 1) {
84
commentNodes[0].replies = commentNodes.slice(1, commentNodes.length);
85
}
86
87
commentNodes[0].isRoot = true;
88
89
return commentNodes[0];
90
}
91
92
private _lastUpdatedAt: string | undefined;
93
94
get lastUpdatedAt() {
95
if (this._lastUpdatedAt === undefined) {
96
let updatedAt = '';
97
// Return result without cahcing as we expect data to arrive later
98
if (!this.commentThreads.length) {
99
return updatedAt;
100
}
101
for (const thread of this.commentThreads) {
102
const threadUpdatedAt = thread.lastUpdatedAt;
103
if (threadUpdatedAt && threadUpdatedAt > updatedAt) {
104
updatedAt = threadUpdatedAt;
105
}
106
}
107
this._lastUpdatedAt = updatedAt;
108
}
109
return this._lastUpdatedAt;
110
}
111
}
112
113
114