Skip to content

Commit

Permalink
Removes working status placeholders if not changed in working tree
Browse files Browse the repository at this point in the history
Adds better sorting of working tree file changes
Changes non-working tree file changes back to use status icons
  • Loading branch information
eamodio committed Sep 28, 2018
1 parent f9c8fdb commit 9eaac0e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3196,7 +3196,7 @@
},
{
"command": "gitlens.explorers.openFile",
"when": "viewItem =~ /gitlens:file\\b/",
"when": "viewItem =~ /gitlens:(file|history-file|status:file)\\b/",
"group": "inline"
},
{
Expand Down
4 changes: 1 addition & 3 deletions src/git/formatters/statusFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ export class StatusFileFormatter extends Formatter<GitFile, IStatusFormatOptions
icon = `${GlyphChars.Space}${GlyphChars.EnDash}${GlyphChars.Space.repeat(2)}${GlyphChars.Check}`;
}
else {
icon = `${GlyphChars.Space}${GlyphChars.EnDash}${GlyphChars.Space.repeat(2)}${GlyphChars.EnDash}${
GlyphChars.Space
}`;
icon = '';
}
return this._padOrTruncate(icon, this._options.tokenOptions!.working);
}
Expand Down
10 changes: 7 additions & 3 deletions src/views/nodes/commitFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export enum CommitFileNodeDisplayAs {
}

export class CommitFileNode extends ExplorerRefNode {
readonly priority: boolean = false;

constructor(
public readonly file: GitFile,
public commit: GitLogCommit,
Expand All @@ -41,6 +39,10 @@ export class CommitFileNode extends ExplorerRefNode {
super(GitUri.fromFile(file, commit.repoPath, commit.sha), parent);
}

get priority(): number {
return 0;
}

get ref(): string {
return this.commit.sha;
}
Expand Down Expand Up @@ -132,7 +134,9 @@ export class CommitFileNode extends ExplorerRefNode {
}

protected get resourceType(): ResourceType {
return ResourceType.CommitFile;
if (!this.commit.isUncommitted) return ResourceType.CommitFile;

return this.commit.isStagedUncommitted ? ResourceType.FileStaged : ResourceType.FileUnstaged;
}

private _tooltip: string | undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/views/nodes/explorerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export enum ResourceType {
Commits = 'gitlens:commits',
ComparisonResults = 'gitlens:results:comparison',
FileHistory = 'gitlens:history-file',
FileStaged = 'gitlens:file:staged',
FileStagedAndUnstaged = 'gitlens:file:staged:unstaged',
FileUnstaged = 'gitlens:file:unstaged',
Folder = 'gitlens:folder',
Message = 'gitlens:message',
Pager = 'gitlens:pager',
Expand All @@ -34,9 +37,8 @@ export enum ResourceType {
Stash = 'gitlens:stash',
StashFile = 'gitlens:file:stash',
Stashes = 'gitlens:stashes',
StatusFile = 'gitlens:file:status',
StatusFileCommits = 'gitlens:status:file:commits',
StatusFiles = 'gitlens:status:files',
StatusFileCommits = 'gitlens:status:file-commits',
StatusUpstream = 'gitlens:status:upstream',
Tag = 'gitlens:tag',
Tags = 'gitlens:tags'
Expand Down
6 changes: 3 additions & 3 deletions src/views/nodes/folderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { ExplorerNode, ResourceType } from './explorerNode';
export interface FileExplorerNode extends ExplorerNode {
folderName: string;
label?: string;
priority: boolean;
priority: number;
relativePath?: string;
root?: Arrays.IHierarchicalItem<FileExplorerNode>;
}

export class FolderNode extends ExplorerNode {
readonly priority: boolean = true;
readonly priority: number = 1;

constructor(
public readonly repoPath: string,
Expand Down Expand Up @@ -60,7 +60,7 @@ export class FolderNode extends ExplorerNode {
children.sort((a, b) => {
return (
(a instanceof FolderNode ? -1 : 1) - (b instanceof FolderNode ? -1 : 1) ||
(a.priority ? -1 : 1) - (b.priority ? -1 : 1) ||
a.priority - b.priority ||
a.label!.localeCompare(b.label!)
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/views/nodes/resultsFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class ResultsFileNode extends ExplorerNode {
this._label = undefined;
}

get priority(): boolean {
return false;
get priority(): number {
return 0;
}

getCommand(): Command | undefined {
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/resultsFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ResultsFilesNode extends ExplorerNode {
children = (await root.getChildren()) as FileExplorerNode[];
}
else {
children.sort((a, b) => (a.priority ? -1 : 1) - (b.priority ? -1 : 1) || a.label!.localeCompare(b.label!));
children.sort((a, b) => a.priority - b.priority || a.label!.localeCompare(b.label!));
}

return children;
Expand Down
85 changes: 64 additions & 21 deletions src/views/nodes/statusFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as path from 'path';
import { Command, ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from 'vscode';
import { Commands, DiffWithPreviousCommandArgs } from '../../commands';
import { Container } from '../../container';
import {
GitFile,
GitFileWithCommit,
Expand All @@ -16,6 +17,9 @@ import { CommitFileNode, CommitFileNodeDisplayAs } from './commitFileNode';
import { ExplorerNode, ResourceType } from './explorerNode';

export class StatusFileNode extends ExplorerNode {
private readonly _hasStagedChanges: boolean = false;
private readonly _hasUnstagedChanges: boolean = false;

constructor(
public readonly repoPath: string,
public readonly file: GitFile,
Expand All @@ -24,6 +28,17 @@ export class StatusFileNode extends ExplorerNode {
public readonly explorer: Explorer
) {
super(GitUri.fromFile(file, repoPath, 'HEAD'), parent);

for (const c of this.commits) {
if (c.isStagedUncommitted) {
this._hasStagedChanges = true;
}
else if (c.isUncommitted) {
this._hasUnstagedChanges = true;
}

if (this._hasStagedChanges && this._hasUnstagedChanges) break;
}
}

async getChildren(): Promise<ExplorerNode[]> {
Expand All @@ -45,36 +60,60 @@ export class StatusFileNode extends ExplorerNode {
async getTreeItem(): Promise<TreeItem> {
const item = new TreeItem(this.label, TreeItemCollapsibleState.None);

if (this.commits.length === 1 && this.commit.isUncommitted) {
item.contextValue = ResourceType.StatusFile;

if (this.commit.isStagedUncommitted) {
if ((this._hasStagedChanges || this._hasUnstagedChanges) && this.commits.length === 1) {
if (this._hasStagedChanges) {
item.contextValue = ResourceType.FileStaged;
item.tooltip = StatusFileFormatter.fromTemplate(
'${file}\n${directory}/\n\n${status} in Index (staged)',
this.file
);
}
else {
item.contextValue = ResourceType.FileUnstaged;
item.tooltip = StatusFileFormatter.fromTemplate(
'${file}\n${directory}/\n\n${status} in Working Tree',
this.file
);
}

// Use the file icon and decorations
item.resourceUri = Uri.file(path.resolve(this.repoPath, this.file.fileName));
item.iconPath = ThemeIcon.File;

item.command = this.getCommand();
}
else {
item.collapsibleState = TreeItemCollapsibleState.Collapsed;
item.contextValue = ResourceType.StatusFileCommits;
if (this._hasStagedChanges || this._hasUnstagedChanges) {
if (this._hasStagedChanges && this._hasUnstagedChanges) {
item.contextValue = ResourceType.FileStagedAndUnstaged;
}
else if (this._hasStagedChanges) {
item.contextValue = ResourceType.FileStaged;
}
else {
item.contextValue = ResourceType.FileUnstaged;
}

// Use the file icon and decorations
item.resourceUri = Uri.file(path.resolve(this.repoPath, this.file.fileName));
item.iconPath = ThemeIcon.File;
}
else {
item.contextValue = ResourceType.StatusFileCommits;

const icon = GitFile.getStatusIcon(this.file.status);
item.iconPath = {
dark: Container.context.asAbsolutePath(path.join('images', 'dark', icon)),
light: Container.context.asAbsolutePath(path.join('images', 'light', icon))
};
}
item.tooltip = StatusFileFormatter.fromTemplate(
`\${file}\n\${directory}/\n\n\${status} in ${this.getChangedIn()}`,
this.file
);
}

// Use the file icon and decorations
item.resourceUri = Uri.file(path.resolve(this.repoPath, this.file.fileName));
item.iconPath = ThemeIcon.File;

// Only cache the label for a single refresh
this._label = undefined;

Expand Down Expand Up @@ -110,8 +149,11 @@ export class StatusFileNode extends ExplorerNode {
return this.commits[0];
}

get priority(): boolean {
return this.commit.isUncommitted;
get priority(): number {
if (this._hasStagedChanges && !this._hasUnstagedChanges) return -3;
if (this._hasStagedChanges) return -2;
if (this._hasUnstagedChanges) return -1;
return 0;
}

private _relativePath: string | undefined;
Expand All @@ -125,20 +167,21 @@ export class StatusFileNode extends ExplorerNode {

private getChangedIn(): string {
const changedIn = [];

let commits = 0;
for (const c of this.commits) {
if (c.isUncommitted) {
if (c.isStagedUncommitted) {
changedIn.push('Index (staged)');
}
else {
changedIn.push('Working Tree');
}

continue;
}
if (this._hasUnstagedChanges) {
commits++;
changedIn.push('Working Tree');
}

if (this._hasStagedChanges) {
commits++;
changedIn.push('Index (staged)');
}

if (this.commits.length > commits) {
commits = this.commits.length - commits;
}

if (commits > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/statusFilesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class StatusFilesNode extends ExplorerNode {
children = (await root.getChildren()) as FileExplorerNode[];
}
else {
children.sort((a, b) => (a.priority ? -1 : 1) - (b.priority ? -1 : 1) || a.label!.localeCompare(b.label!));
children.sort((a, b) => a.priority - b.priority || a.label!.localeCompare(b.label!));
}

return children;
Expand Down

0 comments on commit 9eaac0e

Please sign in to comment.