0

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.

Martin Lottering
  • 1,624
  • 19
  • 31

2 Answers2

2

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • thank you for the reply. That actually makes more sense. I am trying it out but the `AdditionalDocuments` on the new `Project` and the old project is empty. It is a .json file that I am trying to edit as a 2nd document, but I'm not sure how to load the .json `Document`. – Martin Lottering Nov 02 '17 at 20:08
0

Use Document.Project.Documents to access all documents in the project. There are some other ways to get documents, but they depend on a task you try to solve. Describe what do you try to achieve if you need more info.

Oxoron
  • 664
  • 1
  • 7
  • 26