Skip to content

Commit

Permalink
Add timestamps to comments proposal (#139849)
Browse files Browse the repository at this point in the history
Part of #139524
  • Loading branch information
alexr00 committed Jan 13, 2022
1 parent 2dd7925 commit adb8450
Show file tree
Hide file tree
Showing 12 changed files with 305 additions and 53 deletions.
146 changes: 109 additions & 37 deletions src/vs/base/common/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const week = day * 7;
const month = day * 30;
const year = day * 365;

export function fromNow(date: number | Date, appendAgoLabel?: boolean): string {
export function fromNow(date: number | Date, appendAgoLabel?: boolean, useFullTimeWords?: boolean): string {
if (typeof date !== 'number') {
date = date.getTime();
}
Expand All @@ -31,39 +31,75 @@ export function fromNow(date: number | Date, appendAgoLabel?: boolean): string {
value = seconds;

if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.seconds.singular.ago', '{0} sec ago', value)
: localize('date.fromNow.seconds.plural.ago', '{0} secs ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.seconds.singular.ago.fullWord', '{0} second ago', value)
: localize('date.fromNow.seconds.singular.ago', '{0} sec ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.seconds.plural.ago.fullWord', '{0} seconds ago', value)
: localize('date.fromNow.seconds.plural.ago', '{0} secs ago', value);
}
} else {
return value === 1
? localize('date.fromNow.seconds.singular', '{0} sec', value)
: localize('date.fromNow.seconds.plural', '{0} secs', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.seconds.singular.fullWord', '{0} second', value)
: localize('date.fromNow.seconds.singular', '{0} sec', value);
} else {
return useFullTimeWords
? localize('date.fromNow.seconds.plural.fullWord', '{0} seconds', value)
: localize('date.fromNow.seconds.plural', '{0} secs', value);
}
}
}

if (seconds < hour) {
value = Math.floor(seconds / minute);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.minutes.singular.ago', '{0} min ago', value)
: localize('date.fromNow.minutes.plural.ago', '{0} mins ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.minutes.singular.ago.fullWord', '{0} minute ago', value)
: localize('date.fromNow.minutes.singular.ago', '{0} min ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.minutes.plural.ago.fullWord', '{0} minutes ago', value)
: localize('date.fromNow.minutes.plural.ago', '{0} mins ago', value);
}
} else {
return value === 1
? localize('date.fromNow.minutes.singular', '{0} min', value)
: localize('date.fromNow.minutes.plural', '{0} mins', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.minutes.singular.fullWord', '{0} minute', value)
: localize('date.fromNow.minutes.singular', '{0} min', value);
} else {
return useFullTimeWords
? localize('date.fromNow.minutes.plural.fullWord', '{0} minutes', value)
: localize('date.fromNow.minutes.plural', '{0} mins', value);
}
}
}

if (seconds < day) {
value = Math.floor(seconds / hour);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.hours.singular.ago', '{0} hr ago', value)
: localize('date.fromNow.hours.plural.ago', '{0} hrs ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.hours.singular.ago.fullWord', '{0} hour ago', value)
: localize('date.fromNow.hours.singular.ago', '{0} hr ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.hours.plural.ago.fullWord', '{0} hours ago', value)
: localize('date.fromNow.hours.plural.ago', '{0} hrs ago', value);
}
} else {
return value === 1
? localize('date.fromNow.hours.singular', '{0} hr', value)
: localize('date.fromNow.hours.plural', '{0} hrs', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.hours.singular.fullWord', '{0} hour', value)
: localize('date.fromNow.hours.singular', '{0} hr', value);
} else {
return useFullTimeWords
? localize('date.fromNow.hours.plural.fullWord', '{0} hours', value)
: localize('date.fromNow.hours.plural', '{0} hrs', value);
}
}
}

Expand All @@ -83,38 +119,74 @@ export function fromNow(date: number | Date, appendAgoLabel?: boolean): string {
if (seconds < month) {
value = Math.floor(seconds / week);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.weeks.singular.ago', '{0} wk ago', value)
: localize('date.fromNow.weeks.plural.ago', '{0} wks ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.weeks.singular.ago.fullWord', '{0} week ago', value)
: localize('date.fromNow.weeks.singular.ago', '{0} wk ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.weeks.plural.ago.fullWord', '{0} weeks ago', value)
: localize('date.fromNow.weeks.plural.ago', '{0} wks ago', value);
}
} else {
return value === 1
? localize('date.fromNow.weeks.singular', '{0} wk', value)
: localize('date.fromNow.weeks.plural', '{0} wks', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.weeks.singular.fullWord', '{0} week', value)
: localize('date.fromNow.weeks.singular', '{0} wk', value);
} else {
return useFullTimeWords
? localize('date.fromNow.weeks.plural.fullWord', '{0} weeks', value)
: localize('date.fromNow.weeks.plural', '{0} wks', value);
}
}
}

if (seconds < year) {
value = Math.floor(seconds / month);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.months.singular.ago', '{0} mo ago', value)
: localize('date.fromNow.months.plural.ago', '{0} mos ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.months.singular.ago.fullWord', '{0} month ago', value)
: localize('date.fromNow.months.singular.ago', '{0} mo ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.months.plural.ago.fullWord', '{0} months ago', value)
: localize('date.fromNow.months.plural.ago', '{0} mos ago', value);
}
} else {
return value === 1
? localize('date.fromNow.months.singular', '{0} mo', value)
: localize('date.fromNow.months.plural', '{0} mos', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.months.singular.fullWord', '{0} month', value)
: localize('date.fromNow.months.singular', '{0} mo', value);
} else {
return useFullTimeWords
? localize('date.fromNow.months.plural.fullWord', '{0} months', value)
: localize('date.fromNow.months.plural', '{0} mos', value);
}
}
}

value = Math.floor(seconds / year);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.years.singular.ago', '{0} yr ago', value)
: localize('date.fromNow.years.plural.ago', '{0} yrs ago', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.years.singular.ago.fullWord', '{0} year ago', value)
: localize('date.fromNow.years.singular.ago', '{0} yr ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.years.plural.ago.fullWord', '{0} years ago', value)
: localize('date.fromNow.years.plural.ago', '{0} yrs ago', value);
}
} else {
return value === 1
? localize('date.fromNow.years.singular', '{0} yr', value)
: localize('date.fromNow.years.plural', '{0} yrs', value);
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.years.singular.fullWord', '{0} year', value)
: localize('date.fromNow.years.singular', '{0} yr', value);
} else {
return useFullTimeWords
? localize('date.fromNow.years.plural.fullWord', '{0} years', value)
: localize('date.fromNow.years.plural', '{0} yrs', value);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/vs/base/common/marshalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const enum MarshalledId {
TimelineActionContext,
NotebookCellActionContext,
TestItemContext,
Date,
}

export interface MarshalledObject {
Expand Down Expand Up @@ -68,6 +69,7 @@ export function revive<T = any>(obj: any, depth = 0): Revived<T> {
switch ((<MarshalledObject>obj).$mid) {
case MarshalledId.Uri: return <any>URI.revive(obj);
case MarshalledId.Regexp: return <any>new RegExp(obj.source, obj.flags);
case MarshalledId.Date: return <any>new Date(obj.source);
}

if (
Expand Down
1 change: 1 addition & 0 deletions src/vs/editor/common/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,7 @@ export interface Comment {
readonly commentReactions?: CommentReaction[];
readonly label?: string;
readonly mode?: CommentMode;
readonly detail?: Date | string;
}

/**
Expand Down
22 changes: 19 additions & 3 deletions src/vs/workbench/api/browser/mainThreadComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import { Registry } from 'vs/platform/registry/common/platform';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { ICommentInfo, ICommentService } from 'vs/workbench/contrib/comments/browser/commentService';
import { CommentsPanel } from 'vs/workbench/contrib/comments/browser/commentsView';
import { CommentProviderFeatures, ExtHostCommentsShape, ExtHostContext, IExtHostContext, MainContext, MainThreadCommentsShape, CommentThreadChanges } from '../common/extHost.protocol';
import { CommentProviderFeatures, ExtHostCommentsShape, ExtHostContext, IExtHostContext, MainContext, MainThreadCommentsShape, CommentThreadChanges, CommentChanges } from '../common/extHost.protocol';
import { COMMENTS_VIEW_ID, COMMENTS_VIEW_TITLE } from 'vs/workbench/contrib/comments/browser/commentsTreeViewer';
import { ViewContainer, IViewContainersRegistry, Extensions as ViewExtensions, ViewContainerLocation, IViewsRegistry, IViewsService, IViewDescriptorService } from 'vs/workbench/common/views';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { Codicon } from 'vs/base/common/codicons';
import { registerIcon } from 'vs/platform/theme/common/iconRegistry';
import { localize } from 'vs/nls';
import { MarshalledId } from 'vs/base/common/marshalling';
import { MarshalledId, revive } from 'vs/base/common/marshalling';


export class MainThreadCommentThread implements modes.CommentThread {
Expand Down Expand Up @@ -139,11 +139,27 @@ export class MainThreadCommentThread implements modes.CommentThread {
if (modified('range')) { this._range = changes.range!; }
if (modified('label')) { this._label = changes.label; }
if (modified('contextValue')) { this._contextValue = changes.contextValue === null ? undefined : changes.contextValue; }
if (modified('comments')) { this._comments = changes.comments; }
if (modified('comments')) { this._comments = this.commentsFromCommentChanges(changes.comments); }
if (modified('collapseState')) { this._collapsibleState = changes.collapseState; }
if (modified('canReply')) { this.canReply = changes.canReply!; }
}

private commentsFromCommentChanges(comments?: CommentChanges[]): modes.Comment[] | undefined {
return comments?.map(comment => {
return {
body: comment.body,
uniqueIdInThread: comment.uniqueIdInThread,
userName: comment.userName,
commentReactions: comment.commentReactions,
contextValue: comment.contextValue,
detail: comment.detail ? <Date | string>revive<Date | string>(comment.detail) : undefined,
label: comment.label,
mode: comment.mode,
userIconPath: comment.userIconPath
};
});
}

dispose() {
this._isDisposed = true;
this._onDidChangeCollasibleState.dispose();
Expand Down
18 changes: 16 additions & 2 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SerializedError } from 'vs/base/common/errors';
import { IRelativePattern } from 'vs/base/common/glob';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { IDisposable } from 'vs/base/common/lifecycle';
import { revive } from 'vs/base/common/marshalling';
import { MarshalledId, revive } from 'vs/base/common/marshalling';
import * as performance from 'vs/base/common/performance';
import Severity from 'vs/base/common/severity';
import { Dto } from 'vs/base/common/types';
Expand Down Expand Up @@ -160,11 +160,25 @@ export interface CommentProviderFeatures {
options?: modes.CommentOptions;
}

export interface CommentChanges {
readonly uniqueIdInThread: number;
readonly body: IMarkdownString;
readonly userName: string;
readonly userIconPath?: string;
readonly contextValue?: string;
readonly commentReactions?: modes.CommentReaction[];
readonly label?: string;
readonly mode?: modes.CommentMode;
readonly detail?: {
$mid: MarshalledId.Date
} | string;
}

export type CommentThreadChanges = Partial<{
range: IRange,
label: string,
contextValue: string | null,
comments: modes.Comment[],
comments: CommentChanges[],
collapseState: modes.CommentThreadCollapsibleState;
canReply: boolean;
}>;
Expand Down
Loading

0 comments on commit adb8450

Please sign in to comment.