Search code examples
c#roslynroslyn-code-analysis

Can a CodeFixProvider be used to change more than one document?


I have a CodeFixProvider that can change a document, and it works.

But I also need to change a second document when the first document is changed.

I can write the code to fix the second document, but I wouldn't know when to execute it, because I cannot see an event on the CodeFixProvider to report when it actually is selected by the user and applied to the first document. I don't want to apply the changes to the second document if the CodeFixProvider is simply being previewed.

Has anyone solved this problem before? Or have a suggestion? Any help would be appreciated.


Solution

  • I don't want to apply the changes to the second document if the CodeFixProvider is simply being previewed.

    Actually, you do. Code fix providers don't change anything about the user's actual code directly (remember that Roslyn's entire hierarchy is immutable). Instead, they create a new Solution with the code fix applied.

    You should change all relevant documents no matter what, and let Roslyn figure out what to do with the changes.