Skip to content

Commit

Permalink
Implement JoinText feature for the editor
Browse files Browse the repository at this point in the history
Join selected lines into one line using space char as the separator
Example:
  ...
  aaa
  bbb
  ccc
  ...
  ->
  aaa bbb ccc
  • Loading branch information
0x7c13 committed Mar 14, 2022
1 parent 5cea0d7 commit 140d254
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
64 changes: 64 additions & 0 deletions src/Notepads/Controls/TextEditor/TextEditorCore.JoinText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace Notepads.Controls.TextEditor
{
using System;
using System.Collections.Generic;
using Windows.UI.Text;
using Microsoft.AppCenter.Analytics;
using Notepads.Services;

public partial class TextEditorCore
{
/// <summary>
/// Join selected lines into one line using space char as the separator
/// Example:
/// ...
/// aaa
/// bbb
/// ccc
/// ...
/// ->
/// aaa bbb ccc
/// </summary>
private void JoinText()
{
try
{
GetTextSelectionPosition(out var start, out var end);
GetLineColumnSelection(out var startLine,
out var endLine,
out var startColumn,
out var endColumn,
out _,
out _);

// Does not make any sense to join 1 line
if (startLine == endLine) return;

var document = GetText();
var lines = GetDocumentLinesCache();

var startLineInitialIndex = start - startColumn + 1;
var endLineFinalIndex = end - endColumn + lines[endLine - 1].Length + 1;
if (endLineFinalIndex > document.Length) endLineFinalIndex = document.Length;

if (document[endLineFinalIndex - 1] == RichEditBoxDefaultLineEnding) endLineFinalIndex--;
if (endLineFinalIndex - startLineInitialIndex <= 0) return;

var selectedLines = document.Substring(startLineInitialIndex, endLineFinalIndex - startLineInitialIndex);
var joinedLines = selectedLines.Replace(RichEditBoxDefaultLineEnding, ' ');

var newContent = document.Remove(startLineInitialIndex, endLineFinalIndex - startLineInitialIndex)
.Insert(startLineInitialIndex, joinedLines);

Document.SetText(TextSetOptions.None, newContent);
Document.Selection.SetRange(start, end);
}
catch (Exception ex)
{
LoggingService.LogError($"[{nameof(TextEditorCore)}] Failed to join text: {ex}");
Analytics.TrackEvent("TextEditorCore_FailedToJoinText",
new Dictionary<string, string> { { "Exception", ex.ToString() } });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool DisplayLineNumbers
}

private readonly IList<TextBlock> _renderedLineNumberBlocks = new List<TextBlock>();
private readonly Dictionary<string, double> _miniRequisiteIntegerTextRenderingWidthCache = new Dictionary<string, double>();
private readonly Dictionary<string, double> _minRequisiteIntegerTextRenderingWidthCache = new Dictionary<string, double>();
private readonly SolidColorBrush _lineNumberDarkModeForegroundBrush = new SolidColorBrush("#99EEEEEE".ToColor());
private readonly SolidColorBrush _lineNumberLightModeForegroundBrush = new SolidColorBrush("#99000000".ToColor());

Expand Down Expand Up @@ -127,9 +127,9 @@ private double CalculateMinimumRequisiteIntegerTextRenderingWidth(FontFamily fon
{
var cacheKey = $"{fontFamily.Source}-{(int)fontSize}-{numberTextLength}";

if (_miniRequisiteIntegerTextRenderingWidthCache.ContainsKey(cacheKey))
if (_minRequisiteIntegerTextRenderingWidthCache.ContainsKey(cacheKey))
{
return _miniRequisiteIntegerTextRenderingWidthCache[cacheKey];
return _minRequisiteIntegerTextRenderingWidthCache[cacheKey];
}

double minRequisiteWidth = 0;
Expand All @@ -144,7 +144,7 @@ private double CalculateMinimumRequisiteIntegerTextRenderingWidth(FontFamily fon
}
}

_miniRequisiteIntegerTextRenderingWidthCache[cacheKey] = minRequisiteWidth;
_minRequisiteIntegerTextRenderingWidthCache[cacheKey] = minRequisiteWidth;
return minRequisiteWidth;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Notepads/Controls/TextEditor/TextEditorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void Dispose()

_lineNumberCanvas?.Children.Clear();
_renderedLineNumberBlocks.Clear();
_miniRequisiteIntegerTextRenderingWidthCache.Clear();
_minRequisiteIntegerTextRenderingWidthCache.Clear();

SelectionChanged -= OnSelectionChanged;
TextWrappingChanged -= OnTextWrappingChanged;
Expand Down Expand Up @@ -258,6 +258,7 @@ private KeyboardCommandHandler GetKeyboardCommandHandler()
new KeyboardCommand<KeyRoutedEventArgs>(VirtualKey.F5, (args) => InsertDateTimeString()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.E, (args) => SearchInWeb()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.D, (args) => DuplicateText()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.J, (args) => JoinText()),
new KeyboardCommand<KeyRoutedEventArgs>(VirtualKey.Tab, (args) => AddIndentation(AppSettingsService.EditorDefaultTabIndents)),
new KeyboardCommand<KeyRoutedEventArgs>(false, false, true, VirtualKey.Tab, (args) => RemoveIndentation(AppSettingsService.EditorDefaultTabIndents)),
new KeyboardCommand<KeyRoutedEventArgs>(false, true, false, VirtualKey.Up, (args) => MoveTextUp()),
Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Notepads.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="Controls\Print\PrintPageFormat.xaml.cs">
<DependentUpon>PrintPageFormat.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\TextEditor\TextEditorCore.JoinText.cs" />
<Compile Include="Controls\TextEditor\TextEditorCore.ExternalEventListener.cs" />
<Compile Include="Controls\TextEditor\TextEditorCore.LineHighlighter.cs" />
<Compile Include="Controls\TextEditor\TextEditorCore.LineNumbers.cs" />
Expand Down

0 comments on commit 140d254

Please sign in to comment.