Skip to content

Commit

Permalink
#402 New mode for the Find Widget, which will additionally open the C…
Browse files Browse the repository at this point in the history
…ommit Details View as you navigate through each of the matched commits.
  • Loading branch information
mhutchie committed Dec 5, 2020
1 parent 67311a6 commit d896e99
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 72 deletions.
35 changes: 31 additions & 4 deletions src/extensionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as vscode from 'vscode';
import { Avatar, AvatarCache } from './avatarManager';
import { getConfig } from './config';
import { BooleanOverride, CodeReview, ErrorInfo, FileViewType, GitGraphViewGlobalState, GitRepoSet, GitRepoState, RepoCommitOrdering } from './types';
import { BooleanOverride, CodeReview, ErrorInfo, FileViewType, GitGraphViewGlobalState, GitGraphViewWorkspaceState, GitRepoSet, GitRepoState, RepoCommitOrdering } from './types';
import { GitExecutable, getPathFromStr } from './utils';
import { Disposable } from './utils/disposable';
import { Event } from './utils/event';
Expand All @@ -15,6 +15,7 @@ const IGNORED_REPOS = 'ignoredRepos';
const LAST_ACTIVE_REPO = 'lastActiveRepo';
const LAST_KNOWN_GIT_PATH = 'lastKnownGitPath';
const REPO_STATES = 'repoStates';
const WORKSPACE_VIEW_STATE = 'workspaceViewState';

export const DEFAULT_REPO_STATE: GitRepoState = {
cdvDivider: 0.5,
Expand All @@ -36,11 +37,17 @@ export const DEFAULT_REPO_STATE: GitRepoState = {
showTags: BooleanOverride.Default
};

const DEFAULT_GLOBAL_VIEW_STATE: GitGraphViewGlobalState = {
const DEFAULT_GIT_GRAPH_VIEW_GLOBAL_STATE: GitGraphViewGlobalState = {
alwaysAcceptCheckoutCommit: false,
issueLinkingConfig: null
};

const DEFAULT_GIT_GRAPH_VIEW_WORKSPACE_STATE: GitGraphViewWorkspaceState = {
findIsCaseSensitive: false,
findIsRegex: false,
findOpenCommitDetailsView: false
};

export interface CodeReviewData {
lastActive: number;
lastViewedFile: string | null;
Expand Down Expand Up @@ -149,8 +156,8 @@ export class ExtensionState extends Disposable {
* @returns The global state.
*/
public getGlobalViewState() {
const globalViewState = this.globalState.get<GitGraphViewGlobalState>(GLOBAL_VIEW_STATE, DEFAULT_GLOBAL_VIEW_STATE);
return Object.assign({}, DEFAULT_GLOBAL_VIEW_STATE, globalViewState);
const globalViewState = this.globalState.get<GitGraphViewGlobalState>(GLOBAL_VIEW_STATE, DEFAULT_GIT_GRAPH_VIEW_GLOBAL_STATE);
return Object.assign({}, DEFAULT_GIT_GRAPH_VIEW_GLOBAL_STATE, globalViewState);
}

/**
Expand All @@ -162,6 +169,26 @@ export class ExtensionState extends Disposable {
}


/* Workspace View State */

/**
* Get the workspace state of the Git Graph View.
* @returns The workspace state.
*/
public getWorkspaceViewState() {
const workspaceViewState = this.workspaceState.get<GitGraphViewWorkspaceState>(WORKSPACE_VIEW_STATE, DEFAULT_GIT_GRAPH_VIEW_WORKSPACE_STATE);
return Object.assign({}, DEFAULT_GIT_GRAPH_VIEW_WORKSPACE_STATE, workspaceViewState);
}

/**
* Set the workspace state of the Git Graph View.
* @param state The workspace state.
*/
public setWorkspaceViewState(state: GitGraphViewWorkspaceState) {
return this.updateWorkspaceState(WORKSPACE_VIEW_STATE, state);
}


/* Ignored Repos */

/**
Expand Down
9 changes: 8 additions & 1 deletion src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,12 @@ export class GitGraphView extends Disposable {
case 'setRepoState':
this.repoManager.setRepoState(msg.repo, msg.state);
break;
case 'setWorkspaceViewState':
this.sendMessage({
command: 'setWorkspaceViewState',
error: await this.extensionState.setWorkspaceViewState(msg.state)
});
break;
case 'showErrorMessage':
showErrorMessage(msg.message);
break;
Expand Down Expand Up @@ -629,6 +635,7 @@ export class GitGraphView extends Disposable {
loadCommitsRefreshId: this.loadCommitsRefreshId
};
const globalState = this.extensionState.getGlobalViewState();
const workspaceState = this.extensionState.getWorkspaceViewState();

let body, numRepos = Object.keys(initialState.repos).length, colorVars = '', colorParams = '';
for (let i = 0; i < initialState.config.graph.colours.length; i++) {
Expand Down Expand Up @@ -661,7 +668,7 @@ export class GitGraphView extends Disposable {
<div id="footer"></div>
</div>
<div id="scrollShadow"></div>
<script nonce="${nonce}">var globalState = ${JSON.stringify(globalState)}, initialState = ${JSON.stringify(initialState)};</script>
<script nonce="${nonce}">var initialState = ${JSON.stringify(initialState)}, globalState = ${JSON.stringify(globalState)}, workspaceState = ${JSON.stringify(workspaceState)};</script>
<script nonce="${nonce}" src="${this.getMediaUri('out.min.js')}"></script>
</body>`;
} else {
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ export interface GitGraphViewGlobalState {
issueLinkingConfig: IssueLinkingConfig | null;
}

export interface GitGraphViewWorkspaceState {
findIsCaseSensitive: boolean;
findIsRegex: boolean;
findOpenCommitDetailsView: boolean;
}

export interface CommitDetailsViewConfig {
readonly autoCenter: boolean;
readonly fileTreeCompactFolders: boolean;
Expand Down Expand Up @@ -1051,6 +1057,14 @@ export interface RequestSetRepoState extends RepoRequest {
readonly state: GitRepoState;
}

export interface RequestSetWorkspaceViewState extends BaseMessage {
readonly command: 'setWorkspaceViewState';
readonly state: GitGraphViewWorkspaceState;
}
export interface ResponseSetWorkspaceViewState extends ResponseWithErrorInfo {
readonly command: 'setWorkspaceViewState';
}

export interface RequestShowErrorDialog extends BaseMessage {
readonly command: 'showErrorMessage';
readonly message: string;
Expand Down Expand Up @@ -1167,6 +1181,7 @@ export type RequestMessage =
| RequestRevertCommit
| RequestSetGlobalViewState
| RequestSetRepoState
| RequestSetWorkspaceViewState
| RequestShowErrorDialog
| RequestStartCodeReview
| RequestTagDetails
Expand Down Expand Up @@ -1223,6 +1238,7 @@ export type ResponseMessage =
| ResponseResetToCommit
| ResponseRevertCommit
| ResponseSetGlobalViewState
| ResponseSetWorkspaceViewState
| ResponseStartCodeReview
| ResponseTagDetails
| ResponseViewDiff
Expand Down
101 changes: 97 additions & 4 deletions tests/extensionState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('fs');

import * as fs from 'fs';
import { ExtensionState } from '../src/extensionState';
import { BooleanOverride, FileViewType, GitGraphViewGlobalState, RepoCommitOrdering } from '../src/types';
import { BooleanOverride, FileViewType, GitGraphViewGlobalState, GitGraphViewWorkspaceState, RepoCommitOrdering } from '../src/types';
import { GitExecutable } from '../src/utils';
import { EventEmitter } from '../src/utils/event';

Expand Down Expand Up @@ -409,7 +409,7 @@ describe('ExtensionState', () => {
describe('getGlobalViewState', () => {
it('Should return the stored global view state', () => {
// Setup
const globalViewState = {
const globalViewState: GitGraphViewGlobalState = {
alwaysAcceptCheckoutCommit: true,
issueLinkingConfig: null
};
Expand Down Expand Up @@ -456,7 +456,10 @@ describe('ExtensionState', () => {
describe('setGlobalViewState', () => {
it('Should successfully store the global view state', async () => {
// Setup
const globalViewState = {} as GitGraphViewGlobalState;
const globalViewState: GitGraphViewGlobalState = {
alwaysAcceptCheckoutCommit: true,
issueLinkingConfig: null
};
extensionContext.globalState.update.mockResolvedValueOnce(null);

// Run
Expand All @@ -469,7 +472,10 @@ describe('ExtensionState', () => {

it('Should return an error message when vscode is unable to store the global view state', async () => {
// Setup
const globalViewState = {} as GitGraphViewGlobalState;
const globalViewState: GitGraphViewGlobalState = {
alwaysAcceptCheckoutCommit: true,
issueLinkingConfig: null
};
extensionContext.globalState.update.mockRejectedValueOnce(null);

// Run
Expand All @@ -481,6 +487,93 @@ describe('ExtensionState', () => {
});
});

describe('getWorkspaceViewState', () => {
it('Should return the stored workspace view state', () => {
// Setup
const workspaceViewState: GitGraphViewWorkspaceState = {
findIsCaseSensitive: true,
findIsRegex: false,
findOpenCommitDetailsView: true
};
extensionContext.workspaceState.get.mockReturnValueOnce(workspaceViewState);

// Run
const result = extensionState.getWorkspaceViewState();

// Assert
expect(result).toStrictEqual(workspaceViewState);
});

it('Should assign missing workspace view state variables to their default values', () => {
// Setup
extensionContext.workspaceState.get.mockReturnValueOnce({
findIsCaseSensitive: true,
findIsRegex: false
});

// Run
const result = extensionState.getWorkspaceViewState();

// Assert
expect(result).toStrictEqual({
findIsCaseSensitive: true,
findIsRegex: false,
findOpenCommitDetailsView: false
});
});

it('Should return the default workspace view state if it is not defined', () => {
// Setup
extensionContext.workspaceState.get.mockImplementationOnce((_, defaultValue) => defaultValue);

// Run
const result = extensionState.getWorkspaceViewState();

// Assert
expect(result).toStrictEqual({
findIsCaseSensitive: false,
findIsRegex: false,
findOpenCommitDetailsView: false
});
});
});

describe('setWorkspaceViewState', () => {
it('Should successfully store the workspace view state', async () => {
// Setup
const workspaceViewState: GitGraphViewWorkspaceState = {
findIsCaseSensitive: true,
findIsRegex: false,
findOpenCommitDetailsView: true
};
extensionContext.workspaceState.update.mockResolvedValueOnce(null);

// Run
const result = await extensionState.setWorkspaceViewState(workspaceViewState);

// Assert
expect(extensionContext.workspaceState.update).toHaveBeenCalledWith('workspaceViewState', workspaceViewState);
expect(result).toBe(null);
});

it('Should return an error message when vscode is unable to store the workspace view state', async () => {
// Setup
const workspaceViewState: GitGraphViewWorkspaceState = {
findIsCaseSensitive: true,
findIsRegex: false,
findOpenCommitDetailsView: true
};
extensionContext.workspaceState.update.mockRejectedValueOnce(null);

// Run
const result = await extensionState.setWorkspaceViewState(workspaceViewState);

// Assert
expect(extensionContext.workspaceState.update).toHaveBeenCalledWith('workspaceViewState', workspaceViewState);
expect(result).toBe('Visual Studio Code was unable to save the Git Graph Workspace State Memento.');
});
});

describe('getIgnoredRepos', () => {
it('Should return the stored ignored repositories', () => {
// Setup
Expand Down
Loading

0 comments on commit d896e99

Please sign in to comment.