Skip to content

Commit

Permalink
Reworked client persistance retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontanCombust committed Jul 18, 2024
1 parent 9ceb420 commit 7d4cb60
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 95 deletions.
16 changes: 7 additions & 9 deletions editors/vscode/src/commands/debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';

import * as persistence from '../persistence';
import { getPersistence } from '../persistence';
import * as tdcp from '../providers/text_document_content_providers'
import { Cmd } from './index'

Expand Down Expand Up @@ -42,8 +42,8 @@ export function commandShowScriptAst(context: vscode.ExtensionContext): Cmd {
editor.revealRange(new vscode.Range(targetPos, targetPos), vscode.TextEditorRevealType.AtTop);
}

const rememberedChoices = persistence.RememberedChoices.Memento.fetchOrDefault(context);
if (!rememberedChoices.neverShowAgainDebugAstNotif) {
const db = getPersistence(context);
if (!db.neverShowAgainDebugAstNotif) {
enum Answer {
Close = "I understand",
NeverShowAgain = "Never show this message again"
Expand All @@ -55,8 +55,7 @@ export function commandShowScriptAst(context: vscode.ExtensionContext): Cmd {
);

if (answer == Answer.NeverShowAgain) {
rememberedChoices.neverShowAgainDebugAstNotif = true;
rememberedChoices.store(context);
db.neverShowAgainDebugAstNotif = true;
}
}
});
Expand Down Expand Up @@ -138,9 +137,9 @@ export function commandShowScriptCst(context: vscode.ExtensionContext): Cmd {
editor.revealRange(new vscode.Range(targetPos, targetPos), vscode.TextEditorRevealType.AtTop);
}

const rememberedChoices = persistence.RememberedChoices.Memento.fetchOrDefault(context);
const db = getPersistence(context);
// using the same memento for AST warning for simplicity
if (!rememberedChoices.neverShowAgainDebugAstNotif) {
if (!db.neverShowAgainDebugAstNotif) {
enum Answer {
Close = "I understand",
NeverShowAgain = "Never show this message again"
Expand All @@ -152,8 +151,7 @@ export function commandShowScriptCst(context: vscode.ExtensionContext): Cmd {
);

if (answer == Answer.NeverShowAgain) {
rememberedChoices.neverShowAgainDebugAstNotif = true;
rememberedChoices.store(context);
db.neverShowAgainDebugAstNotif = true;
}
}
});
Expand Down
13 changes: 6 additions & 7 deletions editors/vscode/src/commands/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getLanguageClient } from "../lsp/lang_client"
import { VanillaFilesProvider } from '../providers/vanilla_files_provider';
import { ScriptContentProvider } from '../providers/script_content_provider';
import * as requests from '../lsp/requests';
import * as persistence from '../persistence';
import { getPersistence } from '../persistence';
import * as utils from '../utils';
import { Cmd } from './index'

Expand Down Expand Up @@ -152,12 +152,11 @@ async function initializeProjectInDirectory(client: lsp.LanguageClient, projectD
Answer.YesHere, Answer.YesInNew, Answer.No);

if (answer != undefined && answer != Answer.No) {
const memento = new persistence.OpenManifestOnInit.Memento(
projectDirUri,
manifestUri
);

await memento.store(context);
const db = getPersistence(context);
db.openManifestOnInit = {
workspaceUri: projectDirUri,
manifestUri,
};

const openNewWindow = answer == Answer.YesInNew;
await vscode.commands.executeCommand("vscode.openFolder", projectDirUri, {
Expand Down
8 changes: 4 additions & 4 deletions editors/vscode/src/lsp/lang_client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import * as lsp from 'vscode-languageclient/node';

import * as persistence from '../persistence';
import { getPersistence } from '../persistence';
import * as config from '../config';
import * as handlers from './handlers';
import * as tdcp from '../providers/text_document_content_providers'
Expand Down Expand Up @@ -71,8 +71,8 @@ export async function createLanguageClient(ctx: vscode.ExtensionContext, cfg: co

// Start the client. This will also launch the server
return client.start().then(_ => {
const memento = persistence.OpenManifestOnInit.Memento.fetch(ctx);

const db = getPersistence(ctx);
const memento = db.openManifestOnInit;
if (memento != undefined) {
// If a new project has just been created in this directory and the user agreed to open it, show them the manifest of said project
if (vscode.workspace.workspaceFolders?.some(f => f.uri.fsPath == memento.workspaceUri.fsPath)) {
Expand All @@ -84,7 +84,7 @@ export async function createLanguageClient(ctx: vscode.ExtensionContext, cfg: co
(err) => client?.debug('Manifest could not be shown: ' + err)
);

persistence.OpenManifestOnInit.Memento.erase(ctx);
db.openManifestOnInit = undefined;
}
}
});
Expand Down
116 changes: 46 additions & 70 deletions editors/vscode/src/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,66 @@
import * as vscode from 'vscode';


// Used for opening the manifest file of the newly created project
export namespace OpenManifestOnInit {
export const KEY = "OpenManifestOnInit";

export class Memento {
public workspaceUri: vscode.Uri;
public manifestUri: vscode.Uri;

constructor(workspaceUri: vscode.Uri, manifestUri: vscode.Uri) {
this.workspaceUri = workspaceUri;
this.manifestUri = manifestUri;
}

public async store(context: vscode.ExtensionContext) {
const dto: MementoDto = {
workspaceUriStr: this.workspaceUri.toString(),
manifestUriStr: this.manifestUri.toString(),
};
export function getPersistence(ctx: vscode.ExtensionContext): Persistence {
return new Persistence(ctx)
}

await context.globalState.update(KEY, dto);
}
export class Persistence {
constructor(
readonly ctx: vscode.ExtensionContext
) {}

public static fetch(context: vscode.ExtensionContext): Memento | undefined {
const dto = context.globalState.get<MementoDto>(KEY);
if (dto) {
const memento = new Memento(
vscode.Uri.parse(dto.workspaceUriStr),
vscode.Uri.parse(dto.manifestUriStr),
);

return memento;
} else {
return undefined;
// Used for opening the manifest file of the newly created project
get openManifestOnInit(): OpenManifestOnInit | undefined {
const dto = this.ctx.globalState.get<OpenManifestOnInitDto>("OpenManifestOnInit");
if (dto) {
return {
workspaceUri: vscode.Uri.parse(dto.workspaceUriStr),
manifestUri: vscode.Uri.parse(dto.manifestUriStr),
}
} else {
return undefined;
}
}

public static erase(context: vscode.ExtensionContext) {
context.globalState.update(KEY, undefined);
set openManifestOnInit(value: OpenManifestOnInit | undefined) {
let dto: OpenManifestOnInitDto | undefined = undefined;
if (value) {
dto = {
workspaceUriStr: value.workspaceUri.toString(),
manifestUriStr: value.manifestUri.toString(),
}
}
}

interface MementoDto {
workspaceUriStr: string,
manifestUriStr: string
this.ctx.globalState.update("OpenManifestOnInit", dto);
}
}

export namespace RememberedChoices {
export const KEY = "RememberedChoices";

export class Memento {
public neverShowAgainDebugAstNotif: boolean;
public neverShowAgainForeignScriptWarning: boolean;

constructor(dto: MementoDto) {
this.neverShowAgainDebugAstNotif = dto.neverShowAgainDebugAstNotif;
this.neverShowAgainForeignScriptWarning = dto.neverShowAgainForeignScriptWarning;
}

public async store(context: vscode.ExtensionContext) {
const dto: MementoDto = {
neverShowAgainDebugAstNotif: this.neverShowAgainDebugAstNotif,
neverShowAgainForeignScriptWarning: this.neverShowAgainForeignScriptWarning
};
get neverShowAgainDebugAstNotif(): boolean {
return this.ctx.globalState.get<boolean>("NeverShowAgainDebugAstNotif") ?? false;
}

context.globalState.update(KEY, dto);
}
set neverShowAgainDebugAstNotif(value: boolean) {
this.ctx.globalState.update("NeverShowAgainDebugAstNotif", value)
}

public static fetchOrDefault(context: vscode.ExtensionContext): Memento {
const dto = context.globalState.get<MementoDto>(KEY);

if (dto) {
return new Memento(dto);
} else {
return new Memento({
neverShowAgainDebugAstNotif: false,
neverShowAgainForeignScriptWarning: false
})
}
}
get neverShowAgainForeignScriptWarning(): boolean {
return this.ctx.globalState.get<boolean>("NeverShowAgainForeignScriptWarning") ?? false;
}

interface MementoDto {
neverShowAgainDebugAstNotif: boolean;
neverShowAgainForeignScriptWarning: boolean;
set neverShowAgainForeignScriptWarning(value: boolean) {
this.ctx.globalState.update("NeverShowAgainForeignScriptWarning", value)
}
}
}

export interface OpenManifestOnInit {
workspaceUri: vscode.Uri,
manifestUri: vscode.Uri
}

interface OpenManifestOnInitDto {
workspaceUriStr: string,
manifestUriStr: string
}
9 changes: 4 additions & 5 deletions editors/vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as vscode from 'vscode';
import * as fs from 'fs/promises';
import * as fspath from 'path';

import * as persistence from './persistence';
import { getPersistence } from './persistence';
import * as model from './lsp/model'


export async function showForeignScriptWarning(context: vscode.ExtensionContext) {
const rememberedChoices = persistence.RememberedChoices.Memento.fetchOrDefault(context);
if (!rememberedChoices.neverShowAgainForeignScriptWarning) {
const db = getPersistence(context);
if (!db.neverShowAgainForeignScriptWarning) {
enum Answer {
Close = "I understand",
NeverShowAgain = "Don't show this message again",
Expand All @@ -25,8 +25,7 @@ export async function showForeignScriptWarning(context: vscode.ExtensionContext)
);

if (answer == Answer.NeverShowAgain) {
rememberedChoices.neverShowAgainForeignScriptWarning = true;
rememberedChoices.store(context);
db.neverShowAgainForeignScriptWarning = true;
}
else if (answer == Answer.SeeManual) {
await vscode.env.openExternal(manualUri);
Expand Down

0 comments on commit 7d4cb60

Please sign in to comment.