Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions extensions/ql-vscode/src/databases/config/db-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,6 @@ export interface LocalDatabase {
storagePath: string;
}

export type ExpandedDbItem =
| RootLocalExpandedDbItem
| LocalUserDefinedListExpandedDbItem
| RootRemoteExpandedDbItem
| RemoteUserDefinedListExpandedDbItem;

export enum ExpandedDbItemKind {
RootLocal = "rootLocal",
LocalUserDefinedList = "localUserDefinedList",
RootRemote = "rootRemote",
RemoteUserDefinedList = "remoteUserDefinedList",
}

export interface RootLocalExpandedDbItem {
kind: ExpandedDbItemKind.RootLocal;
}

export interface LocalUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.LocalUserDefinedList;
listName: string;
}

export interface RootRemoteExpandedDbItem {
kind: ExpandedDbItemKind.RootRemote;
}

export interface RemoteUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.RemoteUserDefinedList;
listName: string;
}

export function cloneDbConfig(config: DbConfig): DbConfig {
return {
databases: {
Expand Down
34 changes: 32 additions & 2 deletions extensions/ql-vscode/src/databases/db-item-expansion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
import { ExpandedDbItem, ExpandedDbItemKind } from "./config/db-config";
import { DbItem, DbItemKind } from "./db-item";

export function calculateNewExpandedState(
export type ExpandedDbItem =
| RootLocalExpandedDbItem
| LocalUserDefinedListExpandedDbItem
| RootRemoteExpandedDbItem
| RemoteUserDefinedListExpandedDbItem;

export enum ExpandedDbItemKind {
RootLocal = "rootLocal",
LocalUserDefinedList = "localUserDefinedList",
RootRemote = "rootRemote",
RemoteUserDefinedList = "remoteUserDefinedList",
}

export interface RootLocalExpandedDbItem {
kind: ExpandedDbItemKind.RootLocal;
}

export interface LocalUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.LocalUserDefinedList;
listName: string;
}

export interface RootRemoteExpandedDbItem {
kind: ExpandedDbItemKind.RootRemote;
}

export interface RemoteUserDefinedListExpandedDbItem {
kind: ExpandedDbItemKind.RemoteUserDefinedList;
listName: string;
}

export function updateItemInExpandedState(
currentExpandedItems: ExpandedDbItem[],
dbItem: DbItem,
itemExpanded: boolean,
Expand Down
28 changes: 13 additions & 15 deletions extensions/ql-vscode/src/databases/db-manager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { App } from "../common/app";
import { AppEvent, AppEventEmitter } from "../common/events";
import { ValueResult } from "../common/value-result";
import { ExpandedDbItem } from "./config/db-config";
import { DbConfigStore } from "./config/db-config-store";
import { DbItem, DbListKind } from "./db-item";
import { calculateNewExpandedState } from "./db-item-expansion";
import { updateItemInExpandedState, ExpandedDbItem } from "./db-item-expansion";
import {
getSelectedDbItem,
mapDbItemToSelectedDbItem,
Expand Down Expand Up @@ -45,7 +44,7 @@ export class DbManager {
return ValueResult.fail(configResult.errors);
}

const expandedItems = this.getCurrentExpandedItems();
const expandedItems = this.getExpandedItems();

return ValueResult.ok([
createRemoteTree(configResult.value, expandedItems),
Expand All @@ -68,23 +67,15 @@ export class DbManager {
dbItem: DbItem,
itemExpanded: boolean,
): Promise<void> {
const configResult = this.dbConfigStore.getConfig();
if (configResult.isFailure) {
throw Error("Cannot update expanded state if config is not loaded");
}
const currentExpandedItems = this.getExpandedItems();

const currentExpandedItems = this.getCurrentExpandedItems();

const newExpandedItems = calculateNewExpandedState(
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
itemExpanded,
);

await this.app.workspaceState.update(
DbManager.DB_EXPANDED_STATE_KEY,
newExpandedItems,
);
await this.setExpandedItems(newExpandedItems);
}

public async addNewRemoteRepo(
Expand Down Expand Up @@ -133,11 +124,18 @@ export class DbManager {
return this.dbConfigStore.doesRemoteDbExist(nwo, listName);
}

private getCurrentExpandedItems(): ExpandedDbItem[] {
private getExpandedItems(): ExpandedDbItem[] {
const items = this.app.workspaceState.get<ExpandedDbItem[]>(
DbManager.DB_EXPANDED_STATE_KEY,
);

return items || [];
}

private async setExpandedItems(items: ExpandedDbItem[]): Promise<void> {
await this.app.workspaceState.update(
DbManager.DB_EXPANDED_STATE_KEY,
items,
);
}
}
3 changes: 1 addition & 2 deletions extensions/ql-vscode/src/databases/db-tree-creator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
DbConfig,
ExpandedDbItem,
ExpandedDbItemKind,
LocalDatabase,
LocalList,
RemoteRepositoryList,
Expand All @@ -18,6 +16,7 @@ import {
RootLocalDbItem,
RootRemoteDbItem,
} from "./db-item";
import { ExpandedDbItem, ExpandedDbItemKind } from "./db-item-expansion";

export function createRemoteTree(
dbConfig: DbConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,109 +1,111 @@
import {
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/config/db-config";
import {
RemoteUserDefinedListDbItem,
RootRemoteDbItem,
} from "../../../src/databases/db-item";
import { calculateNewExpandedState } from "../../../src/databases/db-item-expansion";
import {
updateItemInExpandedState,
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/db-item-expansion";
import {
createRemoteUserDefinedListDbItem,
createRootRemoteDbItem,
} from "../../factories/db-item-factories";

describe("db item expansion", () => {
it("should add an expanded item to an existing list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
describe("updateItemInExpandedState", () => {
it("should add an expanded item to an existing list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];

const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});

const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
true,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
true,
);

expect(newExpandedItems).toEqual([
...currentExpandedItems,
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
expect(newExpandedItems).toEqual([
...currentExpandedItems,
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});

it("should add an expanded item to an empty list", () => {
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});
it("should add an expanded item to an empty list", () => {
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list2",
});

const newExpandedItems = calculateNewExpandedState([], dbItem, true);
const newExpandedItems = updateItemInExpandedState([], dbItem, true);

expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list2",
},
]);
});

it("should remove a collapsed item from a list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];
it("should remove a collapsed item from a list", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
{
kind: ExpandedDbItemKind.RemoteUserDefinedList,
listName: "list1",
},
];

const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list1",
});
const dbItem: RemoteUserDefinedListDbItem =
createRemoteUserDefinedListDbItem({
listName: "list1",
});

const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
false,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
false,
);

expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RootRemote,
},
]);
});
expect(newExpandedItems).toEqual([
{
kind: ExpandedDbItemKind.RootRemote,
},
]);
});

it("should remove a collapsed item from a list that becomes empty", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
];
it("should remove a collapsed item from a list that becomes empty", () => {
const currentExpandedItems: ExpandedDbItem[] = [
{
kind: ExpandedDbItemKind.RootRemote,
},
];

const dbItem: RootRemoteDbItem = createRootRemoteDbItem();
const dbItem: RootRemoteDbItem = createRootRemoteDbItem();

const newExpandedItems = calculateNewExpandedState(
currentExpandedItems,
dbItem,
false,
);
const newExpandedItems = updateItemInExpandedState(
currentExpandedItems,
dbItem,
false,
);

expect(newExpandedItems).toEqual([]);
expect(newExpandedItems).toEqual([]);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
DbConfig,
ExpandedDbItem,
ExpandedDbItemKind,
SelectedDbItemKind,
} from "../../../src/databases/config/db-config";
import {
Expand All @@ -10,6 +8,10 @@ import {
isRemoteRepoDbItem,
isRemoteUserDefinedListDbItem,
} from "../../../src/databases/db-item";
import {
ExpandedDbItem,
ExpandedDbItemKind,
} from "../../../src/databases/db-item-expansion";
import {
createLocalTree,
createRemoteTree,
Expand Down