-
Notifications
You must be signed in to change notification settings - Fork 192
Foundations for editable math: replaceNode fix, caret preservation, baseline-correct cursor #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubham030
wants to merge
5
commits into
simpleclub:main
Choose a base branch
from
shubham030:fix/editable-math-foundations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4f34b8
fix(ast): SyntaxTree.replaceNode TypeError on non-leaf parents
shubham030 82989f1
fix(widgets): preserve selection across ast mutations
shubham030 6352387
feat(widgets): accept an external MathController; fix caret showCurso…
shubham030 5c5078f
fix(render): anchor caret to alphabetic baseline; stable baseline for…
shubham030 8951767
fix: address CodeRabbit review feedback
shubham030 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Regression tests for SyntaxTree.replaceNode. | ||
| // | ||
| // Before the fix, replaceNode built a `List<GreenNode?>` and passed it to | ||
| // the concrete overrides of `updateChildren`, which narrow the parameter | ||
| // type in every subclass (EquationRowNode wants List<GreenNode>, FracNode | ||
| // wants List<EquationRowNode>, etc.). This produced a runtime _TypeError | ||
| // on virtually any in-tree mutation. These tests cover the three parent | ||
| // shapes exhaustively: an EquationRowNode parent, a FracNode parent, and a | ||
| // MultiscriptsNode parent (nullable-slot case). | ||
|
|
||
| import 'package:flutter_math_fork/ast.dart'; | ||
| import 'package:flutter_math_fork/tex.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| SyntaxTree _parse(String tex) => | ||
| SyntaxTree(greenRoot: TexParser(tex, const TexParserSettings()).parse()); | ||
|
|
||
| void main() { | ||
| group('SyntaxTree.replaceNode', () { | ||
| test('replaces a direct child of EquationRowNode', () { | ||
| final tree = _parse(r'x + y'); | ||
| final root = tree.root; | ||
| // Find the first non-null red child — on `x + y` it will be the | ||
| // SymbolNode for `x`. | ||
| final firstChild = root.children.firstWhere((c) => c != null)!; | ||
| expect(firstChild.value, isA<SymbolNode>()); | ||
|
|
||
| final replacement = SymbolNode(symbol: 'z'); | ||
| final updated = tree.replaceNode(firstChild, replacement); | ||
|
|
||
| // The root row should still have the same number of children, | ||
| // with the first one replaced. | ||
| expect(updated.greenRoot, isA<EquationRowNode>()); | ||
| final newFirst = updated.greenRoot.children.first; | ||
| expect(newFirst, isA<SymbolNode>()); | ||
| expect((newFirst as SymbolNode).symbol, 'z'); | ||
| expect(updated.greenRoot.children.length, | ||
| tree.greenRoot.children.length); | ||
| }); | ||
|
|
||
| test('replaces the numerator of a FracNode', () { | ||
| final tree = _parse(r'\frac{1}{2}'); | ||
| // Walk the red tree: root -> FracNode -> numerator (EquationRowNode) | ||
| final fracRed = tree.root.children.firstWhere((c) => c != null)!; | ||
| expect(fracRed.value, isA<FracNode>()); | ||
| final numeratorRed = fracRed.children.firstWhere((c) => c != null)!; | ||
| expect(numeratorRed.value, isA<EquationRowNode>()); | ||
|
|
||
| final replacement = EquationRowNode( | ||
| children: [SymbolNode(symbol: '9')], | ||
| ); | ||
| final updated = tree.replaceNode(numeratorRed, replacement); | ||
|
|
||
| final newFrac = updated.greenRoot.children.first as FracNode; | ||
| final newNumLeaf = newFrac.numerator.children.first as SymbolNode; | ||
| expect(newNumLeaf.symbol, '9'); | ||
| // Denominator must be untouched. | ||
| final denomLeaf = newFrac.denominator.children.first as SymbolNode; | ||
| expect(denomLeaf.symbol, '2'); | ||
| }); | ||
|
|
||
| test('replaces the superscript slot of a MultiscriptsNode (nullable)', () { | ||
| final tree = _parse(r'x^{2}'); | ||
| final multiRed = tree.root.children.firstWhere((c) => c != null)!; | ||
| expect(multiRed.value, isA<MultiscriptsNode>()); | ||
| // Children of MultiscriptsNode are [base, sub, sup, presub, presup] — | ||
| // many of which are null. The non-null ones here should be base & sup. | ||
| final nonNullChildren = | ||
| multiRed.children.whereType<SyntaxNode>().toList(); | ||
| expect(nonNullChildren.length, greaterThanOrEqualTo(2)); | ||
|
|
||
| // Locate the sup slot — it's the one whose green value contains the `2`. | ||
| final supRed = nonNullChildren.firstWhere((c) { | ||
| final row = c.value as EquationRowNode; | ||
| return row.children.any( | ||
| (x) => x is SymbolNode && x.symbol == '2'); | ||
| }); | ||
|
|
||
| final replacement = EquationRowNode( | ||
| children: [SymbolNode(symbol: '7')], | ||
| ); | ||
| final updated = tree.replaceNode(supRed, replacement); | ||
|
|
||
| final newMulti = updated.greenRoot.children.first as MultiscriptsNode; | ||
| expect(newMulti.sup, isNotNull); | ||
| final newSupLeaf = newMulti.sup!.children.first as SymbolNode; | ||
| expect(newSupLeaf.symbol, '7'); | ||
| // Base is untouched. | ||
| final newBaseLeaf = newMulti.base.children.first as SymbolNode; | ||
| expect(newBaseLeaf.symbol, 'x'); | ||
| }); | ||
|
|
||
| test('replacing the root wraps in an EquationRowNode', () { | ||
| final tree = _parse(r'\frac{1}{2}'); | ||
| final replacement = SymbolNode(symbol: 'q'); | ||
| final updated = tree.replaceNode(tree.root, replacement); | ||
| // A LeafNode replacement at the root must be wrapped. | ||
| expect(updated.greenRoot, isA<EquationRowNode>()); | ||
| expect(updated.greenRoot.children.length, 1); | ||
| expect((updated.greenRoot.children.first as SymbolNode).symbol, 'q'); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.