Cleans up some duplicate code

This commit is contained in:
Eric Amodio
2017-06-12 10:33:58 -04:00
parent 5624567daa
commit 64ae82075e
6 changed files with 74 additions and 21 deletions

View File

@@ -17,9 +17,18 @@ export interface ICommitFormatOptions {
export class CommitFormatter {
private _commit: GitCommit;
private _options: ICommitFormatOptions;
constructor(private commit: GitCommit, options?: ICommitFormatOptions) {
constructor(commit: GitCommit, options?: ICommitFormatOptions) {
this.reset(commit, options);
}
reset(commit: GitCommit, options?: ICommitFormatOptions) {
this._commit = commit;
if (options === undefined && this._options !== undefined) return;
options = options || {};
if (options.tokenOptions == null) {
options.tokenOptions = {};
@@ -33,31 +42,31 @@ export class CommitFormatter {
}
get ago() {
const ago = moment(this.commit.date).fromNow();
const ago = moment(this._commit.date).fromNow();
return this._padOrTruncate(ago, this._options.tokenOptions!.ago);
}
get author() {
const author = this.commit.author;
const author = this._commit.author;
return this._padOrTruncate(author, this._options.tokenOptions!.author);
}
get authorAgo() {
const authorAgo = `${this.commit.author}, ${moment(this.commit.date).fromNow()}`;
const authorAgo = `${this._commit.author}, ${moment(this._commit.date).fromNow()}`;
return this._padOrTruncate(authorAgo, this._options.tokenOptions!.authorAgo);
}
get date() {
const date = moment(this.commit.date).format(this._options.dateFormat!);
const date = moment(this._commit.date).format(this._options.dateFormat!);
return this._padOrTruncate(date, this._options.tokenOptions!.date);
}
get id() {
return this.commit.shortSha;
return this._commit.shortSha;
}
get message() {
const message = this.commit.isUncommitted ? 'Uncommitted change' : this.commit.message;
const message = this._commit.isUncommitted ? 'Uncommitted change' : this._commit.message;
return this._padOrTruncate(message, this._options.tokenOptions!.message);
}
@@ -113,6 +122,18 @@ export class CommitFormatter {
return s;
}
private static _formatter: CommitFormatter | undefined = undefined;
static fromCommit(commit: GitCommit, options?: ICommitFormatOptions): CommitFormatter {
if (CommitFormatter._formatter === undefined) {
CommitFormatter._formatter = new CommitFormatter(commit, options);
}
else {
CommitFormatter._formatter.reset(commit, options);
}
return CommitFormatter._formatter;
}
static fromTemplate(template: string, commit: GitCommit, dateFormat: string | null): string;
static fromTemplate(template: string, commit: GitCommit, options?: ICommitFormatOptions): string;
static fromTemplate(template: string, commit: GitCommit, dateFormatOrOptions?: string | null | ICommitFormatOptions): string;

View File

@@ -102,6 +102,28 @@ export class GitUri extends Uri {
const uri = Uri.file(path.resolve(repoPath, original ? status.originalFileName || status.fileName : status.fileName));
return new GitUri(uri, repoPathOrCommit);
}
static getDirectory(fileName: string): string {
const directory: string | undefined = GitService.normalizePath(path.dirname(fileName));
return (!directory || directory === '.') ? '' : directory;
}
static getFormattedPath(fileNameOrUri: string | Uri, separator: string = ' \u00a0\u2022\u00a0 '): string {
let fileName: string;
if (fileNameOrUri instanceof Uri) {
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(separator);
fileName = fileNameOrUri.fsPath;
}
else {
fileName = fileNameOrUri;
}
const directory = GitUri.getDirectory(fileName);
return !directory
? path.basename(fileName)
: `${path.basename(fileName)}${separator}${directory}`;
}
}
export interface IGitCommitInfo {

View File

@@ -1,5 +1,6 @@
'use strict';
import { Uri } from 'vscode';
import { GitUri } from '../gitUri';
import * as path from 'path';
export interface GitStatus {
@@ -32,6 +33,14 @@ export class GitStatusFile implements IGitStatusFile {
this.originalFileName = originalFileName;
}
getFormattedDirectory(includeOriginal: boolean = false): string {
return GitStatusFile.getFormattedDirectory(this, includeOriginal);
}
getFormattedPath(separator: string = ' \u00a0\u2022\u00a0 '): string {
return GitUri.getFormattedPath(this.fileName, separator);
}
getIcon() {
return getGitStatusIcon(this.status);
}
@@ -39,6 +48,13 @@ export class GitStatusFile implements IGitStatusFile {
get Uri(): Uri {
return Uri.file(path.resolve(this.repoPath, this.fileName));
}
static getFormattedDirectory(status: IGitStatusFile, includeOriginal: boolean = false): string {
const directory = GitUri.getDirectory(status.fileName);
return (includeOriginal && status.status === 'R' && status.originalFileName)
? `${directory} \u00a0\u2190\u00a0 ${status.originalFileName}`
: directory;
}
}
const statusOcticonsMap = {