From e26cf56a2b9baa7429dccc1d8218cbb45e0142e4 Mon Sep 17 00:00:00 2001 From: Haven1433 Date: Wed, 17 Aug 2022 22:31:15 -0500 Subject: [PATCH] don't allow writing nameless anchors with nothing pointing to them --- src/HexManiac.Core/ViewModels/ViewPort.cs | 13 ++++++++++++- .../Before_Baseclass/StringModelTests.cs | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/HexManiac.Core/ViewModels/ViewPort.cs b/src/HexManiac.Core/ViewModels/ViewPort.cs index 8a1c8423..370d5ecf 100644 --- a/src/HexManiac.Core/ViewModels/ViewPort.cs +++ b/src/HexManiac.Core/ViewModels/ViewPort.cs @@ -762,7 +762,18 @@ namespace HavenSoft.HexManiac.Core.ViewModels { } if (run.Start <= index) { var token = new NoDataChangeDeltaModel(); - var errorInfo = PokemonModel.ApplyAnchor(Model, token, run.Start, AnchorText); + + // During edits, typing `^` is allowed, and is used as a way to remove an anchor. + // When doing AnchorText edits, clearing the name/format when there are no pointers is an error. + ErrorInfo errorInfo = ErrorInfo.NoError; + if (AnchorText == AnchorStart.ToString() && run.PointerSources.Count == 0) { + errorInfo = new ErrorInfo("An anchor with nothing pointing to it must have a name."); + } + + if (errorInfo == ErrorInfo.NoError) { + errorInfo = PokemonModel.ApplyAnchor(Model, token, run.Start, AnchorText); + } + if (errorInfo == ErrorInfo.NoError) { OnError?.Invoke(this, string.Empty); var newRun = Model.GetNextRun(index); diff --git a/src/HexManiac.Tests/Before_Baseclass/StringModelTests.cs b/src/HexManiac.Tests/Before_Baseclass/StringModelTests.cs index c231020f..c2bfa666 100644 --- a/src/HexManiac.Tests/Before_Baseclass/StringModelTests.cs +++ b/src/HexManiac.Tests/Before_Baseclass/StringModelTests.cs @@ -658,6 +658,16 @@ namespace HavenSoft.HexManiac.Tests { Assert.True(ViewPort.ChangeHistory.IsSaved); } + [Fact] + public void TextAnchor_ChangeToNoInfoAnchor_NoNameIsError() { + SetFullModel(0xFF); + ViewPort.Edit("^text\"\" "); + + ViewPort.AnchorText = "^"; + + Assert.NotEmpty(Errors); + } + private void Write(IDataModel model, ref int i, string characters) { foreach (var c in characters.ToCharArray()) model[i++] = (byte)PCSString.PCS.IndexOf(c.ToString());