belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Suggestion - VS - Configuration hot reload #1367

Closed scabana closed 2 weeks ago

scabana commented 3 weeks ago

Environments

Steps to reproduce

  1. Have the VS extension installed
  2. Open VS + solution
  3. outside of VS, create a .vs/$(SolutionName)/v17/csharpier.json containing "RunOnSave": true
  4. ctrl-s on a c# file in the previously opened VS

Expected behavior

The c# file should be formatted.

Actual behavior

The file based configuration has not been loaded from disk when the file changed.

Experiment

Updated CSharpierOptions.cs's LoadAsync to contain for following code:

var filePath = await getFilePath();

static FileSystemWatcher Create(string filePath)
{
    FileSystemWatcher fsw = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath));

    static void OnSomethingChanged(object sender, FileSystemEventArgs e)
    {
        ThreadHelper.Generic.BeginInvoke(() => Instance.Load());
    }

    fsw.Changed += OnSomethingChanged;
    fsw.Created += OnSomethingChanged;
    fsw.Renamed += OnSomethingChanged;
    fsw.EnableRaisingEvents = true;

    return fsw;
}

_fileSystemWatcher = _fileSystemWatcher ?? Create(filePath);

if (filePath == null || !File.Exists(filePath))
{
    return;
}

And now the config gets reloaded when the file changed.

We are using some automation to enable CSharpier automatically when someone is using VS, the dev experience would be better if it didn't need a VS restart to be picked up.

I can open a PR with the updated code.

belav commented 2 weeks ago

This is a great suggestion, if you can open the PR I can get out an updated version of the extension. Thanks!

scabana commented 2 weeks ago

.assign