From 9ffd617fadd6132c22ccae0a245818f4e027f86c Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Sat, 23 Nov 2019 15:51:40 +0200 Subject: [PATCH 1/3] add an additional check to prevent NRE --- CSharpMath.Editor/Keyboards/MathKeyboard.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CSharpMath.Editor/Keyboards/MathKeyboard.cs b/CSharpMath.Editor/Keyboards/MathKeyboard.cs index 92f75afcd..f601f355b 100644 --- a/CSharpMath.Editor/Keyboards/MathKeyboard.cs +++ b/CSharpMath.Editor/Keyboards/MathKeyboard.cs @@ -103,22 +103,24 @@ void HandleScriptButton(bool isSuperScript) { SetScript(emptyAtom, MathAtoms.PlaceholderList); MathList.InsertAndAdvance(ref _insertionIndex, emptyAtom, subIndexType); } else { - var prevAtom = MathList.AtomAt(_insertionIndex.Previous); -#warning Simplify to tuple patterns when C# 8 is out - switch ((GetScript(prevAtom) is null, _insertionIndex.FinalSubIndexType is MathListSubIndexType.BetweenBaseAndScripts)) { - case var t when t == (true, true): + var isBetweenBaseAndScripts = _insertionIndex.FinalSubIndexType is MathListSubIndexType.BetweenBaseAndScripts; + var prevIndexCorrected = + isBetweenBaseAndScripts ? _insertionIndex.LevelDown() : _insertionIndex.Previous; + var prevAtom = MathList.AtomAt(prevIndexCorrected); + switch ((GetScript(prevAtom) is null, isBetweenBaseAndScripts)) { + case (true, true): SetScript(MathList.AtomAt(_insertionIndex.LevelDown()), MathAtoms.PlaceholderList); _insertionIndex = _insertionIndex.LevelDown().LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); break; - case var t when t == (true, false): + case (true, false): SetScript(prevAtom, MathAtoms.PlaceholderList); _insertionIndex = _insertionIndex.Previous.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); break; - case var t when t == (false, true): + case (false, true): // If we are already inside the nucleus, then we come out and go up to the script _insertionIndex = _insertionIndex.LevelDown().LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(GetScript(prevAtom).Atoms.Count)); break; - case var t when t == (false, false): + case (false, false): _insertionIndex = _insertionIndex.Previous.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(GetScript(prevAtom).Atoms.Count)); break; } From 65cb3358e8c8f3a475ebe9c30db96b13a98f9dc0 Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Sat, 23 Nov 2019 17:51:15 +0200 Subject: [PATCH 2/3] tidy --- CSharpMath.Editor/Keyboards/MathKeyboard.cs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CSharpMath.Editor/Keyboards/MathKeyboard.cs b/CSharpMath.Editor/Keyboards/MathKeyboard.cs index f601f355b..55d5f348b 100644 --- a/CSharpMath.Editor/Keyboards/MathKeyboard.cs +++ b/CSharpMath.Editor/Keyboards/MathKeyboard.cs @@ -107,21 +107,21 @@ void HandleScriptButton(bool isSuperScript) { var prevIndexCorrected = isBetweenBaseAndScripts ? _insertionIndex.LevelDown() : _insertionIndex.Previous; var prevAtom = MathList.AtomAt(prevIndexCorrected); - switch ((GetScript(prevAtom) is null, isBetweenBaseAndScripts)) { - case (true, true): - SetScript(MathList.AtomAt(_insertionIndex.LevelDown()), MathAtoms.PlaceholderList); - _insertionIndex = _insertionIndex.LevelDown().LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); + switch (GetScript(prevAtom), isBetweenBaseAndScripts) { + case (null, true): + SetScript(prevAtom, MathAtoms.PlaceholderList); + _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); break; - case (true, false): + case (null, false): SetScript(prevAtom, MathAtoms.PlaceholderList); - _insertionIndex = _insertionIndex.Previous.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); + _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); break; - case (false, true): + case (var script, true): // If we are already inside the nucleus, then we come out and go up to the script - _insertionIndex = _insertionIndex.LevelDown().LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(GetScript(prevAtom).Atoms.Count)); + _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(script.Atoms.Count)); break; - case (false, false): - _insertionIndex = _insertionIndex.Previous.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(GetScript(prevAtom).Atoms.Count)); + case (var script, false): + _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(script.Atoms.Count)); break; } } From 37690b70ac32fa8ee7bf545a1dfb335c559cf271 Mon Sep 17 00:00:00 2001 From: FoggyFinder Date: Sat, 23 Nov 2019 19:03:31 +0200 Subject: [PATCH 3/3] even more --- CSharpMath.Editor/Keyboards/MathKeyboard.cs | 23 +++++---------------- 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/CSharpMath.Editor/Keyboards/MathKeyboard.cs b/CSharpMath.Editor/Keyboards/MathKeyboard.cs index 55d5f348b..81b368010 100644 --- a/CSharpMath.Editor/Keyboards/MathKeyboard.cs +++ b/CSharpMath.Editor/Keyboards/MathKeyboard.cs @@ -104,26 +104,13 @@ void HandleScriptButton(bool isSuperScript) { MathList.InsertAndAdvance(ref _insertionIndex, emptyAtom, subIndexType); } else { var isBetweenBaseAndScripts = _insertionIndex.FinalSubIndexType is MathListSubIndexType.BetweenBaseAndScripts; - var prevIndexCorrected = - isBetweenBaseAndScripts ? _insertionIndex.LevelDown() : _insertionIndex.Previous; + var prevIndexCorrected = isBetweenBaseAndScripts ? _insertionIndex.LevelDown() : _insertionIndex.Previous; var prevAtom = MathList.AtomAt(prevIndexCorrected); - switch (GetScript(prevAtom), isBetweenBaseAndScripts) { - case (null, true): - SetScript(prevAtom, MathAtoms.PlaceholderList); - _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); - break; - case (null, false): - SetScript(prevAtom, MathAtoms.PlaceholderList); - _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(0)); - break; - case (var script, true): - // If we are already inside the nucleus, then we come out and go up to the script - _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(script.Atoms.Count)); - break; - case (var script, false): - _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(script.Atoms.Count)); - break; + var script = GetScript(prevAtom); + if (script is null) { + SetScript(prevAtom, MathAtoms.PlaceholderList); } + _insertionIndex = prevIndexCorrected.LevelUpWithSubIndex(subIndexType, MathListIndex.Level0Index(script?.Atoms?.Count ?? 0)); } }