Skip to content

Commit

Permalink
Adds parent links to all nodes
Browse files Browse the repository at this point in the history
When tracking the active file, only select the repo if in another
  • Loading branch information
eamodio committed Sep 12, 2018
1 parent ae2c47b commit 8ea9233
Show file tree
Hide file tree
Showing 32 changed files with 206 additions and 142 deletions.
10 changes: 8 additions & 2 deletions src/views/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export abstract class ExplorerBase<TRoot extends ExplorerNode> implements TreeDa
return this._root.getChildren();
}

getParent(): ExplorerNode | undefined {
return undefined;
getParent(node: ExplorerNode): ExplorerNode | undefined {
return node.getParent();
}

getTreeItem(node: ExplorerNode): TreeItem | Promise<TreeItem> {
Expand All @@ -102,6 +102,12 @@ export abstract class ExplorerBase<TRoot extends ExplorerNode> implements TreeDa
this._onDidChangeVisibility.fire(e);
}

get selection(): ExplorerNode[] {
if (this._tree === undefined || this._root === undefined) return [];

return this._tree.selection;
}

get visible(): boolean {
return this._tree !== undefined ? this._tree.visible : false;
}
Expand Down
15 changes: 8 additions & 7 deletions src/views/nodes/branchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode
constructor(
public readonly branch: GitBranch,
uri: GitUri,
protected readonly explorer: GitExplorer,
private readonly markCurrent: boolean = true
parent: ExplorerNode,
public readonly explorer: GitExplorer,
private readonly _markCurrent: boolean = true
) {
super(uri);
super(uri, parent);
}

get id(): string {
return `gitlens:repository(${this.branch.repoPath}):branch(${this.branch.name})${
this.branch.remote ? ':remote' : ''
}${this.markCurrent ? ':current' : ''}`;
}${this._markCurrent ? ':current' : ''}`;
}

get current(): boolean {
Expand All @@ -52,7 +53,7 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode
maxCount: this.maxCount || this.explorer.config.defaultItemLimit,
ref: this.ref
});
if (log === undefined) return [new MessageNode('No commits yet')];
if (log === undefined) return [new MessageNode(this, 'No commits yet')];

const branches = await Container.git.getBranches(this.uri.repoPath);
// Get the sha length, since `git branch` can return variable length shas
Expand All @@ -72,7 +73,7 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode
const children: (CommitNode | ShowMoreNode)[] = [
...Iterables.map(
log.commits.values(),
c => new CommitNode(c, this.explorer, this.branch, getBranchTips)
c => new CommitNode(c, this, this.explorer, this.branch, getBranchTips)
)
];

Expand Down Expand Up @@ -113,7 +114,7 @@ export class BranchNode extends ExplorerRefNode implements PageableExplorerNode
}

const item = new TreeItem(
`${this.markCurrent && this.current ? `${GlyphChars.Check} ${GlyphChars.Space}` : ''}${name}`,
`${this._markCurrent && this.current ? `${GlyphChars.Check} ${GlyphChars.Space}` : ''}${name}`,
TreeItemCollapsibleState.Collapsed
);
item.id = this.id;
Expand Down
10 changes: 6 additions & 4 deletions src/views/nodes/branchOrTagFolderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export class BranchOrTagFolderNode extends ExplorerNode {
public readonly folderName: string,
public readonly relativePath: string | undefined,
public readonly root: Arrays.IHierarchicalItem<BranchNode | TagNode>,
private readonly explorer: Explorer,
private readonly expanded: boolean = false
parent: ExplorerNode,
public readonly explorer: Explorer,
private readonly _expanded: boolean = false
) {
super(GitUri.fromRepoPath(repoPath));
super(GitUri.fromRepoPath(repoPath), parent);
}

get id(): string {
Expand All @@ -42,6 +43,7 @@ export class BranchOrTagFolderNode extends ExplorerNode {
folder.name,
folder.relativePath,
folder,
this,
this.explorer,
expanded
)
Expand All @@ -58,7 +60,7 @@ export class BranchOrTagFolderNode extends ExplorerNode {
async getTreeItem(): Promise<TreeItem> {
const item = new TreeItem(
this.label,
this.expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed
this._expanded ? TreeItemCollapsibleState.Expanded : TreeItemCollapsibleState.Collapsed
);
item.id = this.id;
item.contextValue = ResourceType.Folder;
Expand Down
19 changes: 14 additions & 5 deletions src/views/nodes/branchesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export class BranchesNode extends ExplorerNode {

constructor(
uri: GitUri,
private readonly repo: Repository,
private readonly explorer: GitExplorer
public readonly repo: Repository,
parent: ExplorerNode,
public readonly explorer: GitExplorer
) {
super(uri);
super(uri, parent);
}

get id(): string {
Expand All @@ -35,7 +36,7 @@ export class BranchesNode extends ExplorerNode {
const branchNodes = [
...Iterables.filterMap(
branches,
b => (b.remote ? undefined : new BranchNode(b, this.uri, this.explorer))
b => (b.remote ? undefined : new BranchNode(b, this.uri, this, this.explorer))
)
];
if (this.explorer.config.branches.layout === ExplorerBranchesLayout.List) return branchNodes;
Expand All @@ -47,7 +48,15 @@ export class BranchesNode extends ExplorerNode {
this.explorer.config.files.compact
);

const root = new BranchOrTagFolderNode('branch', this.repo.path, '', undefined, hierarchy, this.explorer);
const root = new BranchOrTagFolderNode(
'branch',
this.repo.path,
'',
undefined,
hierarchy,
this,
this.explorer
);
this._children = await root.getChildren();
}
return this._children;
Expand Down
5 changes: 3 additions & 2 deletions src/views/nodes/commitFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ export class CommitFileNode extends ExplorerRefNode {
constructor(
public readonly status: IGitStatusFile,
public commit: GitLogCommit,
protected readonly explorer: Explorer,
parent: ExplorerNode,
public readonly explorer: Explorer,
private readonly _displayAs: CommitFileNodeDisplayAs,
private readonly _selection?: Selection
) {
super(GitUri.fromFileStatus(status, commit.repoPath, commit.sha));
super(GitUri.fromFileStatus(status, commit.repoPath, commit.sha), parent);
}

get ref(): string {
Expand Down
9 changes: 5 additions & 4 deletions src/views/nodes/commitNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import { FolderNode, IFileExplorerNode } from './folderNode';
export class CommitNode extends ExplorerRefNode {
constructor(
public readonly commit: GitLogCommit,
private readonly explorer: Explorer,
parent: ExplorerNode,
public readonly explorer: Explorer,
public readonly branch?: GitBranch,
private readonly getBranchTips?: (sha: string) => string | undefined
) {
super(commit.toGitUri());
super(commit.toGitUri(), parent);
}

get ref(): string {
Expand All @@ -31,7 +32,7 @@ export class CommitNode extends ExplorerRefNode {
let children: IFileExplorerNode[] = [
...Iterables.map(
commit.fileStatuses,
s => new CommitFileNode(s, commit.toFileCommit(s), this.explorer, CommitFileNodeDisplayAs.File)
s => new CommitFileNode(s, commit.toFileCommit(s), this, this.explorer, CommitFileNodeDisplayAs.File)
)
];

Expand All @@ -43,7 +44,7 @@ export class CommitNode extends ExplorerRefNode {
this.explorer.config.files.compact
);

const root = new FolderNode(this.repoPath, '', undefined, hierarchy, this.explorer);
const root = new FolderNode(this.repoPath, '', undefined, hierarchy, this, this.explorer);
children = (await root.getChildren()) as IFileExplorerNode[];
}
else {
Expand Down
16 changes: 9 additions & 7 deletions src/views/nodes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExplorerNode, ResourceType, unknownGitUri } from '../nodes/explorerNode

export class MessageNode extends ExplorerNode {
constructor(
parent: ExplorerNode,
private readonly _message: string,
private readonly _tooltip?: string,
private readonly _iconPath?:
Expand All @@ -18,7 +19,7 @@ export class MessageNode extends ExplorerNode {
}
| ThemeIcon
) {
super(unknownGitUri);
super(unknownGitUri, parent);
}

getChildren(): ExplorerNode[] | Promise<ExplorerNode[]> {
Expand All @@ -36,6 +37,7 @@ export class MessageNode extends ExplorerNode {

export class UpdateableMessageNode extends ExplorerNode {
constructor(
parent: ExplorerNode,
public readonly id: string,
private _message: string,
private _tooltip?: string,
Expand All @@ -48,7 +50,7 @@ export class UpdateableMessageNode extends ExplorerNode {
}
| ThemeIcon
) {
super(unknownGitUri);
super(unknownGitUri, parent);
}

getChildren(): ExplorerNode[] | Promise<ExplorerNode[]> {
Expand Down Expand Up @@ -101,10 +103,10 @@ export abstract class PagerNode extends ExplorerNode {

constructor(
protected readonly message: string,
protected readonly node: ExplorerNode,
protected readonly parent: ExplorerNode,
protected readonly explorer: Explorer
) {
super(unknownGitUri);
super(unknownGitUri, parent);
}

getChildren(): ExplorerNode[] | Promise<ExplorerNode[]> {
Expand All @@ -126,23 +128,23 @@ export abstract class PagerNode extends ExplorerNode {
return {
title: 'Refresh',
command: this.explorer.getQualifiedCommand('refreshNode'),
arguments: [this.node, this._args]
arguments: [this.parent, this._args]
} as Command;
}
}

export class ShowMoreNode extends PagerNode {
constructor(
type: string,
node: ExplorerNode,
parent: ExplorerNode,
explorer: Explorer,
maxCount: number = Container.config.advanced.maxListItems
) {
super(
maxCount === 0
? `Show All ${type} ${GlyphChars.Space}${GlyphChars.Dash}${GlyphChars.Space} this may take a while`
: `Show More ${type}`,
node,
parent,
explorer
);
this._args.maxCount = maxCount;
Expand Down
11 changes: 9 additions & 2 deletions src/views/nodes/explorerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export interface NamedRef {
export const unknownGitUri = new GitUri();

export abstract class ExplorerNode {
constructor(uri: GitUri) {
constructor(
uri: GitUri,
private readonly _parent: ExplorerNode | undefined
) {
this._uri = uri;
}

Expand All @@ -59,6 +62,9 @@ export abstract class ExplorerNode {
}

abstract getChildren(): ExplorerNode[] | Promise<ExplorerNode[]>;
getParent(): ExplorerNode | undefined {
return this._parent;
}
abstract getTreeItem(): TreeItem | Promise<TreeItem>;

getCommand(): Command | undefined {
Expand Down Expand Up @@ -99,9 +105,10 @@ export abstract class SubscribeableExplorerNode<TExplorer extends Explorer> exte

constructor(
uri: GitUri,
parent: ExplorerNode | undefined,
public readonly explorer: TExplorer
) {
super(uri);
super(uri, parent);

const disposables = [this.explorer.onDidChangeVisibility(this.onVisibilityChanged, this)];

Expand Down
10 changes: 5 additions & 5 deletions src/views/nodes/fileHistoryNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { MessageNode } from './common';
import { ExplorerNode, ResourceType, SubscribeableExplorerNode } from './explorerNode';

export class FileHistoryNode extends SubscribeableExplorerNode<FileHistoryExplorer> {
constructor(uri: GitUri, explorer: FileHistoryExplorer) {
super(uri, explorer);
constructor(uri: GitUri, parent: ExplorerNode, explorer: FileHistoryExplorer) {
super(uri, parent, explorer);
}

async getChildren(): Promise<ExplorerNode[]> {
Expand Down Expand Up @@ -63,20 +63,20 @@ export class FileHistoryNode extends SubscribeableExplorerNode<FileHistoryExplor
previousSha,
status.originalFileName || status.fileName
);
children.push(new CommitFileNode(status, commit, this.explorer, displayAs));
children.push(new CommitFileNode(status, commit, this, this.explorer, displayAs));
}

const log = await Container.git.getLogForFile(this.uri.repoPath, this.uri.fsPath, { ref: this.uri.sha });
if (log !== undefined) {
children.push(
...Iterables.map(
log.commits.values(),
c => new CommitFileNode(c.fileStatuses[0], c, this.explorer, displayAs)
c => new CommitFileNode(c.fileStatuses[0], c, this, this.explorer, displayAs)
)
);
}

if (children.length === 0) return [new MessageNode('No file history')];
if (children.length === 0) return [new MessageNode(this, 'No file history')];
return children;
}

Expand Down
6 changes: 3 additions & 3 deletions src/views/nodes/fileHistoryTrackerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class FileHistoryTrackerNode extends SubscribeableExplorerNode<FileHistor
private _child: FileHistoryNode | undefined;

constructor(explorer: FileHistoryExplorer) {
super(unknownGitUri, explorer);
super(unknownGitUri, undefined, explorer);
}

dispose() {
Expand All @@ -33,10 +33,10 @@ export class FileHistoryTrackerNode extends SubscribeableExplorerNode<FileHistor
async getChildren(): Promise<ExplorerNode[]> {
if (this._child === undefined) {
if (this.uri === unknownGitUri) {
return [new MessageNode('There are no editors open that can provide file history')];
return [new MessageNode(this, 'There are no editors open that can provide file history')];
}

this._child = new FileHistoryNode(this.uri, this.explorer);
this._child = new FileHistoryNode(this.uri, this, this.explorer);
}

return [this._child];
Expand Down
7 changes: 4 additions & 3 deletions src/views/nodes/folderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export class FolderNode extends ExplorerNode {
public readonly folderName: string,
public readonly relativePath: string | undefined,
public readonly root: Arrays.IHierarchicalItem<IFileExplorerNode>,
private readonly explorer: Explorer
parent: ExplorerNode,
public readonly explorer: Explorer
) {
super(GitUri.fromRepoPath(repoPath));
super(GitUri.fromRepoPath(repoPath), parent);
}

async getChildren(): Promise<(FolderNode | IFileExplorerNode)[]> {
Expand All @@ -42,7 +43,7 @@ export class FolderNode extends ExplorerNode {
for (const folder of Objects.values(this.root.children)) {
if (folder.value === undefined) {
children.push(
new FolderNode(this.repoPath, folder.name, folder.relativePath, folder, this.explorer)
new FolderNode(this.repoPath, folder.name, folder.relativePath, folder, this, this.explorer)
);
continue;
}
Expand Down

0 comments on commit 8ea9233

Please sign in to comment.