Skip to content

Commit

Permalink
Remove date formatter
Browse files Browse the repository at this point in the history
Since we are using native date formatting the formatter abstraction is no longer needed
  • Loading branch information
eamodio committed Jan 29, 2022
1 parent b7c1697 commit f107ef2
Show file tree
Hide file tree
Showing 16 changed files with 253 additions and 380 deletions.
36 changes: 19 additions & 17 deletions src/annotations/autolinks.ts
Expand Up @@ -4,8 +4,12 @@ import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitRemote, IssueOrPullRequest } from '../git/models';
import { Logger } from '../logger';
import { Dates, debug, Encoding, Iterables, Strings } from '../system';
import { fromNow } from '../system/date';
import { debug } from '../system/decorators/log';
import { encodeUrl } from '../system/encoding';
import { every, join, map } from '../system/iterable';
import { PromiseCancelledError, raceAll } from '../system/promise';
import { escapeMarkdown, escapeRegex, getSuperscript } from '../system/string';

const numRegex = /<num>/g;

Expand Down Expand Up @@ -70,9 +74,7 @@ export class Autolinks implements Disposable {

if (ref.messageRegex === undefined) {
ref.messageRegex = new RegExp(
`(?<=^|\\s|\\(|\\\\\\[)(${Strings.escapeRegex(ref.prefix)}([${
ref.alphanumeric ? '\\w' : '0-9'
}]+))\\b`,
`(?<=^|\\s|\\(|\\\\\\[)(${escapeRegex(ref.prefix)}([${ref.alphanumeric ? '\\w' : '0-9'}]+))\\b`,
ref.ignoreCase ? 'gi' : 'g',
);
}
Expand All @@ -90,7 +92,7 @@ export class Autolinks implements Disposable {
if (ids.size === 0) return undefined;

const issuesOrPullRequests = await raceAll(ids.values(), id => provider.getIssueOrPullRequest(id), timeout);
if (issuesOrPullRequests.size === 0 || Iterables.every(issuesOrPullRequests.values(), pr => pr === undefined)) {
if (issuesOrPullRequests.size === 0 || every(issuesOrPullRequests.values(), pr => pr === undefined)) {
return undefined;
}

Expand Down Expand Up @@ -146,15 +148,15 @@ export class Autolinks implements Disposable {
try {
if (ref.messageMarkdownRegex === undefined) {
ref.messageMarkdownRegex = new RegExp(
`(?<=^|\\s|\\(|\\\\\\[)(${Strings.escapeRegex(Strings.escapeMarkdown(ref.prefix))}([${
`(?<=^|\\s|\\(|\\\\\\[)(${escapeRegex(escapeMarkdown(ref.prefix))}([${
ref.alphanumeric ? '\\w' : '0-9'
}]+))\\b`,
ref.ignoreCase ? 'gi' : 'g',
);
}

if (issuesOrPullRequests == null || issuesOrPullRequests.size === 0) {
const replacement = `[$1](${Encoding.encodeUrl(ref.url.replace(numRegex, '$2'))}${
const replacement = `[$1](${encodeUrl(ref.url.replace(numRegex, '$2'))}${
ref.title ? ` "${ref.title.replace(numRegex, '$2')}"` : ''
})`;
ref.linkify = (text: string, markdown: boolean) =>
Expand All @@ -171,7 +173,7 @@ export class Autolinks implements Disposable {
return text.replace(ref.messageMarkdownRegex!, (_substring, linkText, num) => {
const issue = issuesOrPullRequests?.get(num);

const issueUrl = Encoding.encodeUrl(ref.url.replace(numRegex, num));
const issueUrl = encodeUrl(ref.url.replace(numRegex, num));

let title = '';
if (ref.title) {
Expand All @@ -191,15 +193,15 @@ export class Autolinks implements Disposable {
issue,
)} [**${issueTitle}**](${issueUrl}${title}")\\\n${GlyphChars.Space.repeat(
5,
)}${linkText} ${issue.closed ? 'closed' : 'opened'} ${Dates.getFormatter(
)}${linkText} ${issue.closed ? 'closed' : 'opened'} ${fromNow(
issue.closedDate ?? issue.date,
).fromNow()}`,
)}`,
);
}

title += `\n${GlyphChars.Dash.repeat(2)}\n${issueTitle}\n${
issue.closed ? 'Closed' : 'Opened'
}, ${Dates.getFormatter(issue.closedDate ?? issue.date).fromNow()}`;
}, ${fromNow(issue.closedDate ?? issue.date)}`;
}
}
title += '"';
Expand All @@ -223,17 +225,17 @@ export class Autolinks implements Disposable {
`${linkText}: ${
issue instanceof PromiseCancelledError
? 'Details timed out'
: `${issue.title} ${GlyphChars.Dot} ${
issue.closed ? 'Closed' : 'Opened'
}, ${Dates.getFormatter(issue.closedDate ?? issue.date).fromNow()}`
: `${issue.title} ${GlyphChars.Dot} ${issue.closed ? 'Closed' : 'Opened'}, ${fromNow(
issue.closedDate ?? issue.date,
)}`
}`,
);
return `${linkText}${Strings.getSuperscript(index)}`;
return `${linkText}${getSuperscript(index)}`;
});

return includeFootnotes && footnotes != null && footnotes.size !== 0
? `${text}\n${GlyphChars.Dash.repeat(2)}\n${Iterables.join(
Iterables.map(footnotes, ([i, footnote]) => `${Strings.getSuperscript(i)} ${footnote}`),
? `${text}\n${GlyphChars.Dash.repeat(2)}\n${join(
map(footnotes, ([i, footnote]) => `${getSuperscript(i)} ${footnote}`),
'\n',
)}`
: text;
Expand Down
15 changes: 5 additions & 10 deletions src/commands/git/fetch.ts
Expand Up @@ -2,7 +2,9 @@ import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranchReference, GitReference, Repository } from '../../git/models';
import { FlagsQuickPickItem } from '../../quickpicks';
import { Arrays, Dates, Strings } from '../../system';
import { isStringArray } from '../../system/array';
import { fromNow } from '../../system/date';
import { pad } from '../../system/string';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import {
appendReposToTitle,
Expand Down Expand Up @@ -86,12 +88,7 @@ export class FetchGitCommand extends QuickCommand<State> {
while (this.canStepsContinue(state)) {
context.title = this.title;

if (
state.counter < 1 ||
state.repos == null ||
state.repos.length === 0 ||
Arrays.isStringArray(state.repos)
) {
if (state.counter < 1 || state.repos == null || state.repos.length === 0 || isStringArray(state.repos)) {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
Expand Down Expand Up @@ -137,9 +134,7 @@ export class FetchGitCommand extends QuickCommand<State> {
if (state.repos.length === 1) {
const lastFetched = await state.repos[0].getLastFetched();
if (lastFetched !== 0) {
lastFetchedOn = `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched ${Dates.getFormatter(
new Date(lastFetched),
).fromNow()}`;
lastFetchedOn = `${pad(GlyphChars.Dot, 2, 2)}Last fetched ${fromNow(new Date(lastFetched))}`;
}
}

Expand Down
26 changes: 10 additions & 16 deletions src/commands/git/pull.ts
Expand Up @@ -2,7 +2,9 @@ import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranch, GitBranchReference, GitReference, Repository } from '../../git/models';
import { Directive, DirectiveQuickPickItem, FlagsQuickPickItem } from '../../quickpicks';
import { Arrays, Dates, Strings } from '../../system';
import { isStringArray } from '../../system/array';
import { fromNow } from '../../system/date';
import { pad, pluralize } from '../../system/string';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import {
appendReposToTitle,
Expand Down Expand Up @@ -92,12 +94,7 @@ export class PullGitCommand extends QuickCommand<State> {
while (this.canStepsContinue(state)) {
context.title = this.title;

if (
state.counter < 1 ||
state.repos == null ||
state.repos.length === 0 ||
Arrays.isStringArray(state.repos)
) {
if (state.counter < 1 || state.repos == null || state.repos.length === 0 || isStringArray(state.repos)) {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
Expand Down Expand Up @@ -182,10 +179,9 @@ export class PullGitCommand extends QuickCommand<State> {
label: this.title,
detail: `Will pull${
branch.state.behind
? ` ${Strings.pluralize(
'commit',
branch.state.behind,
)} into ${GitReference.toString(branch)}`
? ` ${pluralize('commit', branch.state.behind)} into ${GitReference.toString(
branch,
)}`
: ` into ${GitReference.toString(branch)}`
}`,
}),
Expand All @@ -198,14 +194,12 @@ export class PullGitCommand extends QuickCommand<State> {

let lastFetchedOn = '';
if (lastFetched !== 0) {
lastFetchedOn = `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched ${Dates.getFormatter(
new Date(lastFetched),
).fromNow()}`;
lastFetchedOn = `${pad(GlyphChars.Dot, 2, 2)}Last fetched ${fromNow(new Date(lastFetched))}`;
}

const pullDetails =
status?.state.behind != null
? ` ${Strings.pluralize('commit', status.state.behind)} into $(repo) ${repo.formattedName}`
? ` ${pluralize('commit', status.state.behind)} into $(repo) ${repo.formattedName}`
: ` into $(repo) ${repo.formattedName}`;

step = this.createConfirmStep(
Expand All @@ -227,7 +221,7 @@ export class PullGitCommand extends QuickCommand<State> {
onDidClickButton: async (quickpick, button) => {
if (button !== QuickCommandButtons.Fetch || quickpick.busy) return false;

quickpick.title = `Confirm ${context.title}${Strings.pad(GlyphChars.Dot, 2, 2)}Fetching${
quickpick.title = `Confirm ${context.title}${pad(GlyphChars.Dot, 2, 2)}Fetching${
GlyphChars.Ellipsis
}`;

Expand Down
43 changes: 15 additions & 28 deletions src/commands/git/push.ts
Expand Up @@ -3,7 +3,9 @@ import { BuiltInGitConfiguration, GlyphChars } from '../../constants';
import { Container } from '../../container';
import { GitBranch, GitBranchReference, GitReference, Repository } from '../../git/models';
import { Directive, DirectiveQuickPickItem, FlagsQuickPickItem } from '../../quickpicks';
import { Arrays, Dates, Strings } from '../../system';
import { isStringArray } from '../../system/array';
import { fromNow } from '../../system/date';
import { pad, pluralize } from '../../system/string';
import { ViewsWithRepositoryFolders } from '../../views/viewBase';
import {
appendReposToTitle,
Expand Down Expand Up @@ -98,12 +100,7 @@ export class PushGitCommand extends QuickCommand<State> {
while (this.canStepsContinue(state)) {
context.title = this.title;

if (
state.counter < 1 ||
state.repos == null ||
state.repos.length === 0 ||
Arrays.isStringArray(state.repos)
) {
if (state.counter < 1 || state.repos == null || state.repos.length === 0 || isStringArray(state.repos)) {
skippedStepOne = false;
if (context.repos.length === 1) {
skippedStepOne = true;
Expand Down Expand Up @@ -233,15 +230,10 @@ export class PushGitCommand extends QuickCommand<State> {
label: `Force ${this.title}${useForceWithLease ? ' (with lease)' : ''}`,
description: `--force${useForceWithLease ? '-with-lease' : ''}`,
detail: `Will force push${useForceWithLease ? ' (with lease)' : ''} ${
branch?.state.ahead
? ` ${Strings.pluralize('commit', branch.state.ahead)}`
: ''
branch?.state.ahead ? ` ${pluralize('commit', branch.state.ahead)}` : ''
}${branch.getRemoteName() ? ` to ${branch.getRemoteName()}` : ''}${
branch != null && branch.state.behind > 0
? `, overwriting ${Strings.pluralize(
'commit',
branch.state.behind,
)}${
? `, overwriting ${pluralize('commit', branch.state.behind)}${
branch?.getRemoteName()
? ` on ${branch.getRemoteName()}`
: ''
Expand All @@ -255,17 +247,14 @@ export class PushGitCommand extends QuickCommand<State> {
label: `Cancel ${this.title}`,
detail: `Cannot push; ${GitReference.toString(
branch,
)} is behind ${branch.getRemoteName()} by ${Strings.pluralize(
'commit',
branch.state.behind,
)}`,
)} is behind ${branch.getRemoteName()} by ${pluralize('commit', branch.state.behind)}`,
}),
);
} else if (branch != null && branch?.state.ahead > 0) {
step = this.createConfirmStep(appendReposToTitle(`Confirm ${context.title}`, state, context), [
FlagsQuickPickItem.create<Flags>(state.flags, [branch.getRemoteName()!], {
label: this.title,
detail: `Will push ${Strings.pluralize(
detail: `Will push ${pluralize(
'commit',
branch.state.ahead,
)} from ${GitReference.toString(branch)} to ${branch.getRemoteName()}`,
Expand Down Expand Up @@ -343,9 +332,7 @@ export class PushGitCommand extends QuickCommand<State> {

const lastFetched = await repo.getLastFetched();
if (lastFetched !== 0) {
lastFetchedOn = `${Strings.pad(GlyphChars.Dot, 2, 2)}Last fetched ${Dates.getFormatter(
new Date(lastFetched),
).fromNow()}`;
lastFetchedOn = `${pad(GlyphChars.Dot, 2, 2)}Last fetched ${fromNow(new Date(lastFetched))}`;
}

let pushDetails;
Expand All @@ -358,9 +345,9 @@ export class PushGitCommand extends QuickCommand<State> {
: ''
}${status?.upstream ? ` to ${GitBranch.getRemote(status.upstream)}` : ''}`;
} else {
pushDetails = `${
status?.state.ahead ? ` ${Strings.pluralize('commit', status.state.ahead)}` : ''
}${status?.upstream ? ` to ${GitBranch.getRemote(status.upstream)}` : ''}`;
pushDetails = `${status?.state.ahead ? ` ${pluralize('commit', status.state.ahead)}` : ''}${
status?.upstream ? ` to ${GitBranch.getRemote(status.upstream)}` : ''
}`;
}

step = this.createConfirmStep(
Expand All @@ -379,7 +366,7 @@ export class PushGitCommand extends QuickCommand<State> {
description: `--force${useForceWithLease ? '-with-lease' : ''}`,
detail: `Will force push${useForceWithLease ? ' (with lease)' : ''} ${pushDetails}${
status != null && status.state.behind > 0
? `, overwriting ${Strings.pluralize('commit', status.state.behind)}${
? `, overwriting ${pluralize('commit', status.state.behind)}${
status?.upstream ? ` on ${GitBranch.getRemote(status.upstream)}` : ''
}`
: ''
Expand All @@ -391,7 +378,7 @@ export class PushGitCommand extends QuickCommand<State> {
label: `Cancel ${this.title}`,
detail: `Cannot push; ${GitReference.toString(branch)} is behind${
status?.upstream ? ` ${GitBranch.getRemote(status.upstream)}` : ''
} by ${Strings.pluralize('commit', status.state.behind)}`,
} by ${pluralize('commit', status.state.behind)}`,
})
: undefined,
);
Expand All @@ -400,7 +387,7 @@ export class PushGitCommand extends QuickCommand<State> {
step.onDidClickButton = async (quickpick, button) => {
if (button !== QuickCommandButtons.Fetch || quickpick.busy) return false;

quickpick.title = `Confirm ${context.title}${Strings.pad(GlyphChars.Dot, 2, 2)}Fetching${
quickpick.title = `Confirm ${context.title}${pad(GlyphChars.Dot, 2, 2)}Fetching${
GlyphChars.Ellipsis
}`;

Expand Down
18 changes: 8 additions & 10 deletions src/git/models/branch.ts
@@ -1,7 +1,10 @@
import { BranchSorting, configuration, DateStyle } from '../../configuration';
import { Starred, WorkspaceState } from '../../constants';
import { Container } from '../../container';
import { Dates, debug, memoize, Strings } from '../../system';
import { formatDate, fromNow } from '../../system/date';
import { debug } from '../../system/decorators/log';
import { memoize } from '../../system/decorators/memoize';
import { sortCompare } from '../../system/string';
import { GitBranchReference, GitReference, GitRevision } from '../models';
import { PullRequest, PullRequestState } from './pullRequest';
import { GitRemote } from './remote';
Expand Down Expand Up @@ -78,7 +81,7 @@ export class GitBranch implements GitBranchReference {
(a.name === 'master' ? -1 : 1) - (b.name === 'master' ? -1 : 1) ||
(a.name === 'develop' ? -1 : 1) - (b.name === 'develop' ? -1 : 1) ||
(b.remote ? -1 : 1) - (a.remote ? -1 : 1) ||
Strings.sortCompare(a.name, b.name),
sortCompare(a.name, b.name),
);
case BranchSorting.NameDesc:
return branches.sort(
Expand All @@ -92,7 +95,7 @@ export class GitBranch implements GitBranchReference {
(a.name === 'master' ? -1 : 1) - (b.name === 'master' ? -1 : 1) ||
(a.name === 'develop' ? -1 : 1) - (b.name === 'develop' ? -1 : 1) ||
(b.remote ? -1 : 1) - (a.remote ? -1 : 1) ||
Strings.sortCompare(b.name, a.name),
sortCompare(b.name, a.name),
);
case BranchSorting.DateDesc:
default:
Expand Down Expand Up @@ -152,22 +155,17 @@ export class GitBranch implements GitBranchReference {
return this.detached ? this.sha! : this.name;
}

@memoize()
private get dateFormatter(): Dates.DateFormatter | undefined {
return this.date == null ? undefined : Dates.getFormatter(this.date);
}

@memoize<GitBranch['formatDate']>(format => (format == null ? 'MMMM Do, YYYY h:mma' : format))
formatDate(format?: string | null): string {
if (format == null) {
format = 'MMMM Do, YYYY h:mma';
}

return this.dateFormatter?.format(format) ?? '';
return this.date != null ? formatDate(this.date, format) : '';
}

formatDateFromNow(): string {
return this.dateFormatter?.fromNow() ?? '';
return this.date != null ? fromNow(this.date) : '';
}

@debug()
Expand Down

0 comments on commit f107ef2

Please sign in to comment.