Skip to content

Commit

Permalink
Fix #8, NewProjectPanel default directory
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Apr 5, 2024
1 parent f0cf28e commit cefe28c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export async function activate(context: ExtensionContext): Promise<void> {

const settings = Settings.createInstance(
context.workspaceState,
context.globalState,
context.extension.packageJSON as PackageJSON
);
GithubApiCache.createInstance(context);
Expand Down
36 changes: 33 additions & 3 deletions src/settings.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Memento, WorkspaceConfiguration } from "vscode";
import { homedir } from "os";
import { type Memento, Uri, type WorkspaceConfiguration } from "vscode";
import { workspace } from "vscode";

/**
Expand Down Expand Up @@ -26,24 +27,37 @@ export interface PackageJSON {
publisher: string;
}

export type GlobalStateType = Memento & {
setKeysForSync(keys: readonly string[]): void;
};

const LAST_PROJECT_ROOT_STATE_KEY = "lastProjectRoot";

export default class Settings {
private static instance?: Settings;
private config: WorkspaceConfiguration;
public context: Memento;
public globalState: GlobalStateType;
private pkg: PackageJSON;

private constructor(context: Memento, packageJSON: PackageJSON) {
private constructor(
context: Memento,
globalState: GlobalStateType,
packageJSON: PackageJSON
) {
this.context = context;
this.globalState = globalState;
this.pkg = packageJSON;

this.config = workspace.getConfiguration(packageJSON.name);
}

public static createInstance(
context: Memento,
globalState: GlobalStateType,
packageJSON: PackageJSON
): Settings {
Settings.instance = new Settings(context, packageJSON);
Settings.instance = new Settings(context, globalState, packageJSON);

return Settings.instance;
}
Expand Down Expand Up @@ -109,4 +123,20 @@ export default class Settings {
public getExtensionId(): string {
return [this.pkg.publisher, this.pkg.name].join(".");
}

public async setLastProjectRoot(root: Uri): Promise<void> {
// saving fsPath not uri project as it would get corrupted and lose its
// fsPath property after loading
await this.globalState.update(LAST_PROJECT_ROOT_STATE_KEY, root.fsPath);
}

public getLastProjectRoot(): Uri {
const fsPath = this.globalState.get<string>(
LAST_PROJECT_ROOT_STATE_KEY,
// default to home directory as new project root
homedir()
);

return Uri.file(fsPath);
}
}
4 changes: 4 additions & 0 deletions src/webview/newProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ export class NewProjectPanel {
this._extensionUri = extensionUri;
this._settings = settings;
this._isProjectImport = isProjectImport;
// set local property as it's an indicator for initial projectRoot update
// later during webview initialization
projectUri = projectUri ?? this._settings.getLastProjectRoot();
this._projectRoot = projectUri;
this._isCreateFromExampleOnly = createFromExample;

Expand Down Expand Up @@ -440,6 +443,7 @@ export class NewProjectPanel {
if (newLoc && newLoc[0]) {
// overwrite preview folderUri
this._projectRoot = newLoc[0];
await this._settings.setLastProjectRoot(newLoc[0]);

// update webview
await this._panel.webview.postMessage({
Expand Down

0 comments on commit cefe28c

Please sign in to comment.