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
This commit is contained in:
haven1433
2022-12-04 23:04:57 -06:00
parent 7d8ae74a00
commit a185ebe207
3 changed files with 17 additions and 2 deletions

View File

@@ -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 =

View File

@@ -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);

View File

@@ -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;