Skip to content

Commit

Permalink
Generalized and improved step to cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 24, 2023
1 parent d084e81 commit 1fac753
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/components/editor/util/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export const StepForwardInput: Command = {
};

export const StepBackNode: Command = {
symbol: '',
symbol: '•←',
description: (l) => l.ui.timeline.button.backNode,
visible: Visibility.Visible,
category: Category.Evaluate,
Expand All @@ -271,15 +271,15 @@ export const StepBackNode: Command = {
control: true,
key: 'ArrowLeft',
keySymbol: '←',
active: ({ caret }) => caret?.isNode() ?? false,
execute: (context) =>
context.caret?.position instanceof Node
? context.evaluator.stepBackToNode(context.caret.position)
: undefined,
active: ({ caret }) => caret !== undefined,
execute: ({ caret, evaluator }) => {
const target = caret?.getExpressionAt();
if (target) evaluator.stepBackToNode(target);
},
};

export const StepForwardNode: Command = {
symbol: '',
symbol: '⇢•',
description: (l) => l.ui.timeline.button.forwardNode,
visible: Visibility.Visible,
category: Category.Evaluate,
Expand All @@ -288,11 +288,11 @@ export const StepForwardNode: Command = {
shift: true,
alt: true,
control: true,
active: (context) => context.caret?.isNode() ?? false,
execute: (context) =>
context.caret?.position instanceof Node
? context.evaluator.stepToNode(context.caret.position)
: undefined,
active: ({ caret }) => caret !== undefined,
execute: ({ caret, evaluator }) => {
const target = caret?.getExpressionAt();
if (target) evaluator.stepToNode(target);
},
};

export const Restart: Command = {
Expand Down
7 changes: 6 additions & 1 deletion src/components/project/ProjectView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@
caret: layout.isFullscreenNonSource()
? undefined
: Array.from($editors.values()).find((editor) => editor.focused)
?.caret,
?.caret ?? Array.from($editors.values())[0]?.caret,
project,
/** We intentionally depend on the evaluation store because it updates when the evaluator's state changes */
evaluator: $evaluation.evaluator,
Expand All @@ -1048,6 +1048,11 @@
toggleBlocks,
help: () => (showHelpDialog = !showHelpDialog),
};
$: console.log(
Array.from($editors.values()).find((editor) => editor.focused)?.caret
);
const commandContextStore = writable(commandContext);
$: commandContextStore.set(commandContext);
setContext(ProjectCommandContextSymbol, commandContextStore);
Expand Down
11 changes: 11 additions & 0 deletions src/edit/Caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ export default class Caret {
: undefined;
}

getExpressionAt() {
const start =
this.position instanceof Node
? this.position
: this.tokenExcludingSpace;
if (start === undefined) return undefined;
return this.source.root
.getAncestors(start)
.find((n): n is Expression => n instanceof Expression);
}

getNodeInside() {
return typeof this.position === 'number'
? this.insideToken()
Expand Down
4 changes: 2 additions & 2 deletions src/locale/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3903,11 +3903,11 @@
"play": "evaluate the program to the end, responding to inputs in real time",
"pause": "pause the program, enabling stepping forward and backwards",
"backStep": "step back one",
"backNode": "step back to previous node evaluation",
"backNode": "step to previous evaluation of cursor",
"backInput": "back one input",
"out": "step out of this function",
"forwardStep": "step forward one",
"forwardNode": "step forward to next node evaluation",
"forwardNode": "step to next evaluation of cursor",
"forwardInput": "step forward to next stream input",
"present": "to the end",
"start": "to the beginning",
Expand Down

0 comments on commit 1fac753

Please sign in to comment.