From 768096e5cf9fa1c4db069c7b196976e354c22f4c Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 23 May 2024 17:32:51 -0700 Subject: [PATCH] typegen: add support for generics --- flake.lock | 8 ++++---- typegen/src/parse.rs | 6 +++++- typegen/src/resolver.rs | 42 +++++++++++++++++++---------------------- types/QtQuick.json | 20 ++++++++++++++++++++ 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/flake.lock b/flake.lock index 9a4e7c9..29bb0ee 100644 --- a/flake.lock +++ b/flake.lock @@ -22,11 +22,11 @@ ] }, "locked": { - "lastModified": 1716289830, - "narHash": "sha256-kPxodbNTnCLtJ6MFih9LPULXh5l3st63t9isWIwvYSQ=", + "lastModified": 1716510487, + "narHash": "sha256-kdQ4CjAl9H7VSfo+vWfQZynBpqCeNovIgnHg6iQ+3hs=", "ref": "refs/heads/master", - "rev": "af45502913e0bb857755b2fa7e3ef991fff033ba", - "revCount": 179, + "rev": "5016dbf0d4c9e12f7e3d5f872b3063785d20b2bf", + "revCount": 184, "type": "git", "url": "https://git.outfoxxed.me/outfoxxed/quickshell" }, diff --git a/typegen/src/parse.rs b/typegen/src/parse.rs index bc2d37f..33637ea 100644 --- a/typegen/src/parse.rs +++ b/typegen/src/parse.rs @@ -155,7 +155,7 @@ impl Parser { let class = class?; let comment = class.name("comment").map(|m| m.as_str()); - let name = class.name("name").unwrap().as_str(); + let mut name = class.name("name").unwrap().as_str(); let mut superclass = class.name("super").map(|m| m.as_str()); let body = class.name("body").unwrap().as_str(); @@ -189,6 +189,10 @@ impl Parser { "QSDOC_BASECLASS must have the base class as an argument", )) }, + "QSDOC_CNAME" => { + name = args + .expect("QSDOC_CNAME must specify the cname as an argument"); + }, "Q_OBJECT" => classtype = Some(ClassType::Object), "Q_GADGET" => classtype = Some(ClassType::Gadget), "QML_ELEMENT" | "QSDOC_ELEMENT" => qml_name = Some(name), diff --git a/typegen/src/resolver.rs b/typegen/src/resolver.rs index e89ac95..273f1ee 100644 --- a/typegen/src/resolver.rs +++ b/typegen/src/resolver.rs @@ -56,7 +56,11 @@ pub fn resolve_types( } }; - fn qmlparamtype(ctype: &str, typespec: &TypeSpec) -> outform::Type { + fn qmlparamtype(mut ctype: &str, typespec: &TypeSpec) -> outform::Type { + if ctype.ends_with('*') { + ctype = &ctype[0..ctype.len() - 1]; + } + let qtype = typespec .typemap .iter() @@ -79,7 +83,7 @@ pub fn resolve_types( } fn solveprop(prop: &Property, typespec: &TypeSpec) -> outform::Property { - let mut ctype = &prop.type_[..]; + let ctype = &prop.type_[..]; let flags = { let mut flags = Vec::new(); @@ -112,32 +116,24 @@ pub fn resolve_types( flags, }, None => { - let mut list = false; + let (ctype, of) = match ctype.split_once('<') { + None => (ctype, None), + Some((ctype, mut remaining)) => { + if remaining.ends_with('*') { + remaining = &remaining[0..remaining.len() - 1]; + } - if ctype.starts_with("QQmlListProperty<") { - ctype = &ctype[17..ctype.len() - 1]; - list = true; - } else if ctype.starts_with("QList<") { - ctype = &ctype[6..ctype.len() - 1]; - list = true; - } else if ctype.starts_with("QVector<") { - ctype = &ctype[8..ctype.len() - 1]; - list = true; - } + // closing `>` + remaining = &remaining[0..remaining.len() - 1]; - if ctype.ends_with('*') { - ctype = &ctype[0..ctype.len() - 1]; - } + (ctype, Some(remaining)) + }, + }; let mut type_ = qmlparamtype(ctype, typespec); - if list { - type_ = outform::Type { - type_: outform::TypeSource::Qt, - module: "qml".to_string(), - name: "list".to_string(), - of: Some(Box::new(type_)), - }; + if let Some(of) = of { + type_.of = Some(Box::new(qmlparamtype(of, typespec))); } outform::Property { diff --git a/types/QtQuick.json b/types/QtQuick.json index 29df6a3..f4c2211 100644 --- a/types/QtQuick.json +++ b/types/QtQuick.json @@ -25,6 +25,11 @@ "cname": "quint32", "module": null }, + { + "name": "int", + "cname": "qsizetype", + "module": null + }, { "name": "real", "cname": "qreal", @@ -60,6 +65,21 @@ "cname": "QObject", "module": "qml.QtQml" }, + { + "name": "list", + "cname": "QQmlListProperty", + "module": null + }, + { + "name": "list", + "cname": "QList", + "module": null + }, + { + "name": "list", + "cname": "QVector", + "module": null + }, { "name": "Component", "cname": "QQmlComponent",