void Command_Edit_Repeat(EditRepeatDialog.Result result) { var results = GetFixedExpressionResults <int>(result.Expression); if (results.Any(repeatCount => repeatCount < 0)) { throw new Exception("Repeat count must be >= 0"); } ReplaceSelections(Selections.AsParallel().AsOrdered().Select((range, index) => RepeatString(GetString(range), results[index])).ToList()); if (result.SelectRepetitions) { var sels = new List <Range>(); for (var ctr = 0; ctr < Selections.Count; ++ctr) { if (results[ctr] != 0) { var selection = Selections[ctr]; var len = selection.Length / results[ctr]; for (var index = selection.Start; index < selection.End; index += len) { sels.Add(new Range(index + len, index)); } } } SetSelections(sels); } }
void Command_Edit_Bookmarks_Toggle() { var linePairs = Selections.AsParallel().AsOrdered().Select(range => new { start = Data.GetOffsetLine(range.Start), end = Data.GetOffsetLine(range.End) }).ToList(); if (linePairs.Any(pair => pair.start != pair.end)) { throw new Exception("Selections must be on a single line."); } var bookmarks = Bookmarks.ToList(); var lineRanges = linePairs.AsParallel().AsOrdered().Select(pair => new Range(Data.GetOffset(pair.start, 0))).ToList(); var comparer = Comparer <Range> .Create((r1, r2) => r1.Start.CompareTo(r2.Start)); var indexes = lineRanges.AsParallel().Select(range => new { range = range, index = bookmarks.BinarySearch(range, comparer) }).Reverse().ToList(); if (indexes.Any(index => index.index < 0)) { foreach (var pair in indexes) { if (pair.index < 0) { bookmarks.Insert(~pair.index, pair.range); } } } else { foreach (var pair in indexes) { bookmarks.RemoveAt(pair.index); } } SetBookmarks(bookmarks); }
void Command_Edit_Find_Replace(EditFindReplaceDialog.Result result) { var text = result.Text; var replace = result.Replace; if (!result.IsRegex) { text = Regex.Escape(text); replace = replace.Replace("$", "$$"); } if (result.WholeWords) { text = $"\\b{text}\\b"; } if (result.EntireSelection) { text = $"\\A{text}\\Z"; } var options = RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline; if (!result.MatchCase) { options |= RegexOptions.IgnoreCase; } var regex = new Regex(text, options); var regions = result.SelectionOnly ? Selections.ToList() : new List <Range> { FullRange }; var sels = regions.AsParallel().AsOrdered().SelectMany(region => Data.RegexMatches(regex, region.Start, region.Length, result.MultiLine, false, false)).Select(tuple => Range.FromIndex(tuple.Item1, tuple.Item2)).ToList(); SetSelections(sels); ReplaceSelections(Selections.AsParallel().AsOrdered().Select(range => regex.Replace(GetString(range), result.Replace)).ToList()); }
void Command_Select_Lines(bool includeEndings) { var lineSets = Selections.AsParallel().Select(range => new { start = Data.GetOffsetLine(range.Start), end = Data.GetOffsetLine(Math.Max(range.Start, range.End - 1)) }).ToList(); var hasLine = new bool[Data.NumLines]; foreach (var set in lineSets) { for (var ctr = set.start; ctr <= set.end; ++ctr) { hasLine[ctr] = true; } } var lines = new List <int>(); for (var line = 0; line < hasLine.Length; ++line) { if ((hasLine[line]) && (!Data.IsDiffGapLine(line))) { lines.Add(line); } } SetSelections(lines.AsParallel().AsOrdered().Select(line => Range.FromIndex(Data.GetOffset(line, 0), Data.GetLineLength(line) + (includeEndings ? Data.GetEndingLength(line) : 0))).ToList()); }
void Command_Numeric_Round(NumericFloorRoundCeilingDialog.Result result) { var baseValue = new NEExpression(result.BaseValue).EvaluateList <double>(GetVariables(), Selections.Count()); var interval = new NEExpression(result.Interval).EvaluateList <double>(GetVariables(), Selections.Count()); ReplaceSelections(Selections.AsParallel().AsOrdered().Select((range, index) => (Math.Round((double.Parse(GetString(range), NumberStyles.Float) - baseValue[index]) / interval[index], MidpointRounding.AwayFromZero) * interval[index] + baseValue[index]).ToString()).ToList()); }
void SelectRegEx(string pattern) { var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase); var results = Selections.AsParallel().AsOrdered().Select(region => Data.RegexMatches(regex, region.Start, region.Length, false, false, false)).SelectMany().Select(tuple => Range.FromIndex(tuple.Item1, tuple.Item2)).ToList(); SetSelections(results); }
void Command_Diff_NextPrevious(bool next, bool shiftDown) { if (DiffTarget == null) { throw new Exception("Diff not in progress"); } if ((TabsParent.GetIndex(this) < DiffTarget.TabsParent.GetIndex(DiffTarget)) && (DiffTarget.Active)) { return; } var lines = Selections.AsParallel().AsOrdered().Select(range => GetDiffNextPrevious(range, next)).ToList(); for (var pass = 0; pass < 2; ++pass) { var target = pass == 0 ? this : DiffTarget; var sels = lines.Select(tuple => new Range(target.Data.GetOffset(tuple.Item2, 0, true), target.Data.GetOffset(tuple.Item1, 0, true))).ToList(); if (shiftDown) { sels.AddRange(target.Selections); } target.SetSelections(sels); } }
void Command_Image_OverlayColor(ImageAddOverlayColorDialog.Result result) { var results = GetFixedExpressionResults <string>(result.Expression); var strs = Selections.AsParallel().AsOrdered().Select((range, index) => OverlayColor(results[index], GetString(range))).ToList(); ReplaceSelections(strs); }
void Command_Image_AdjustColor(ImageAdjustColorDialog.Result result) { var results = GetFixedExpressionResults <double>(result.Expression); var strs = Selections.AsParallel().AsOrdered().Select((range, index) => AdjustColor(GetString(range), results[index], result.Alpha, result.Red, result.Green, result.Blue)).ToList(); ReplaceSelections(strs); }
void Command_Numeric_Cycle(NumericCycleDialog.Result result) { var variables = GetVariables(); var minimums = new NEExpression(result.Minimum).EvaluateList <double>(variables, Selections.Count()); var maximums = new NEExpression(result.Maximum).EvaluateList <double>(variables, Selections.Count()); ReplaceSelections(Selections.AsParallel().AsOrdered().Select((range, index) => Cycle(double.Parse(GetString(range)), minimums[index], maximums[index], result.IncludeBeginning).ToString()).ToList()); }
void Command_Numeric_RandomNumber(NumericRandomNumberDialog.Result result) { var variables = GetVariables(); var minValues = new NEExpression(result.MinValue).EvaluateList <int>(variables, Selections.Count()); var maxValues = new NEExpression(result.MaxValue).EvaluateList <int>(variables, Selections.Count()); ReplaceSelections(Selections.AsParallel().Select((range, index) => random.Next(minValues[index], maxValues[index] + 1).ToString()).ToList()); }
void Command_Edit_Bookmarks_NextPreviousBookmark(bool next, bool selecting) { if (!Bookmarks.Any()) { return; } SetSelections(Selections.AsParallel().AsOrdered().Select(range => GetNextPrevBookmark(range, next, selecting)).ToList()); }
void Command_Numeric_Scale(NumericScaleDialog.Result result) { var variables = GetVariables(); var prevMins = new NEExpression(result.PrevMin).EvaluateList <double>(variables, Selections.Count()); var prevMaxs = new NEExpression(result.PrevMax).EvaluateList <double>(variables, Selections.Count()); var newMins = new NEExpression(result.NewMin).EvaluateList <double>(variables, Selections.Count()); var newMaxs = new NEExpression(result.NewMax).EvaluateList <double>(variables, Selections.Count()); ReplaceSelections(Selections.AsParallel().AsOrdered().Select((range, index) => ((double.Parse(GetString(range)) - prevMins[index]) * (newMaxs[index] - newMins[index]) / (prevMaxs[index] - prevMins[index]) + newMins[index]).ToString()).ToList()); }
void Command_Numeric_Select_MinMax(bool max) { if (!Selections.Any()) { throw new Exception("No selections"); } var values = Selections.AsParallel().AsOrdered().Select(range => double.Parse(GetString(range))).ToList(); var find = max ? values.OrderByDescending().First() : values.OrderBy().First(); SetSelections(values.Indexes(value => value == find).Select(index => Selections[index]).ToList()); }
void Command_Numeric_Fraction_Fraction() { ReplaceSelections(Selections.AsParallel().AsOrdered().Select(range => GetString(range)).Select(str => { var idx = str.IndexOf('.'); if (idx == -1) { return("0"); } return(str.Substring(idx)); }).ToList()); }
void Command_Edit_Navigate_WordLeftRight(bool next, bool selecting) { if ((!selecting) && (Selections.Any(range => range.HasSelection))) { SetSelections(Selections.AsParallel().AsOrdered().Select(range => new Range(next ? range.End : range.Start)).ToList()); return; } var func = next ? (Func <int, int>)GetNextWord : GetPrevWord; SetSelections(Selections.AsParallel().AsOrdered().Select(range => MoveCursor(range, func(range.Cursor), selecting)).ToList()); }
void Command_Numeric_Select_Fraction_Fraction() { SetSelections(Selections.AsParallel().AsOrdered().Select(range => { var str = GetString(range); var idx = str.IndexOf('.'); if (idx == -1) { return(Range.FromIndex(range.End, 0)); } return(new Range(range.Start + idx, range.End)); }).ToList()); }
void Command_Select_ToggleOpenClose(bool shiftDown) { SetSelections(Selections.AsParallel().AsOrdered().Select(range => { var newPos = Data.GetOppositeBracket(range.Cursor); if (newPos == -1) { return(range); } return(MoveCursor(range, newPos, shiftDown)); }).ToList()); }
EditFindReplaceDialog.Result Command_Edit_Find_Replace_Dialog() { string text = null; var selectionOnly = Selections.AsParallel().Any(range => range.HasSelection); if (Selections.Count == 1) { var sel = Selections.Single(); if ((selectionOnly) && (Data.GetOffsetLine(sel.Cursor) == Data.GetOffsetLine(sel.Anchor)) && (sel.Length < 1000)) { selectionOnly = false; text = GetString(sel); } } return(EditFindReplaceDialog.Run(WindowParent, text, selectionOnly)); }
List <int> GetOrdering(SortType type, bool caseSensitive, bool ascending) { var entries = Selections.AsParallel().Select((range, index) => Tuple.Create(GetString(range), index)); var stringComparer = caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase; switch (type) { case SortType.Smart: entries = OrderByAscDesc(entries, entry => entry.Item1, ascending, Helpers.SmartComparer(caseSensitive)); break; case SortType.String: entries = OrderByAscDesc(entries, entry => entry.Item1, ascending, stringComparer); break; case SortType.Length: entries = OrderByAscDesc(entries, entry => entry.Item1.Length, ascending); break; case SortType.Integer: entries = OrderByAscDesc(entries, entry => BigInteger.Parse(entry.Item1), ascending); break; case SortType.Float: entries = OrderByAscDesc(entries, entry => double.Parse(entry.Item1), ascending); break; case SortType.Hex: entries = OrderByAscDesc(entries, entry => BigInteger.Parse("0" + entry.Item1, NumberStyles.HexNumber), ascending); break; case SortType.DateTime: entries = OrderByAscDesc(entries, entry => DateTime.Parse(entry.Item1), ascending); break; case SortType.Keys: entries = OrderByAscDesc(entries, entry => entry.Item1, ascending, Comparer <string> .Create((value1, value2) => (keysHash.ContainsKey(value1) ? keysHash[value1] : int.MaxValue).CompareTo(keysHash.ContainsKey(value2) ? keysHash[value2] : int.MaxValue))); break; case SortType.Clipboard: { var sort = Clipboard.Distinct().Select((key, index) => new { key, index }).ToDictionary(entry => entry.key, entry => entry.index); entries = OrderByAscDesc(entries, entry => entry.Item1, ascending, Comparer <string> .Create((value1, value2) => (sort.ContainsKey(value1) ? sort[value1] : int.MaxValue).CompareTo(sort.ContainsKey(value2) ? sort[value2] : int.MaxValue))); } break; case SortType.Reverse: entries = entries.Reverse(); break; case SortType.Randomize: entries = entries.OrderBy(entry => random.Next()); break; case SortType.Frequency: { entries = entries.ToList().AsParallel(); var frequency = entries.GroupBy(a => a.Item1, stringComparer).ToDictionary(a => a.Key, a => a.Count(), stringComparer); entries = OrderByAscDesc(entries, entry => frequency[entry.Item1], ascending); } break; } return(entries.Select(entry => entry.Item2).ToList()); }
void Command_Edit_Find_MassFind(EditFindMassFindDialog.Result result) { var texts = GetVariableExpressionResults <string>(result.Text); if ((result.KeepMatching) || (result.RemoveMatching)) { var set = new HashSet <string>(texts, result.MatchCase ? (IEqualityComparer <string>)EqualityComparer <string> .Default : StringComparer.OrdinalIgnoreCase); SetSelections(Selections.AsParallel().AsOrdered().Where(range => set.Contains(GetString(range)) == result.KeepMatching).ToList()); return; } var searcher = new Searcher(texts, result.MatchCase); var selections = result.SelectionOnly ? Selections.ToList() : new List <Range> { FullRange }; var ranges = selections.AsParallel().AsOrdered().SelectMany(selection => Data.StringMatches(searcher, selection.Start, selection.Length)).Select(tuple => Range.FromIndex(tuple.Item1, tuple.Item2)).ToList(); SetSelections(ranges); }
void Command_Table_LineSelectionsToTable() { if (!Selections.Any()) { return; } var lineSets = Selections.AsParallel().AsOrdered().Select(range => new { start = Data.GetOffsetLine(range.Start), end = Data.GetOffsetLine(range.End) }).ToList(); if (lineSets.Any(range => range.start != range.end)) { throw new Exception("Cannot have multi-line selections"); } var sels = GetSelectionStrings(); var lines = lineSets.Select(range => range.start).ToList(); var rows = Enumerable.Range(0, Selections.Count).GroupBy(index => lines[index]).Select(group => group.Select(index => sels[index]).ToList()).ToList(); OpenTable(new Table(rows, false)); }
void Command_Numeric_Add_Sum() { if (!Selections.Any()) { return; } var result = Selections.Where(range => !range.HasSelection).FirstOrDefault(); if (result == null) { result = Selections[Math.Max(0, Math.Min(CurrentSelection, Selections.Count - 1))]; } var sum = Selections.AsParallel().Where(range => range.HasSelection).Select(range => double.Parse(GetString(range))).Sum(); SetSelections(new List <Range> { result }); ReplaceSelections(sum.ToString()); }
void Command_Numeric_Add_ForwardReverseSum(bool forward, bool undo) { var numbers = Selections.AsParallel().AsOrdered().Select(range => double.Parse(GetString(range))).ToList(); double total = 0; var start = forward ? 0 : numbers.Count - 1; var end = forward ? numbers.Count : -1; var step = forward ? 1 : -1; for (var ctr = start; ctr != end; ctr += step) { if (undo) { numbers[ctr] -= total; } total += numbers[ctr]; if (!undo) { numbers[ctr] = total; } } ReplaceSelections(numbers.Select(num => num.ToString()).ToList()); }
void Command_Text_Select_ByWidth(TextWidthDialog.Result result) { var results = GetFixedExpressionResults <int>(result.Expression); SetSelections(Selections.AsParallel().AsOrdered().Where((range, index) => range.Length == results[index]).ToList()); }
void Command_Text_Select_Trim(TextTrimDialog.Result result) => SetSelections(Selections.AsParallel().AsOrdered().Select(range => TrimRange(range, result)).ToList());
void Command_Text_RandomText(TextRandomTextDialog.Result result) { var results = GetFixedExpressionResults <int>(result.Expression); ReplaceSelections(Selections.AsParallel().AsOrdered().Select((range, index) => GetRandomData(result.Chars, results[index])).ToList()); }
void Command_Text_GUID() => ReplaceSelections(Selections.AsParallel().Select(range => Guid.NewGuid().ToString()).ToList());
void Command_Text_SingleLine() => ReplaceSelections(Selections.AsParallel().AsOrdered().Select(range => GetString(range).Replace("\r", "").Replace("\n", "")).ToList());
void Command_Text_Trim(TextTrimDialog.Result result) => ReplaceSelections(Selections.AsParallel().AsOrdered().Select(str => TrimString(GetString(str), result)).ToList());