Skip to content

Commit

Permalink
Adds comparison pinning
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jan 3, 2019
1 parent 4b92070 commit 71e5c5d
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Adds pinning of comparisons in the _Compare_ view — pinned comparisons will persist across reloads

## [9.3.0] - 2019-01-02

### Added
Expand Down
69 changes: 62 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,24 @@
"light": "images/light/icon-locked.svg"
}
},
{
"command": "gitlens.views.compare.pinComparision",
"title": "Pin Comparision",
"category": "GitLens",
"icon": {
"dark": "images/dark/icon-pin.svg",
"light": "images/light/icon-pin.svg"
}
},
{
"command": "gitlens.views.compare.unpinComparision",
"title": "Unpin Comparision",
"category": "GitLens",
"icon": {
"dark": "images/dark/icon-pinned.svg",
"light": "images/light/icon-pinned.svg"
}
},
{
"command": "gitlens.views.compare.swapComparision",
"title": "Swap Comparision",
Expand Down Expand Up @@ -2788,7 +2806,11 @@
{
"command": "gitlens.views.refreshNode",
"title": "Refresh",
"category": "GitLens"
"category": "GitLens",
"icon": {
"dark": "images/dark/icon-refresh.svg",
"light": "images/light/icon-refresh.svg"
}
}
],
"menus": {
Expand Down Expand Up @@ -3345,6 +3367,14 @@
"command": "gitlens.views.compare.setKeepResultsToOff",
"when": "false"
},
{
"command": "gitlens.views.compare.pinComparision",
"when": "false"
},
{
"command": "gitlens.views.compare.unpinComparision",
"when": "false"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "false"
Expand Down Expand Up @@ -4364,27 +4394,52 @@
},
{
"command": "gitlens.views.dismissNode",
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare|search)\\b(?!:(commits|files))/",
"group": "inline@2"
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare:results\\b(?!.*?\\+pinned\\b.*?)|search)\\b(?!:(commits|files))/",
"group": "inline@99"
},
{
"command": "gitlens.views.dismissNode",
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare|search)\\b(?!:(commits|files))/",
"when": "viewItem =~ /gitlens:(compare:picker:ref|compare:results\\b(?!.*?\\+pinned\\b.*?)|search)\\b(?!:(commits|files))/",
"group": "1_gitlens@1"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "viewItem == gitlens:compare:results",
"when": "viewItem =~ /gitlens:compare:results\\b/",
"group": "inline@1"
},
{
"command": "gitlens.views.compare.pinComparision",
"when": "viewItem =~ /gitlens:compare:results\\b(?!.*?\\+pinned\\b.*?)/",
"group": "inline@2"
},
{
"command": "gitlens.views.compare.unpinComparision",
"when": "viewItem =~ /gitlens:compare:results\\b.*?\\+pinned\\b.*?/",
"group": "inline@2"
},
{
"command": "gitlens.views.refreshNode",
"when": "viewItem =~ /gitlens:compare:results\\b/",
"group": "inline@3"
},
{
"command": "gitlens.views.compare.swapComparision",
"when": "viewItem == gitlens:compare:results",
"when": "viewItem =~ /gitlens:compare:results\\b/",
"group": "2_gitlens@1"
},
{
"command": "gitlens.views.compare.pinComparision",
"when": "viewItem =~ /gitlens:compare:results\\b(?!.*?\\+pinned\\b.*?)/",
"group": "3_gitlens@1"
},
{
"command": "gitlens.views.compare.unpinComparision",
"when": "viewItem =~ /gitlens:compare:results\\b.*?\\+pinned\\b.*?/",
"group": "3_gitlens@1"
},
{
"command": "gitlens.views.openDirectoryDiff",
"when": "viewItem == gitlens:compare:results",
"when": "viewItem =~ /gitlens:compare:results\\b/",
"group": "7_gitlens@1"
},
{
Expand Down
16 changes: 16 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ export const ImageMimetypes: { [key: string]: string } = {
'.bmp': 'image/bmp'
};

export interface NamedRef {
label?: string;
ref: string;
}

export interface PinnedComparison {
path: string;
ref1: NamedRef;
ref2: NamedRef;
}

export interface PinnedComparisons {
[id: string]: PinnedComparison;
}

export interface StarredBranches {
[id: string]: boolean;
}
Expand All @@ -138,6 +153,7 @@ export interface StarredRepositories {

export enum WorkspaceState {
DefaultRemote = 'gitlens:remote:default',
PinnedComparisons = 'gitlens:pinned:comparisons',
StarredBranches = 'gitlens:starred:branches',
StarredRepositories = 'gitlens:starred:repositories',
ViewsCompareKeepResults = 'gitlens:views:compare:keepResults',
Expand Down
57 changes: 54 additions & 3 deletions src/views/compareView.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict';
import { commands, ConfigurationChangeEvent } from 'vscode';
import { CompareViewConfig, configuration, ViewFilesLayout, ViewsConfig } from '../configuration';
import { CommandContext, setCommandContext, WorkspaceState } from '../constants';
import {
CommandContext,
NamedRef,
PinnedComparison,
PinnedComparisons,
setCommandContext,
WorkspaceState
} from '../constants';
import { Container } from '../container';
import { CompareNode, CompareResultsNode, NamedRef, ViewNode } from './nodes';
import { CompareNode, CompareResultsNode, nodeSupportsConditionalDismissal, ViewNode } from './nodes';
import { ViewBase } from './viewBase';

export class CompareView extends ViewBase<CompareNode> {
Expand Down Expand Up @@ -46,6 +53,9 @@ export class CompareView extends ViewBase<CompareNode> {
() => this.setKeepResults(false),
this
);

commands.registerCommand(this.getQualifiedCommand('pinComparision'), this.pinComparision, this);
commands.registerCommand(this.getQualifiedCommand('unpinComparision'), this.unpinComparision, this);
commands.registerCommand(this.getQualifiedCommand('swapComparision'), this.swapComparision, this);

commands.registerCommand(this.getQualifiedCommand('selectForCompare'), this.selectForCompare, this);
Expand Down Expand Up @@ -86,6 +96,7 @@ export class CompareView extends ViewBase<CompareNode> {

dismissNode(node: ViewNode) {
if (this._root === undefined) return;
if (nodeSupportsConditionalDismissal(node) && node.canDismiss() === false) return;

this._root.dismiss(node);
}
Expand All @@ -111,6 +122,34 @@ export class CompareView extends ViewBase<CompareNode> {
void root.selectForCompare(repoPath, ref);
}

getPinnedComparisons() {
const pinned = Container.context.workspaceState.get<PinnedComparisons>(WorkspaceState.PinnedComparisons);
if (pinned == null) return [];

return Object.values(pinned).map(p => new CompareResultsNode(this, p.path, p.ref1, p.ref2, true));
}

async updatePinnedComparison(id: string, pin?: PinnedComparison) {
let pinned = Container.context.workspaceState.get<PinnedComparisons>(WorkspaceState.PinnedComparisons);
if (pinned == null) {
pinned = Object.create(null);
}

if (pin !== undefined) {
pinned![id] = {
path: pin.path,
ref1: pin.ref1,
ref2: pin.ref2
};
}
else {
const { [id]: _, ...rest } = pinned!;
pinned = rest;
}

await Container.context.workspaceState.update(WorkspaceState.PinnedComparisons, pinned);
}

private async addResults(results: ViewNode) {
if (!this.visible) {
void (await this.show());
Expand All @@ -131,9 +170,21 @@ export class CompareView extends ViewBase<CompareNode> {
setCommandContext(CommandContext.ViewsCompareKeepResults, enabled);
}

private async pinComparision(node: ViewNode) {
if (!(node instanceof CompareResultsNode)) return;

return node.pin();
}

private swapComparision(node: ViewNode) {
if (!(node instanceof CompareResultsNode)) return;

node.swap();
return node.swap();
}

private async unpinComparision(node: ViewNode) {
if (!(node instanceof CompareResultsNode)) return;

return node.unpin();
}
}
26 changes: 18 additions & 8 deletions src/views/nodes/compareNode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
import { TreeItem, TreeItemCollapsibleState } from 'vscode';
import { getRepoPathOrPrompt } from '../../commands';
import { CommandContext, GlyphChars, setCommandContext } from '../../constants';
import { CommandContext, GlyphChars, NamedRef, setCommandContext } from '../../constants';
import { GitService } from '../../git/gitService';
import { BranchesAndTagsQuickPick, CommandQuickPickItem } from '../../quickpicks';
import { debug, Functions, gate, log } from '../../system';
import { CompareView } from '../compareView';
import { MessageNode } from './common';
import { ComparePickerNode } from './comparePickerNode';
import { NamedRef, ResourceType, unknownGitUri, ViewNode } from './viewNode';
import { ResourceType, unknownGitUri, ViewNode } from './viewNode';

interface RepoRef {
label: string;
Expand All @@ -34,17 +34,21 @@ export class CompareNode extends ViewNode<CompareView> {
// Not really sure why I can't reuse this node -- but if I do the Tree errors out with an id already exists error
this._comparePickerNode = new ComparePickerNode(this.view, this);
this._children = [this._comparePickerNode];

const pinned = this.view.getPinnedComparisons();
if (pinned.length !== 0) {
this._children.push(...pinned);
}
}
else if (
this._selectedRef !== undefined &&
(this._comparePickerNode === undefined || !this._children.includes(this._comparePickerNode))
) {
else if (this._comparePickerNode === undefined || !this._children.includes(this._comparePickerNode)) {
// Not really sure why I can't reuse this node -- but if I do the Tree errors out with an id already exists error
this._comparePickerNode = new ComparePickerNode(this.view, this);
this._children.splice(0, 0, this._comparePickerNode);

const node = this._comparePickerNode;
setImmediate(() => this.view.reveal(node, { focus: false, select: true }));
if (this._selectedRef !== undefined) {
const node = this._comparePickerNode;
setImmediate(() => this.view.reveal(node, { focus: false, select: true }));
}
}

return this._children;
Expand All @@ -62,6 +66,12 @@ export class CompareNode extends ViewNode<CompareView> {
if (this._children.length !== 0 && replace) {
this._children.length = 0;
this._children.push(results);

// Re-add the pinned comparisons
const pinned = this.view.getPinnedComparisons();
if (pinned.length !== 0) {
this._children.push(...pinned);
}
}
else {
if (this._comparePickerNode !== undefined) {
Expand Down

0 comments on commit 71e5c5d

Please sign in to comment.