Reputation: 1135
I'm trying to write a little Roslyn util to rename variables/members/parameters in the project. It seems the best course of action is Renamer.RenameSymbolAsync method. In order to use it, I need a solution and semantic symbol. So far I'm having hard time getting this information. Here's what I tried so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.FindSymbols;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Text;
namespace PlayCodeAnalysis
{
class Program
{
static void Main(string[] args)
{
var solutionPath = @"D:\Development\lams\src\Lams.sln";
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var doc = solution.Projects.First().Documents.First();
var model = doc.GetSemanticModelAsync().Result;
var syntax = doc.GetSyntaxRootAsync().Result;
var s = syntax.DescendantNodes().OfType<ParameterSyntax>().ToList();
var symbol = model.GetSymbolInfo(s[0]).Symbol;
//Renamer.RenameSymbolAsync(solution,)
}
}
}
The problem I'm having is that symbol
ends up being empty, I'm not sure why. s
resolves correctly to a list of all parameters in the file, but I can't convert it to a symbol to pass into the renamer.
The document that is being targeted in this particular case looks like this, and the s[0]
in this case ends up being inCollection
:
using System.Collections.Generic;
using System.Data.Entity;
using System.Threading.Tasks;
namespace System.Linq
{
public static class AsyncEnumerable
{
public static async Task<ILookup<TKey, T>> ToLookupAsync<T, TKey>(this Task<IEnumerable<T>> inCollection, Func<T, TKey> inKeySelector)
{
return (await inCollection.ConfigureAwait(false)).ToLookup(inKeySelector);
}
}
}
Upvotes: 0
Views: 616
Reputation: 354586
From a quick glance at the Roslyn source, you may have to use GetDeclaredSymbol
instead of GetSymbolInfo
on the ParameterSyntax
.
Upvotes: 2