From bdb0f8707ec658cb2068ec4556f997eff1026ff5 Mon Sep 17 00:00:00 2001 From: WMJ Date: Thu, 3 Jan 2019 11:09:09 +0800 Subject: [PATCH] ! Simplified code --- Codist/SmartBars/SmartBar.CommonEdit.cs | 35 +++++++------------------ 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/Codist/SmartBars/SmartBar.CommonEdit.cs b/Codist/SmartBars/SmartBar.CommonEdit.cs index e6cb9311..605b28b9 100644 --- a/Codist/SmartBars/SmartBar.CommonEdit.cs +++ b/Codist/SmartBars/SmartBar.CommonEdit.cs @@ -76,26 +76,29 @@ protected SnapshotSpan Replace(CommandContext ctx, Func replaceH return firstModified; } - /// Surround each span of the corrent selection with and , and optionally select the first modified span if is . + /// When selection is not surrounded with and , surround each span of the corrent selection with and , and optionally select the first modified span if is ; when surrounded, remove them. /// The new span after modification. If modification is unsuccessful, the default of is returned. - protected SnapshotSpan SurroundWith(CommandContext ctx, string prefix, string suffix, bool selectModified) { + protected static SnapshotSpan SurroundWith(CommandContext ctx, string prefix, string suffix, bool selectModified) { var firstModified = new SnapshotSpan(); var psLength = prefix.Length + suffix.Length; var removed = false; string t = null; ctx.KeepToolBar(false); using (var edit = ctx.View.TextSnapshot.TextBuffer.CreateEdit()) { - foreach (var item in View.Selection.SelectedSpans) { + foreach (var item in ctx.View.Selection.SelectedSpans) { t = item.GetText(); + // remove surrounding items if (t.Length > psLength && t.StartsWith(prefix, StringComparison.Ordinal) - && t.EndsWith(suffix, StringComparison.Ordinal)) { + && t.EndsWith(suffix, StringComparison.Ordinal) + && t.IndexOf(prefix, prefix.Length, t.Length - prefix.Length) <= t.IndexOf(suffix, prefix.Length, t.Length - psLength)) { if (edit.Replace(item, t.Substring(prefix.Length, t.Length - psLength)) && firstModified.Snapshot == null) { firstModified = item; removed = true; } } + // surround items else if (edit.Replace(item, prefix + t + suffix) && firstModified.Snapshot == null) { firstModified = item; } @@ -108,7 +111,7 @@ protected SnapshotSpan SurroundWith(CommandContext ctx, string prefix, string su FindNext(ctx, t); } else if (selectModified) { - View.SelectSpan(firstModified); + ctx.View.SelectSpan(firstModified); } } } @@ -306,26 +309,8 @@ static CommandItem[] GetSurroundingCommands() { TextEditorHelper.ExecuteEditorCommand("Edit.SurroundWith"); }), new CommandItem(KnownImageIds.MaskedTextBox, "Toggle Parentheses", ctx => { - if (ctx.View.TryGetFirstSelectionSpan(out var span) == false) { - return; - } - using (var ed = ctx.View.TextBuffer.CreateEdit()) { - var t = span.GetText(); - if (t.Length > 1 - && t[0] == '(' && t[t.Length - 1] == ')' - && t.IndexOf('(', 1, t.Length - 1) <= t.IndexOf(')', 1, t.Length - 2) - /*avoid toggling "(1 + 1)* (2 + 2)" to "1 + 1) * (2 + 2" */ - ) { - t = t.Substring(1, t.Length - 2); - } - else { - t = "(" + t + ")"; - } - if (ed.Replace(span, t)) { - ed.Apply(); - ctx.View.Selection.Select(new SnapshotSpan(ctx.View.TextSnapshot, span.Start, t.Length), false); - ctx.KeepToolBar(false); - } + if (ctx.View.TryGetFirstSelectionSpan(out var span)) { + SurroundWith(ctx, "(", ")", true); } }), };