-
-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement JoinText feature for the editor
Join selected lines into one line using space char as the separator Example: ... aaa bbb ccc ... -> aaa bbb ccc
- Loading branch information
Showing
4 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/Notepads/Controls/TextEditor/TextEditorCore.JoinText.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters