From a185ebe207f85acef5fdefbcbbba8184c3acd99f Mon Sep 17 00:00:00 2001 From: haven1433 Date: Sun, 4 Dec 2022 23:04:57 -0600 Subject: [PATCH] track cursor when changing text during script compiles for example, when adding {} to the end of a script, or when changing ?????? to an actual address --- src/HexManiac.Core/ViewModels/TextEditorViewModel.cs | 5 +++++ src/HexManiac.Core/ViewModels/Tools/CodeTool.cs | 12 ++++++++++++ src/HexManiac.WPF/Controls/TextEditor.xaml.cs | 2 -- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/HexManiac.Core/ViewModels/TextEditorViewModel.cs b/src/HexManiac.Core/ViewModels/TextEditorViewModel.cs index f67d5829..22ee9d25 100644 --- a/src/HexManiac.Core/ViewModels/TextEditorViewModel.cs +++ b/src/HexManiac.Core/ViewModels/TextEditorViewModel.cs @@ -46,6 +46,11 @@ namespace HavenSoft.HexManiac.Core.ViewModels { public string CommentContent { get; private set; } = string.Empty; public string TextContent { get; private set; } = string.Empty; + public void PushCaretUpdate(int index) { + CaretIndex = index; + RequestCaretMove.Raise(this); + } + private void UpdateLayers() { if (content.Length == 0) { PlainContent = AccentContent = diff --git a/src/HexManiac.Core/ViewModels/Tools/CodeTool.cs b/src/HexManiac.Core/ViewModels/Tools/CodeTool.cs index 2fc20a45..1943d317 100644 --- a/src/HexManiac.Core/ViewModels/Tools/CodeTool.cs +++ b/src/HexManiac.Core/ViewModels/Tools/CodeTool.cs @@ -371,6 +371,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools { var oldScripts = parser.CollectScripts(model, start); var originalCodeContent = codeContent; var code = parser.Compile(history.CurrentChange, model, start, ref codeContent, out var movedData); + if (originalCodeContent != codeContent) body.SaveCaret(); if (code == null) { return; } @@ -528,6 +529,12 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools { public int CaretPosition { get => Editor.CaretIndex; set { + if (savedCaret >= 0) { + value = savedCaret; + savedCaret = -1; + Editor.PushCaretUpdate(value); + return; + } if (Editor.CaretIndex == value) return; var lines = Content.Split('\r', '\n').ToList(); var contentBoundaryCount = 0; @@ -571,6 +578,11 @@ namespace HavenSoft.HexManiac.Core.ViewModels.Tools { NotifyPropertyChanged(nameof(CaretPosition)); }); } + + private int savedCaret = -1; + public void SaveCaret() { + savedCaret = CaretPosition + 1; + } } public record HelpContext(string Line, int Index); diff --git a/src/HexManiac.WPF/Controls/TextEditor.xaml.cs b/src/HexManiac.WPF/Controls/TextEditor.xaml.cs index ed4054d6..5803bfbb 100644 --- a/src/HexManiac.WPF/Controls/TextEditor.xaml.cs +++ b/src/HexManiac.WPF/Controls/TextEditor.xaml.cs @@ -1,6 +1,5 @@ using HavenSoft.HexManiac.Core.ViewModels; using System; -using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -40,7 +39,6 @@ namespace HavenSoft.HexManiac.WPF.Controls { public TextEditor() { InitializeComponent(); TransparentLayer.SelectionChanged += (sender, e) => { - // if (ViewModel != null) ViewModel.CaretIndex = TransparentLayer.SelectionStart; SelectionChanged?.Invoke(this, e); }; DataContextChanged += HandleDataContextChanged;