Rewrite replace all for list view

This commit is contained in:
niksedk 2021-10-31 20:39:39 +01:00
parent 6de094bca7
commit 57ca4b3d6e
4 changed files with 99 additions and 60 deletions

View File

@ -257,5 +257,9 @@ namespace Nikse.SubtitleEdit.Core.Common
return string.Join(Environment.NewLine, text.SplitToLines()); return string.Join(Environment.NewLine, text.SplitToLines());
} }
public static int CountNewLineSafe(Regex regularExpression, string text)
{
return regularExpression.Matches(string.Join(Environment.NewLine, text.SplitToLines())).Count;
}
} }
} }

View File

@ -6462,73 +6462,30 @@ namespace Nikse.SubtitleEdit.Forms
if (replaceDialog.ReplaceAll) if (replaceDialog.ReplaceAll)
{ {
if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex);
SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
RestoreSubtitleListviewIndices();
if (_replaceStartLineIndex >= 1) // Prompt for start over
{ {
if (_findHelper.SelectedIndex > stopAtIndex) _replaceStartLineIndex = 0;
string msgText = _language.ReplaceContinueNotFound;
if (replaceCount > 0)
{ {
break; msgText = string.Format(_language.ReplaceXContinue, replaceCount);
} }
SetTextForFindAndReplace(true, replaceDialog.ReplaceAll); if (MessageBox.Show(msgText, _language.ReplaceContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
searchStringFound = true;
replaceCount++;
}
else
{
textBoxListViewText.Visible = true;
_subtitleListViewIndex = -1;
if (firstIndex >= 0 && firstIndex < SubtitleListview1.Items.Count)
{ {
SubtitleListview1.Items[firstIndex].Selected = true; stopAtIndex = firstIndex;
SubtitleListview1.Items[firstIndex].Focused = true; _findHelper.StartLineIndex = 0;
SubtitleListview1.Focus(); _findHelper.SelectedIndex = 0;
textBoxListViewText.Text = _subtitle.Paragraphs[firstIndex].Text; replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex);
if (_subtitleOriginal != null && textBoxListViewTextOriginal.Visible) SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
{
var orginial = Utilities.GetOriginalParagraph(_findHelper.SelectedIndex, _subtitle.Paragraphs[_findHelper.SelectedIndex], _subtitleOriginal.Paragraphs);
if (orginial != null)
{
textBoxListViewTextOriginal.Text = orginial.Text;
}
}
}
else
{
SubtitleListview1.SelectIndexAndEnsureVisible(0, true);
}
ShowStatus(string.Format(_language.NoMatchFoundX, _findHelper.FindText));
if (_replaceStartLineIndex >= 1) // Prompt for start over
{
_replaceStartLineIndex = 0;
string msgText = _language.ReplaceContinueNotFound;
if (replaceCount > 0)
{
msgText = string.Format(_language.ReplaceXContinue, replaceCount);
}
if (MessageBox.Show(msgText, _language.ReplaceContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
{
stopAtIndex = firstIndex;
_findHelper.MatchInOriginal = false;
_findHelper.StartLineIndex = 0;
_findHelper.SelectedIndex = 0;
_findHelper.SelectedPosition = 0;
_findHelper.ReplaceFromPosition = 0;
SetTextForFindAndReplace(false, replaceDialog.ReplaceAll);
if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle))
{
SetTextForFindAndReplace(true, replaceDialog.ReplaceAll);
_findHelper.SelectedPosition += _findHelper.ReplaceText.Length;
_findHelper.ReplaceFromPosition = _findHelper.SelectedPosition;
searchStringFound = true;
replaceCount++;
}
}
} }
} }
break;
} }
else if (replaceDialog.FindOnly) else if (replaceDialog.FindOnly)
{ {

View File

@ -0,0 +1,77 @@
using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.Enums;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic
{
public static class ReplaceAllHelper
{
internal static int ReplaceAll(FindReplaceDialogHelper findHelper, Subtitle subtitle, Subtitle subtitleOriginal, bool allowEditOfOriginalSubtitle, int stopAtIndex)
{
if (findHelper.FindReplaceType.FindType == FindType.RegEx)
{
var count = ReplaceAllRegEx(findHelper, subtitle, stopAtIndex);
if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.Count > 0)
{
count += ReplaceAllRegEx(findHelper, subtitleOriginal, stopAtIndex);
}
return count;
}
var replaceCount = ReplaceAllNonRegEx(findHelper, subtitle, stopAtIndex);
if (allowEditOfOriginalSubtitle && subtitleOriginal?.Paragraphs.Count > 0)
{
replaceCount += ReplaceAllNonRegEx(findHelper, subtitleOriginal, stopAtIndex);
}
return replaceCount;
}
private static int ReplaceAllNonRegEx(FindReplaceDialogHelper findHelper, Subtitle subtitle, int stopAtIndex)
{
var replaceCount = 0;
for (int i = findHelper.StartLineIndex; i < subtitle.Paragraphs.Count; i++)
{
if (i >= stopAtIndex)
{
break;
}
var p = subtitle.Paragraphs[i];
var start = -1;
while (findHelper.FindNext(p.Text, start))
{
p.Text = p.Text.Remove(findHelper.SelectedIndex, findHelper.FindTextLength).Insert(findHelper.SelectedIndex, findHelper.ReplaceText);
start = findHelper.SelectedIndex + findHelper.FindTextLength;
replaceCount++;
}
}
return replaceCount;
}
private static int ReplaceAllRegEx(FindReplaceDialogHelper findHelper, Subtitle subtitle, int stopAtIndex)
{
var replaceCount = 0;
for (int i = findHelper.StartLineIndex; i < subtitle.Paragraphs.Count; i++)
{
if (i >= stopAtIndex)
{
break;
}
var p = subtitle.Paragraphs[i];
var before = p.Text;
var r = new Regex(RegexUtils.FixNewLine(findHelper.FindText), RegexOptions.Multiline);
p.Text = RegexUtils.ReplaceNewLineSafe(r, p.Text, findHelper.ReplaceText);
if (before != p.Text)
{
replaceCount += RegexUtils.CountNewLineSafe(r, p.Text);
}
}
return replaceCount;
}
}
}

View File

@ -1225,6 +1225,7 @@
<Compile Include="Logic\LanguageDeserializer.cs" /> <Compile Include="Logic\LanguageDeserializer.cs" />
<Compile Include="Logic\LanguageStructure.cs" /> <Compile Include="Logic\LanguageStructure.cs" />
<Compile Include="Logic\Networking\SeNetworkService.cs" /> <Compile Include="Logic\Networking\SeNetworkService.cs" />
<Compile Include="Logic\ReplaceAllHelper.cs" />
<Compile Include="Logic\SceneChangesGenerator.cs" /> <Compile Include="Logic\SceneChangesGenerator.cs" />
<Compile Include="Logic\ListViewSorter.cs" /> <Compile Include="Logic\ListViewSorter.cs" />
<Compile Include="Logic\NativeMethods.cs" /> <Compile Include="Logic\NativeMethods.cs" />