-
Notifications
You must be signed in to change notification settings - Fork 3
/
RimworldReferenceProvider.cs
200 lines (156 loc) · 8.51 KB
/
RimworldReferenceProvider.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.IO;
using System.Linq;
using System.Text;
using JetBrains.DataFlow;
using JetBrains.Lifetimes;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Caches;
using JetBrains.ReSharper.Psi.Impl.Reflection2;
using JetBrains.ReSharper.Psi.Resolve;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Util;
using JetBrains.ReSharper.Psi.Web.WebConfig;
using JetBrains.ReSharper.Psi.Xml;
using JetBrains.ReSharper.Psi.Xml.Impl.Tree;
using JetBrains.ReSharper.Psi.Xml.Tree;
using ReSharperPlugin.RimworldDev.SymbolScope;
namespace ReSharperPlugin.RimworldDev.TypeDeclaration;
[ReferenceProviderFactory]
public class RimworldReferenceProvider : IReferenceProviderFactory
{
public RimworldReferenceProvider(Lifetime lifetime) =>
Changed = new Signal<IReferenceProviderFactory>(lifetime, GetType().FullName);
public IReferenceFactory CreateFactory(IPsiSourceFile sourceFile, IFile file, IWordIndex wordIndexForChecks)
{
return sourceFile.PrimaryPsiLanguage.Is<XmlLanguage>() && sourceFile.GetExtensionWithDot()
.Equals(".xml", StringComparison.CurrentCultureIgnoreCase)
? new RimworldReferenceFactory()
: null;
}
public ISignal<IReferenceProviderFactory> Changed { get; }
}
/**
* The reference provider is just that, it provides a reference from XML into C# (And hopefully someday the other way
* around with Find Usages), where you can Ctrl+Click into the C# for a property or class
*/
public class RimworldReferenceFactory : IReferenceFactory
{
public ReferenceCollection GetReferences(ITreeNode element, ReferenceCollection oldReferences)
{
if (!ScopeHelper.UpdateScopes(element.GetSolution())) return new ReferenceCollection();
if (element.Parent != null && element.NodeType.ToString() == "TEXT" &&
element.Parent.GetText().StartsWith("<defName>"))
return GetReferenceForDeclaredElement(element, oldReferences);
if (element.NodeType.ToString() == "TEXT") return GetReferencesForText(element, oldReferences);
if (element is not XmlIdentifier identifier) return new ReferenceCollection();
if (element.GetSourceFile() is not { } sourceFile) return new ReferenceCollection();
var rimworldSymbolScope = ScopeHelper.RimworldScope;
var allSymbolScopes = ScopeHelper.AllScopes;
var hierarchy = RimworldXMLItemProvider.GetHierarchy(identifier);
if (hierarchy.Count == 0)
{
var className = identifier.GetText();
var allWorkingScopes =
ScopeHelper.AllScopes.Where(scope => scope.GetTypeElementByCLRName(className) is not null);
var @class = className.Contains(".")
? ScopeHelper.GetScopeForClass(className).GetTypeElementByCLRName(className)
: rimworldSymbolScope.GetElementsByShortName(className).FirstOrDefault();
if (@class == null) return new ReferenceCollection();
return new ReferenceCollection(new RimworldXmlReference(@class, identifier));
}
var classContext =
RimworldXMLItemProvider.GetContextFromHierachy(hierarchy, rimworldSymbolScope, allSymbolScopes);
if (classContext == null) return new ReferenceCollection();
var possibleFields = RimworldXMLItemProvider.GetAllFields(classContext, rimworldSymbolScope);
var field = possibleFields
.FirstOrDefault(
field => field.ShortName == identifier.GetText()
) ?? possibleFields.FirstOrDefault(field =>
field.GetAttributeInstances(AttributesSource.Self).Any(
attribute =>
{
var match = identifier.GetText();
if (attribute.GetClrName().FullName != "Verse.LoadAliasAttribute") return false;
if (attribute.PositionParameterCount != 1) return false;
var test = field.GetXMLDoc(true);
if (!attribute.PositionParameter(0).ConstantValue.IsString())
{
var writer = new StringWriter(new StringBuilder());
if (attribute is not MetadataAttributeInstance) return false;
((MetadataAttributeInstance)attribute).Dump(writer, "");
writer.Close();
if (writer.ToString().Contains($"Arguments: \"{match}\"")) return true;
return false;
}
return attribute.PositionParameter(0).ConstantValue.StringValue == match;
}));
if (field == null) return new ReferenceCollection();
if (field.ShortName != identifier.GetText())
{
return new ReferenceCollection(new RimworldXmlReference(field, identifier, field.ShortName));
}
return new ReferenceCollection(new RimworldXmlReference(field, identifier));
}
private ReferenceCollection GetReferencesForText(ITreeNode element, ReferenceCollection oldReferences)
{
var hierarchy = RimworldXMLItemProvider.GetHierarchy(element);
if (hierarchy.Count == 0) return new ReferenceCollection();
var classContext =
RimworldXMLItemProvider.GetContextFromHierachy(hierarchy, ScopeHelper.RimworldScope, ScopeHelper.AllScopes);
if (classContext == null) return new ReferenceCollection();
var rimworldSymbolScope = ScopeHelper.RimworldScope;
var allSymbolScopes = ScopeHelper.AllScopes;
if (classContext.GetType().Name == "Enum")
{
var @class = rimworldSymbolScope.GetElementsByShortName(classContext.ShortName).FirstOrDefault();
var col = new ReferenceCollection(new RimworldXmlReference(@class, element));
return col;
// return new ReferenceCollection();
}
if (!ScopeHelper.ExtendsFromVerseDef(classContext.GetClrName().FullName))
return new ReferenceCollection();
var xmlSymbolTable = element.GetSolution().GetComponent<RimworldSymbolScope>();
var defType = classContext.ShortName;
var defName = element.GetText();
var defNameTag = new DefNameValue(defType, defName);
defNameTag = xmlSymbolTable.GetDefName(defNameTag);
if (xmlSymbolTable.GetTagByDef(defNameTag) is not { } tag)
return new ReferenceCollection();
return new ReferenceCollection(
new RimworldXmlDefReference(element, tag, defNameTag.DefType, defNameTag.DefName));
}
/**
* This is a bit of a hacky workaround. Since we're not constructing our own Custom Language, we don't have control
* over the Psi Tree. Unfortunately, Find Usages expects to be able to find an `IDeclaration` which is a `ITreeNode`
* in the Psi Tree, which would be created on the Tree Construction.
*
* Since we can't do that, in order to be able to invoke Find Usages on the declared element itself, we're going to
* make a hacky workaround and just give it a reference... to itself
*/
private ReferenceCollection GetReferenceForDeclaredElement(ITreeNode element, ReferenceCollection oldReferences)
{
// We're currently in a text node inside a <defName> inside another ThingDef node. We want to get that node
var defTypeName = element.Parent?.Parent?
// And then get the TagHeader (<ThingDef>) of that node
.Children().FirstOrDefault(childElement => childElement is XmlTagHeaderNode)?
// And then get the text that provides the ID of that node (ThingDef)
.Children().FirstOrDefault(childElement => childElement is XmlIdentifier)?.GetText();
if (defTypeName is null) new ReferenceCollection();
var defName = element.GetText();
var xmlSymbolTable = element.GetSolution().GetComponent<RimworldSymbolScope>();
var defNameTag = new DefNameValue(defTypeName, defName);
if (!xmlSymbolTable.HasTag(defNameTag)) return new ReferenceCollection();
defNameTag = xmlSymbolTable.GetDefName(defNameTag);
if (xmlSymbolTable.GetTagByDef(defNameTag) is not { } tag)
return new ReferenceCollection();
return new ReferenceCollection(new RimworldXmlDefReference(element, tag, defNameTag.DefType,
defNameTag.DefName));
}
public bool HasReference(ITreeNode element, IReferenceNameContainer names)
{
if (element.NodeType.ToString() != "TEXT") return false;
return !element.Parent.GetText().Contains("defName") && names.Contains(element.GetText());
}
}