Skip to content

Commit 7ae569f

Browse files
feat(history): cache the root folders in the store (#23)
* feat(history): cache the root folders in the store * version bump
1 parent c283f71 commit 7ae569f

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "open-vscode",
33
"displayName": "vscode-open",
44
"description": "Open Visual Studio Code with a URL",
5-
"version": "1.1.4",
5+
"version": "1.1.5",
66
"publisher": "robertohuertasm",
77
"license": "MIT",
88
"author": {

src/opener.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getOpenedRepoHistory,
77
removeOpenedRepoFromHistory,
88
resetPendingUriToOpen,
9+
addOpenedRepoToHistory,
910
} from './repoHistory';
1011

1112
// The kind of URIS we are handling will be like this:
@@ -130,15 +131,7 @@ export class Opener {
130131
}
131132

132133
private async openNewWorkspace(forceNewWindow = false) {
133-
// has this repo been ever opened?
134-
const knownRepoInfo = await getOpenedRepoHistory(this.context);
135-
const knownRepo = knownRepoInfo[this.repoName];
136-
console.log(`knownRepo ${this.repoName}: ${knownRepo}`);
137-
// if the repo is not found in the history, we're going to search for it in the config roots
138-
const repoPath = knownRepo
139-
? vscode.Uri.file(knownRepo)
140-
: await this.findFolderInConfigRoots();
141-
134+
const repoPath = await this.findFolderInSources();
142135
if (this.file) {
143136
// NOTE: opening a folder causes the extension to reload again.
144137
// This means that if you want to open a file, you need to do it on extension activation.
@@ -152,6 +145,30 @@ export class Opener {
152145
await this.openFolder(repoPath, forceNewWindow);
153146
}
154147

148+
private async findFolderInSources(): Promise<vscode.Uri | undefined> {
149+
// has this repo been ever opened?
150+
const knownRepoInfo = await getOpenedRepoHistory(this.context);
151+
const knownRepo = knownRepoInfo[this.repoName];
152+
console.log(`knownRepo ${this.repoName}: ${knownRepo}`);
153+
// if the repo is not found in the history, we're going to search for it in the config roots
154+
if (knownRepo) {
155+
return vscode.Uri.file(knownRepo);
156+
}
157+
const repoInRootsUri = await this.findFolderInConfigRoots();
158+
if (repoInRootsUri) {
159+
// we're going to store the repo in the history
160+
await addOpenedRepoToHistory(
161+
{
162+
name: this.repoName,
163+
uri: repoInRootsUri,
164+
},
165+
this.context,
166+
);
167+
return repoInRootsUri;
168+
}
169+
return undefined;
170+
}
171+
155172
private async getCurrentCommit(
156173
repo: Repository,
157174
): Promise<Branch | undefined> {

src/repoHistory.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@ import * as vscode from 'vscode';
33
const OPENED_REPO_HISTORY_KEY = 'openedRepoHistory';
44
const PENDING_URI_TO_OPEN = 'vscode-open.last-uri';
55

6+
export interface HistoryRecord {
7+
readonly name: string;
8+
readonly uri: vscode.Uri;
9+
}
10+
611
export async function getOpenedRepoHistory(
712
context: vscode.ExtensionContext,
813
): Promise<Record<string, string | undefined>> {
914
return (await context.globalState.get(OPENED_REPO_HISTORY_KEY)) || {};
1015
}
1116

1217
export async function addOpenedRepoToHistory(
13-
folder: vscode.WorkspaceFolder,
18+
record: HistoryRecord,
1419
context: vscode.ExtensionContext,
1520
): Promise<void> {
1621
const repos = await getOpenedRepoHistory(context);
17-
repos[folder.name] = folder.uri.fsPath;
22+
repos[record.name] = record.uri.fsPath;
1823
await context.globalState.update(OPENED_REPO_HISTORY_KEY, repos);
1924
// re-check, there are some cases where this might not work
2025
const check = await getOpenedRepoHistory(context);
21-
if (!check[folder.name]) {
22-
console.error(`Failed to save ${folder.name} to repo history`);
26+
if (!check[record.name]) {
27+
console.error(`Failed to save ${record.name} to repo history`);
2328
} else {
24-
console.log(`Added repo ${folder.name} to the repo history`);
29+
console.log(`Added repo ${record.name} to the repo history`);
2530
}
2631
}
2732

0 commit comments

Comments
 (0)