how can I properly use the roslyn to renamer all method in unity3d project ?
Version Used: 2.7.0
Steps to Reproduce:
I have a unity game project and use the visual studio 2017 as the default scripts editor, now i want to change a method's name with a number suffix in a file "Test.cs", so i create a dotnet console project that uses roslyn to to this.
"Test.cs" file code is below:
using System.Collections.Generic;
using UnityEngine;
public class A
{
public void XX() { Debug.LogError("XX"); }
}
public class B : A
{
}
public class Test
{
private static void TT()
{
Dictionary<string, A> dics = new Dictionary<string, A>();
for (int i = 1; i <= 10; i++)
{
A a = i % 2 == 0 ? new A() : new B();
dics.Add(i.ToString(), a);
}
foreach (var pair in dics)
{
pair.Value.XX();
}
}
}
my console code is like below:
private static async Task Main(string[] args)
{
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
// open solution
string slnPath = @"E:\Tumei006Temp\Tumei006Temp.sln";
string projectName = "Tumei006Temp";
Solution solution = await workspace.OpenSolutionAsync(slnPath);
// get the specified project
Project project = null;
foreach (var pro in solution.Projects)
{
if (pro.Name.Equals(projectName))
{
project = pro;
break;
}
}
if (project == null)
{
Console.WriteLine("can't get the project");
return;
}
// get the specified document
Document doc = null;
string docPath = @"E:\Tumei006Temp\Assets\Scripts\Ztest\Test.cs";
foreach (var dc in project.Documents)
{
if (dc.FilePath.Equals(docPath))
{
doc = dc;
break;
}
}
if (doc == null)
{
Console.WriteLine("can't get the document");
return;
}
Console.WriteLine("----------start changing------------");
var compilation = await project.GetCompilationAsync();
var root = await doc.GetSyntaxRootAsync();
var model = compilation.GetSemanticModel(root.SyntaxTree);
var methods = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
foreach (var method in methods)
{
var symbol = model.GetDeclaredSymbol(method);
if (symbol.Name.Equals("XX"))
{
string newMethodName = symbol.Name + "_2018";
solution = await Renamer.RenameSymbolAsync(solution, symbol, newMethodName, solution.Options);
workspace.TryApplyChanges(solution);
break;
}
}
Console.WriteLine("---------finish--------------");
}
Expected Behavior:
using System.Collections.Generic;
using UnityEngine;
public class A
{
public void XX_2018() { Debug.LogError("XX"); }
}
public class B : A
{
}
public class Test
{
private static void TT()
{
Dictionary<string, A> dics = new Dictionary<string, A>();
for (int i = 1; i <= 10; i++)
{
A a = i % 2 == 0 ? new A() : new B();
dics.Add(i.ToString(), a);
}
foreach (var pair in dics)
{
pair.Value.XX_2018();
}
}
}
Actual Behavior: I hope the XX() method and all its references can be rename automatic, however XX() method references don't change. like below:
using System.Collections.Generic;
using UnityEngine;
public class A
{
public void XX_2018() { Debug.LogError("XX"); }
}
public class B : A
{
}
public class Test
{
private static void TT()
{
Dictionary<string, A> dics = new Dictionary<string, A>();
for (int i = 1; i <= 10; i++)
{
A a = i % 2 == 0 ? new A() : new B();
dics.Add(i.ToString(), a);
}
foreach (var pair in dics)
{
pair.Value.XX();
}
}
}
Note: when test on "not unity projects" , this works well, however when test on "unity project" it's wrong.
I think it's a bug with my test.
What bug was it exactly? I'm dealing with the very same issue as described above. Did you find a way how to make method references to be renamed as well?
Thanks a lot.
@josef-stromsky sorry, i can't find any good way to solve this problem.
Should Renamer.RenameSymbolAsync work this way? In other cases all the references are renamed too. I would consider it to be a bug.
@DFVSQY, what is your opinion?
@josef-stromsky I didn't have a try. I have give up this and i think it's a bug.