Adds new ${directory} token

Changes ${path} token to be the full path
Adds relative formatting support
This commit is contained in:
Eric Amodio
2017-09-26 18:19:33 -04:00
parent c44e4c6968
commit 4d18bf708d
6 changed files with 499 additions and 453 deletions

View File

@@ -6,7 +6,10 @@ import { GitStatusFile, IGitStatusFile, IGitStatusFileWithCommit } from '../mode
import * as path from 'path';
export interface IStatusFormatOptions extends IFormatOptions {
relativePath?: string;
tokenOptions?: {
directory?: Strings.ITokenOptions;
file?: Strings.ITokenOptions;
filePath?: Strings.ITokenOptions;
path?: Strings.ITokenOptions;
@@ -15,18 +18,23 @@ export interface IStatusFormatOptions extends IFormatOptions {
export class StatusFileFormatter extends Formatter<IGitStatusFile, IStatusFormatOptions> {
get directory() {
const directory = GitStatusFile.getFormattedDirectory(this._item, false, this._options.relativePath);
return this._padOrTruncate(directory, this._options.tokenOptions!.file);
}
get file() {
const file = path.basename(this._item.fileName);
return this._padOrTruncate(file, this._options.tokenOptions!.file);
}
get filePath() {
const filePath = GitStatusFile.getFormattedPath(this._item);
const filePath = GitStatusFile.getFormattedPath(this._item, undefined, this._options.relativePath);
return this._padOrTruncate(filePath, this._options.tokenOptions!.filePath);
}
get path() {
const directory = GitStatusFile.getFormattedDirectory(this._item, false);
const directory = GitStatusFile.getRelativePath(this._item, this._options.relativePath);
return this._padOrTruncate(directory, this._options.tokenOptions!.file);
}

View File

@@ -63,11 +63,14 @@ export class GitUri extends Uri {
return Uri.file(this.sha ? this.path : this.fsPath);
}
getFormattedPath(separator: string = Strings.pad(GlyphChars.Dot, 2, 2)): string {
getFormattedPath(separator: string = Strings.pad(GlyphChars.Dot, 2, 2), relativeTo?: string): string {
let directory = path.dirname(this.fsPath);
if (this.repoPath) {
directory = path.relative(this.repoPath, directory);
}
if (relativeTo !== undefined) {
directory = path.relative(relativeTo, directory);
}
directory = GitService.normalizePath(directory);
return (!directory || directory === '.')
@@ -75,8 +78,12 @@ export class GitUri extends Uri {
: `${path.basename(this.fsPath)}${separator}${directory}`;
}
getRelativePath(): string {
return GitService.normalizePath(path.relative(this.repoPath || '', this.fsPath));
getRelativePath(relativeTo?: string): string {
let relativePath = path.relative(this.repoPath || '', this.fsPath);
if (relativeTo !== undefined) {
relativePath = path.relative(relativeTo, relativePath);
}
return GitService.normalizePath(relativePath);
}
static async fromUri(uri: Uri, git: GitService) {
@@ -104,15 +111,19 @@ export class GitUri extends Uri {
return new GitUri(uri, repoPathOrCommit);
}
static getDirectory(fileName: string): string {
const directory: string | undefined = GitService.normalizePath(path.dirname(fileName));
static getDirectory(fileName: string, relativeTo?: string): string {
let directory: string | undefined = path.dirname(fileName);
if (relativeTo !== undefined) {
directory = path.relative(relativeTo, directory);
}
directory = GitService.normalizePath(directory);
return (!directory || directory === '.') ? '' : directory;
}
static getFormattedPath(fileNameOrUri: string | Uri, separator: string = Strings.pad(GlyphChars.Dot, 2, 2)): string {
static getFormattedPath(fileNameOrUri: string | Uri, separator: string = Strings.pad(GlyphChars.Dot, 2, 2), relativeTo?: string): string {
let fileName: string;
if (fileNameOrUri instanceof Uri) {
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(separator);
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(separator, relativeTo);
fileName = fileNameOrUri.fsPath;
}
@@ -120,11 +131,29 @@ export class GitUri extends Uri {
fileName = fileNameOrUri;
}
const directory = GitUri.getDirectory(fileName);
const directory = GitUri.getDirectory(fileName, relativeTo);
return !directory
? path.basename(fileName)
: `${path.basename(fileName)}${separator}${directory}`;
}
static getRelativePath(fileNameOrUri: string | Uri, relativeTo?: string, repoPath?: string): string {
let fileName: string;
if (fileNameOrUri instanceof Uri) {
if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getRelativePath(relativeTo);
fileName = fileNameOrUri.fsPath;
}
else {
fileName = fileNameOrUri;
}
let relativePath = path.relative(repoPath || '', fileName);
if (relativeTo !== undefined) {
relativePath = path.relative(relativeTo, relativePath);
}
return GitService.normalizePath(relativePath);
}
}
export interface IGitCommitInfo {

View File

@@ -56,15 +56,19 @@ export class GitStatusFile implements IGitStatusFile {
return Uri.file(path.resolve(this.repoPath, this.fileName));
}
static getFormattedDirectory(status: IGitStatusFile, includeOriginal: boolean = false): string {
const directory = GitUri.getDirectory(status.fileName);
static getFormattedDirectory(status: IGitStatusFile, includeOriginal: boolean = false, relativeTo?: string): string {
const directory = GitUri.getDirectory(status.fileName, relativeTo);
return (includeOriginal && status.status === 'R' && status.originalFileName)
? `${directory} ${Strings.pad(GlyphChars.ArrowLeft, 1, 1)} ${status.originalFileName}`
: directory;
}
static getFormattedPath(status: IGitStatusFile, separator: string = Strings.pad(GlyphChars.Dot, 2, 2)): string {
return GitUri.getFormattedPath(status.fileName, separator);
static getFormattedPath(status: IGitStatusFile, separator: string = Strings.pad(GlyphChars.Dot, 2, 2), relativeTo?: string): string {
return GitUri.getFormattedPath(status.fileName, separator, relativeTo);
}
static getRelativePath(status: IGitStatusFile, relativeTo?: string): string {
return GitUri.getRelativePath(status.fileName, relativeTo);
}
}