How to properly rename a variable from a code-fix #46129
Unanswered
Evangelink
asked this question in
General
Replies: 3 comments
-
The entry point to code to generate a name, accounting for other names in scope, is here: |
Beta Was this translation helpful? Give feedback.
0 replies
-
@sharwell Thanks for the pointer, regarding |
Beta Was this translation helpful? Give feedback.
0 replies
-
You'll need to copy all of the implementation necessary for that feature over to roslyn-analyzers. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When trying to implement some analyzer code-fix for rule RS0046 - Avoid 'opt' suffix which renames a variable, I have tried to use the
LookupSymbols
API to avoid name clash but I am facing a couple of issues:Name clash for method parameters.
Consider the following method
but I do not want to register a code-fix which renames
sOpt
intos
andselectionOpt
intoselection
because it will cause a name clash.Doing some experiments around the
SemanticModel.LookupSymbols
API, I have managed to detect thesOpt
case by passing a position in the method block (last statement span end) but from this position I cannot detect the variableselection
which is now out-of-scope. The only thing I can think of is to collect all symbols but that seems rather inefficient and I assume there are lots of analyzers/code-fix in a similar situation.On dotnet/roslyn-analyzers#3813, @sharwell recommended to have a look at roslyn's name suggestion completion provider, which I guess is https://github.com/dotnet/roslyn/blob/master/src/Features/CSharp/Portable/Completion/CompletionProviders/DeclarationNameCompletionProvider.cs but I couldn't really find which part needs to be used.
Rename all references
I am currently using the
Renamer.RenameSymbolAsync
API to rename the variable but I have noticed that if I rename an interface or base class parameter name, the implementations are not renamed. Is there a way to rename in chain?Thank you for the help!
Beta Was this translation helpful? Give feedback.
All reactions