-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeModifier.cs
More file actions
67 lines (60 loc) · 2.86 KB
/
CodeModifier.cs
File metadata and controls
67 lines (60 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.ComponentModel.Design;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Analyzer
{
public static class Program
{
public static void Main(string[] args)
{
const string testFileToParse = @"C:\Users\sakib\RiderProjects\Analyzer\Test.cs";
var filetoParse = @"D:\Research\SanAndreasUnity\Assets\Scripts\Behaviours\Weapon.cs";
const string targetClass = "Weapon";
const string targetField = "AmmoInClip";
var fileContent = File.ReadAllText(filetoParse);
var syntaxTree = CSharpSyntaxTree.ParseText(fileContent);
var root = syntaxTree.GetRoot();
MemberDeclarationSyntax mds = null;
ClassDeclarationSyntax cds = null;
ClassDeclarationSyntax cdsNew = null;
foreach (var classDeclaration in root.DescendantNodes().OfType<ClassDeclarationSyntax>())
{
if (classDeclaration.Identifier.Text != targetClass) continue;
cds = classDeclaration;
var memberDeclarationSyntaxList = classDeclaration.Members.ToList<MemberDeclarationSyntax>();
foreach (var memberDeclarationSyntax in memberDeclarationSyntaxList)
{
if (memberDeclarationSyntax is FieldDeclarationSyntax syntax)
{
var fds = syntax;
if (fds.Declaration.Variables.All(variable => variable.Identifier.Text != targetField))
continue;
mds = fds;
break;
}
if (memberDeclarationSyntax is PropertyDeclarationSyntax syn)
{
var pds = syn;
if (pds.Identifier.Text != targetField) continue;
mds = pds;
break;
}
}
if (mds == null) continue;
FieldDeclarationSyntax aField = SF.FieldDeclaration(
SF.VariableDeclaration(
SF.ParseTypeName("int"),
SF.SeparatedList(new[] { SF.VariableDeclarator(SF.Identifier(" _a = 159")) })
))
.AddModifiers(mds.Modifiers.ToArray())
.AddAttributeLists(mds.AttributeLists.ToArray());
cdsNew = classDeclaration.InsertNodesBefore(mds, new List<SyntaxNode> {aField});
break;
}
root = root.ReplaceNode(cds, cdsNew);
Console.WriteLine(root.ToString());
}
}
}