This is my current Work In Progress plugin for JetBrains Rider (and ReSharper?) to bring some intelligence to Rimworld XML Definitions. Because all of the XML that you write for Rimworld is backed by C#, all the properties you define and the values that are allowed are just properties in a C# class, we're able to fetch the auto-complete options from C# itself. Expanding further from that, we can also link the XML back into the C# classes behind them, allowing you to Ctrl+Click into the definitions on which the XML sits.
DefDatabase<Def>.GetNamed()
[DefOf]
classesCtrl+Click
to go references
[DefOf]
fields, or DefDatabase<Def>.GetNamed()
calls, jump to the XML definition for that DefNameClass=""
attributes to fetch the correct class to autocomplete from, such as in comps<MyMod.CustomThingDef>
)About.xml
to automatically include the defs of mods that you rely on in your workspaceIf you're interested in understanding how this plugin works or want to contribute, this section will probably be of interest to you.
To begin with, there are two "entry points" into this plugin from the IDE:
The RimworldReferenceProvider
and
the RimworldXMLItemProvider
.
The RimworldReferenceProvider
is a class
that tells Rider what references exist for a given XML Tag. In this context, a Reference is what the tag is attached to, so in
our case the C# classes and properties that lie behind the XML. This provider works by collecting the path of the XML tag
into a list (['Defs', 'ThingDef', 'defName']
as an example) and then walking down that list and matching it up to C# classes
and properties in those classes to come up with a C# Reference, then return that reference (In our example, it would have a reference
to ThingDef.defName
). This is the entry point if a user tries to Ctrl+Click on an XML Tag.
The second entry point is when a user starts entering a new tag name and we want to display the autocomplete list for
their options. This is the RimworldXMLItemProvider
.
The ItemProvider gets asked if it can provide any lookup items with it's IsAvailable
method, and if it return true
it'll then be asked to provide those items by having AddLookupItems
called.
Adding lookup items works in much the same way that adding references does: It creates a path list with
GetHierarchy
and then parses that list into a given C# Class with GetContextFromHierarchy
. The next step
is where it differs from ReferenceProvider: Rather than returning a reference to that class, it fetches all the fields
declared by that class or it's super classes and adds them to it's lookup list. This is the autocomplete list shown
to the user.
A few small extra things are needed to make it all work smoothly. The first is the
RimworldXMLCompletionContextProvider
.
By default the context that we're given in our Lookup Item Provider doesn't give us access to the full solution,
which we need to be able to look up the attached modules to the solution to find the Rimworld C# Module. So we have
our own custom context provider which grabs the solution from the XML File and adds it to the context.
The other is the RimworldXmlReference
which is just a class that we have to define to hold our reference from the XML to the C#.
VS Code Plugin When?
I don't use VS Code, so likely not any time soon.