From 7dbec5cf69b508dd8cf560e829711405e7dd2595 Mon Sep 17 00:00:00 2001 From: Michael Hutchison Date: Sun, 2 Jun 2019 17:23:54 +1000 Subject: [PATCH] #91 New setting to enable Git Graph to open to the repository containing the active Text Editor document. --- package.json | 7 ++++++- src/config.ts | 4 ++++ src/extension.ts | 26 ++++++++++++++++++-------- src/repoManager.ts | 7 +++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 9ed98a49..d93592eb 100644 --- a/package.json +++ b/package.json @@ -214,7 +214,7 @@ "default": 0, "description": "Specifies the maximum depth of subfolders to search when discovering repositories in the workspace." }, - "git-graph.openDiffTabLocation":{ + "git-graph.openDiffTabLocation": { "type": "string", "enum": [ "Active", @@ -227,6 +227,11 @@ "default": "Active", "description": "Specifies which pane the Visual Studio Code Diff is opened in." }, + "git-graph.openToTheRepoOfTheActiveTextEditorDocument": { + "type": "boolean", + "default": false, + "description": "Open Git Graph to the repository containing the active Text Editor document." + }, "git-graph.referenceLabelAlignment": { "type": "string", "enum": [ diff --git a/src/config.ts b/src/config.ts index df016084..1e200a62 100644 --- a/src/config.ts +++ b/src/config.ts @@ -77,6 +77,10 @@ class Config { return this.workspaceConfiguration.get('openDiffTabLocation', 'Active') === 'Active' ? vscode.ViewColumn.Active : vscode.ViewColumn.Beside; } + public openToTheRepoOfTheActiveTextEditorDocument() { + return this.workspaceConfiguration.get('openToTheRepoOfTheActiveTextEditorDocument', false); + } + public refLabelAlignment(): RefLabelAlignment { return this.workspaceConfiguration.get('referenceLabelAlignment', 'Normal'); } diff --git a/src/extension.ts b/src/extension.ts index dae5b75c..0432f5c0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import { AvatarManager } from './avatarManager'; +import { getConfig } from './config'; import { DataSource } from './dataSource'; import { DiffDocProvider } from './diffDocProvider'; import { ExtensionState } from './extensionState'; @@ -17,15 +18,24 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand('git-graph.view', args => { - let loadRepo = typeof args === 'object' && args.rootUri ? getPathFromUri(args.rootUri) : null; - if (loadRepo !== null && !repoManager.isKnownRepo(loadRepo)) { - repoManager.registerRepo(loadRepo, true).then(valid => { - if (!valid) loadRepo = null; - GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo); - }); - } else { - GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo); + let loadRepo: string | null = null; + + if (typeof args === 'object' && args.rootUri) { + // If command is run from the SCP menu, load the specific repo + loadRepo = getPathFromUri(args.rootUri); + if (!repoManager.isKnownRepo(loadRepo)) { + repoManager.registerRepo(loadRepo, true).then(valid => { + if (!valid) loadRepo = null; + GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo); + }); + return; + } + } else if (getConfig().openToTheRepoOfTheActiveTextEditorDocument() && vscode.window.activeTextEditor) { + // If the config setting is enabled, load the repo containing the active text editor document + loadRepo = repoManager.getRepoContainingFile(getPathFromUri(vscode.window.activeTextEditor.document.uri)); } + + GitGraphView.createOrShow(context.extensionPath, dataSource, extensionState, avatarManager, repoManager, loadRepo); }), vscode.commands.registerCommand('git-graph.addGitRepository', () => { vscode.window.showOpenDialog({ canSelectFiles: false, canSelectFolders: true, canSelectMany: false }).then(uris => { diff --git a/src/repoManager.ts b/src/repoManager.ts index 36545f3a..b40c836a 100644 --- a/src/repoManager.ts +++ b/src/repoManager.ts @@ -121,6 +121,13 @@ export class RepoManager { } return repos; } + public getRepoContainingFile(path: string) { + let repoPaths = Object.keys(this.repos), repo = null; + for (let i = 0; i < repoPaths.length; i++) { + if (path.startsWith(repoPaths[i] + '/') && (repo === null || repo.length < repoPaths[i].length)) repo = repoPaths[i]; + } + return repo; + } public isKnownRepo(repo: string) { return typeof this.repos[repo] !== 'undefined'; }