Skip to content

Commit

Permalink
Fixing bug with options page when using csharpier without a solution
Browse files Browse the repository at this point in the history
closes #1407
  • Loading branch information
belav committed Dec 20, 2024
1 parent 3c6f9ce commit de3f372
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void LoadFrom(CSharpierOptions newInstance)
ThreadHelper.JoinableTaskFactory
);

private static FileSystemWatcher _hotReloadWatcher;
private static FileSystemWatcher? hotReloadWatcher;

public static CSharpierOptions Instance
{
Expand All @@ -83,7 +83,7 @@ private static async Task<CSharpierOptions> CreateAsync()

public void Load()
{
ThreadHelper.JoinableTaskFactory.Run(LoadAsync);
ThreadHelper.JoinableTaskFactory.Run(this.LoadAsync);
}

private async Task LoadAsync()
Expand Down Expand Up @@ -140,7 +140,7 @@ await LoadOptionsFromFile(

public void Save()
{
ThreadHelper.JoinableTaskFactory.Run(SaveAsync);
ThreadHelper.JoinableTaskFactory.Run(this.SaveAsync);
}

public async Task SaveAsync()
Expand Down Expand Up @@ -204,6 +204,7 @@ await SaveOptions(

private static async Task<string?> GetSolutionOptionsFileNameAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
#pragma warning disable VSSDK006
var solution =
await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SVsSolution))
Expand All @@ -212,16 +213,20 @@ await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SVsSolution))
solution!.GetSolutionInfo(out _, out _, out var userOptsFile);

return userOptsFile != null
? Path.Combine(Path.GetDirectoryName(userOptsFile), "csharpier.json")
? Path.Combine(Path.GetDirectoryName(userOptsFile)!, "csharpier.json")
: null;
}

private static async Task InitializeHotReloadWatcherAsync()
{
string filePath = await GetSolutionOptionsFileNameAsync();
var filePath = await GetSolutionOptionsFileNameAsync();
if (filePath is null)
{
return;
}

_hotReloadWatcher = new FileSystemWatcher(
Path.GetDirectoryName(filePath),
hotReloadWatcher = new FileSystemWatcher(
Path.GetDirectoryName(filePath)!,
Path.GetFileName(filePath)
);

Expand All @@ -236,11 +241,11 @@ static void OnFileChanged(object sender, FileSystemEventArgs e)
#pragma warning restore
}

_hotReloadWatcher.Changed += OnFileChanged;
_hotReloadWatcher.Created += OnFileChanged;
_hotReloadWatcher.Renamed += OnFileChanged;
hotReloadWatcher.Changed += OnFileChanged;
hotReloadWatcher.Created += OnFileChanged;
hotReloadWatcher.Renamed += OnFileChanged;

_hotReloadWatcher.EnableRaisingEvents = true;
hotReloadWatcher.EnableRaisingEvents = true;
}

private class OptionsDto
Expand Down

0 comments on commit de3f372

Please sign in to comment.