Divisadero
Divisadero

Reputation: 913

How to provide fix for all parameters of the method (Roslyn - FixProvider)

I wrote code analyzer & fix provider for adding validations to ensure argument is not null. It works good for multiple methods, but I cannot check all the parameters at once. The analyzer will mark all the parameters that are not checked already, but fix can be done only for one parameter at once.

Details: It is not immutable nodes issue, the code adds using statement if needed.

In the analyzer i register diagnostic for every parameter.

  Diagnostic diagnostic = Diagnostic.Create(
                    Rule,
                    parameter.GetLocation(),
                    null,
                    prop.ToImmutableDictionary(),
                    parameter.Identifier.Text);

In the fix provider I am taking first diagnostic from the context and register code fix for that.

Maybe I am doing something wrong and thus do not get multiple diagnostics in the fix provider.

What is the correct way to make multiple fixes? Should be the key/id of the diagnostic the same? Or should the diagnostic be registered directly to complain about all the parameters.

Upvotes: 2

Views: 131

Answers (1)

Divisadero
Divisadero

Reputation: 913

I have found hopefully good solution:

The analyzer report diagnostics for every invalid parameter so you can generate validation for specific parameter. After that the analyzer reports one more diagnostic for all the invalid parameters and sets the method identifier as the location.

The same code fix provider is used to solve both types of diagnostics. The specific parameter one (which sends the single parameter data) and all the parameters one (which sends collection of the parameters data).

Upvotes: 1

Related Questions