From 9d043b73e9c31b85edb4938a0ab4d6a6e54e4ad4 Mon Sep 17 00:00:00 2001 From: bcssov Date: Thu, 28 Mar 2024 21:35:18 +0100 Subject: [PATCH] Add edit here option --- .../LocalizationResources.cs | 1 + src/IronyModManager/Localization/de.json | 1 + src/IronyModManager/Localization/en.json | 1 + src/IronyModManager/Localization/es.json | 1 + src/IronyModManager/Localization/fr.json | 1 + src/IronyModManager/Localization/hr.json | 1 + src/IronyModManager/Localization/ru.json | 1 + src/IronyModManager/Localization/zh.json | 1 + .../Controls/MergeViewerControlViewModel.cs | 37 ++++++++- .../Controls/MergeViewerControlView.xaml.cs | 76 ++++++++++++++++++- 10 files changed, 116 insertions(+), 5 deletions(-) diff --git a/src/IronyModManager.Shared/LocalizationResources.cs b/src/IronyModManager.Shared/LocalizationResources.cs index c71ef6ae..af7db1e9 100644 --- a/src/IronyModManager.Shared/LocalizationResources.cs +++ b/src/IronyModManager.Shared/LocalizationResources.cs @@ -449,6 +449,7 @@ public static class ContextMenu public const string MoveUp = Prefix + "MoveUp"; public const string MoveDown = Prefix + "MoveDown"; public const string Edit = Prefix + "Edit"; + public const string EditHere = Prefix + "EditHere"; public const string CopyText = Prefix + "CopyText"; public const string Delete = Prefix + "Delete"; public const string Undo = Prefix + "Undo"; diff --git a/src/IronyModManager/Localization/de.json b/src/IronyModManager/Localization/de.json index 2799d29a..d1c49d07 100644 --- a/src/IronyModManager/Localization/de.json +++ b/src/IronyModManager/Localization/de.json @@ -341,6 +341,7 @@ "MoveUp": "Nach Oben", "MoveDown": "Nach Uten", "Edit": "Bearbeiten", + "EditHere": "Hier bearbeiten", "CopyText": "Text Kopieren", "Delete": "Löschen", "Undo": "Rückgängig Machen", diff --git a/src/IronyModManager/Localization/en.json b/src/IronyModManager/Localization/en.json index d4814f62..479fe1dc 100644 --- a/src/IronyModManager/Localization/en.json +++ b/src/IronyModManager/Localization/en.json @@ -341,6 +341,7 @@ "MoveUp": "Move Up", "MoveDown": "Move Down", "Edit": "Edit", + "EditHere": "Edit Here", "CopyText": "Copy Text", "Delete": "Delete", "Undo": "Undo", diff --git a/src/IronyModManager/Localization/es.json b/src/IronyModManager/Localization/es.json index 62067e8a..dd415c7d 100644 --- a/src/IronyModManager/Localization/es.json +++ b/src/IronyModManager/Localization/es.json @@ -341,6 +341,7 @@ "MoveUp": "Mover Arriba", "MoveDown": "Mover Abajo", "Edit": "Editar", + "EditHere": "Editar aquí", "CopyText": "Copiar Texto", "Delete": "Eliminar", "Undo": "Deshacer", diff --git a/src/IronyModManager/Localization/fr.json b/src/IronyModManager/Localization/fr.json index d1298b5e..be2d8e9c 100644 --- a/src/IronyModManager/Localization/fr.json +++ b/src/IronyModManager/Localization/fr.json @@ -341,6 +341,7 @@ "MoveUp": "Monter", "MoveDown": "Descendre", "Edit": "Éditer", + "EditHere": "Editer ici", "CopyText": "Copier le texte", "Delete": "Supprimer", "Undo": "annuler", diff --git a/src/IronyModManager/Localization/hr.json b/src/IronyModManager/Localization/hr.json index 3f119d82..e13ef6d3 100644 --- a/src/IronyModManager/Localization/hr.json +++ b/src/IronyModManager/Localization/hr.json @@ -341,6 +341,7 @@ "MoveUp": "Pomakni gore", "MoveDown": "Pomakni dolje", "Edit": "Uredi", + "EditHere": "Uredi ovdje", "CopyText": "Kopiraj Tekst", "Delete": "Obriši", "Undo": "Poništi", diff --git a/src/IronyModManager/Localization/ru.json b/src/IronyModManager/Localization/ru.json index c0833aae..15e50d0d 100644 --- a/src/IronyModManager/Localization/ru.json +++ b/src/IronyModManager/Localization/ru.json @@ -341,6 +341,7 @@ "MoveUp": "Вверх", "MoveDown": "Вниз", "Edit": "Изменить", + "EditHere": "Редактировать здесь", "CopyText": "Копировать текст", "Delete": "Удалить", "Undo": "Отменить", diff --git a/src/IronyModManager/Localization/zh.json b/src/IronyModManager/Localization/zh.json index c95a6ade..2baa9298 100644 --- a/src/IronyModManager/Localization/zh.json +++ b/src/IronyModManager/Localization/zh.json @@ -341,6 +341,7 @@ "MoveUp": "上移", "MoveDown": "下移", "Edit": "编辑", + "EditHere": "在此编辑", "CopyText": "复制全部文本", "Delete": "删除", "Undo": "撤销", diff --git a/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs b/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs index 4fe575b1..e6c0128b 100644 --- a/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs +++ b/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs @@ -427,6 +427,31 @@ public class MergeViewerControlViewModel( /// The edit this command. public virtual ReactiveCommand EditThisCommand { get; protected set; } + /// + /// Gets or sets a value representing the edit this here. + /// + /// + /// The edit this here. + /// + [StaticLocalization(LocalizationResources.Conflict_Solver.ContextMenu.EditHere)] + public virtual string EditThisHere { get; protected set; } + + /// + /// Gets or sets a value representing the edit this here command. + /// + /// + /// The edit this here command. + /// + public virtual ReactiveCommand EditThisHereCommand { get; protected set; } + + /// + /// Gets or sets a value indicating whether the focus selected line. + /// + /// + /// true if focus selected line; otherwise, false. + /// + public virtual bool FocusSelectedLine { get; protected set; } + /// /// Gets or sets a value indicating whether this instance is read only mode. /// @@ -1343,6 +1368,8 @@ protected override void OnActivated(CompositeDisposable disposables) EditThisCommand = ReactiveCommand.Create((bool leftSide) => { SetEditThis(leftSide); }).DisposeWith(disposables); + EditThisHereCommand = ReactiveCommand.Create((bool leftSide) => { SetEditThis(leftSide, true); }).DisposeWith(disposables); + CopyTextCommand = ReactiveCommand.Create((bool leftSide) => { CopyTextAsync(leftSide).ConfigureAwait(true); }).DisposeWith(disposables); NextConflictCommand = ReactiveCommand.Create((bool leftSide) => { FindConflict(leftSide, true, false); }).DisposeWith(disposables); @@ -1636,7 +1663,8 @@ protected virtual void PerformUndo(bool leftSide) /// Sets the edit this. /// /// if set to true [left side]. - protected virtual void SetEditThis(bool leftSide) + /// The focus line. + protected virtual void SetEditThis(bool leftSide, bool focusLine = false) { EditingLeft = leftSide; EditingRight = !leftSide; @@ -1645,6 +1673,13 @@ protected virtual void SetEditThis(bool leftSide) LeftDocument = new TextDocument(LeftSide); RightDocument = new TextDocument(RightSide); + + if (focusLine) + { + FocusSelectedLine = false; + } + + FocusSelectedLine = focusLine; } /// diff --git a/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs b/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs index 83a6ee00..461625fb 100644 --- a/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs +++ b/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Disposables; +using System.Reactive.Linq; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; @@ -27,6 +28,7 @@ using AvaloniaEdit.Search; using AvaloniaEdit.Utils; using DiffPlex.DiffBuilder.Model; +using DynamicData; using IronyModManager.Common; using IronyModManager.Common.Views; using IronyModManager.DI; @@ -36,6 +38,7 @@ using IronyModManager.ViewModels.Controls; using ReactiveUI; using static IronyModManager.ViewModels.Controls.MergeViewerControlViewModel; +using ChangeType = DiffPlex.DiffBuilder.Model.ChangeType; namespace IronyModManager.Views.Controls { @@ -104,6 +107,11 @@ public class MergeViewerControlView : BaseControl /// private SearchPanel editorSearchPanelRight; + /// + /// A private int named lineClicked. + /// + private int lineClicked; + /// /// A private bool named syncingDiffScroll. /// @@ -627,7 +635,43 @@ void evalKey() diffLeftRenderer.ColorConverter = s; diffRightRenderer.ColorConverter = s; Dispatcher.UIThread.SafeInvoke(RedrawEditorDiffs); - }); + }).DisposeWith(disposables); + + this.WhenAnyValue(v => v.ViewModel.FocusSelectedLine).Where(_ => ViewModel.EditingText).Subscribe(s => + { + async Task scroll(int line, IronyModManager.Controls.TextEditor text) + { + // Allow for things to settle + await Task.Delay(50); + await Dispatcher.UIThread.SafeInvokeAsync(() => + { + text.Focus(); + text.TextArea.Caret.Location = new TextLocation(line, 1); + text.ScrollToLine(line); + }); + } + + if (!s) + { + return; + } + + var originalCol = ViewModel.EditingLeft ? ViewModel.LeftDiff : ViewModel.RightDiff; + var col = ViewModel.EditingLeft ? ViewModel.LeftSideSelected : ViewModel.RightSideSelected; + var text = ViewModel.EditingLeft ? editorLeft : editorRight; + if (col != null && col.Count != 0) + { + var item = col.FirstOrDefault()!; + if (item.Type != ChangeType.Imaginary) + { + var line = originalCol.Where(p => p.Type != ChangeType.Imaginary).IndexOf(item) + 1; + if (line > 0) + { + scroll(line, text).ConfigureAwait(false); + } + } + } + }).DisposeWith(disposables); base.OnActivated(disposables); } @@ -704,7 +748,7 @@ void setEditMode() var col = leftDiff ? ViewModel!.LeftSideSelected : ViewModel!.RightSideSelected; var sourceCol = leftDiff ? ViewModel.LeftDiff : ViewModel.RightDiff; - if (range.Item1 > sourceCol.Count - 1 || range.Item2 > sourceCol.Count) + if (range.Item1 > sourceCol.Count - 1 || range.Item2 > sourceCol.Count - 1) { return; } @@ -716,7 +760,7 @@ void setEditMode() } }; - void handlePointerPressed(PointerPressedEventArgs args) + void handlePointerPressed(PointerPressedEventArgs args, bool toggleLine = true) { var pos = args.GetPosition(diff); pos = new Point(0, pos.Y.CoerceValue(0, diff.Bounds.Height) + diff.VerticalOffset); @@ -731,6 +775,11 @@ void handlePointerPressed(PointerPressedEventArgs args) return; } + if (toggleLine) + { + lineClicked = idx + 1; + } + if (col.Count == 0) { col.Add(sourceCol[idx]); @@ -754,7 +803,7 @@ void handlePointerPressed(PointerPressedEventArgs args) ((IronyModManager.Controls.TextArea)diff.TextArea).LeftPointerPressed += (_, eventArgs) => { - handlePointerPressed(eventArgs); + handlePointerPressed(eventArgs, false); }; } @@ -961,6 +1010,25 @@ private List GetEditableMenuItems(bool leftSide) new() { Header = ViewModel.PrevConflict, Command = ViewModel.PrevConflictCommand, CommandParameter = leftSide }, new() { Header = "-" }, new() { Header = ViewModel.EditThis, Command = ViewModel.EditThisCommand, CommandParameter = leftSide }, + new() + { + Header = ViewModel.EditThisHere, + Command = ReactiveCommand.Create(() => + { + var col = leftSide ? ViewModel!.LeftSideSelected : ViewModel!.RightSideSelected; + var ln = lineClicked - 1; + var sourceCol = leftSide ? ViewModel.LeftDiff : ViewModel.RightDiff; + if (ln > sourceCol.Count - 1) + { + return; + } + + col.Clear(); + col.Add(sourceCol[ln]); + + ViewModel.EditThisHereCommand.Execute(leftSide).Subscribe(); + }) + }, new() { Header = ViewModel.CopyText, Command = ViewModel.CopyTextCommand, CommandParameter = leftSide }, new() { Header = "-" }, new() { Header = ViewModel.DeleteText, Command = ViewModel.DeleteTextCommand, CommandParameter = leftSide },