From ad540e94cd9ea8301a5ddf7d254f4e1673eaa334 Mon Sep 17 00:00:00 2001 From: Heku Date: Wed, 10 Nov 2021 08:15:54 +0800 Subject: [PATCH 01/17] Translate, cleanup and bug fix (#854) * Translate a newly added line * Remove unused package references * Eliminate redundant output via package reference manner * Add missing references to fix unit tests build issue * Fix cleanup external files issue #748 --- CodeMaid.UnitTests/CodeMaid.UnitTests.csproj | 3 ++ CodeMaid.VS2022/CodeMaid.VS2022.csproj | 11 +----- CodeMaid/CodeMaid.csproj | 11 +----- CodeMaidShared/CodeMaidShared.projitems | 1 + CodeMaidShared/Helpers/TextDocumentHelper.cs | 25 ++++--------- CodeMaidShared/Helpers/UIThread.cs | 37 +++++++++++++++++++ .../Cleaning/CodeCleanupAvailabilityLogic.cs | 7 +++- .../CodeReorganizationAvailabilityLogic.cs | 7 +++- .../Properties/Resources.zh-Hans.resx | 2 +- 9 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 CodeMaidShared/Helpers/UIThread.cs diff --git a/CodeMaid.UnitTests/CodeMaid.UnitTests.csproj b/CodeMaid.UnitTests/CodeMaid.UnitTests.csproj index 9faa2cbb..23fc90cc 100644 --- a/CodeMaid.UnitTests/CodeMaid.UnitTests.csproj +++ b/CodeMaid.UnitTests/CodeMaid.UnitTests.csproj @@ -80,6 +80,9 @@ + + 16.10.31321.278 + 17.0.0 diff --git a/CodeMaid.VS2022/CodeMaid.VS2022.csproj b/CodeMaid.VS2022/CodeMaid.VS2022.csproj index bd38845e..10e8a1cc 100644 --- a/CodeMaid.VS2022/CodeMaid.VS2022.csproj +++ b/CodeMaid.VS2022/CodeMaid.VS2022.csproj @@ -723,21 +723,14 @@ 17.0.0-previews-2-31512-422 + runtime + all 17.0.3177-preview3 runtime; build; native; contentfiles; analyzers all - - 13.0.1 - - - 5.0.0 - - - 4.5.0 - diff --git a/CodeMaid/CodeMaid.csproj b/CodeMaid/CodeMaid.csproj index 8b156e10..e0ea7465 100644 --- a/CodeMaid/CodeMaid.csproj +++ b/CodeMaid/CodeMaid.csproj @@ -341,21 +341,14 @@ 16.10.31321.278 + runtime + all 16.11.35 runtime; build; native; contentfiles; analyzers all - - 13.0.1 - - - 5.0.0 - - - 4.5.0 - diff --git a/CodeMaidShared/CodeMaidShared.projitems b/CodeMaidShared/CodeMaidShared.projitems index 0b2472de..8fa3bac2 100644 --- a/CodeMaidShared/CodeMaidShared.projitems +++ b/CodeMaidShared/CodeMaidShared.projitems @@ -42,6 +42,7 @@ + diff --git a/CodeMaidShared/Helpers/TextDocumentHelper.cs b/CodeMaidShared/Helpers/TextDocumentHelper.cs index 0706f4f8..76378bb1 100644 --- a/CodeMaidShared/Helpers/TextDocumentHelper.cs +++ b/CodeMaidShared/Helpers/TextDocumentHelper.cs @@ -42,7 +42,7 @@ internal static class TextDocumentHelper internal static IEnumerable FindMatches(TextDocument textDocument, string patternString) { var matches = new List(); - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -69,7 +69,7 @@ internal static IEnumerable FindMatches(TextDocument textDocument, st internal static IEnumerable FindMatches(TextSelection textSelection, string patternString) { var matches = new List(); - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); if (TryGetTextBufferAt(textSelection.Parent.Parent.FullName, out ITextBuffer textBuffer)) @@ -95,7 +95,7 @@ internal static IEnumerable FindMatches(TextSelection textSelection, internal static EditPoint FirstOrDefaultMatch(TextDocument textDocument, string patternString) { EditPoint result = null; - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -125,7 +125,7 @@ internal static bool TryFindNextMatch(EditPoint startPoint, ref EditPoint endPoi bool result = false; EditPoint resultEndPoint = null; - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -154,7 +154,7 @@ internal static bool TryFindNextMatch(EditPoint startPoint, ref EditPoint endPoi internal static string GetTextToFirstMatch(TextPoint startPoint, string matchString) { string result = null; - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -299,7 +299,7 @@ internal static void SelectCodeItem(Document document, BaseCodeItem codeItem) /// The replacement string. internal static void SubstituteAllStringMatches(TextDocument textDocument, string patternString, string replacementString) { - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -320,7 +320,7 @@ internal static void SubstituteAllStringMatches(TextDocument textDocument, strin /// The replacement string. internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString) { - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -342,7 +342,7 @@ internal static void SubstituteAllStringMatches(TextSelection textSelection, str /// The replacement string. internal static void SubstituteAllStringMatches(EditPoint startPoint, EditPoint endPoint, string patternString, string replacementString) { - RunOnUIThread(() => + UIThread.Run(() => { ThreadHelper.ThrowIfNotOnUIThread(); @@ -440,15 +440,6 @@ private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable - { - await CodeMaidPackage.Instance.JoinableTaskFactory.SwitchToMainThreadAsync(); - action(); - }); - } - private static bool TryGetTextBufferAt(string filePath, out ITextBuffer textBuffer) { IVsWindowFrame windowFrame; diff --git a/CodeMaidShared/Helpers/UIThread.cs b/CodeMaidShared/Helpers/UIThread.cs new file mode 100644 index 00000000..df45539e --- /dev/null +++ b/CodeMaidShared/Helpers/UIThread.cs @@ -0,0 +1,37 @@ +using Microsoft.VisualStudio.Shell; +using System; + +namespace SteveCadwallader.CodeMaid.Helpers +{ + internal static class UIThread + { + public static void Run(Action action) + { + if (ThreadHelper.CheckAccess()) + { + action(); + } + else + { + ThreadHelper.JoinableTaskFactory.Run(async () => + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + action(); + }); + } + } + + public static T Run(Func func) + { + if (ThreadHelper.CheckAccess()) + { + return func(); + } + return ThreadHelper.JoinableTaskFactory.Run(async () => + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + return func(); + }); + } + } +} \ No newline at end of file diff --git a/CodeMaidShared/Logic/Cleaning/CodeCleanupAvailabilityLogic.cs b/CodeMaidShared/Logic/Cleaning/CodeCleanupAvailabilityLogic.cs index 4f9f4b54..21af739b 100644 --- a/CodeMaidShared/Logic/Cleaning/CodeCleanupAvailabilityLogic.cs +++ b/CodeMaidShared/Logic/Cleaning/CodeCleanupAvailabilityLogic.cs @@ -464,8 +464,11 @@ private static bool PromptUserAboutCleaningExternalFiles(Document document) CanRemember = true }; - var window = new YesNoPromptWindow { DataContext = viewModel }; - var response = window.ShowModal(); + var response = UIThread.Run(() => + { + var window = new YesNoPromptWindow { DataContext = viewModel }; + return window.ShowDialog(); + }); if (!response.HasValue) { diff --git a/CodeMaidShared/Logic/Reorganizing/CodeReorganizationAvailabilityLogic.cs b/CodeMaidShared/Logic/Reorganizing/CodeReorganizationAvailabilityLogic.cs index 7cb53f57..35976e8d 100644 --- a/CodeMaidShared/Logic/Reorganizing/CodeReorganizationAvailabilityLogic.cs +++ b/CodeMaidShared/Logic/Reorganizing/CodeReorganizationAvailabilityLogic.cs @@ -173,8 +173,11 @@ private static bool PromptUserAboutReorganizingPreprocessorConditionals(Document CanRemember = true }; - var window = new YesNoPromptWindow { DataContext = viewModel }; - var response = window.ShowModal(); + var response = UIThread.Run(() => + { + var window = new YesNoPromptWindow { DataContext = viewModel }; + return window.ShowDialog(); + }); if (!response.HasValue) { diff --git a/CodeMaidShared/Properties/Resources.zh-Hans.resx b/CodeMaidShared/Properties/Resources.zh-Hans.resx index c1b41679..f5c15d43 100644 --- a/CodeMaidShared/Properties/Resources.zh-Hans.resx +++ b/CodeMaidShared/Properties/Resources.zh-Hans.resx @@ -925,6 +925,6 @@ 您有挂起的更改。 是否要在继续之前保存它们? - Only for methods (TBT) + 仅应用于方法 \ No newline at end of file From dc2f7bca22a31ae60116ae1ad3e981255286dd90 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Tue, 9 Nov 2021 19:20:13 -0500 Subject: [PATCH 02/17] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09c112c8..4133393b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [ ] Features - [ ] Fixes + - [x] [#854](https://github.com/codecadwallader/codemaid/pull/854) - Translate new resource to Chinese, simplify build outputs and fix for prompt to clean external files when another dialog is already open - thanks [heku](https://github.com/heku)! ## Previous Releases From 59d7a8092ac421486009ec6a41486e99a91cdfc8 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Fri, 19 Nov 2021 08:37:50 -0500 Subject: [PATCH 03/17] Recognize WebForms as a language (#877) Added in VS2022 --- CodeMaidShared/Helpers/CodeLanguageHelper.cs | 68 +++++++++++++++----- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/CodeMaidShared/Helpers/CodeLanguageHelper.cs b/CodeMaidShared/Helpers/CodeLanguageHelper.cs index df35a159..2d9a9f77 100644 --- a/CodeMaidShared/Helpers/CodeLanguageHelper.cs +++ b/CodeMaidShared/Helpers/CodeLanguageHelper.cs @@ -14,27 +14,61 @@ internal static CodeLanguage GetCodeLanguage(string language) { switch (language) { - case "Basic": return CodeLanguage.VisualBasic; - case "CSharp": return CodeLanguage.CSharp; + case "Basic": + return CodeLanguage.VisualBasic; + + case "CSharp": + return CodeLanguage.CSharp; + case "C/C++": - case "C/C++ (VisualGDB)": return CodeLanguage.CPlusPlus; - case "CSS": return CodeLanguage.CSS; - case "F#": return CodeLanguage.FSharp; + case "C/C++ (VisualGDB)": + return CodeLanguage.CPlusPlus; + + case "CSS": + return CodeLanguage.CSS; + + case "F#": + return CodeLanguage.FSharp; + case "HTML": - case "HTMLX": return CodeLanguage.HTML; + case "HTMLX": + case "WebForms": + return CodeLanguage.HTML; + case "JavaScript": case "JScript": - case "Node.js": return CodeLanguage.JavaScript; - case "JSON": return CodeLanguage.JSON; - case "LESS": return CodeLanguage.LESS; - case "PHP": return CodeLanguage.PHP; - case "PowerShell": return CodeLanguage.PowerShell; - case "R": return CodeLanguage.R; - case "SCSS": return CodeLanguage.SCSS; - case "TypeScript": return CodeLanguage.TypeScript; - case "XAML": return CodeLanguage.XAML; - case "XML": return CodeLanguage.XML; - default: return CodeLanguage.Unknown; + case "Node.js": + return CodeLanguage.JavaScript; + + case "JSON": + return CodeLanguage.JSON; + + case "LESS": + return CodeLanguage.LESS; + + case "PHP": + return CodeLanguage.PHP; + + case "PowerShell": + return CodeLanguage.PowerShell; + + case "R": + return CodeLanguage.R; + + case "SCSS": + return CodeLanguage.SCSS; + + case "TypeScript": + return CodeLanguage.TypeScript; + + case "XAML": + return CodeLanguage.XAML; + + case "XML": + return CodeLanguage.XML; + + default: + return CodeLanguage.Unknown; } } } From ee2ac89fa1d82b5519b62d2a6d01fed83ef7f64f Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Fri, 19 Nov 2021 08:40:34 -0500 Subject: [PATCH 04/17] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4133393b..2b99d877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [ ] Fixes - [x] [#854](https://github.com/codecadwallader/codemaid/pull/854) - Translate new resource to Chinese, simplify build outputs and fix for prompt to clean external files when another dialog is already open - thanks [heku](https://github.com/heku)! + - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) ## Previous Releases From 77925e46c87886d1fab56cbe64455c69835742a8 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Tue, 30 Nov 2021 08:33:15 -0500 Subject: [PATCH 05/17] Add Razor to recognized code languages list (#885) --- CHANGELOG.md | 1 + CodeMaidShared/Helpers/CodeLanguageHelper.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b99d877..07a58a85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [ ] Fixes - [x] [#854](https://github.com/codecadwallader/codemaid/pull/854) - Translate new resource to Chinese, simplify build outputs and fix for prompt to clean external files when another dialog is already open - thanks [heku](https://github.com/heku)! - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) + - [x] [#880](https://github.com/codecadwallader/codemaid/pull/880) - Recognize Razor in VS2022 (API change) ## Previous Releases diff --git a/CodeMaidShared/Helpers/CodeLanguageHelper.cs b/CodeMaidShared/Helpers/CodeLanguageHelper.cs index 2d9a9f77..3c3abfa2 100644 --- a/CodeMaidShared/Helpers/CodeLanguageHelper.cs +++ b/CodeMaidShared/Helpers/CodeLanguageHelper.cs @@ -32,6 +32,7 @@ internal static CodeLanguage GetCodeLanguage(string language) case "HTML": case "HTMLX": + case "Razor": case "WebForms": return CodeLanguage.HTML; From bb3d309b7f868dc049a70a03de6e40be4ac1500a Mon Sep 17 00:00:00 2001 From: Ludovic Flender Date: Tue, 30 Nov 2021 14:41:47 +0100 Subject: [PATCH 06/17] Fixed multiple bugs on file headers update (#882) * Fixed header replacement management: if a header is already present and a new one is added in another place using the "Replace" option, then the old one is removed. e.g. if a header is present at document start and we add a new one after usings with the Replace option, the one at document start is now removed * Fixed bugs happening on file headers update: - header update on files with no header and no using is now working - header piece being sometimes inserted after header is now fixed * Fixed last bug and unit tests Co-authored-by: Ludovic Flender --- .../Helpers/FileHeaderHelperTests.cs | 10 ++--- CodeMaidShared/Helpers/FileHeaderHelper.cs | 37 +++++++++++++++++-- .../Logic/Cleaning/FileHeaderLogic.cs | 17 +++++++-- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/CodeMaid.UnitTests/Helpers/FileHeaderHelperTests.cs b/CodeMaid.UnitTests/Helpers/FileHeaderHelperTests.cs index cfd4d093..d904f261 100644 --- a/CodeMaid.UnitTests/Helpers/FileHeaderHelperTests.cs +++ b/CodeMaid.UnitTests/Helpers/FileHeaderHelperTests.cs @@ -1,4 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; using NUnit.Framework; using SteveCadwallader.CodeMaid.Helpers; using System; @@ -152,8 +152,8 @@ public void GetHeaderLengthMultiSingleLine(string tag, string text) Assert.IsTrue(headerLength == expectedLength, $"Expecting {expectedLength}, found {headerLength}"); } - [TestCase("//", "using System;\r\n// header\r\n", 11)] - [TestCase("//", "using System;\r\n// header I\r\n// header II\r\n", 26)] + [TestCase("//", "using System;\r\n// header\r\nnamespace ", 11)] + [TestCase("//", "using System;\r\n// header I\r\n// header II\r\nnamespace ", 26)] public void GetHeaderLengthMultiSingleLineSkipUsings(string tag, string text, int expectedLength) { var headerLength = FileHeaderHelper.GetHeaderLength(text, tag, true); @@ -174,7 +174,7 @@ public void GetHeaderLengthMultiSingleLineWithCode(string tag, string text, int [TestCase("//", "using System;\r\n\r\n// header \r\nnamespace System.Windows;\r\npublic class Test\r\n", 13)] [TestCase("//", "using EnvDTE;\r\nusing System;\r\nusing SteveCadwallader.CodeMaid.Helpers;\r\n\r\n\r\n// header \r\n// more header \r\nnamespace SteveCadwallader.CodeMaid;\r\n", 29)] - [TestCase("//", "using System;\r\n\r\n// header \r\n[assembly: AssemblyTitle(\"SteveCadwallader.CodeMaid.UnitTests\")]\r\n", 13)] + [TestCase("//", "using System;\r\n\r\n// header \r\n[assembly: AssemblyTitle(\"SteveCadwallader.CodeMaid.UnitTests\")]\r\nnamespace ", 13)] public void GetHeaderLengthMultiSingleLineWithCodeSkipUsings(string tag, string text, int expectedLength) { var headerLength = FileHeaderHelper.GetHeaderLength(text, tag, true); @@ -193,7 +193,7 @@ public void GetHeaderLengthMultiSingleLineWithEmptyLines(string tag, string text Assert.IsTrue(headerLength == expectedLength, $"Expecting {expectedLength}, found {headerLength}"); } - [TestCase("//", "using System;\r\n\r\n\r\n// header \r\n// header\r\nnamespace\r\n//not header\r\n public class Test\r\n", 23)] + [TestCase("//", "using System;\r\n\r\n\r\n// header \r\n// header\r\nnamespace \r\n//not header\r\n public class Test\r\n", 23)] [TestCase("//", "using EnvDTE;\r\nusing System;\r\nusing SteveCadwallader.CodeMaid.Helpers;\r\n// header \r\n// more header \r\n namespace System.Text;\r\n{\r\n", 29)] public void GetHeaderLengthMultiSingleLineWithEmptyLinesSkipUsings(string tag, string text, int expectedLength) { diff --git a/CodeMaidShared/Helpers/FileHeaderHelper.cs b/CodeMaidShared/Helpers/FileHeaderHelper.cs index 7efe3bad..aa0a57b6 100644 --- a/CodeMaidShared/Helpers/FileHeaderHelper.cs +++ b/CodeMaidShared/Helpers/FileHeaderHelper.cs @@ -1,4 +1,4 @@ -using EnvDTE; +using EnvDTE; using SteveCadwallader.CodeMaid.Properties; using SteveCadwallader.CodeMaid.UI.Enumerations; using System; @@ -182,6 +182,12 @@ private static int GetHeaderLength(string text, string commentSyntax) header.AddRange(GetLinesStartingWith(commentSyntax, lines, header.Count)); var nbChar = 0; + + if (header.Count() == 0) + { + return 0; + } + header.ToList().ForEach(x => nbChar += x.Length + 1); return nbChar; @@ -218,6 +224,12 @@ private static int GetHeaderLength(string text, string commentSyntaxStart, strin } var nbChar = 0; + + if (header.Count() == 0) + { + return 0; + } + header.ToList().ForEach(x => nbChar += x.Length + 1); return nbChar; @@ -234,7 +246,7 @@ private static int GetHeaderLengthSkipUsings(string text, string commentSyntax) var nbChar = 0; header.ToList().ForEach(x => nbChar += x.Length + 1); - return nbChar + 1; + return nbChar == 0 ? 0 : nbChar + 1; } private static int GetHeaderLengthSkipUsings(string text, string commentSyntaxStart, string commentSyntaxEnd) @@ -251,6 +263,12 @@ private static int GetHeaderLengthSkipUsings(string text, string commentSyntaxSt var header = text.Substring(startIndex, endIndex - startIndex); var nbNewLines = Regex.Matches(header, Environment.NewLine).Count; + + if (header.Length == 0 && nbNewLines == 0) + { + return 0; + } + return header.Length + commentSyntaxEnd.Length - nbNewLines + 1; } @@ -307,13 +325,24 @@ private static string SkipUsings(string document) var startIndex = 0; var lastUsingIndex = 0; - while (startIndex < namespaceIndex && startIndex++ != -1) + while (startIndex < namespaceIndex) { lastUsingIndex = startIndex; startIndex = document.IndexOf("using ", startIndex); + + if (startIndex++ == -1) + { + break; + } + } + + var afterUsingIndex = 0; + + if (lastUsingIndex > 0) + { + afterUsingIndex = document.IndexOf($"{Environment.NewLine}", lastUsingIndex) + 1; } - var afterUsingIndex = document.IndexOf($"{Environment.NewLine}", lastUsingIndex) + 1; return document.Substring(afterUsingIndex).TrimStart(); } diff --git a/CodeMaidShared/Logic/Cleaning/FileHeaderLogic.cs b/CodeMaidShared/Logic/Cleaning/FileHeaderLogic.cs index eed68ebd..c9d19992 100644 --- a/CodeMaidShared/Logic/Cleaning/FileHeaderLogic.cs +++ b/CodeMaidShared/Logic/Cleaning/FileHeaderLogic.cs @@ -144,9 +144,8 @@ private void InsertFileHeaderAfterUsings(TextDocument textDocument, string setti Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(); var currentHeader = GetCurrentHeader(textDocument, true).Trim(); - var newHeader = settingsFileHeader.Trim(); - if (string.Equals(currentHeader, newHeader)) + if (currentHeader.StartsWith(settingsFileHeader.Trim())) { return; } @@ -156,6 +155,11 @@ private void InsertFileHeaderAfterUsings(TextDocument textDocument, string setti headerBlockStart.MoveToLineAndOffset(nbLinesToSkip + 1, 1); + if (!settingsFileHeader.StartsWith(Environment.NewLine)) + { + settingsFileHeader = Environment.NewLine + settingsFileHeader; + } + headerBlockStart.Insert(settingsFileHeader); } @@ -166,7 +170,7 @@ private void InsertFileHeaderDocumentStart(TextDocument textDocument, string set var cursor = textDocument.StartPoint.CreateEditPoint(); var existingFileHeader = cursor.GetText(settingsFileHeader.Length); - if (!existingFileHeader.StartsWith(settingsFileHeader.TrimStart())) + if (!existingFileHeader.StartsWith(settingsFileHeader.Trim())) { cursor.Insert(settingsFileHeader); } @@ -192,10 +196,12 @@ private void ReplaceFileHeader(TextDocument textDocument, string settingsFileHea switch (FileHeaderHelper.GetFileHeaderPositionFromSettings(textDocument)) { case HeaderPosition.DocumentStart: + ReplaceFileHeaderAfterUsings(textDocument, string.Empty); // Removes header after usings if present ReplaceFileHeaderDocumentStart(textDocument, settingsFileHeader); return; case HeaderPosition.AfterUsings: + ReplaceFileHeaderDocumentStart(textDocument, string.Empty); // Removes header at document start if present ReplaceFileHeaderAfterUsings(textDocument, settingsFileHeader); return; @@ -229,6 +235,11 @@ private void ReplaceFileHeaderAfterUsings(TextDocument textDocument, string sett var currentHeaderLength = GetHeaderLength(textDocument, true); + if (!settingsFileHeader.StartsWith(Environment.NewLine)) + { + settingsFileHeader = Environment.NewLine + settingsFileHeader; + } + headerBlockStart.ReplaceText(currentHeaderLength, settingsFileHeader, (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers); } From ad23d33d29e4a6db7008dd2e016025ed002e02ec Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Tue, 30 Nov 2021 08:43:11 -0500 Subject: [PATCH 07/17] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07a58a85..e0597ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [x] [#854](https://github.com/codecadwallader/codemaid/pull/854) - Translate new resource to Chinese, simplify build outputs and fix for prompt to clean external files when another dialog is already open - thanks [heku](https://github.com/heku)! - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) - [x] [#880](https://github.com/codecadwallader/codemaid/pull/880) - Recognize Razor in VS2022 (API change) + - [x] [#882](https://github.com/codecadwallader/codemaid/pull/882) - Fixed multiple bugs on file headers update - thanks [lflender](https://github.com/lflender)! ## Previous Releases From 83c554d586425b4cdd3906d4d9b71a8f376a4e34 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Tue, 30 Nov 2021 08:43:38 -0500 Subject: [PATCH 08/17] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0597ed8..9d43b5a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,8 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [ ] Fixes - [x] [#854](https://github.com/codecadwallader/codemaid/pull/854) - Translate new resource to Chinese, simplify build outputs and fix for prompt to clean external files when another dialog is already open - thanks [heku](https://github.com/heku)! - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) - - [x] [#880](https://github.com/codecadwallader/codemaid/pull/880) - Recognize Razor in VS2022 (API change) - [x] [#882](https://github.com/codecadwallader/codemaid/pull/882) - Fixed multiple bugs on file headers update - thanks [lflender](https://github.com/lflender)! + - [x] [#885](https://github.com/codecadwallader/codemaid/pull/885) - Recognize Razor in VS2022 (API change) ## Previous Releases From e85e61d5a76bbafca92d0c83e355099bd30e63f4 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Sun, 6 Feb 2022 16:17:31 -0500 Subject: [PATCH 09/17] Provide off-by-one offset for LineCharOffset. (#907) Resolves #902 --- CodeMaidShared/Helpers/TextDocumentHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeMaidShared/Helpers/TextDocumentHelper.cs b/CodeMaidShared/Helpers/TextDocumentHelper.cs index 76378bb1..d941dba8 100644 --- a/CodeMaidShared/Helpers/TextDocumentHelper.cs +++ b/CodeMaidShared/Helpers/TextDocumentHelper.cs @@ -404,7 +404,7 @@ private static int GetSnapshotPositionForTextPoint(ITextSnapshot textSnapshot, T ThreadHelper.ThrowIfNotOnUIThread(); var textSnapshotLine = textSnapshot.GetLineFromLineNumber(textPoint.Line - 1); - return textSnapshotLine.Start.Position + textPoint.LineCharOffset; + return textSnapshotLine.Start.Position + textPoint.LineCharOffset - 1; } private static Span GetSnapshotSpanForExtent(ITextSnapshot textSnapshot, EditPoint startPoint, EditPoint endPoint) From cbf2819b2c7572d9f26f5f93f03637c4e9cfb0fb Mon Sep 17 00:00:00 2001 From: Chris Blyth Date: Thu, 17 Feb 2022 13:11:39 +0000 Subject: [PATCH 10/17] Update GetTextToFirstMatch to use the start point as a basis for finding match (#905) * Update GetTextToFirstMatch to use the start point as a basis for finding match (#867) * Include more "endings" for stuct and properties to handle inline definition without `{` --- CodeMaidShared/Helpers/CodeElementHelper.cs | 4 ++-- CodeMaidShared/Helpers/TextDocumentHelper.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CodeMaidShared/Helpers/CodeElementHelper.cs b/CodeMaidShared/Helpers/CodeElementHelper.cs index 2a07405e..11d6a353 100644 --- a/CodeMaidShared/Helpers/CodeElementHelper.cs +++ b/CodeMaidShared/Helpers/CodeElementHelper.cs @@ -184,7 +184,7 @@ internal static string GetPropertyDeclaration(CodeProperty codeProperty) ? codeProperty.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter) : codeProperty.StartPoint; - return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"\{"); + return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"[\{;]"); } /// @@ -197,7 +197,7 @@ internal static string GetStructDeclaration(CodeStruct codeStruct) // Get the start point after the attributes. var startPoint = codeStruct.GetStartPoint(vsCMPart.vsCMPartHeader); - return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"\{"); + return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"[\{;]"); } } } \ No newline at end of file diff --git a/CodeMaidShared/Helpers/TextDocumentHelper.cs b/CodeMaidShared/Helpers/TextDocumentHelper.cs index d941dba8..03abb05c 100644 --- a/CodeMaidShared/Helpers/TextDocumentHelper.cs +++ b/CodeMaidShared/Helpers/TextDocumentHelper.cs @@ -161,9 +161,9 @@ internal static string GetTextToFirstMatch(TextPoint startPoint, string matchStr if (TryGetTextBufferAt(startPoint.Parent.Parent.FullName, out ITextBuffer textBuffer)) { IFinder finder = GetFinder(matchString, textBuffer); - if (finder.TryFind(out Span match)) + if (finder.TryFind(GetSnapshotPositionForTextPoint(textBuffer.CurrentSnapshot, startPoint), out Span match)) { - result = textBuffer.CurrentSnapshot.GetText(match); + result = textBuffer.CurrentSnapshot.GetText(startPoint.AbsoluteCharOffset, match.Start - startPoint.AbsoluteCharOffset); } } }); From e8ec23f96e17dcbea87197b05ed92bef9e9816b3 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Thu, 17 Feb 2022 08:13:52 -0500 Subject: [PATCH 11/17] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d43b5a8..f9131e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) - [x] [#882](https://github.com/codecadwallader/codemaid/pull/882) - Fixed multiple bugs on file headers update - thanks [lflender](https://github.com/lflender)! - [x] [#885](https://github.com/codecadwallader/codemaid/pull/885) - Recognize Razor in VS2022 (API change) + - [x] [#905](https://github.com/codecadwallader/codemaid/pull/905) - Fix issue causing keyword modifiers to be swapped - thanks [BlythMeister](https://github.com/BluthMeister)! ## Previous Releases From 7fb6ce85821c77c3de1a2e679138751347d82827 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Thu, 17 Feb 2022 08:14:18 -0500 Subject: [PATCH 12/17] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9131e34..25467c2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [x] [#877](https://github.com/codecadwallader/codemaid/pull/877) - Recognize WebForms in VS2022 (API change) - [x] [#882](https://github.com/codecadwallader/codemaid/pull/882) - Fixed multiple bugs on file headers update - thanks [lflender](https://github.com/lflender)! - [x] [#885](https://github.com/codecadwallader/codemaid/pull/885) - Recognize Razor in VS2022 (API change) - - [x] [#905](https://github.com/codecadwallader/codemaid/pull/905) - Fix issue causing keyword modifiers to be swapped - thanks [BlythMeister](https://github.com/BluthMeister)! + - [x] [#905](https://github.com/codecadwallader/codemaid/pull/905) - Fix issue causing keyword modifiers to be swapped - thanks [BlythMeister](https://github.com/BlythMeister)! ## Previous Releases From da2a2f880d2839cf7ea16cdf08ebf90253f7175d Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Thu, 23 Jun 2022 08:39:54 -0400 Subject: [PATCH 13/17] Add some UI padding per #801 (#929) --- .../UI/Dialogs/CleanupProgress/CleanupProgressWindow.xaml | 2 +- CodeMaidShared/UI/Dialogs/Prompts/YesNoPromptWindow.xaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CodeMaidShared/UI/Dialogs/CleanupProgress/CleanupProgressWindow.xaml b/CodeMaidShared/UI/Dialogs/CleanupProgress/CleanupProgressWindow.xaml index 1d633341..77a4c8aa 100644 --- a/CodeMaidShared/UI/Dialogs/CleanupProgress/CleanupProgressWindow.xaml +++ b/CodeMaidShared/UI/Dialogs/CleanupProgress/CleanupProgressWindow.xaml @@ -10,7 +10,7 @@ d:DataContext="{d:DesignInstance local:CleanupProgressViewModel, IsDesignTimeCreatable=False}" Icon="/source.extension.ico" Title="{x:Static p:Resources.CodeMaidCleanupProgress}" TextElement.FontFamily="{Binding General_Font, Source={x:Static p:Settings.Default}}" - Height="180" Width="400" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" + Height="190" Width="400" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" ui:WindowAttachedProperties.DialogResult="{Binding DialogResult}" Closing="OnClosing" FocusManager.FocusedElement="{Binding ElementName=cancelButton}"> diff --git a/CodeMaidShared/UI/Dialogs/Prompts/YesNoPromptWindow.xaml b/CodeMaidShared/UI/Dialogs/Prompts/YesNoPromptWindow.xaml index 8a9d0f7b..50cc7f7e 100644 --- a/CodeMaidShared/UI/Dialogs/Prompts/YesNoPromptWindow.xaml +++ b/CodeMaidShared/UI/Dialogs/Prompts/YesNoPromptWindow.xaml @@ -11,7 +11,7 @@ d:DataContext="{d:DesignInstance local:YesNoPromptViewModel, IsDesignTimeCreatable=False}" Icon="/source.extension.ico" Title="{Binding Title}" TextElement.FontFamily="{Binding General_Font, Source={x:Static p:Settings.Default}}" - Height="180" Width="400" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" + Height="190" Width="400" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" ui:WindowAttachedProperties.DialogResult="{Binding DialogResult}" FocusManager.FocusedElement="{Binding ElementName=noButton}"> From 2164afa5ab55a50e2541f5fb68565664ad12363d Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Thu, 23 Jun 2022 08:41:28 -0400 Subject: [PATCH 14/17] Update CHANGELOG.md --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25467c2e..5018a26d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ These changes have not been released to the Visual Studio marketplace, but (if c - [x] [#882](https://github.com/codecadwallader/codemaid/pull/882) - Fixed multiple bugs on file headers update - thanks [lflender](https://github.com/lflender)! - [x] [#885](https://github.com/codecadwallader/codemaid/pull/885) - Recognize Razor in VS2022 (API change) - [x] [#905](https://github.com/codecadwallader/codemaid/pull/905) - Fix issue causing keyword modifiers to be swapped - thanks [BlythMeister](https://github.com/BlythMeister)! + - [x] [#929](https://github.com/codecadwallader/codemaid/pull/929) - Add some UI padding ## Previous Releases @@ -31,7 +32,6 @@ These are the changes to each version that has been released to the Visual Studi - [x] Fixes - [x] [#800](https://github.com/codecadwallader/codemaid/pull/800) - Reorganizing: Fix dialog showing literal newline characters - ## 11.2 **2021-01-02** @@ -85,7 +85,7 @@ These are the changes to each version that has been released to the Visual Studi - [x] [#519](https://github.com/codecadwallader/codemaid/pull/519) - Simplify the code by removing unnecessary guids - thanks [heku](https://github.com/heku)! - [x] [#525](https://github.com/codecadwallader/codemaid/pull/525) - Make all features switchable - thanks [heku](https://github.com/heku)! - [x] [#545](https://github.com/codecadwallader/codemaid/pull/545) - Ignore comment lines starting with certain prefixes - thanks [willemduncan](https://github.com/willemduncan)! - + - [x] Fixes - [x] [#479](https://github.com/codecadwallader/codemaid/pull/479) - Update XAML Styler integration mappings - thanks [grochocki](https://github.com/grochocki)! - [x] [#496](https://github.com/codecadwallader/codemaid/pull/496) - Fix the .NET Framework minimum required version (which is v4.6) @@ -186,4 +186,4 @@ These are the changes to each version that has been released to the Visual Studi - [x] #193 - Added undo transaction to insert region command (thanks Matthias Reitinger!) - [x] Remove multithread operations performance option that was root of problems like #212 -### CHANGELOG started with v0.9.1, see [Blog](http://www.codemaid.net/news/) for deeper history +### CHANGELOG started with v0.9.1, see [Blog](http://www.codemaid.net/news/) for deeper history \ No newline at end of file From 3c852c1657ad4bac32b4a03bed4bba041f1bede9 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Thu, 10 Nov 2022 08:22:54 -0500 Subject: [PATCH 15/17] Use an alternate LazyThreadSafetyMode per recommendation. (#951) --- CodeMaidShared/Model/CodeItems/BaseCodeItemElement.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CodeMaidShared/Model/CodeItems/BaseCodeItemElement.cs b/CodeMaidShared/Model/CodeItems/BaseCodeItemElement.cs index c2b0459b..595e4081 100644 --- a/CodeMaidShared/Model/CodeItems/BaseCodeItemElement.cs +++ b/CodeMaidShared/Model/CodeItems/BaseCodeItemElement.cs @@ -1,6 +1,7 @@ using EnvDTE; using SteveCadwallader.CodeMaid.Helpers; using System; +using System.Threading; namespace SteveCadwallader.CodeMaid.Model.CodeItems { @@ -122,7 +123,7 @@ public override void RefreshCachedPositionAndName() /// A lazy initializer for the specified function. protected static Lazy LazyTryDefault(Func func) { - return new Lazy(() => TryDefault(func)); + return new Lazy(() => TryDefault(func), LazyThreadSafetyMode.PublicationOnly); } /// From c25e452e635f29ce5a064788cffa2a65b3a62753 Mon Sep 17 00:00:00 2001 From: Steve Cadwallader Date: Sat, 22 Jul 2023 17:30:01 -0400 Subject: [PATCH 16/17] Multi-bind on IsVisibile to bypass a WPF leak issue (#968) --- CodeMaidShared/CodeMaidShared.projitems | 1 + .../UI/Converters/BooleanAndConverter.cs | 67 +++++++++++++++++++ .../BuildProgress/BuildProgressView.xaml | 21 +++--- 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 CodeMaidShared/UI/Converters/BooleanAndConverter.cs diff --git a/CodeMaidShared/CodeMaidShared.projitems b/CodeMaidShared/CodeMaidShared.projitems index 8fa3bac2..a9033ee2 100644 --- a/CodeMaidShared/CodeMaidShared.projitems +++ b/CodeMaidShared/CodeMaidShared.projitems @@ -159,6 +159,7 @@ True + diff --git a/CodeMaidShared/UI/Converters/BooleanAndConverter.cs b/CodeMaidShared/UI/Converters/BooleanAndConverter.cs new file mode 100644 index 00000000..d1aef97a --- /dev/null +++ b/CodeMaidShared/UI/Converters/BooleanAndConverter.cs @@ -0,0 +1,67 @@ +using System; +using System.Globalization; +using System.Linq; +using System.Windows.Data; + +namespace SteveCadwallader.CodeMaid.UI.Converters +{ + /// + /// A converter that performs a logical AND on all values. + /// + public class BooleanAndConverter : IMultiValueConverter + { + /// + /// The default . + /// + public static BooleanAndConverter Default = new BooleanAndConverter(); + + /// + /// Converts source values to a value for the binding target. The data binding engine calls + /// this method when it propagates the values from source bindings to the binding target. + /// + /// + /// The array of values that the source bindings in the produces. The value indicates that the source + /// binding has no value to provide for conversion. + /// + /// The type of the binding target property. + /// The converter parameter to use. + /// The culture to use in the converter. + /// + /// A converted value.If the method returns null, the valid null value is used.A return + /// value of . indicates that the converter + /// did not produce a value, and that the binding will use the if it is available, or else + /// will use the default value.A return value of . indicates that the binding + /// does not transfer the value or use the or the default value. + /// + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + if (values == null || values.Length < 1) return null; + + return values.All(x => (bool)x); + } + + /// + /// Converts a binding target value to the source binding values. + /// + /// The value that the binding target produces. + /// + /// The array of types to convert to. The array length indicates the number and types of + /// values that are suggested for the method to return. + /// + /// The converter parameter to use. + /// The culture to use in the converter. + /// + /// An array of values that have been converted from the target value back to the source values. + /// + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/CodeMaidShared/UI/ToolWindows/BuildProgress/BuildProgressView.xaml b/CodeMaidShared/UI/ToolWindows/BuildProgress/BuildProgressView.xaml index aa6efaa4..8d60c732 100644 --- a/CodeMaidShared/UI/ToolWindows/BuildProgress/BuildProgressView.xaml +++ b/CodeMaidShared/UI/ToolWindows/BuildProgress/BuildProgressView.xaml @@ -1,10 +1,10 @@ @@ -13,8 +13,13 @@ - + + + + + + +