From 2d977262d23fe3343be180e55e77888bbd398c59 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sat, 1 Sep 2018 04:48:05 -0400 Subject: [PATCH] Closes #493 - Adds changes to commits in explorers Adds new ${changes} and ${changesShort} template tokens Adds support for prefixes and suffixes around tokens Fixes missing/mismatched token option issues --- package.json | 4 +- src/git/formatters/commitFormatter.ts | 29 ++++++++++-- src/git/formatters/formatter.ts | 83 +++++++++++++++++++++-------------- src/git/formatters/statusFormatter.ts | 8 +++- src/git/models/logCommit.ts | 81 ++++++++++++++++++++++++++-------- src/git/models/status.ts | 74 +++++++++++++++++-------------- src/quickpicks/commitQuickPick.ts | 2 +- src/quickpicks/commonQuickPicks.ts | 4 +- src/system/string.ts | 19 ++++---- src/views/nodes/commitNode.ts | 8 ++++ src/views/nodes/statusNode.ts | 4 +- 11 files changed, 212 insertions(+), 104 deletions(-) diff --git a/package.json b/package.json index b123d85..58ec54c 100644 --- a/package.json +++ b/package.json @@ -451,7 +451,7 @@ }, "gitlens.explorers.commitFormat": { "type": "string", - "default": "${message} • ${authorAgoOrDate} (${id})", + "default": "${message} • ${authorAgoOrDate}${ (id)}", "description": "Specifies the format of committed changes in the `GitLens` and `GitLens Results` explorers\nAvailable tokens\n ${id} - commit id\n ${author} - commit author\n ${message} - commit message\n ${ago} - relative commit date (e.g. 1 day ago)\n ${date} - formatted commit date (format specified by `gitlens.defaultDateFormat`)\\n ${agoOrDate} - commit date specified by `gitlens.defaultDateStyle`\n ${authorAgo} - commit author, relative commit date\n ${authorAgoOrDate} - commit author, commit date specified by `gitlens.defaultDateStyle`\nSee https://github.com/eamodio/vscode-gitlens/wiki/Advanced-Formatting for advanced formatting", "scope": "window" }, @@ -469,7 +469,7 @@ }, "gitlens.explorers.statusFileFormat": { "type": "string", - "default": "${working}${filePath}", + "default": "${working }${filePath}", "description": "Specifies the format of the status of a working or committed file in the `GitLens` and `GitLens Results` explorers\nAvailable tokens\n ${directory} - directory name\n ${file} - file name\n ${filePath} - formatted file name and path\n ${path} - full file path\n ${working} - optional indicator if the file is uncommitted", "scope": "window" }, diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index 3fffd41..2ba0981 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -3,7 +3,8 @@ import { DateStyle } from '../../configuration'; import { GlyphChars } from '../../constants'; import { Container } from '../../container'; import { Strings } from '../../system'; -import { GitCommit } from '../models/commit'; +import { GitCommit, GitCommitType } from '../models/commit'; +import { GitLogCommit } from '../models/models'; import { Formatter, IFormatOptions } from './formatter'; const emojiMap: { [key: string]: string } = require('../../../emoji/emojis.json'); @@ -19,7 +20,10 @@ export interface ICommitFormatOptions extends IFormatOptions { author?: Strings.ITokenOptions; authorAgo?: Strings.ITokenOptions; authorAgoOrDate?: Strings.ITokenOptions; + changes?: Strings.ITokenOptions; + changesShort?: Strings.ITokenOptions; date?: Strings.ITokenOptions; + id?: Strings.ITokenOptions; message?: Strings.ITokenOptions; }; } @@ -59,7 +63,26 @@ export class CommitFormatter extends Formatter get authorAgoOrDate() { const authorAgo = `${this._item.author}, ${this._agoOrDate}`; - return this._padOrTruncate(authorAgo, this._options.tokenOptions!.authorAgo); + return this._padOrTruncate(authorAgo, this._options.tokenOptions!.authorAgoOrDate); + } + + get changes() { + if (!(this._item instanceof GitLogCommit) || this._item.type === GitCommitType.File) { + return this._padOrTruncate('', this._options.tokenOptions!.changes); + } + + return this._padOrTruncate(this._item.getFormattedDiffStatus(), this._options.tokenOptions!.changes); + } + + get changesShort() { + if (!(this._item instanceof GitLogCommit) || this._item.type === GitCommitType.File) { + return this._padOrTruncate('', this._options.tokenOptions!.changesShort); + } + + return this._padOrTruncate( + this._item.getFormattedDiffStatus({ compact: true, separator: '' }), + this._options.tokenOptions!.changesShort + ); } get date() { @@ -67,7 +90,7 @@ export class CommitFormatter extends Formatter } get id() { - return this._item.shortSha; + return this._padOrTruncate(this._item.shortSha || '', this._options.tokenOptions!.id); } get message() { diff --git a/src/git/formatters/formatter.ts b/src/git/formatters/formatter.ts index 49857b3..079ab78 100644 --- a/src/git/formatters/formatter.ts +++ b/src/git/formatters/formatter.ts @@ -41,50 +41,62 @@ export abstract class Formatter 0 && options.truncateTo !== undefined) { + s = Strings.padLeft(s, diff, undefined, width); + } + } } + else { + max += this.collapsableWhitespace; + this.collapsableWhitespace = 0; - max += this.collapsableWhitespace; - this.collapsableWhitespace = 0; - - const width = Strings.getWidth(s); - const diff = max - width; - if (diff > 0) { - if (options.collapseWhitespace) { - this.collapsableWhitespace = diff; + const width = Strings.getWidth(s); + const diff = max - width; + if (diff > 0) { + if (options.collapseWhitespace) { + this.collapsableWhitespace = diff; + } + + if (options.padDirection === 'left') { + s = Strings.padLeft(s, max, undefined, width); + } + else { + if (options.collapseWhitespace) { + max -= diff; + } + s = Strings.padRight(s, max, undefined, width); + } } - - if (options.padDirection === 'left') return Strings.padLeft(s, max, undefined, width); - - if (options.collapseWhitespace) { - max -= diff; + else if (diff < 0) { + s = Strings.truncate(s, max, undefined, width); } - return Strings.padRight(s, max, undefined, width); } - if (diff < 0) return Strings.truncate(s, max, undefined, width); + if (options.prefix || options.suffix) { + s = `${options.prefix || ''}${s}${options.suffix || ''}`; + } return s; } @@ -107,6 +119,15 @@ export abstract class Formatter { map[token.key] = token.options; @@ -115,13 +136,7 @@ export abstract class Formatter { get directory() { const directory = GitStatusFile.getFormattedDirectory(this._item, false, this._options.relativePath); - return this._padOrTruncate(directory, this._options.tokenOptions!.file); + return this._padOrTruncate(directory, this._options.tokenOptions!.directory); } get file() { @@ -45,7 +46,10 @@ export class StatusFileFormatter extends Formatter