Skip to content

Commit

Permalink
Update config state from configure session commands + add `\set lim…
Browse files Browse the repository at this point in the history
…it` command to repl + respect `set alias/module` commands
  • Loading branch information
jaclarke committed Jun 15, 2023
1 parent 4e015b5 commit 940e7c2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 39 deletions.
30 changes: 23 additions & 7 deletions shared/studio/state/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ export class Connection extends Model({
});
private _queryQueue: PendingQuery[] = [];

private _baseSessionConfig = Session.defaults();

@computed
get _state() {
const sessionState = sessionStateCtx.get(this);

let state = Session.defaults();
let state = this._baseSessionConfig;

if (sessionState?.activeState.globals.length) {
state = state.withGlobals(
Expand Down Expand Up @@ -262,12 +264,26 @@ export class Connection extends Model({
execute: Math.round(executeEndTime - parseEndTime),
};

sessionStateCtx
.get(this)!
.updateGlobalsFromCommand(
statements,
(this.conn as any).lastStateUpdate?.globals
);
const stateUpdate = (this.conn as any).lastStateUpdate;
if (!opts.ignoreSessionConfig && stateUpdate) {
let newState = Session.defaults();
if (stateUpdate.module) {
newState = newState.withModuleAliases({module: stateUpdate.module});
}
if (stateUpdate.aliases) {
newState = newState.withModuleAliases(
(stateUpdate.aliases as [string, string][]).reduce(
(aliases, [key, val]) => {
aliases[key] = val;
return aliases;
},
{} as {[key: string]: string}
)
);
}
this._baseSessionConfig = newState;
sessionStateCtx.get(this)!.updateStateFromCommand(stateUpdate);
}

return {
result: decode(outCodecBuf, resultBuf, opts.newCodec),
Expand Down
82 changes: 50 additions & 32 deletions shared/studio/state/sessionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,22 +416,10 @@ export class SessionState extends Model({
}

@modelAction
updateGlobalsFromCommand(statements: string[], stateUpdate: any) {
const globalNames = new Set(
statements
.map(
(statement) =>
statement.match(/^\s*(?:re)?set\s+global\s+(.+?)(?:\s|:=|$)/i)?.[1]
)
.filter((name) => name) as string[]
);

if (!globalNames.size) {
return;
}

console.log(globalNames, stateUpdate);

updateStateFromCommand(stateUpdate: {
globals?: {[key: string]: any};
config?: {[key: string]: any};
}) {
const dbState = dbCtx.get(this)!;

const schemaDataGlobals = new Map(
Expand All @@ -441,26 +429,17 @@ export class SessionState extends Model({
])
);

for (const globalName of globalNames) {
const type =
schemaDataGlobals.get(globalName) ??
schemaDataGlobals.get(`default::${globalName}`);
console.log(type);
const unsetGlobals = new Set<string>();
for (const [globalName, val] of Object.entries(
stateUpdate.globals ?? {}
)) {
const type = schemaDataGlobals.get(globalName);
if (!type) {
continue;
}

const val = stateUpdate?.[type.name];
if (val === undefined || (val === null && !type.default)) {
const existingGlobalState = this.draftState!.globals[type.name];
if (existingGlobalState) {
existingGlobalState.active = false;
const [newVal, err] = newPrimitiveValue(
type.target as PrimitiveType
);
existingGlobalState.value = frozen(newVal);
existingGlobalState.error = err;
}
if (val === null && !type.default) {
unsetGlobals.add(globalName);
} else {
const editorVal =
val != null
Expand All @@ -475,6 +454,45 @@ export class SessionState extends Model({
};
}
}
for (const [globalName, globalState] of Object.entries(
this.draftState!.globals
)) {
if (
globalState.active &&
(unsetGlobals.has(globalName) || !stateUpdate.globals?.[globalName])
) {
globalState.active = false;
const [newVal, err] = newPrimitiveValue(
globalState.type.data as PrimitiveType
);
globalState.value = frozen(newVal);
globalState.error = err;
}
}

for (const [configName, configState] of Object.entries(
this.draftState!.config
)) {
const val = stateUpdate.config?.[configName];
if (val === undefined) {
if (configState.active) {
configState.active = false;
const [newVal, err] = newPrimitiveValue(
configState.type.data as PrimitiveType
);
configState.value = frozen(newVal);
configState.error = err;
}
} else {
configState.active = true;
configState.value = frozen(
val != null
? valueToEditorValue(val, configState.type.data as PrimitiveType)
: null
);
configState.error = false;
}
}

this.updateActiveState();
this.storeSessionData();
Expand Down
7 changes: 7 additions & 0 deletions shared/studio/tabs/repl/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export function renderCommandResult(result: CommandResult) {
<div className={styles.command}>\c, \connect DBNAME</div>
<div className={styles.info}>Switch to database DBNAME</div>

<div className={styles.heading}>Settings</div>

<div className={styles.command}>\set limit LIMIT</div>
<div className={styles.info}>
Set implicit limit to LIMIT. Set to 0 to disable
</div>

<div className={styles.heading}>Help</div>

<div className={styles.command}>\?, \h, \help</div>
Expand Down
45 changes: 45 additions & 0 deletions shared/studio/tabs/repl/state/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {SchemaObjectType, SchemaScalarType} from "@edgedb/common/schemaData";
import {Repl, ReplHistoryItem} from ".";
import {dbCtx} from "../../../state";
import {instanceCtx} from "../../../state/instance";
import {sessionStateCtx} from "../../../state/sessionState";

export enum CommandOutputKind {
error,
Expand Down Expand Up @@ -92,6 +93,50 @@ export async function handleSlashCommand(
repl.updateSetting("retroMode", !repl.settings.retroMode);
break;
}
case "set": {
switch (args[0]) {
case "limit":
const limit = parseInt(args[1], 10);
if (Number.isNaN(limit) || limit < 0) {
item.setCommandResult({
kind: CommandOutputKind.error,
msg: `invalid limit: '${args[1]}'`,
});
} else {
item.setCommandResult({kind: CommandOutputKind.none});
const sessionState = sessionStateCtx.get(repl)!;
const limitState =
sessionState.draftState?.options["Implicit Limit"];
console.log(sessionState, limitState);
if (limitState) {
if (limit === 0) {
if (limitState.active) {
sessionState.toggleOptionActive("Implicit Limit");
}
} else {
sessionState.updateItemValue(
limitState,
limit.toString(),
false
);
if (!limitState.active) {
sessionState.toggleOptionActive("Implicit Limit");
}
}
sessionState.updateActiveState();
sessionState.storeSessionData();
}
}
break;
default:
item.setCommandResult({
kind: CommandOutputKind.error,
msg: `unknown set command: '${args[0]}'`,
});
break;
}
break;
}
default:
item.setCommandResult({
kind: CommandOutputKind.error,
Expand Down

0 comments on commit 940e7c2

Please sign in to comment.