mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-20 09:45:36 -05:00
Adds a file history explorer view
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
'use strict';
|
||||
import { Strings } from '../system';
|
||||
import { commands, TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCachedCommand, Commands, getCommandUri } from './common';
|
||||
import { ActiveEditorCachedCommand, CommandContext, Commands, getCommandUri } from './common';
|
||||
import { GlyphChars } from '../constants';
|
||||
import { CommitNode } from '../views/explorerNodes';
|
||||
import { GitCommit, GitLog, GitLogCommit, GitService, GitUri } from '../gitService';
|
||||
import { Logger } from '../logger';
|
||||
import { CommandQuickPickItem, CommitDetailsQuickPick, CommitWithFileStatusQuickPickItem } from '../quickPicks';
|
||||
@@ -24,6 +25,18 @@ export class ShowQuickCommitDetailsCommand extends ActiveEditorCachedCommand {
|
||||
super(Commands.ShowQuickCommitDetails);
|
||||
}
|
||||
|
||||
protected async preExecute(context: CommandContext, ...args: any[]): Promise<any> {
|
||||
if (context.type === 'view') {
|
||||
if (context.node instanceof CommitNode) {
|
||||
args = [{ sha: context.node.uri.sha, commit: context.node.commit }];
|
||||
}
|
||||
else {
|
||||
args = [{ sha: context.node.uri.sha }];
|
||||
}
|
||||
}
|
||||
return this.execute(context.editor, context.uri, ...args);
|
||||
}
|
||||
|
||||
async execute(editor?: TextEditor, uri?: Uri, args: ShowQuickCommitDetailsCommandArgs = {}) {
|
||||
uri = getCommandUri(uri, editor);
|
||||
if (uri === undefined) return undefined;
|
||||
|
||||
@@ -296,6 +296,12 @@ export interface IConfig {
|
||||
|
||||
defaultDateFormat: string | null;
|
||||
|
||||
fileHistoryExplorer: {
|
||||
commitFormat: string;
|
||||
// commitFileFormat: string;
|
||||
// dateFormat: string | null;
|
||||
};
|
||||
|
||||
gitExplorer: {
|
||||
commitFormat: string;
|
||||
commitFileFormat: string;
|
||||
|
||||
@@ -21,6 +21,7 @@ import { CodeLensController } from './codeLensController';
|
||||
import { CurrentLineController, LineAnnotationType } from './currentLineController';
|
||||
import { GitContentProvider } from './gitContentProvider';
|
||||
// import { GitExplorer } from './views/gitExplorer';
|
||||
import { FileHistoryExplorer } from './views/fileHistoryExplorer';
|
||||
import { StashExplorer } from './views/stashExplorer';
|
||||
import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider';
|
||||
import { GitContextTracker, GitService } from './gitService';
|
||||
@@ -96,6 +97,7 @@ export async function activate(context: ExtensionContext) {
|
||||
// const explorer = new GitExplorer(context, git);
|
||||
// context.subscriptions.push(window.registerTreeDataProvider('gitlens.gitExplorer', explorer));
|
||||
|
||||
context.subscriptions.push(window.registerTreeDataProvider('gitlens.fileHistoryExplorer', new FileHistoryExplorer(context, git)));
|
||||
context.subscriptions.push(window.registerTreeDataProvider('gitlens.stashExplorer', new StashExplorer(context, git)));
|
||||
|
||||
context.subscriptions.push(commands.registerTextEditorCommand('gitlens.computingFileAnnotations', () => { }));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
import { Iterables } from '../system';
|
||||
import { ExtensionContext, TreeItem, TreeItemCollapsibleState } from 'vscode';
|
||||
import { Command, ExtensionContext, TreeItem, TreeItemCollapsibleState } from 'vscode';
|
||||
import { Commands, DiffWithPreviousCommandArgs } from '../commands';
|
||||
import { CommitFileNode } from './commitFileNode';
|
||||
import { ExplorerNode, ResourceType } from './explorerNode';
|
||||
import { CommitFormatter, GitCommit, GitService, GitUri } from '../gitService';
|
||||
@@ -14,6 +15,8 @@ export class CommitNode extends ExplorerNode {
|
||||
}
|
||||
|
||||
async getChildren(): Promise<ExplorerNode[]> {
|
||||
if (this.commit.type === 'file') Promise.resolve([]);
|
||||
|
||||
const log = await this.git.getLogForRepo(this.commit.repoPath, this.commit.sha, 1);
|
||||
if (log === undefined) return [];
|
||||
|
||||
@@ -24,14 +27,40 @@ export class CommitNode extends ExplorerNode {
|
||||
}
|
||||
|
||||
getTreeItem(): TreeItem {
|
||||
const label = CommitFormatter.fromTemplate(this.template, this.commit, this.git.config.defaultDateFormat);
|
||||
const item = new TreeItem(CommitFormatter.fromTemplate(this.template, this.commit, this.git.config.defaultDateFormat));
|
||||
if (this.commit.type === 'file') {
|
||||
item.collapsibleState = TreeItemCollapsibleState.None;
|
||||
item.command = this.getCommand();
|
||||
item.contextValue = 'commit-file';
|
||||
}
|
||||
else {
|
||||
item.collapsibleState = TreeItemCollapsibleState.Collapsed;
|
||||
item.contextValue = this.resourceType;
|
||||
}
|
||||
|
||||
const item = new TreeItem(label, TreeItemCollapsibleState.Collapsed);
|
||||
item.contextValue = this.resourceType;
|
||||
item.iconPath = {
|
||||
dark: this.context.asAbsolutePath('images/dark/icon-commit.svg'),
|
||||
light: this.context.asAbsolutePath('images/light/icon-commit.svg')
|
||||
};
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
getCommand(): Command | undefined {
|
||||
return {
|
||||
title: 'Compare File with Previous',
|
||||
command: Commands.DiffWithPrevious,
|
||||
arguments: [
|
||||
new GitUri(this.uri, this.commit),
|
||||
{
|
||||
commit: this.commit,
|
||||
line: 0,
|
||||
showOptions: {
|
||||
preserveFocus: true,
|
||||
preview: true
|
||||
}
|
||||
} as DiffWithPreviousCommandArgs
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
89
src/views/fileHistoryExplorer.ts
Normal file
89
src/views/fileHistoryExplorer.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
// import { Arrays } from '../system';
|
||||
import { commands, Event, EventEmitter, ExtensionContext, TextEditor, TreeDataProvider, TreeItem, Uri, window } from 'vscode';
|
||||
import { Commands, DiffWithPreviousCommandArgs, openEditor, OpenFileInRemoteCommandArgs } from '../commands';
|
||||
import { UriComparer } from '../comparers';
|
||||
import { CommitNode, ExplorerNode, FileHistoryNode, TextExplorerNode } from './explorerNodes';
|
||||
import { GitService, GitUri } from '../gitService';
|
||||
|
||||
export * from './explorerNodes';
|
||||
|
||||
export class FileHistoryExplorer implements TreeDataProvider<ExplorerNode> {
|
||||
|
||||
private _node?: ExplorerNode;
|
||||
|
||||
private _onDidChangeTreeData = new EventEmitter<ExplorerNode>();
|
||||
public get onDidChangeTreeData(): Event<ExplorerNode> {
|
||||
return this._onDidChangeTreeData.event;
|
||||
}
|
||||
|
||||
constructor(private context: ExtensionContext, private git: GitService) {
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.refresh', this.refresh, this);
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.openChanges', this.openChanges, this);
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.openFile', this.openFile, this);
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.openFileRevision', this.openFileRevision, this);
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.openFileInRemote', this.openFileInRemote, this);
|
||||
commands.registerCommand('gitlens.fileHistoryExplorer.openFileRevisionInRemote', this.openFileRevisionInRemote, this);
|
||||
|
||||
context.subscriptions.push(window.onDidChangeActiveTextEditor(this.onActiveEditorChanged, this));
|
||||
|
||||
this._node = this.getRootNode(window.activeTextEditor);
|
||||
}
|
||||
|
||||
async getTreeItem(node: ExplorerNode): Promise<TreeItem> {
|
||||
return node.getTreeItem();
|
||||
}
|
||||
|
||||
async getChildren(node?: ExplorerNode): Promise<ExplorerNode[]> {
|
||||
if (this._node === undefined) return [new TextExplorerNode('No active file')];
|
||||
if (node === undefined) return this._node.getChildren();
|
||||
return node.getChildren();
|
||||
}
|
||||
|
||||
private getRootNode(editor: TextEditor | undefined): ExplorerNode | undefined {
|
||||
if (window.visibleTextEditors.length === 0) return undefined;
|
||||
if (editor === undefined) return this._node;
|
||||
|
||||
const uri = this.git.getGitUriForFile(editor.document.uri) || new GitUri(editor.document.uri, { repoPath: this.git.repoPath, fileName: editor.document.uri.fsPath });
|
||||
if (UriComparer.equals(uri, this._node && this._node.uri)) return this._node;
|
||||
|
||||
return new FileHistoryNode(uri, this.context, this.git);
|
||||
}
|
||||
|
||||
private onActiveEditorChanged(editor: TextEditor | undefined) {
|
||||
const node = this.getRootNode(editor);
|
||||
if (node === this._node) return;
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
refresh(node?: ExplorerNode) {
|
||||
this._node = node || this.getRootNode(window.activeTextEditor);
|
||||
this._onDidChangeTreeData.fire();
|
||||
}
|
||||
|
||||
private openChanges(node: CommitNode) {
|
||||
const command = node.getCommand();
|
||||
if (command === undefined || command.arguments === undefined) return;
|
||||
|
||||
const [uri, args] = command.arguments as [Uri, DiffWithPreviousCommandArgs];
|
||||
args.showOptions!.preview = false;
|
||||
return commands.executeCommand(command.command, uri, args);
|
||||
}
|
||||
|
||||
private openFile(node: CommitNode) {
|
||||
return openEditor(node.uri, { preserveFocus: true, preview: false });
|
||||
}
|
||||
|
||||
private openFileRevision(node: CommitNode) {
|
||||
return openEditor(GitService.toGitContentUri(node.uri), { preserveFocus: true, preview: false });
|
||||
}
|
||||
|
||||
private async openFileInRemote(node: CommitNode) {
|
||||
return commands.executeCommand(Commands.OpenFileInRemote, node.commit.uri, { range: false } as OpenFileInRemoteCommandArgs);
|
||||
}
|
||||
|
||||
private async openFileRevisionInRemote(node: CommitNode) {
|
||||
return commands.executeCommand(Commands.OpenFileInRemote, new GitUri(node.commit.uri, node.commit), { range: false } as OpenFileInRemoteCommandArgs);
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,11 @@ export class FileHistoryNode extends ExplorerNode {
|
||||
super(uri);
|
||||
}
|
||||
|
||||
async getChildren(): Promise<CommitNode[]> {
|
||||
async getChildren(): Promise<ExplorerNode[]> {
|
||||
const log = await this.git.getLogForFile(this.uri.repoPath, this.uri.fsPath, this.uri.sha);
|
||||
if (log === undefined) return [];
|
||||
if (log === undefined) return [new TextExplorerNode('No file history')];
|
||||
|
||||
return [...Iterables.map(log.commits.values(), c => new CommitNode(c, this.git.config.gitExplorer.commitFormat, this.context, this.git))];
|
||||
return [...Iterables.map(log.commits.values(), c => new CommitNode(c, this.git.config.fileHistoryExplorer.commitFormat, this.context, this.git))];
|
||||
}
|
||||
|
||||
getTreeItem(): TreeItem {
|
||||
|
||||
Reference in New Issue
Block a user