mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 19:22:53 +01:00
Rewrite replace all for list view
This commit is contained in:
parent
6de094bca7
commit
57ca4b3d6e
@ -257,5 +257,9 @@ namespace Nikse.SubtitleEdit.Core.Common
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6462,73 +6462,30 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
|
||||
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);
|
||||
searchStringFound = true;
|
||||
replaceCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
textBoxListViewText.Visible = true;
|
||||
_subtitleListViewIndex = -1;
|
||||
if (firstIndex >= 0 && firstIndex < SubtitleListview1.Items.Count)
|
||||
if (MessageBox.Show(msgText, _language.ReplaceContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
|
||||
{
|
||||
SubtitleListview1.Items[firstIndex].Selected = true;
|
||||
SubtitleListview1.Items[firstIndex].Focused = true;
|
||||
SubtitleListview1.Focus();
|
||||
textBoxListViewText.Text = _subtitle.Paragraphs[firstIndex].Text;
|
||||
if (_subtitleOriginal != null && textBoxListViewTextOriginal.Visible)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
stopAtIndex = firstIndex;
|
||||
_findHelper.StartLineIndex = 0;
|
||||
_findHelper.SelectedIndex = 0;
|
||||
replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex);
|
||||
SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else if (replaceDialog.FindOnly)
|
||||
{
|
||||
|
77
src/ui/Logic/ReplaceAllHelper.cs
Normal file
77
src/ui/Logic/ReplaceAllHelper.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1225,6 +1225,7 @@
|
||||
<Compile Include="Logic\LanguageDeserializer.cs" />
|
||||
<Compile Include="Logic\LanguageStructure.cs" />
|
||||
<Compile Include="Logic\Networking\SeNetworkService.cs" />
|
||||
<Compile Include="Logic\ReplaceAllHelper.cs" />
|
||||
<Compile Include="Logic\SceneChangesGenerator.cs" />
|
||||
<Compile Include="Logic\ListViewSorter.cs" />
|
||||
<Compile Include="Logic\NativeMethods.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user