Skip to content

Commit

Permalink
Add edit here option
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Mar 28, 2024
1 parent 8d19002 commit 9d043b7
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/IronyModManager.Shared/LocalizationResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "Move Up",
"MoveDown": "Move Down",
"Edit": "Edit",
"EditHere": "Edit Here",
"CopyText": "Copy Text",
"Delete": "Delete",
"Undo": "Undo",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "Mover Arriba",
"MoveDown": "Mover Abajo",
"Edit": "Editar",
"EditHere": "Editar aquí",
"CopyText": "Copiar Texto",
"Delete": "Eliminar",
"Undo": "Deshacer",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "Monter",
"MoveDown": "Descendre",
"Edit": "Éditer",
"EditHere": "Editer ici",
"CopyText": "Copier le texte",
"Delete": "Supprimer",
"Undo": "annuler",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/hr.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "Pomakni gore",
"MoveDown": "Pomakni dolje",
"Edit": "Uredi",
"EditHere": "Uredi ovdje",
"CopyText": "Kopiraj Tekst",
"Delete": "Obriši",
"Undo": "Poništi",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "Вверх",
"MoveDown": "Вниз",
"Edit": "Изменить",
"EditHere": "Редактировать здесь",
"CopyText": "Копировать текст",
"Delete": "Удалить",
"Undo": "Отменить",
Expand Down
1 change: 1 addition & 0 deletions src/IronyModManager/Localization/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
"MoveUp": "上移",
"MoveDown": "下移",
"Edit": "编辑",
"EditHere": "在此编辑",
"CopyText": "复制全部文本",
"Delete": "删除",
"Undo": "撤销",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,31 @@ public class MergeViewerControlViewModel(
/// <value>The edit this command.</value>
public virtual ReactiveCommand<bool, Unit> EditThisCommand { get; protected set; }

/// <summary>
/// Gets or sets a value representing the edit this here.
/// </summary>
/// <value>
/// The edit this here.
/// </value>
[StaticLocalization(LocalizationResources.Conflict_Solver.ContextMenu.EditHere)]
public virtual string EditThisHere { get; protected set; }

/// <summary>
/// Gets or sets a value representing the edit this here command.<see cref="ReactiveUI.ReactiveCommand{bool, System.Reactive.Unit}"/>
/// </summary>
/// <value>
/// The edit this here command.
/// </value>
public virtual ReactiveCommand<bool, Unit> EditThisHereCommand { get; protected set; }

/// <summary>
/// Gets or sets a value indicating whether the focus selected line.
/// </summary>
/// <value>
/// <c>true</c> if focus selected line; otherwise, <c>false</c>.
/// </value>
public virtual bool FocusSelectedLine { get; protected set; }

/// <summary>
/// Gets or sets a value indicating whether this instance is read only mode.
/// </summary>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1636,7 +1663,8 @@ protected virtual void PerformUndo(bool leftSide)
/// Sets the edit this.
/// </summary>
/// <param name="leftSide">if set to <c>true</c> [left side].</param>
protected virtual void SetEditThis(bool leftSide)
/// <param name="focusLine">The focus line.</param>
protected virtual void SetEditThis(bool leftSide, bool focusLine = false)
{
EditingLeft = leftSide;
EditingRight = !leftSide;
Expand All @@ -1645,6 +1673,13 @@ protected virtual void SetEditThis(bool leftSide)

LeftDocument = new TextDocument(LeftSide);
RightDocument = new TextDocument(RightSide);

if (focusLine)
{
FocusSelectedLine = false;
}

FocusSelectedLine = focusLine;
}

/// <summary>
Expand Down
76 changes: 72 additions & 4 deletions src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -104,6 +107,11 @@ public class MergeViewerControlView : BaseControl<MergeViewerControlViewModel>
/// </summary>
private SearchPanel editorSearchPanelRight;

/// <summary>
/// A private int named lineClicked.
/// </summary>
private int lineClicked;

/// <summary>
/// A private bool named syncingDiffScroll.
/// </summary>
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -731,6 +775,11 @@ void handlePointerPressed(PointerPressedEventArgs args)
return;
}

if (toggleLine)
{
lineClicked = idx + 1;
}

if (col.Count == 0)
{
col.Add(sourceCol[idx]);
Expand All @@ -754,7 +803,7 @@ void handlePointerPressed(PointerPressedEventArgs args)

((IronyModManager.Controls.TextArea)diff.TextArea).LeftPointerPressed += (_, eventArgs) =>
{
handlePointerPressed(eventArgs);
handlePointerPressed(eventArgs, false);
};
}

Expand Down Expand Up @@ -961,6 +1010,25 @@ private List<MenuItem> 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 },
Expand Down

0 comments on commit 9d043b7

Please sign in to comment.