├── .gitignore ├── AdjustNamespace.2022 ├── AdjustNamespace.2022.csproj ├── Monikers.imagemanifest ├── Properties │ └── AssemblyInfo.cs ├── Resources │ ├── order16.png │ ├── order512.png │ └── order90.png ├── VSCommandTable.cs ├── VSCommandTable.vsct ├── source.extension.cs └── source.extension.vsixmanifest ├── AdjustNamespace.VsixShared ├── AdjustNamespace.VsixShared.projitems ├── AdjustNamespace.VsixShared.shproj ├── AdjustNamespacePackage.cs ├── Adjusting │ ├── Adjuster │ │ ├── AdjusterFactory.cs │ │ ├── Cs │ │ │ └── RefProcessor.cs │ │ ├── CsAdjuster.cs │ │ ├── IAdjuster.cs │ │ └── XamlAdjuster.cs │ ├── Cleanup.cs │ └── Fixer │ │ ├── FixerContainer.cs │ │ ├── FixerSet.cs │ │ ├── IFixer.cs │ │ └── Specific │ │ ├── AddUsingFixer.cs │ │ ├── NamespaceFixer.cs │ │ └── QualifiedNameFixer.cs ├── Command │ ├── AdjustNamespaceCommand.cs │ ├── AdjustSelectedCommand.cs │ ├── AdjustSolutionCommand.cs │ └── EditSkippedPathsCommand.cs ├── Helper │ ├── BaseViewModel.cs │ ├── ChainViewModel.cs │ ├── DocumentChangerHelper.cs │ ├── HashSetHelper.cs │ ├── ListHelper.cs │ ├── NameHelper.cs │ ├── NamespaceHelper.cs │ ├── RoslynHelper.cs │ ├── SolutionHelper.cs │ ├── WorkspaceHelper.cs │ └── WpfHelper.cs ├── ImageMonikers.cs ├── LICENSE.txt ├── Logging.cs ├── Namespace │ ├── NamespaceCenter.cs │ ├── NamespaceTransition.cs │ └── NamespaceTransitionContainer.cs ├── Options │ └── General.cs ├── Predicate.cs ├── RelayCommand.cs ├── Settings │ ├── AdjustNamespaceSettings.cs │ ├── AdjustNamespaceSettings2.cs │ └── SettingsReader.cs ├── TypeContainer.cs ├── UI │ ├── Control │ │ ├── PerformingUserControl.xaml │ │ ├── PerformingUserControl.xaml.cs │ │ ├── PreparationUserControl.xaml │ │ ├── PreparationUserControl.xaml.cs │ │ ├── SelectedUserControl.xaml │ │ ├── SelectedUserControl.xaml.cs │ │ └── SpaceKeyUpEventTrigger.cs │ ├── FileEx.cs │ ├── StepFactory │ │ ├── IStepFactory.cs │ │ ├── PerformingStepFactory.cs │ │ ├── PreparationStepFactory.cs │ │ └── SelectedStepFactory.cs │ ├── Theme.cs │ └── ViewModel │ │ ├── PerformingParameters.cs │ │ ├── PerformingViewModel.cs │ │ ├── PreparationStepViewModel.cs │ │ ├── Select │ │ ├── ISelectItemViewModel.cs │ │ ├── SelectFileViewModel.cs │ │ ├── SelectFolderViewModel.cs │ │ └── SelectedStepViewModel.cs │ │ └── SelectedStepParameters.cs ├── VsServices.cs ├── Window │ ├── AdjustNamespaceWindow.xaml │ ├── AdjustNamespaceWindow.xaml.cs │ ├── EditSkippedPathsWindow.xaml │ └── EditSkippedPathsWindow.xaml.cs └── Xaml │ ├── BodyProvider │ ├── ClosedXamlBodyProvider.cs │ ├── IXamlBodyProvider.cs │ └── OpenedXamlBodyProvider.cs │ ├── Positioned │ ├── IXamlPerformable.cs │ ├── IXamlPositioned.cs │ ├── XamlAttributeReference.cs │ ├── XamlClass.cs │ ├── XamlControl.cs │ ├── XamlX.cs │ └── XamlXmlns.cs │ ├── XamlDocument.cs │ ├── XamlEngine.cs │ └── XamlStructure.cs ├── AdjustNamespace.sln ├── LICENSE ├── README.md ├── Tests └── Standard │ ├── DatabaseProject │ ├── ClassFile1.cs │ ├── DatabaseProject.sqlproj │ └── MyFolder │ │ └── ClassFile2.cs │ ├── TestMauiApp │ ├── App.xaml │ ├── App.xaml.cs │ ├── AppShell.xaml │ ├── AppShell.xaml.cs │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── MauiProgram.cs │ ├── Platforms │ │ ├── Android │ │ │ ├── AndroidManifest.xml │ │ │ ├── MainActivity.cs │ │ │ ├── MainApplication.cs │ │ │ └── Resources │ │ │ │ └── values │ │ │ │ └── colors.xml │ │ ├── MacCatalyst │ │ │ ├── AppDelegate.cs │ │ │ ├── Info.plist │ │ │ └── Program.cs │ │ ├── Tizen │ │ │ ├── Main.cs │ │ │ └── tizen-manifest.xml │ │ ├── Windows │ │ │ ├── App.xaml │ │ │ ├── App.xaml.cs │ │ │ ├── Package.appxmanifest │ │ │ └── app.manifest │ │ └── iOS │ │ │ ├── AppDelegate.cs │ │ │ ├── Info.plist │ │ │ └── Program.cs │ ├── Properties │ │ └── launchSettings.json │ ├── Resources │ │ ├── AppIcon │ │ │ ├── appicon.svg │ │ │ └── appiconfg.svg │ │ ├── Fonts │ │ │ ├── OpenSans-Regular.ttf │ │ │ └── OpenSans-Semibold.ttf │ │ ├── Images │ │ │ └── dotnet_bot.svg │ │ ├── Raw │ │ │ └── AboutAssets.txt │ │ ├── Splash │ │ │ └── splash.svg │ │ └── Styles │ │ │ ├── Colors.xaml │ │ │ └── Styles.xaml │ └── TestMauiApp.csproj │ ├── TestProject │ ├── A │ │ └── B │ │ │ └── C │ │ │ └── D │ │ │ └── Class1.cs │ ├── Exp1 │ │ ├── Class123.cs │ │ ├── Class4.cs │ │ ├── Class5.cs │ │ └── Class67.cs │ ├── Exp10 │ │ ├── MyClass.cs │ │ ├── Nested.cs │ │ └── Typed.cs │ ├── Exp11 │ │ ├── Folder1 │ │ │ └── Nested.cs │ │ └── Folder2 │ │ │ ├── MyClass12.cs │ │ │ └── Typed12.cs │ ├── Exp12 │ │ ├── Class1 │ │ │ └── Class1.cs │ │ └── Class2.cs │ ├── Exp13 │ │ ├── Class1.cs │ │ └── Exp13 │ │ │ └── Exp13.cs │ ├── Exp2 │ │ ├── Class1.cs │ │ └── Class2.cs │ ├── Exp3 │ │ ├── Class1.cs │ │ └── Class23456.cs │ ├── Exp4 │ │ ├── Class1.cs │ │ └── Class23.cs │ ├── Exp5 │ │ ├── Class1.cs │ │ ├── UserControl1.xaml │ │ ├── UserControl1.xaml.cs │ │ ├── UserControl2.xaml │ │ ├── UserControl2.xaml.cs │ │ ├── UserControl3.xaml │ │ ├── UserControl3.xaml.cs │ │ ├── UserControl4.xaml │ │ ├── UserControl4.xaml.cs │ │ ├── UserControl5.xaml │ │ ├── UserControl5.xaml.cs │ │ ├── UserControl6.xaml │ │ └── UserControl6.xaml.cs │ ├── Exp6 │ │ ├── Class1234.cs │ │ └── Class5.cs │ ├── Exp7 │ │ └── Class1.cs │ ├── Exp8 │ │ └── FakeAttribute.cs │ ├── Exp9 │ │ └── Class100.cs │ ├── Program.cs │ └── TestProject.csproj │ ├── TestSharedProject │ ├── SharedClass1.cs │ ├── SharedFolder │ │ └── SharedClass2.cs │ ├── TestSharedProject.projitems │ └── TestSharedProject.shproj │ ├── TestSolution.sln │ └── adjust_namespaces_settings.xml ├── demo1.png ├── demo2.png └── demo3.png /AdjustNamespace.2022/Monikers.imagemanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /AdjustNamespace.2022/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace; 2 | using System.Reflection; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle(Vsix.Name)] 6 | [assembly: AssemblyDescription(Vsix.Description)] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany(Vsix.Author)] 9 | [assembly: AssemblyProduct(Vsix.Name)] 10 | [assembly: AssemblyCopyright(Vsix.Author)] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: AssemblyVersion(Vsix.Version)] 17 | [assembly: AssemblyFileVersion(Vsix.Version)] 18 | 19 | [assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\Microsoft.Xaml.Behaviors.dll")] 20 | 21 | namespace System.Runtime.CompilerServices 22 | { 23 | public class IsExternalInit { } 24 | } -------------------------------------------------------------------------------- /AdjustNamespace.2022/Resources/order16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lsoft/AdjustNamespace/23be192e706275cc0b66203999431052d3e170f7/AdjustNamespace.2022/Resources/order16.png -------------------------------------------------------------------------------- /AdjustNamespace.2022/Resources/order512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lsoft/AdjustNamespace/23be192e706275cc0b66203999431052d3e170f7/AdjustNamespace.2022/Resources/order512.png -------------------------------------------------------------------------------- /AdjustNamespace.2022/Resources/order90.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lsoft/AdjustNamespace/23be192e706275cc0b66203999431052d3e170f7/AdjustNamespace.2022/Resources/order90.png -------------------------------------------------------------------------------- /AdjustNamespace.2022/VSCommandTable.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This file was generated by VSIX Synchronizer 4 | // 5 | // ------------------------------------------------------------------------------ 6 | namespace AdjustNamespace 7 | { 8 | using System; 9 | 10 | /// 11 | /// Helper class that exposes all GUIDs used across VS Package. 12 | /// 13 | internal sealed partial class PackageGuids 14 | { 15 | public const string guidAdjustNamespacePackageString = "19967834-2536-4a51-82f7-e607434734d2"; 16 | public static Guid guidAdjustNamespacePackage = new Guid(guidAdjustNamespacePackageString); 17 | 18 | public const string guidAdjustNamespacePackageCmdSetString = "3f7538ed-5c20-4d49-89fc-c401bb76df25"; 19 | public static Guid guidAdjustNamespacePackageCmdSet = new Guid(guidAdjustNamespacePackageCmdSetString); 20 | 21 | public const string guidImagesString = "6309e701-5f02-4bc7-8376-02ec55028dbf"; 22 | public static Guid guidImages = new Guid(guidImagesString); 23 | } 24 | /// 25 | /// Helper class that encapsulates all CommandIDs uses across VS Package. 26 | /// 27 | internal sealed partial class PackageIds 28 | { 29 | public const int ContextMenuGroup = 0x1020; 30 | public const int ExtensionMenuGroup = 0x201D; 31 | public const int ExtensionSubMenuGroup = 0x201E; 32 | public const int ExtensionSubMenu = 0x201F; 33 | public const int ExtensionSubMenuGroupService = 0x2020; 34 | public const int DoAdjustCommandId = 0x0300; 35 | public const int AdjustSolutionCommandId = 0x0301; 36 | public const int AdjustSelectedCommandId = 0x0302; 37 | public const int EditSkippedPathsCommandId = 0x0322; 38 | public const int order16 = 0x0001; 39 | } 40 | } -------------------------------------------------------------------------------- /AdjustNamespace.2022/VSCommandTable.vsct: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | Adjust Namespace 44 | 45 | 46 | 47 | 48 | 49 | 50 | 57 | 58 | 65 | 66 | 73 | 74 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /AdjustNamespace.2022/source.extension.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------ 2 | // 3 | // This file was generated by VSIX Synchronizer 4 | // 5 | // ------------------------------------------------------------------------------ 6 | namespace AdjustNamespace 7 | { 8 | internal sealed partial class Vsix 9 | { 10 | public const string Id = "AdjustNamespace.5744262e-0521-4f72-a2d6-aa7d60d8ea96"; 11 | public const string Name = "Adjust C# Namespaces 2022"; 12 | public const string Description = @"Fix incorrect namespaces for a single file, folder, project or a whole solution and rules the resulting regressions in the code (including XAML), e.g. fixes the broken references. This extension works like Resharper `Adjust namespaces` function."; 13 | public const string Language = "en-US"; 14 | public const string Version = "0.3.4"; 15 | public const string Author = "lsoft"; 16 | public const string Tags = "namespace fix C#"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /AdjustNamespace.2022/source.extension.vsixmanifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Adjust C# Namespaces 2022 6 | Fix incorrect namespaces for a single file, folder, project or a whole solution and rules the resulting regressions in the code (including XAML), e.g. fixes the broken references. This extension works like Resharper `Adjust namespaces` function. 7 | https://github.com/lsoft/AdjustNamespace 8 | LICENSE.txt 9 | Resources\order90.png 10 | Resources\order90.png 11 | namespace fix C# 12 | true 13 | 14 | 15 | 16 | amd64 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/AdjustNamespace.VsixShared.shproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | b8f4de5d-eeff-4f14-9c3d-2a6ee41db482 5 | 14.0 6 | 9.0 7 | enable 8 | nullable;CS8766;CS8767 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/AdjustNamespacePackage.cs: -------------------------------------------------------------------------------- 1 | global using Community.VisualStudio.Toolkit; 2 | global using Microsoft.VisualStudio.Shell; 3 | global using System; 4 | global using Task = System.Threading.Tasks.Task; 5 | using AdjustNamespace.Command; 6 | using System.Runtime.InteropServices; 7 | using System.Threading; 8 | 9 | namespace AdjustNamespace 10 | { 11 | [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] 12 | [InstalledProductRegistration(Vsix.Name, Vsix.Description, Vsix.Version)] 13 | [ProvideMenuResource("Menus.ctmenu", 1)] 14 | [Guid(PackageGuids.guidAdjustNamespacePackageString)] 15 | [ProvideOptionPage(typeof(Options.OptionsProvider.GeneralOptions), "Adjust Namespaces", "General", 0, 0, true, SupportsProfiles = true)] 16 | public sealed class AdjustNamespacePackage : ToolkitPackage 17 | { 18 | protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) 19 | { 20 | await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); 21 | 22 | await AdjustNamespaceCommand.InitializeAsync(this); 23 | await AdjustSolutionCommand.InitializeAsync(this); 24 | await AdjustSelectedCommand.InitializeAsync(this); 25 | await EditSkippedPathsCommand.InitializeAsync(this); 26 | 27 | await this.RegisterCommandsAsync(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Adjuster/AdjusterFactory.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Helper; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | 5 | namespace AdjustNamespace.Adjusting.Adjuster 6 | { 7 | public class AdjusterFactory 8 | { 9 | private readonly VsServices _vss; 10 | private readonly NamespaceReplaceRegex _replaceRegex; 11 | private readonly bool _openFilesToEnableUndo; 12 | private readonly NamespaceCenter _namespaceCenter; 13 | private readonly List _xamlFilePaths; 14 | 15 | public static async Task CreateAsync( 16 | VsServices vss, 17 | NamespaceReplaceRegex replaceRegex, 18 | bool openFilesToEnableUndo, 19 | NamespaceCenter namespaceCenter 20 | ) 21 | { 22 | if (replaceRegex is null) 23 | { 24 | throw new ArgumentNullException(nameof(replaceRegex)); 25 | } 26 | 27 | if (namespaceCenter is null) 28 | { 29 | throw new ArgumentNullException(nameof(namespaceCenter)); 30 | } 31 | 32 | //get all xaml files in current solution 33 | var filePaths = await SolutionHelper.GetAllFilesFromAsync(); 34 | var xamlFilePaths = filePaths.FindAll(fp => fp.EndsWith(".xaml")); 35 | 36 | return new AdjusterFactory( 37 | vss, 38 | replaceRegex, 39 | openFilesToEnableUndo, 40 | namespaceCenter, 41 | xamlFilePaths 42 | ); 43 | 44 | } 45 | 46 | private AdjusterFactory( 47 | VsServices vss, 48 | NamespaceReplaceRegex replaceRegex, 49 | bool openFilesToEnableUndo, 50 | NamespaceCenter namespaceCenter, 51 | List xamlFilePaths 52 | ) 53 | { 54 | if (replaceRegex is null) 55 | { 56 | throw new ArgumentNullException(nameof(replaceRegex)); 57 | } 58 | 59 | if (namespaceCenter is null) 60 | { 61 | throw new ArgumentNullException(nameof(namespaceCenter)); 62 | } 63 | 64 | if (xamlFilePaths is null) 65 | { 66 | throw new ArgumentNullException(nameof(xamlFilePaths)); 67 | } 68 | 69 | _vss = vss; 70 | _replaceRegex = replaceRegex; 71 | _openFilesToEnableUndo = openFilesToEnableUndo; 72 | _namespaceCenter = namespaceCenter; 73 | _xamlFilePaths = xamlFilePaths; 74 | } 75 | 76 | public async Task CreateAsync( 77 | string subjectFilePath 78 | ) 79 | { 80 | if (subjectFilePath is null) 81 | { 82 | throw new ArgumentNullException(nameof(subjectFilePath)); 83 | } 84 | 85 | var pii = await SolutionHelper.TryGetProjectItemAsync(subjectFilePath); 86 | if (!pii.HasValue) 87 | { 88 | return null; 89 | } 90 | 91 | var targetNamespace = await NamespaceHelper.TryDetermineTargetNamespaceAsync( 92 | pii.Value.Project, 93 | _vss, 94 | _replaceRegex, 95 | subjectFilePath 96 | ); 97 | if (string.IsNullOrEmpty(targetNamespace)) 98 | { 99 | return null; 100 | } 101 | 102 | if (subjectFilePath.EndsWith(".xaml")) 103 | { 104 | //it's a xaml 105 | 106 | var xamlAdjuster = new XamlAdjuster( 107 | _vss, 108 | _openFilesToEnableUndo, 109 | subjectFilePath, 110 | targetNamespace! 111 | ); 112 | return xamlAdjuster; 113 | } 114 | else 115 | { 116 | //we can do nothing with not a C# documents 117 | var subjectDocument = _vss.Workspace.GetDocument(subjectFilePath); 118 | if (!subjectDocument.IsDocumentInScope()) 119 | { 120 | return null; 121 | } 122 | 123 | var csAdjuster = new CsAdjuster( 124 | _vss, 125 | _openFilesToEnableUndo, 126 | _namespaceCenter, 127 | subjectFilePath, 128 | targetNamespace!, 129 | _xamlFilePaths 130 | ); 131 | 132 | return csAdjuster; 133 | } 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Adjuster/IAdjuster.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | 6 | namespace AdjustNamespace.Adjusting.Adjuster 7 | { 8 | public interface IAdjuster 9 | { 10 | Task AdjustAsync(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Adjuster/XamlAdjuster.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Adjusting.Adjuster; 2 | using AdjustNamespace.Xaml; 3 | using System; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | 7 | namespace AdjustNamespace.Adjusting 8 | { 9 | /// 10 | /// Adjuster for xaml file. 11 | /// 12 | public class XamlAdjuster : IAdjuster 13 | { 14 | private readonly VsServices _vss; 15 | private readonly string _subjectFilePath; 16 | private readonly string _targetNamespace; 17 | private readonly bool _openFilesToEnableUndo; 18 | 19 | public XamlAdjuster( 20 | VsServices vss, 21 | bool openFilesToEnableUndo, 22 | string subjectFilePath, 23 | string targetNamespace 24 | ) 25 | { 26 | if (subjectFilePath is null) 27 | { 28 | throw new ArgumentNullException(nameof(subjectFilePath)); 29 | } 30 | 31 | if (targetNamespace is null) 32 | { 33 | throw new ArgumentNullException(nameof(targetNamespace)); 34 | } 35 | 36 | _vss = vss; 37 | _openFilesToEnableUndo = openFilesToEnableUndo; 38 | _subjectFilePath = subjectFilePath; 39 | _targetNamespace = targetNamespace; 40 | } 41 | 42 | public async Task AdjustAsync() 43 | { 44 | var (xamlDocument, modifiedDocument) = await TryModifyDocumentAsync(); 45 | if (!modifiedDocument.HasValue) 46 | { 47 | return false; 48 | } 49 | 50 | modifiedDocument.Value.SaveIfChangesExistsAgainst(xamlDocument); 51 | 52 | return true; 53 | } 54 | 55 | public async Task IsChangesExistsAsync( 56 | ) 57 | { 58 | var (xamlDocument, modifiedDocument) = await TryModifyDocumentAsync(); 59 | return modifiedDocument.HasValue && modifiedDocument.Value.IsChangesExists(xamlDocument); 60 | } 61 | 62 | private async Task<(XamlDocument, XamlDocument?)> TryModifyDocumentAsync( 63 | ) 64 | { 65 | var xamlEngine = new XamlEngine( 66 | _vss 67 | ); 68 | 69 | var xamlDocument = await xamlEngine.CreateDocumentAsync( 70 | _openFilesToEnableUndo, 71 | _subjectFilePath 72 | ); 73 | 74 | if (!xamlDocument.GetRootInfo(out var rootNamespace, out var rootName)) 75 | { 76 | return (xamlDocument, null); 77 | } 78 | 79 | if (rootNamespace == _targetNamespace) 80 | { 81 | return (xamlDocument, null); 82 | } 83 | 84 | var modifiedXamlDocument = xamlDocument.MoveObject( 85 | rootNamespace!, 86 | rootName!, 87 | _targetNamespace 88 | ); 89 | 90 | return (xamlDocument, modifiedXamlDocument); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Cleanup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis.CSharp.Syntax; 2 | using Microsoft.CodeAnalysis; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using AdjustNamespace.Helper; 8 | using System.Threading; 9 | 10 | namespace AdjustNamespace.Adjusting 11 | { 12 | public class Cleanup 13 | { 14 | private readonly VsServices _vss; 15 | private readonly NamespaceCenter _namespaceCenter; 16 | 17 | public Cleanup( 18 | VsServices vss, 19 | NamespaceCenter namespaceCenter 20 | ) 21 | { 22 | if (namespaceCenter is null) 23 | { 24 | throw new ArgumentNullException(nameof(namespaceCenter)); 25 | } 26 | 27 | _vss = vss; 28 | _namespaceCenter = namespaceCenter; 29 | } 30 | 31 | public async Task RemoveEmptyUsingStatementsForAsync( 32 | string documentFilePath 33 | ) 34 | { 35 | var workspace = _vss.Workspace; 36 | 37 | bool r = true; 38 | do 39 | { 40 | var (document, syntaxRoot) = await workspace.GetDocumentAndSyntaxRootAsync(documentFilePath); 41 | if (document == null || syntaxRoot == null) 42 | { 43 | //something went wrong 44 | //skip this document 45 | return; 46 | } 47 | 48 | var namespaces = syntaxRoot.GetAllDescendants(); 49 | 50 | var toRemove = _namespaceCenter.GetRemovedNamespaces(namespaces); 51 | if (toRemove.Count == 0) 52 | { 53 | continue; 54 | } 55 | 56 | syntaxRoot = syntaxRoot.RemoveNodes(toRemove, SyntaxRemoveOptions.KeepNoTrivia); 57 | if (syntaxRoot != null) 58 | { 59 | var changedDocument = document.WithSyntaxRoot(syntaxRoot); 60 | 61 | r = workspace.TryApplyChanges(changedDocument.Project.Solution); 62 | } 63 | } 64 | while (!r); 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/FixerContainer.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Adjusting.Fixer; 2 | using AdjustNamespace.Adjusting; 3 | using Microsoft.VisualStudio.LanguageServices; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using Microsoft.VisualStudio.Language.CodeCleanUp; 9 | using System.Diagnostics; 10 | 11 | namespace AdjustNamespace.Adjusting.Fixer 12 | { 13 | /// 14 | /// Container for all fixers we produce. 15 | /// 16 | public class FixerContainer 17 | { 18 | private readonly VsServices _vss; 19 | private readonly bool _openFilesToEnableUndo; 20 | 21 | private readonly Dictionary _dict = new(); 22 | 23 | public FixerContainer( 24 | VsServices vss, 25 | bool openFilesToEnableUndo 26 | ) 27 | { 28 | _vss = vss; 29 | _openFilesToEnableUndo = openFilesToEnableUndo; 30 | } 31 | 32 | public T Fixer(string filePath) 33 | where T : IFixer 34 | { 35 | if (!_dict.TryGetValue(filePath, out var fixerSet)) 36 | { 37 | fixerSet = AddFixersFor(filePath); 38 | } 39 | 40 | return fixerSet.Fixer(); 41 | } 42 | 43 | public async Task FixAllAsync() 44 | { 45 | foreach (var pair in _dict) 46 | { 47 | var targetFilePath = pair.Key; 48 | 49 | Debug.WriteLine($"Fix references in {targetFilePath}"); 50 | 51 | await pair.Value.FixAllAsync(); 52 | } 53 | } 54 | 55 | private FixerSet AddFixersFor(string filePath) 56 | { 57 | var fs = new FixerSet(_vss, _openFilesToEnableUndo, filePath); 58 | _dict[filePath] = fs; 59 | return fs; 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/FixerSet.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Adjusting.Fixer.Specific; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace AdjustNamespace.Adjusting.Fixer 7 | { 8 | /// 9 | /// All fixer for specific file. 10 | /// 11 | public readonly struct FixerSet 12 | { 13 | private readonly VsServices _vss; 14 | private readonly bool _openFilesToEnableUndo; 15 | 16 | public readonly string FilePath; 17 | 18 | private readonly List _fixers; 19 | 20 | public FixerSet( 21 | VsServices vss, 22 | bool openFilesToEnableUndo, 23 | string filePath 24 | ) 25 | { 26 | if (filePath is null) 27 | { 28 | throw new ArgumentNullException(nameof(filePath)); 29 | } 30 | 31 | _vss = vss; 32 | _openFilesToEnableUndo = openFilesToEnableUndo; 33 | FilePath = filePath; 34 | 35 | _fixers = new List 36 | { 37 | new QualifiedNameFixer(vss.Workspace, filePath), 38 | new AddUsingFixer(vss.Workspace, filePath), 39 | new NamespaceFixer(vss, filePath), 40 | }; 41 | } 42 | 43 | public T Fixer() 44 | where T : IFixer 45 | { 46 | var result = _fixers.FirstOrDefault(f => f is T); 47 | if (result == null) 48 | { 49 | throw new InvalidOperationException($"Unknown fixer {typeof(T).FullName}"); 50 | } 51 | 52 | return (T)result; 53 | } 54 | 55 | internal async System.Threading.Tasks.Task FixAllAsync() 56 | { 57 | if (_openFilesToEnableUndo) 58 | { 59 | await _vss.OpenFileAsync(FilePath); 60 | } 61 | 62 | foreach (var fixer in _fixers) 63 | { 64 | await fixer.FixAsync(); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/IFixer.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AdjustNamespace.Adjusting.Fixer 4 | { 5 | public interface IFixer 6 | { 7 | public string FilePath 8 | { 9 | get; 10 | } 11 | 12 | Task FixAsync(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/Specific/AddUsingFixer.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Helper; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.CSharp; 4 | using Microsoft.CodeAnalysis.CSharp.Syntax; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Diagnostics; 8 | using System.Linq; 9 | using System.Threading.Tasks; 10 | 11 | namespace AdjustNamespace.Adjusting.Fixer 12 | { 13 | /// 14 | /// Fixer for adding a using statements like using System.Threading.Tasks 15 | /// 16 | public class AddUsingFixer : IFixer 17 | { 18 | private readonly Workspace _workspace; 19 | private readonly HashSet _symbolTargetNamespaces = new (); 20 | 21 | public string FilePath 22 | { 23 | get; 24 | } 25 | 26 | public AddUsingFixer( 27 | Workspace workspace, 28 | string filePath 29 | ) 30 | { 31 | if (workspace is null) 32 | { 33 | throw new ArgumentNullException(nameof(workspace)); 34 | } 35 | 36 | if (filePath is null) 37 | { 38 | throw new ArgumentNullException(nameof(filePath)); 39 | } 40 | 41 | _workspace = workspace; 42 | FilePath = filePath; 43 | } 44 | 45 | 46 | public void AddSubject(string symbolTargetNamespace) 47 | { 48 | if (symbolTargetNamespace is null) 49 | { 50 | throw new ArgumentNullException(nameof(symbolTargetNamespace)); 51 | } 52 | 53 | _symbolTargetNamespaces.Add(symbolTargetNamespace); 54 | } 55 | 56 | 57 | public async Task FixAsync() 58 | { 59 | bool r; 60 | do 61 | { 62 | var (document, syntaxRoot) = await _workspace.GetDocumentAndSyntaxRootAsync(FilePath); 63 | if (document == null || syntaxRoot == null) 64 | { 65 | //skip this document 66 | return; 67 | } 68 | 69 | foreach (var symbolTargetNamespace in _symbolTargetNamespaces) 70 | { 71 | var usingSyntaxes = syntaxRoot 72 | .DescendantNodes() 73 | .OfType() 74 | .ToList(); 75 | 76 | if (usingSyntaxes.Count > 0) 77 | { 78 | if (usingSyntaxes.Any(s => s.Name.ToString() == symbolTargetNamespace)) 79 | { 80 | //that using already exists 81 | continue; 82 | } 83 | } 84 | 85 | Debug.WriteLine($"Fix references in {FilePath}: Add '{symbolTargetNamespace}' "); 86 | 87 | if (usingSyntaxes.Count > 0) 88 | { 89 | var lastUsing = usingSyntaxes.Last(); 90 | 91 | syntaxRoot = syntaxRoot.InsertNodesAfter( 92 | lastUsing, 93 | new[] 94 | { 95 | SyntaxFactory.UsingDirective( 96 | SyntaxFactory.ParseName( 97 | " " + symbolTargetNamespace 98 | ) 99 | ).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed) 100 | .WithLeadingTrivia(lastUsing.GetLeadingTrivia()) 101 | }); 102 | } 103 | else 104 | { 105 | var cus = (CompilationUnitSyntax)syntaxRoot; 106 | var modifiedcus = cus.AddUsings( 107 | SyntaxFactory.UsingDirective( 108 | SyntaxFactory.ParseName( 109 | " " + symbolTargetNamespace 110 | ) 111 | ).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed) 112 | .WithLeadingTrivia(cus.GetLeadingTrivia()) 113 | ); 114 | 115 | syntaxRoot = modifiedcus; 116 | } 117 | } 118 | 119 | var changedDocument = document.WithSyntaxRoot(syntaxRoot); 120 | r = _workspace.TryApplyChanges(changedDocument.Project.Solution); 121 | } 122 | while (!r); 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/Specific/NamespaceFixer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis.CSharp.Syntax; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | using AdjustNamespace.Helper; 8 | using System.Linq; 9 | using Microsoft.CodeAnalysis; 10 | using AdjustNamespace.Namespace; 11 | 12 | namespace AdjustNamespace.Adjusting.Fixer.Specific 13 | { 14 | /// 15 | /// A fixer for editing namespace clauses to the correct one. 16 | /// 17 | public class NamespaceFixer : IFixer 18 | { 19 | private readonly VsServices _vss; 20 | private readonly List _subjectList = new(); 21 | 22 | public string FilePath 23 | { 24 | get; 25 | } 26 | 27 | public NamespaceFixer( 28 | VsServices vss, 29 | string filePath 30 | ) 31 | { 32 | _vss = vss; 33 | FilePath = filePath; 34 | } 35 | 36 | public void AddSubject(NamespaceTransitionContainer ntc) 37 | { 38 | _subjectList.Add(ntc); 39 | } 40 | 41 | public async Task FixAsync() 42 | { 43 | var workspace = _vss.Workspace; 44 | 45 | foreach (var ntc in _subjectList) 46 | { 47 | foreach (var transition in ntc.Transitions.Where(ni => ni.IsRoot)) 48 | { 49 | bool r = true; 50 | do 51 | { 52 | var (openedDocument, syntaxRoot) = await workspace.GetDocumentAndSyntaxRootAsync(FilePath); 53 | if (openedDocument == null || syntaxRoot == null) 54 | { 55 | //something went wrong 56 | //skip this document 57 | return; 58 | } 59 | 60 | if (!syntaxRoot.TryFindNamespaceNodesFor(transition.OriginalName, out var ufNamespaces)) 61 | { 62 | //skip this namespace 63 | break; 64 | } 65 | 66 | var ufNamespace = ufNamespaces.First(); 67 | 68 | //class A : IA {} 69 | //we're moving A into a different namespace, but IA are not. 70 | //we need to insert 'using old namespace' 71 | //otherwise ia will not be resolved 72 | 73 | //we can't determite it is the case or it's not without a costly analysis 74 | //it's a subject for a future work 75 | //so add at 100% cases now 76 | 77 | var cus = syntaxRoot as CompilationUnitSyntax; 78 | if (cus == null) 79 | { 80 | //skip this namespace 81 | break; 82 | } 83 | var newUsingStatement = SyntaxFactory.UsingDirective( 84 | SyntaxFactory.ParseName( 85 | " " + ufNamespace!.Name 86 | ) 87 | ).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); 88 | 89 | cus = cus.AddUsings(newUsingStatement); 90 | 91 | if (!cus.TryFindNamespaceNodesFor(transition.OriginalName, out var fNamespaces)) 92 | { 93 | //skip this namespace 94 | break; 95 | } 96 | 97 | foreach (var fNamespace in fNamespaces!) 98 | { 99 | var newName = SyntaxFactory.ParseName( 100 | transition.ModifiedName 101 | ); 102 | 103 | if (fNamespace is NamespaceDeclarationSyntax) 104 | { 105 | newName = newName 106 | .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed) 107 | ; 108 | } 109 | 110 | var fixedNamespace = fNamespace!.WithName( 111 | newName 112 | ) 113 | .WithLeadingTrivia(fNamespace.GetLeadingTrivia()) 114 | .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed) 115 | ; 116 | 117 | cus = cus!.ReplaceNode( 118 | fNamespace, 119 | fixedNamespace 120 | ); 121 | } 122 | 123 | openedDocument = openedDocument.WithSyntaxRoot(cus!); 124 | 125 | r = workspace.TryApplyChanges(openedDocument.Project.Solution); 126 | } 127 | while (!r); 128 | } 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Adjusting/Fixer/Specific/QualifiedNameFixer.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Helper; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.CSharp.Syntax; 4 | using Microsoft.CodeAnalysis.Text; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace AdjustNamespace.Adjusting.Fixer 11 | { 12 | /// 13 | /// Fixer for fully qualified names like AdjustNamespace.Adjusting.Fixer.QualifiedNameFixer 14 | /// 15 | public class QualifiedNameFixer : IFixer 16 | { 17 | private readonly Workspace _workspace; 18 | private readonly List _arguments = new (); 19 | 20 | public string FilePath 21 | { 22 | get; 23 | } 24 | 25 | public QualifiedNameFixer( 26 | Workspace workspace, 27 | string filePath 28 | ) 29 | { 30 | if (workspace is null) 31 | { 32 | throw new ArgumentNullException(nameof(workspace)); 33 | } 34 | 35 | if (filePath is null) 36 | { 37 | throw new ArgumentNullException(nameof(filePath)); 38 | } 39 | 40 | _workspace = workspace; 41 | FilePath = filePath; 42 | } 43 | 44 | public void AddSubject(QualifiedNameFixerArgument qnsa) 45 | { 46 | _arguments.Add(qnsa); 47 | } 48 | 49 | public async Task FixAsync() 50 | { 51 | await _workspace.ApplyModifiedDocumentAsync( 52 | FilePath, 53 | (document, syntaxRoot) => 54 | { 55 | var nodesToBeReplaced = _arguments.ConvertAll( 56 | a => syntaxRoot.FindNode(a.SubjectNodeSpan).GoDownTo(a.ToReplaceSyntax.GetType())! 57 | ); 58 | 59 | var mSyntaxRoot = syntaxRoot.ReplaceNodes( 60 | nodesToBeReplaced, 61 | (n0, n1) => 62 | { 63 | var founda = _arguments.First(a => n0.Span == a.SubjectNodeSpan); 64 | 65 | return founda.ToReplaceSyntax; 66 | }); 67 | 68 | var changedDocument = document.WithSyntaxRoot(mSyntaxRoot); 69 | return changedDocument; 70 | } 71 | ); 72 | } 73 | 74 | public readonly struct QualifiedNameFixerArgument 75 | { 76 | public readonly TextSpan SubjectNodeSpan; 77 | public readonly SyntaxNode ToReplaceSyntax; 78 | 79 | public QualifiedNameFixerArgument( 80 | TextSpan subjectNodeSpan, 81 | SyntaxNode toReplaceSyntax 82 | ) 83 | { 84 | if (toReplaceSyntax is null) 85 | { 86 | throw new ArgumentNullException(nameof(toReplaceSyntax)); 87 | } 88 | 89 | SubjectNodeSpan = subjectNodeSpan; 90 | ToReplaceSyntax = toReplaceSyntax; 91 | } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Command/AdjustSelectedCommand.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Helper; 2 | using AdjustNamespace.Window; 3 | using Microsoft.VisualStudio.Shell; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.ComponentModel.Design; 7 | 8 | namespace AdjustNamespace.Command 9 | { 10 | /// 11 | /// Command handler 12 | /// 13 | internal sealed class AdjustSelectedCommand 14 | { 15 | public static string ProjectKind = "{52AEFF70-BBD8-11d2-8598-006097C68E81}"; 16 | 17 | 18 | /// 19 | /// Command ID. 20 | /// 21 | public const int CommandId = PackageIds.AdjustSelectedCommandId; 22 | 23 | /// 24 | /// Command menu group (command set GUID). 25 | /// 26 | public static readonly Guid CommandSet = new Guid("3f7538ed-5c20-4d49-89fc-c401bb76df25"); 27 | 28 | /// 29 | /// VS Package that provides this command, not null. 30 | /// 31 | private readonly AsyncPackage package; 32 | 33 | /// 34 | /// Initializes a new instance of the class. 35 | /// Adds our command handlers for menu (commands must exist in the command table file) 36 | /// 37 | /// Owner package, not null. 38 | /// Command service to add command to, not null. 39 | private AdjustSelectedCommand(AsyncPackage package, OleMenuCommandService commandService) 40 | { 41 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 42 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 43 | 44 | var menuCommandID = new CommandID(CommandSet, CommandId); 45 | var menuItem = new MenuCommand(Execute, menuCommandID); 46 | commandService.AddCommand(menuItem); 47 | } 48 | 49 | /// 50 | /// Gets the instance of the command. 51 | /// 52 | public static AdjustSelectedCommand? Instance 53 | { 54 | get; 55 | private set; 56 | } 57 | 58 | /// 59 | /// Gets the service provider from the owner package. 60 | /// 61 | Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider 62 | { 63 | get 64 | { 65 | return package; 66 | } 67 | } 68 | 69 | /// 70 | /// Initializes the singleton instance of the command. 71 | /// 72 | /// Owner package, not null. 73 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 74 | { 75 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 76 | 77 | var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; 78 | Instance = new AdjustSelectedCommand(package, commandService!); 79 | } 80 | 81 | /// 82 | /// This function is the callback used to execute the command when the menu item is clicked. 83 | /// See the constructor to see how the menu item is associated with this function using 84 | /// OleMenuCommandService service and MenuCommand class. 85 | /// 86 | /// Event sender. 87 | /// Event args. 88 | private async void Execute(object sender, EventArgs e) 89 | { 90 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); 91 | 92 | try 93 | { 94 | var vss = await VsServices.CreateAsync(ServiceProvider); 95 | 96 | //HashSet is needed to remove duplicates paths 97 | //this is possible if you click Adjust on xaml file (with cs behind) 98 | var filePaths = new HashSet(); 99 | 100 | var sew = await VS.Windows.GetSolutionExplorerWindowAsync(); 101 | if (sew != null) 102 | { 103 | var selection = await sew.GetSelectionAsync(); 104 | 105 | foreach (var item in selection) 106 | { 107 | var files = await item.ProcessDownRecursivelyForAsync(SolutionItemType.PhysicalFile, null); 108 | filePaths.AddRange( 109 | files.ConvertAll(i => i.FullPath!).FindAll(i => !string.IsNullOrEmpty(i)) 110 | ); 111 | } 112 | } 113 | 114 | if (filePaths.Count > 0) 115 | { 116 | var window = AdjustNamespaceWindow.Create(vss, filePaths); 117 | window.ShowModal(); 118 | } 119 | } 120 | catch (Exception excp) 121 | { 122 | Logging.LogVS(excp); 123 | } 124 | } 125 | 126 | 127 | //private void ShowError(string errorMessage) 128 | //{ 129 | // VsShellUtilities.ShowMessageBox( 130 | // package, 131 | // errorMessage, 132 | // $"Error has been found", 133 | // OLEMSGICON.OLEMSGICON_WARNING, 134 | // OLEMSGBUTTON.OLEMSGBUTTON_OK, 135 | // OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST 136 | // ); 137 | //} 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Command/AdjustSolutionCommand.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Helper; 2 | using AdjustNamespace.Window; 3 | using Microsoft.VisualStudio.Shell; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.ComponentModel.Design; 7 | 8 | namespace AdjustNamespace.Command 9 | { 10 | /// 11 | /// Command handler 12 | /// 13 | internal sealed class AdjustSolutionCommand 14 | { 15 | public static string ProjectKind = "{52AEFF70-BBD8-11d2-8598-006097C68E81}"; 16 | 17 | 18 | /// 19 | /// Command ID. 20 | /// 21 | public const int CommandId = PackageIds.AdjustSolutionCommandId; 22 | 23 | /// 24 | /// Command menu group (command set GUID). 25 | /// 26 | public static readonly Guid CommandSet = new Guid("3f7538ed-5c20-4d49-89fc-c401bb76df25"); 27 | 28 | /// 29 | /// VS Package that provides this command, not null. 30 | /// 31 | private readonly AsyncPackage package; 32 | 33 | /// 34 | /// Initializes a new instance of the class. 35 | /// Adds our command handlers for menu (commands must exist in the command table file) 36 | /// 37 | /// Owner package, not null. 38 | /// Command service to add command to, not null. 39 | private AdjustSolutionCommand(AsyncPackage package, OleMenuCommandService commandService) 40 | { 41 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 42 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 43 | 44 | var menuCommandID = new CommandID(CommandSet, CommandId); 45 | var menuItem = new MenuCommand(Execute, menuCommandID); 46 | commandService.AddCommand(menuItem); 47 | } 48 | 49 | /// 50 | /// Gets the instance of the command. 51 | /// 52 | public static AdjustSolutionCommand? Instance 53 | { 54 | get; 55 | private set; 56 | } 57 | 58 | /// 59 | /// Gets the service provider from the owner package. 60 | /// 61 | Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider 62 | { 63 | get 64 | { 65 | return package; 66 | } 67 | } 68 | 69 | /// 70 | /// Initializes the singleton instance of the command. 71 | /// 72 | /// Owner package, not null. 73 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 74 | { 75 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 76 | 77 | var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; 78 | Instance = new AdjustSolutionCommand(package, commandService!); 79 | } 80 | 81 | /// 82 | /// This function is the callback used to execute the command when the menu item is clicked. 83 | /// See the constructor to see how the menu item is associated with this function using 84 | /// OleMenuCommandService service and MenuCommand class. 85 | /// 86 | /// Event sender. 87 | /// Event args. 88 | private async void Execute(object sender, EventArgs e) 89 | { 90 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); 91 | 92 | try 93 | { 94 | var vss = await VsServices.CreateAsync(ServiceProvider); 95 | 96 | //HashSet is needed to remove duplicates paths 97 | //this is possible if you click Adjust on xaml file (with cs behind) 98 | var filePaths = new HashSet( 99 | await SolutionHelper.GetAllFilesFromAsync() 100 | ); 101 | 102 | if (filePaths.Count > 0) 103 | { 104 | var window = AdjustNamespaceWindow.Create(vss, filePaths); 105 | window.ShowModal(); 106 | } 107 | } 108 | catch (Exception excp) 109 | { 110 | Logging.LogVS(excp); 111 | } 112 | } 113 | 114 | 115 | //private void ShowError(string errorMessage) 116 | //{ 117 | // VsShellUtilities.ShowMessageBox( 118 | // package, 119 | // errorMessage, 120 | // $"Error has been found", 121 | // OLEMSGICON.OLEMSGICON_WARNING, 122 | // OLEMSGBUTTON.OLEMSGBUTTON_OK, 123 | // OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST 124 | // ); 125 | //} 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Command/EditSkippedPathsCommand.cs: -------------------------------------------------------------------------------- 1 | using AdjustNamespace.Window; 2 | using Microsoft.VisualStudio.Shell; 3 | using System; 4 | using System.ComponentModel.Design; 5 | 6 | namespace AdjustNamespace.Command 7 | { 8 | /// 9 | /// Command handler 10 | /// 11 | internal sealed class EditSkippedPathsCommand 12 | { 13 | public static string ProjectKind = "{52AEFF70-BBD8-11d2-8598-006097C68E81}"; 14 | 15 | 16 | /// 17 | /// Command ID. 18 | /// 19 | public const int CommandId = PackageIds.EditSkippedPathsCommandId; 20 | 21 | /// 22 | /// Command menu group (command set GUID). 23 | /// 24 | public static readonly Guid CommandSet = new Guid("3f7538ed-5c20-4d49-89fc-c401bb76df25"); 25 | 26 | /// 27 | /// VS Package that provides this command, not null. 28 | /// 29 | private readonly AsyncPackage package; 30 | 31 | /// 32 | /// Initializes a new instance of the class. 33 | /// Adds our command handlers for menu (commands must exist in the command table file) 34 | /// 35 | /// Owner package, not null. 36 | /// Command service to add command to, not null. 37 | private EditSkippedPathsCommand(AsyncPackage package, OleMenuCommandService commandService) 38 | { 39 | this.package = package ?? throw new ArgumentNullException(nameof(package)); 40 | commandService = commandService ?? throw new ArgumentNullException(nameof(commandService)); 41 | 42 | var menuCommandID = new CommandID(CommandSet, CommandId); 43 | var menuItem = new MenuCommand(Execute, menuCommandID); 44 | commandService.AddCommand(menuItem); 45 | } 46 | 47 | /// 48 | /// Gets the instance of the command. 49 | /// 50 | public static EditSkippedPathsCommand? Instance 51 | { 52 | get; 53 | private set; 54 | } 55 | 56 | /// 57 | /// Gets the service provider from the owner package. 58 | /// 59 | Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider 60 | { 61 | get 62 | { 63 | return package; 64 | } 65 | } 66 | 67 | /// 68 | /// Initializes the singleton instance of the command. 69 | /// 70 | /// Owner package, not null. 71 | public static async System.Threading.Tasks.Task InitializeAsync(AsyncPackage package) 72 | { 73 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken); 74 | 75 | var commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; 76 | Instance = new EditSkippedPathsCommand(package, commandService!); 77 | } 78 | 79 | /// 80 | /// This function is the callback used to execute the command when the menu item is clicked. 81 | /// See the constructor to see how the menu item is associated with this function using 82 | /// OleMenuCommandService service and MenuCommand class. 83 | /// 84 | /// Event sender. 85 | /// Event args. 86 | private async void Execute(object sender, EventArgs e) 87 | { 88 | await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); 89 | 90 | try 91 | { 92 | var vss = await VsServices.CreateAsync(ServiceProvider); 93 | 94 | var w = new EditSkippedPathsWindow( 95 | vss 96 | ); 97 | w.ShowDialog(); 98 | } 99 | catch (Exception excp) 100 | { 101 | Logging.LogVS(excp); 102 | } 103 | } 104 | 105 | 106 | //private void ShowError(string errorMessage) 107 | //{ 108 | // VsShellUtilities.ShowMessageBox( 109 | // package, 110 | // errorMessage, 111 | // $"Error has been found", 112 | // OLEMSGICON.OLEMSGICON_WARNING, 113 | // OLEMSGBUTTON.OLEMSGBUTTON_OK, 114 | // OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST 115 | // ); 116 | //} 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/BaseViewModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Diagnostics; 4 | using System.Windows.Input; 5 | 6 | namespace AdjustNamespace.Helper 7 | { 8 | /// 9 | /// Класс, реализующий базовую функциональность viewmodel идеологии MVVM 10 | /// 11 | public class BaseViewModel : INotifyPropertyChanged, IDisposable 12 | { 13 | 14 | /// 15 | /// Событие изменения свойства 16 | /// 17 | public event PropertyChangedEventHandler? PropertyChanged; 18 | 19 | /// 20 | /// Тестовое свойство бросания исключения в случае не нахождения биндинга 21 | /// 22 | protected bool _throwOnInvalidPropertyName; 23 | 24 | /// 25 | /// конструктор 26 | /// 27 | /// Диспатчер WPF 28 | protected BaseViewModel() 29 | { 30 | } 31 | 32 | /// 33 | /// Активация евента изменения бинденого свойства 34 | /// 35 | protected void OnPropertyChanged() 36 | { 37 | OnPropertyChanged(string.Empty); 38 | } 39 | 40 | /// 41 | /// Активация евента изменения бинденого свойства 42 | /// 43 | /// 44 | protected virtual void OnPropertyChanged(string propertyName) 45 | { 46 | VerifyPropertyName(propertyName); 47 | 48 | var handler = PropertyChanged; 49 | if (handler != null) 50 | { 51 | var e = new PropertyChangedEventArgs(propertyName); 52 | handler(this, e); 53 | } 54 | 55 | CommandManager.InvalidateRequerySuggested(); 56 | } 57 | 58 | [Conditional("DEBUG")] 59 | [DebuggerStepThrough] 60 | public void VerifyPropertyName(string propertyName) 61 | { 62 | if (!string.IsNullOrEmpty(propertyName)) 63 | { 64 | // Verify that the property name matches a real, 65 | // public, instance property on this object. 66 | var propertiesList = TypeDescriptor.GetProperties(this); 67 | if (propertiesList[propertyName] == null) 68 | { 69 | var msg = "Invalid property name: " + propertyName; 70 | 71 | if (_throwOnInvalidPropertyName) 72 | { 73 | throw new Exception(msg); 74 | } 75 | 76 | Debug.Fail(msg); 77 | } 78 | } 79 | } 80 | 81 | protected virtual void DisposeViewModel() 82 | { 83 | 84 | } 85 | 86 | #region Implementation of IDisposable 87 | 88 | public void Dispose() 89 | { 90 | DisposeViewModel(); 91 | } 92 | 93 | #endregion 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/ChainViewModel.cs: -------------------------------------------------------------------------------- 1 | namespace AdjustNamespace.Helper 2 | { 3 | public abstract class ChainViewModel : BaseViewModel 4 | { 5 | protected ChainViewModel() 6 | : base() 7 | { 8 | } 9 | 10 | public abstract System.Threading.Tasks.Task StartAsync(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/DocumentChangerHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Build.Framework.XamlTypes; 2 | using Microsoft.CodeAnalysis; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace AdjustNamespace.Helper 9 | { 10 | public static class DocumentChangerHelper 11 | { 12 | public static async Task ApplyModifiedDocumentAsync( 13 | this Workspace workspace, 14 | string filePath, 15 | Func provider 16 | ) 17 | { 18 | if (workspace is null) 19 | { 20 | throw new ArgumentNullException(nameof(workspace)); 21 | } 22 | 23 | if (filePath is null) 24 | { 25 | throw new ArgumentNullException(nameof(filePath)); 26 | } 27 | 28 | if (provider is null) 29 | { 30 | throw new ArgumentNullException(nameof(provider)); 31 | } 32 | 33 | bool r; 34 | do 35 | { 36 | var (document, syntaxRoot) = await workspace.GetDocumentAndSyntaxRootAsync(filePath); 37 | if (document == null || syntaxRoot == null) 38 | { 39 | //skip this document 40 | return; 41 | } 42 | 43 | var changedDocument = provider(document, syntaxRoot); 44 | if (changedDocument is null) 45 | { 46 | return; 47 | } 48 | 49 | r = workspace.TryApplyChanges(changedDocument.Project.Solution); 50 | } 51 | while (!r); 52 | } 53 | 54 | public static async Task ApplyModifiedDocumentAsync( 55 | this Workspace workspace, 56 | Func> provider 57 | ) 58 | { 59 | if (workspace is null) 60 | { 61 | throw new ArgumentNullException(nameof(workspace)); 62 | } 63 | 64 | if (provider is null) 65 | { 66 | throw new ArgumentNullException(nameof(provider)); 67 | } 68 | 69 | bool r; 70 | do 71 | { 72 | var changedDocument = await provider(workspace); 73 | if (changedDocument is null) 74 | { 75 | return; 76 | } 77 | 78 | r = workspace.TryApplyChanges(changedDocument.Project.Solution); 79 | } 80 | while (!r); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/HashSetHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace AdjustNamespace.Helper 6 | { 7 | internal static class HashSetHelper 8 | { 9 | public static void AddRange(this HashSet set, IEnumerable list) 10 | { 11 | foreach(var i in list) 12 | { 13 | set.Add(i); 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/RoslynHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using Microsoft.CodeAnalysis.CSharp.Syntax; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace AdjustNamespace.Helper 9 | { 10 | public static class RoslynHelper 11 | { 12 | public static QualifiedNameSyntax? ToUpperSymbol( 13 | this QualifiedNameSyntax qns, 14 | SemanticModel semanticModel 15 | ) 16 | { 17 | while (true) 18 | { 19 | var symbol = semanticModel.GetSymbolInfo(qns.Left).Symbol; 20 | if (symbol == null) 21 | { 22 | return null; 23 | } 24 | if (symbol.Kind != SymbolKind.NamedType) 25 | { 26 | return qns; 27 | } 28 | 29 | var pqns = qns.Left as QualifiedNameSyntax; 30 | if (pqns == null) 31 | { 32 | return null; 33 | } 34 | 35 | qns = pqns; 36 | } 37 | 38 | } 39 | 40 | public static bool IsGlobal(this SyntaxNode node) 41 | { 42 | if (node is null) 43 | { 44 | throw new System.ArgumentNullException(nameof(node)); 45 | } 46 | 47 | return node.ToString().StartsWith("global::"); 48 | } 49 | 50 | public static T? ToUpperSyntax(this SyntaxNode node) 51 | where T : SyntaxNode 52 | { 53 | while (node != null) 54 | { 55 | if (!(node.Parent is T)) 56 | { 57 | return (T)node; 58 | } 59 | 60 | node = node.Parent; 61 | } 62 | 63 | return default; 64 | } 65 | 66 | public static SyntaxNode? GoDownTo(this SyntaxNode node, Type targetType) 67 | { 68 | if (node.GetType() == targetType) 69 | { 70 | return node; 71 | } 72 | 73 | var toProcess = node.ChildNodes().ToList(); 74 | 75 | while (toProcess.Count > 0) 76 | { 77 | //check any children to match 78 | foreach (var child in toProcess) 79 | { 80 | if (child.GetType() == targetType) 81 | { 82 | return child; 83 | } 84 | } 85 | 86 | //not found in direct children 87 | //get children from next level of depth 88 | var toProcess2 = new List(); 89 | foreach (var child in toProcess) 90 | { 91 | toProcess2.AddRange(child.ChildNodes()); 92 | } 93 | 94 | toProcess = toProcess2; 95 | } 96 | 97 | return default; 98 | } 99 | 100 | public static List GetAllDescendants( 101 | this SyntaxNode s 102 | ) 103 | where T : SyntaxNode 104 | { 105 | var r = s 106 | .DescendantNodes() 107 | .OfType() 108 | .ToList() 109 | ; 110 | 111 | return r; 112 | } 113 | 114 | public static IEnumerable GetAllTypes(this INamespaceSymbol @namespace) 115 | { 116 | foreach (var type in @namespace.GetTypeMembers()) 117 | foreach (var nestedType in type.GetNestedTypes()) 118 | yield return nestedType; 119 | 120 | foreach (var nestedNamespace in @namespace.GetNamespaceMembers()) 121 | foreach (var type in nestedNamespace.GetAllTypes()) 122 | yield return type; 123 | } 124 | 125 | 126 | public static IEnumerable GetNestedTypes(this INamedTypeSymbol type) 127 | { 128 | yield return type; 129 | foreach (var nestedType in type.GetTypeMembers() 130 | .SelectMany(nestedType => nestedType.GetNestedTypes())) 131 | yield return nestedType; 132 | } 133 | 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Helper/WpfHelper.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Windows; 3 | using System.Windows.Media; 4 | 5 | namespace AdjustNamespace.Helper 6 | { 7 | public static class WpfHelper 8 | { 9 | public static IEnumerable FindLogicalChildren(this DependencyObject depObj) where T : DependencyObject 10 | { 11 | if (depObj != null) 12 | { 13 | foreach (var rawChild in LogicalTreeHelper.GetChildren(depObj)) 14 | { 15 | if (rawChild is DependencyObject) 16 | { 17 | var child = (DependencyObject)rawChild; 18 | if (child is T) 19 | { 20 | yield return (T)child; 21 | } 22 | 23 | foreach (var childOfChild in child.FindLogicalChildren()) 24 | { 25 | yield return childOfChild; 26 | } 27 | } 28 | } 29 | } 30 | } 31 | public static List FindVisualChildren( 32 | this DependencyObject obj 33 | ) 34 | where TChildItem : DependencyObject 35 | { 36 | var result = new List(); 37 | 38 | obj.FindVisualChildren( 39 | result 40 | ); 41 | 42 | return result; 43 | } 44 | 45 | public static void FindVisualChildren( 46 | this DependencyObject obj, 47 | List result 48 | ) 49 | where TChildItem : DependencyObject 50 | { 51 | for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 52 | { 53 | var child = VisualTreeHelper.GetChild(obj, i); 54 | 55 | if (child is TChildItem tchi) 56 | { 57 | result.Add(tchi); 58 | } 59 | else 60 | { 61 | child.FindVisualChildren(result); 62 | } 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/ImageMonikers.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.Imaging.Interop; 2 | using System; 3 | 4 | namespace AdjustNamespace 5 | { 6 | public static class ImageMonikers 7 | { 8 | public static ImageMoniker Logo 9 | { 10 | get; 11 | } = new ImageMoniker 12 | { 13 | Guid = new Guid("872022f4-493a-4d7b-97f5-8b474662c341"), 14 | Id = 0 15 | }; 16 | 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Авдеев Вячеслав 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Logging.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.Runtime.CompilerServices; 5 | using System.Threading; 6 | 7 | namespace AdjustNamespace 8 | { 9 | /// 10 | /// Logging helpers. 11 | /// Taken from https://github.com/bert2/microscope completely. 12 | /// Take a look to that repo, it's amazing! 13 | /// 14 | public static class Logging 15 | { 16 | private static readonly object @lock = new object(); 17 | 18 | // Logs go to: C:\Users\\AppData\Local\Temp\dpdt_extension.*.log 19 | private static readonly string vsLogFile = $"{Path.GetTempPath()}/AdjustNamespace.vs.log"; 20 | 21 | [Conditional("DEBUG")] 22 | public static void LogVS( 23 | object? data = null, 24 | [CallerFilePath] string? file = null, 25 | [CallerMemberName] string? method = null) 26 | => Log(vsLogFile, file!, method!, data); 27 | 28 | public static void Log( 29 | string logFile, 30 | string callingFile, 31 | string callingMethod, 32 | object? data = null) 33 | { 34 | lock (@lock) 35 | { 36 | File.AppendAllText( 37 | logFile, 38 | $"{DateTime.Now:HH:mm:ss.fff} " 39 | + $"{Process.GetCurrentProcess().Id,5} " 40 | + $"{Thread.CurrentThread.ManagedThreadId,3} " 41 | + $"{Path.GetFileNameWithoutExtension(callingFile)}.{callingMethod}()" 42 | + $"{(data == null ? "" : $": {data}")}\n"); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Namespace/NamespaceCenter.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp.Syntax; 3 | using Microsoft.VisualStudio.LanguageServices; 4 | using Microsoft.VisualStudio.Threading; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Threading.Tasks; 8 | using System.Xml.Linq; 9 | 10 | namespace AdjustNamespace.Adjusting 11 | { 12 | /// 13 | /// Namespace state container. It accumulates a changes during adjusting. 14 | /// 15 | public class NamespaceCenter 16 | { 17 | private readonly Dictionary> _types; 18 | 19 | /// 20 | /// Emptyfied namespaces. They need to be removed at the end of adjusting procedure. 21 | /// 22 | private readonly HashSet _namespacesToRemove; 23 | 24 | private NamespaceCenter( 25 | Dictionary> types 26 | ) 27 | { 28 | if (types is null) 29 | { 30 | throw new ArgumentNullException(nameof(types)); 31 | } 32 | 33 | _types = types; 34 | _namespacesToRemove = new HashSet(); 35 | } 36 | 37 | /// 38 | /// Filter incoming namespaces and return only those are allowed to delete 39 | /// (namespaces that does not exists after adjusting). 40 | /// 41 | public List GetRemovedNamespaces( 42 | IReadOnlyList namespacesToCheck 43 | ) 44 | { 45 | if (namespacesToCheck is null) 46 | { 47 | throw new ArgumentNullException(nameof(namespacesToCheck)); 48 | } 49 | 50 | if (namespacesToCheck.Count == 0) 51 | { 52 | return new List(); 53 | } 54 | 55 | var toRemove = new List(namespacesToCheck.Count); 56 | 57 | foreach (var n in namespacesToCheck) 58 | { 59 | var nname = n.Name.ToString(); 60 | 61 | if (!_namespacesToRemove.Contains(nname)) 62 | { 63 | //there are a types in this namespace 64 | continue; 65 | } 66 | 67 | toRemove.Add(n); 68 | } 69 | 70 | return toRemove; 71 | } 72 | 73 | public void TypeRemoved(ITypeSymbol type) 74 | { 75 | var cnn = type.ContainingNamespace.ToDisplayString(); 76 | if (!_types.TryGetValue(cnn, out var set)) 77 | { 78 | return; 79 | } 80 | 81 | var tn = type.ToDisplayString(); 82 | 83 | if (!set.Contains(tn)) 84 | { 85 | return; 86 | } 87 | 88 | set.Remove(tn); 89 | 90 | if (set.Count == 0) 91 | { 92 | _namespacesToRemove.Add(cnn); 93 | } 94 | } 95 | 96 | /// 97 | /// Build a namespace center for a whole workspace. 98 | /// 99 | public static async Task CreateForAsync( 100 | VisualStudioWorkspace workspace 101 | ) 102 | { 103 | if (workspace is null) 104 | { 105 | throw new ArgumentNullException(nameof(workspace)); 106 | } 107 | 108 | var typeContainer = await TypeContainer.CreateForAsync(workspace); 109 | 110 | var types = new Dictionary>(typeContainer.DictByFullName.Count); 111 | foreach (var nte in typeContainer.DictByFullName.Values) 112 | { 113 | var key = nte.ContainingNamespaceName; 114 | if (!types.ContainsKey(key)) 115 | { 116 | types[key] = new HashSet(); 117 | } 118 | types[key].Add(nte.TypeFullName); 119 | } 120 | 121 | var result = new NamespaceCenter(types); 122 | return result; 123 | } 124 | 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Namespace/NamespaceTransition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace AdjustNamespace.Namespace 6 | { 7 | public readonly struct NamespaceTransition 8 | { 9 | /// 10 | /// Original namespace name. 11 | /// 12 | public readonly string OriginalName; 13 | 14 | /// 15 | /// Desired (target) namespace name. 16 | /// (namespace name bringed in accordance with its file folders) 17 | /// 18 | public readonly string ModifiedName; 19 | 20 | /// 21 | /// Is this original namespace is root in the document. 22 | /// 23 | public readonly bool IsRoot; 24 | 25 | public NamespaceTransition( 26 | string originalName, 27 | string modifiedName, 28 | bool isRoot 29 | ) 30 | { 31 | if (originalName is null) 32 | { 33 | throw new ArgumentNullException(nameof(originalName)); 34 | } 35 | 36 | if (modifiedName is null) 37 | { 38 | throw new ArgumentNullException(nameof(modifiedName)); 39 | } 40 | 41 | OriginalName = originalName; 42 | ModifiedName = modifiedName; 43 | IsRoot = isRoot; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Options/General.cs: -------------------------------------------------------------------------------- 1 | using Community.VisualStudio.Toolkit; 2 | using System.ComponentModel; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace AdjustNamespace.Options 6 | { 7 | internal partial class OptionsProvider 8 | { 9 | [ComVisible(true)] 10 | public class GeneralOptions : BaseOptionPage 11 | { 12 | } 13 | } 14 | 15 | public class General : BaseOptionModel 16 | { 17 | [Category("General")] 18 | [DisplayName("FilesAdjusted")] 19 | [Description("How many files were processed (adjusted namespaces).")] 20 | [DefaultValue(0)] 21 | [Browsable(false)] 22 | public int FilesAdjusted { get; set; } = 0; 23 | 24 | [Category("General")] 25 | [DisplayName("StarsGiven")] 26 | [Description("Stars are given already, no need to make a noise.")] 27 | [DefaultValue(false)] 28 | [Browsable(false)] 29 | public bool StarsGiven { get; set; } = false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Predicate.cs: -------------------------------------------------------------------------------- 1 | using Document = Microsoft.CodeAnalysis.Document; 2 | 3 | namespace AdjustNamespace 4 | { 5 | public static class Predicate 6 | { 7 | public static bool IsProjectInScope(this Microsoft.CodeAnalysis.Project? project) 8 | { 9 | if (project == null) 10 | { 11 | return false; 12 | } 13 | if (!project.SupportsCompilation) 14 | { 15 | return false; 16 | } 17 | if (project.Language != "C#") 18 | { 19 | return false; 20 | } 21 | return true; 22 | } 23 | 24 | public static bool IsDocumentInScope(this Document? document) 25 | { 26 | if (document is null) 27 | { 28 | return false; 29 | } 30 | if (document.FilePath is null) 31 | { 32 | return false; 33 | } 34 | if (!document.SupportsSyntaxTree) 35 | { 36 | return false; 37 | } 38 | if (!document.SupportsSemanticModel) 39 | { 40 | return false; 41 | } 42 | 43 | return true; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Settings/AdjustNamespaceSettings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Text; 5 | 6 | namespace AdjustNamespace.Settings 7 | { 8 | public class AdjustNamespaceSettings 9 | { 10 | public List SkippedFolderSuffixes 11 | { 12 | get; 13 | set; 14 | } = null!; 15 | 16 | 17 | public AdjustNamespaceSettings() 18 | { 19 | SkippedFolderSuffixes = new List(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Settings/AdjustNamespaceSettings2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Text; 5 | 6 | namespace AdjustNamespace.Settings 7 | { 8 | public class AdjustNamespaceSettings2 9 | { 10 | private readonly string _solutionFolder; 11 | 12 | public readonly AdjustNamespaceSettings Settings; 13 | 14 | public AdjustNamespaceSettings2( 15 | string solutionFolder, 16 | AdjustNamespaceSettings settings 17 | ) 18 | { 19 | if (solutionFolder is null) 20 | { 21 | throw new ArgumentNullException(nameof(solutionFolder)); 22 | } 23 | 24 | if (settings is null) 25 | { 26 | throw new ArgumentNullException(nameof(settings)); 27 | } 28 | _solutionFolder = solutionFolder; 29 | Settings = settings; 30 | } 31 | 32 | public bool IsSkippedFolder(string fullFolderPath) 33 | { 34 | fullFolderPath = Path.GetFullPath(fullFolderPath) 35 | .Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); 36 | 37 | foreach (var sfs in Settings.SkippedFolderSuffixes) 38 | { 39 | if (Path.IsPathRooted(sfs)) 40 | { 41 | var rsfs = Path.GetFullPath(sfs) 42 | .Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); 43 | 44 | if (rsfs == fullFolderPath) 45 | { 46 | return true; 47 | } 48 | } 49 | else 50 | { 51 | var rsfs = Path.Combine(_solutionFolder, sfs) 52 | .Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); 53 | 54 | if (rsfs == fullFolderPath) 55 | { 56 | return true; 57 | } 58 | } 59 | } 60 | 61 | return false; 62 | } 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/Settings/SettingsReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Text; 5 | using System.Xml.Serialization; 6 | 7 | namespace AdjustNamespace.Settings 8 | { 9 | public class SettingsReader 10 | { 11 | public const string SettingFileName = "adjust_namespaces_settings.xml"; 12 | 13 | private static readonly XmlSerializer _serializer = new XmlSerializer(typeof(AdjustNamespaceSettings)); 14 | private readonly string _solutionFolder; 15 | 16 | public SettingsReader(string solutionFolder) 17 | { 18 | if (solutionFolder is null) 19 | { 20 | throw new ArgumentNullException(nameof(solutionFolder)); 21 | } 22 | 23 | _solutionFolder = solutionFolder; 24 | } 25 | 26 | public AdjustNamespaceSettings? ReadSettings( 27 | ) 28 | { 29 | var settingsFilePath = Path.Combine(_solutionFolder, SettingFileName); 30 | if(!File.Exists(settingsFilePath)) 31 | { 32 | return null; 33 | } 34 | 35 | using (var fs = new FileStream(settingsFilePath, FileMode.Open)) 36 | { 37 | var result = (AdjustNamespaceSettings)_serializer.Deserialize(fs); 38 | return result; 39 | } 40 | } 41 | 42 | public void Save( 43 | AdjustNamespaceSettings settings 44 | ) 45 | { 46 | if (settings is null) 47 | { 48 | throw new ArgumentNullException(nameof(settings)); 49 | } 50 | 51 | var settingsFilePath = Path.Combine(_solutionFolder, SettingFileName); 52 | if (File.Exists(settingsFilePath)) 53 | { 54 | File.Delete(settingsFilePath); 55 | } 56 | 57 | using (var fs = new FileStream(settingsFilePath, FileMode.Create)) 58 | { 59 | _serializer.Serialize( 60 | fs, 61 | settings 62 | ); 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /AdjustNamespace.VsixShared/UI/Control/PerformingUserControl.xaml: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 25 | 26 | 34 | 35 |