Skip to content

Instantly share code, notes, and snippets.

@PrestonKnopp
Last active February 22, 2024 19:35
Show Gist options
  • Save PrestonKnopp/574848585732003dbbf6620115f864b9 to your computer and use it in GitHub Desktop.
Save PrestonKnopp/574848585732003dbbf6620115f864b9 to your computer and use it in GitHub Desktop.
Echo gdscript builtins from @GDScript.xml and @GlobalScope.xml for nvim-treesitter gdscript queries.
## Run: `nim r <thisfile>`
##
## Run this from the root of godot git repo.
##
## Parses and echos to stdout builtins from @GDScript.xml and @GlobalScope.xml
## in a format that is easy to include in
## https://github.com/nvim-treesitter/nvim-treesitter gdscript queries.
##
## - methods <methods><method name="EXTRACT_ME"></method>
## - members which are singletons <members><member name="EXTRACT_ME"></member></members>
## - constants <constants><constant name="EXTRACT_ME"></constant></constants>
## - annotations <annotations><annotation name="EXTRACT_ME"></annotation></annotations>
##
## Following that it echos builtin value types like Vector2, Projection from all
## the docs in doc/classes.
import std/[algorithm, os, sequtils, strutils, wordwrap, xmlparser, xmltree]
const
docPath = "doc/classes"
docPaths = [
"doc/classes/@GlobalScope.xml", "modules/gdscript/doc_classes/@GDScript.xml"
]
nodeNames = ["method", "member", "constant", "annotation"]
# ---
proc echoIdents(idents: var seq[string]) =
sort idents
echo idents.mapIt("\"$1\"" % it).join(" ").wrapWords(splitLongWords = false)
echo "\n"
# ---
for itPath in docPaths:
echo "; ", itPath, "\n"
let root = loadXml(itPath)
for itNodeName in nodeNames:
echo "; ", itNodeName, "\n"
var idents: seq[string]
for itNode in root.findAll(itNodeName):
var name = itNode.attr("name")
if name[0] == '@':
# Removes @ prefix.
name = name[1 .. ^1]
idents.add(name)
if idents.len > 0:
echoIdents idents
# ---
# Find built-in types like Vector2
echo "; ", docPath
echo "; builtin value types\n"
var idents: seq[string]
for class in walkFiles(docPath / "*.xml"):
let root = loadXml(class)
for itCtr in root.findAll("constructor"):
idents.add itCtr.attr("name")
# All these constructors will have the same name, so just break after the
# first one.
break
echoIdents idents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment