From 4b0439036da9faef76a215676c6b635e3c7bddc2 Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Sat, 13 Jul 2024 09:13:29 -0700 Subject: [PATCH] Define `Input`s corresponding definition to enable code localization. --- CHANGELOG.md | 1 + src/nodes/Context.ts | 2 ++ src/nodes/Input.ts | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba71505fb..ddd00df1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http: - Fixed closing text delimiter localization. - Hide comma separator when localizing names and docs. - Permit comma separators between text literals, docs, and names, allowing line breaks for text. +- Define `Input`s corresponding definition to enable code localization. ## 0.10.4 2024-07-08 diff --git a/src/nodes/Context.ts b/src/nodes/Context.ts index 55ad1450f..b8941e0c9 100644 --- a/src/nodes/Context.ts +++ b/src/nodes/Context.ts @@ -50,9 +50,11 @@ export default class Context { visit(node: Node) { this.stack.push(node); } + unvisit() { this.stack.pop(); } + visited(node: Node) { return this.stack.includes(node); } diff --git a/src/nodes/Input.ts b/src/nodes/Input.ts index 4da91b9ba..0b7bf12e0 100644 --- a/src/nodes/Input.ts +++ b/src/nodes/Input.ts @@ -22,6 +22,7 @@ import SimpleExpression from './SimpleExpression'; import Evaluate from './Evaluate'; import Refer from '@edit/Refer'; import ExpressionPlaceholder from './ExpressionPlaceholder'; +import type Definition from './Definition'; export default class Input extends SimpleExpression { readonly name: Token; @@ -122,6 +123,20 @@ export default class Input extends SimpleExpression { return this.value.evaluateTypeGuards(current, guard); } + /** Get the bind to which this input corresponds. */ + getCorrespondingDefinition(context: Context): Definition | undefined { + const parent = context.getRoot(this)?.getParent(this); + if (parent instanceof Evaluate) { + const fun = parent.getFunction(context); + if (fun) + return fun.inputs.find((input) => + input.hasName(this.getName()), + ); + } + + return undefined; + } + getName() { return this.name.getText(); }