From e51b6722bf00401b6312ac33d8a85a63fc7586f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Fri, 28 Apr 2023 17:21:07 +0200 Subject: [PATCH 1/6] Fix rare bug that inserted additional newlines when duplicating lines --- HotCommands/Commands/DuplicateSelection.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/HotCommands/Commands/DuplicateSelection.cs b/HotCommands/Commands/DuplicateSelection.cs index 01ebb8e..ac77976 100644 --- a/HotCommands/Commands/DuplicateSelection.cs +++ b/HotCommands/Commands/DuplicateSelection.cs @@ -65,22 +65,15 @@ public static int HandleCommand(IWpfTextView textView, IClassifier classifier, { SnapshotSpan selectionSpan = selection.Extent.SnapshotSpan; bool isDuplicateLinesForThisSelection = isDuplicateLines || selectionSpan.Length == 0; // When selection length is zero we treat it as duplicate lines command (or should we not?). - - SnapshotSpan duplicationSpan; - bool isMissingNewLine; - if (isDuplicateLinesForThisSelection) - { - duplicationSpan = GetContainingLines(selectionSpan); - isMissingNewLine = duplicationSpan.End == edit.Snapshot.Length; - } - else - { - duplicationSpan = selectionSpan; - isMissingNewLine = false; - } + + SnapshotSpan duplicationSpan = isDuplicateLinesForThisSelection ? + GetContainingLines(selectionSpan) : selectionSpan; string textToInsert = duplicationSpan.GetText(); + bool isMissingNewLine = isDuplicateLinesForThisSelection ? + !textToInsert.EndsWith(Environment.NewLine) : false; // Should only be true if we want to duplicate the last line in the document (there is no trailing new-line there). + SnapshotPoint insertPos; if (isReversed) { From b894f7abae707983c50dae7016f0c2450c1dde99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Fri, 28 Apr 2023 17:21:39 +0200 Subject: [PATCH 2/6] Fix commands not being available sometimes (e.g. in .xml files) --- HotCommands/HotCommandsCommandFilter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HotCommands/HotCommandsCommandFilter.cs b/HotCommands/HotCommandsCommandFilter.cs index ef8dc9c..a2f234f 100644 --- a/HotCommands/HotCommandsCommandFilter.cs +++ b/HotCommands/HotCommandsCommandFilter.cs @@ -105,6 +105,7 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int case Constants.ToggleCommentCmdId: case Constants.ExpandSelectionCmdId: case Constants.FormatCodeCmdId: + case Constants.DuplicateLinesUpCmdId: case Constants.DuplicateLinesDownCmdId: case Constants.DuplicateSelectionCmdId: case Constants.DuplicateSelectionReverseCmdId: @@ -112,7 +113,7 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int case Constants.MoveMemberDownCmdId: case Constants.GoToPreviousMemberCmdId: case Constants.GoToNextMemberCmdId: - prgCmds[0].cmdf |= (uint)OLECMDF.OLECMDF_ENABLED; + prgCmds[0].cmdf |= (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED); return VSConstants.S_OK; } } From 79cfed9ca4a43bc45c69a4d05479d0d822ba1840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Fri, 28 Apr 2023 17:21:53 +0200 Subject: [PATCH 3/6] Bump version --- HotCommands/source.extension.vsixmanifest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HotCommands/source.extension.vsixmanifest b/HotCommands/source.extension.vsixmanifest index 154fbf7..5b183d3 100644 --- a/HotCommands/source.extension.vsixmanifest +++ b/HotCommands/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + Hot Commands A collection of commands and shortcuts for enhanced productivity in Visual Studio IDE. VS2022+ https://marketplace.visualstudio.com/items?itemName=JustinClareburtMSFT.HotCommandsforVisualStudio From e592f30f9c01999d1e6422c7ba540a5a21abca50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Sat, 29 Apr 2023 12:42:10 +0200 Subject: [PATCH 4/6] Fix C# exclusive commands being enabled for non-C# content types --- HotCommands/HotCommandsCommandFilter.cs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/HotCommands/HotCommandsCommandFilter.cs b/HotCommands/HotCommandsCommandFilter.cs index a2f234f..fbd38bb 100644 --- a/HotCommands/HotCommandsCommandFilter.cs +++ b/HotCommands/HotCommandsCommandFilter.cs @@ -100,22 +100,35 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int // Command handling registration if (pguidCmdGroup == Constants.HotCommandsGuid && cCmds == 1) { + const uint supportedAndEnabledStatus = (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED); + + string contentTypeName = textView.TextBuffer.ContentType.TypeName; + + uint commandStatus = default; switch (prgCmds[0].cmdID) { - case Constants.ToggleCommentCmdId: - case Constants.ExpandSelectionCmdId: - case Constants.FormatCodeCmdId: + // Any content commands case Constants.DuplicateLinesUpCmdId: case Constants.DuplicateLinesDownCmdId: case Constants.DuplicateSelectionCmdId: case Constants.DuplicateSelectionReverseCmdId: + commandStatus = supportedAndEnabledStatus; + break; + + // CSharp content commands + case Constants.ToggleCommentCmdId: + case Constants.ExpandSelectionCmdId: + case Constants.FormatCodeCmdId: case Constants.MoveMemberUpCmdId: case Constants.MoveMemberDownCmdId: case Constants.GoToPreviousMemberCmdId: case Constants.GoToNextMemberCmdId: - prgCmds[0].cmdf |= (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED); - return VSConstants.S_OK; + commandStatus = contentTypeName == "CSharp" ? supportedAndEnabledStatus : default; + break; } + + prgCmds[0].cmdf |= commandStatus; + return VSConstants.S_OK; } if (Next != null) From cb33f4af934811e0633e8837fd97fb801854e6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Sat, 29 Apr 2023 13:11:50 +0200 Subject: [PATCH 5/6] Remove reliance on Environment.NewLine to determine if duplication span is missing a new-line. --- HotCommands/Commands/DuplicateSelection.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/HotCommands/Commands/DuplicateSelection.cs b/HotCommands/Commands/DuplicateSelection.cs index ac77976..0bb2e42 100644 --- a/HotCommands/Commands/DuplicateSelection.cs +++ b/HotCommands/Commands/DuplicateSelection.cs @@ -66,14 +66,22 @@ public static int HandleCommand(IWpfTextView textView, IClassifier classifier, SnapshotSpan selectionSpan = selection.Extent.SnapshotSpan; bool isDuplicateLinesForThisSelection = isDuplicateLines || selectionSpan.Length == 0; // When selection length is zero we treat it as duplicate lines command (or should we not?). - SnapshotSpan duplicationSpan = isDuplicateLinesForThisSelection ? - GetContainingLines(selectionSpan) : selectionSpan; + SnapshotSpan duplicationSpan; + bool isMissingNewLine; // Should only be true when duplicating the last line in the document. + if (isDuplicateLinesForThisSelection) + { + duplicationSpan = GetContainingLines(selectionSpan); + isMissingNewLine = duplicationSpan.End == edit.Snapshot.Length + && edit.Snapshot.GetLineFromLineNumber(edit.Snapshot.LineCount - 1).Length > 0; + } + else + { + duplicationSpan = selectionSpan; + isMissingNewLine = false; + } string textToInsert = duplicationSpan.GetText(); - bool isMissingNewLine = isDuplicateLinesForThisSelection ? - !textToInsert.EndsWith(Environment.NewLine) : false; // Should only be true if we want to duplicate the last line in the document (there is no trailing new-line there). - SnapshotPoint insertPos; if (isReversed) { From 3f2fc7d46991464c6655875d6bb865585ed543c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20Foghammar=20N=C3=B5mtak?= Date: Sun, 30 Apr 2023 00:34:38 +0200 Subject: [PATCH 6/6] Enable ToggleComment command for 'Code' content types --- HotCommands/HotCommandsCommandFilter.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/HotCommands/HotCommandsCommandFilter.cs b/HotCommands/HotCommandsCommandFilter.cs index fbd38bb..e51848e 100644 --- a/HotCommands/HotCommandsCommandFilter.cs +++ b/HotCommands/HotCommandsCommandFilter.cs @@ -102,12 +102,12 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int { const uint supportedAndEnabledStatus = (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED); - string contentTypeName = textView.TextBuffer.ContentType.TypeName; + var contentType = textView.TextBuffer.ContentType; uint commandStatus = default; switch (prgCmds[0].cmdID) { - // Any content commands + // 'Any' content commands case Constants.DuplicateLinesUpCmdId: case Constants.DuplicateLinesDownCmdId: case Constants.DuplicateSelectionCmdId: @@ -115,15 +115,19 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int commandStatus = supportedAndEnabledStatus; break; - // CSharp content commands + // 'Code' content commands case Constants.ToggleCommentCmdId: + commandStatus = contentType.IsOfType("Code") ? supportedAndEnabledStatus : default; + break; + + // 'CSharp' content commands case Constants.ExpandSelectionCmdId: case Constants.FormatCodeCmdId: case Constants.MoveMemberUpCmdId: case Constants.MoveMemberDownCmdId: case Constants.GoToPreviousMemberCmdId: case Constants.GoToNextMemberCmdId: - commandStatus = contentTypeName == "CSharp" ? supportedAndEnabledStatus : default; + commandStatus = contentType.IsOfType("CSharp") ? supportedAndEnabledStatus : default; break; }