From f2b2b986246dbfe63cd2f7af64a0e1611e1faba3 Mon Sep 17 00:00:00 2001 From: Simon Le Bourdais-Cabana Date: Fri, 1 Nov 2024 17:04:05 -0400 Subject: [PATCH] VS Extension now hot reloads its config (#1369) This fixes #1367. The `.vs/$(SolutionName)/v17/csharpier.json` file is now watched for changes and will reload the configuration when the file is changed. --------- Co-authored-by: Bela VanderVoort --- .../source.extension.vsixmanifest | 2 +- .../source.extension.vsixmanifest | 2 +- .../CSharpierOptions.cs | 36 +++++++++++++++++-- Src/CSharpier.VisualStudio/ChangeLog.md | 5 ++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest index 0560438db..3d56dd21c 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + CSharpier CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules. https://github.com/belav/csharpier diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest index 8022f5ad2..74c19ca62 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudio2019/source.extension.vsixmanifest @@ -1,7 +1,7 @@ - + CSharpier 2019 CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules. https://github.com/belav/csharpier diff --git a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierOptions.cs b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierOptions.cs index 7ef8fbab3..8e7e65d36 100644 --- a/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierOptions.cs +++ b/Src/CSharpier.VisualStudio/CSharpier.VisualStudioShared/CSharpierOptions.cs @@ -58,6 +58,8 @@ protected void LoadFrom(CSharpierOptions newInstance) private static readonly AsyncLazy liveModel = new(CreateAsync, ThreadHelper.JoinableTaskFactory); + private static FileSystemWatcher _hotReloadWatcher; + public static CSharpierOptions Instance { get @@ -73,6 +75,7 @@ private static async Task CreateAsync() { var instance = new CSharpierOptions(); await instance.LoadAsync(); + await InitializeHotReloadWatcherAsync(); return instance; } @@ -112,7 +115,7 @@ Action doStuff } await LoadOptionsFromFile( - this.GetSolutionOptionsFileNameAsync, + GetSolutionOptionsFileNameAsync, o => { newInstance.SolutionRunOnSave = o.RunOnSave; @@ -170,7 +173,7 @@ async Task SaveOptions(Func> getFilePath, OptionsDto optionsDto) } await SaveOptions( - this.GetSolutionOptionsFileNameAsync, + GetSolutionOptionsFileNameAsync, new OptionsDto { RunOnSave = this.SolutionRunOnSave } ); @@ -197,7 +200,7 @@ await SaveOptions( ); } - private async Task GetSolutionOptionsFileNameAsync() + private static async Task GetSolutionOptionsFileNameAsync() { #pragma warning disable VSSDK006 var solution = @@ -211,6 +214,33 @@ await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SVsSolution)) : null; } + private static async Task InitializeHotReloadWatcherAsync() + { + string filePath = await GetSolutionOptionsFileNameAsync(); + + _hotReloadWatcher = new FileSystemWatcher( + Path.GetDirectoryName(filePath), + Path.GetFileName(filePath) + ); + + static void OnFileChanged(object sender, FileSystemEventArgs e) + { +#pragma warning disable VSTHRD103 // Call async methods when in an async method + ThreadHelper.JoinableTaskFactory.Run(async () => + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + await Instance.LoadAsync(); + }); +#pragma warning restore + } + + _hotReloadWatcher.Changed += OnFileChanged; + _hotReloadWatcher.Created += OnFileChanged; + _hotReloadWatcher.Renamed += OnFileChanged; + + _hotReloadWatcher.EnableRaisingEvents = true; + } + private class OptionsDto { public bool? RunOnSave { get; set; } diff --git a/Src/CSharpier.VisualStudio/ChangeLog.md b/Src/CSharpier.VisualStudio/ChangeLog.md index 21017ca73..fcb651242 100644 --- a/Src/CSharpier.VisualStudio/ChangeLog.md +++ b/Src/CSharpier.VisualStudio/ChangeLog.md @@ -1,4 +1,7 @@ -## [1.8.0] +## [1.9.0] +- Configuration will hot reload: changes to `.vs/$(SolutionName)/v17/csharpier.json` will trigger a reload of the configuration in an opened Visual Studio instance. + +## [1.8.0] - Use dotnet tool list to find both local and global installs of csharpier. ## [1.7.4]