|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +use crate::registry::plugin::PluginItem; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +/// Created for documentation on |
| 11 | +/// ```ignore |
| 12 | +/// #[derive(GodotClass)] |
| 13 | +/// /// Documented |
| 14 | +/// struct Struct { |
| 15 | +/// /// documented |
| 16 | +/// x: f32, |
| 17 | +/// } |
| 18 | +/// ``` |
| 19 | +#[derive(Clone, Copy, Debug, Default)] |
| 20 | +pub struct StructDocs { |
| 21 | + pub base: &'static str, |
| 22 | + pub description: &'static str, |
| 23 | + pub members: &'static str, |
| 24 | +} |
| 25 | + |
| 26 | +/// Created for documentation on |
| 27 | +/// ```ignore |
| 28 | +/// #[godot_api] |
| 29 | +/// impl Struct { |
| 30 | +/// #[func] |
| 31 | +/// /// This function panics! |
| 32 | +/// fn panic() -> f32 { panic!() } |
| 33 | +/// } |
| 34 | +/// ``` |
| 35 | +#[derive(Clone, Copy, Debug, Default)] |
| 36 | +pub struct InherentImplDocs { |
| 37 | + pub methods: &'static str, |
| 38 | + pub signals: &'static str, |
| 39 | + pub constants: &'static str, |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Default)] |
| 43 | +struct DocPieces { |
| 44 | + definition: StructDocs, |
| 45 | + inherent: InherentImplDocs, |
| 46 | + virtual_methods: &'static str, |
| 47 | +} |
| 48 | + |
| 49 | +#[doc(hidden)] |
| 50 | +/// This function scours the registered plugins to find their documentation pieces, |
| 51 | +/// and strings them together. |
| 52 | +/// |
| 53 | +/// It returns an iterator over XML documents. |
| 54 | +pub fn gather_xml_docs() -> impl Iterator<Item = String> { |
| 55 | + let mut map = HashMap::<&'static str, DocPieces>::new(); |
| 56 | + crate::private::iterate_plugins(|x| match x.item { |
| 57 | + PluginItem::InherentImpl { |
| 58 | + docs: Some(docs), .. |
| 59 | + } => map.entry(x.class_name.as_str()).or_default().inherent = docs, |
| 60 | + PluginItem::ITraitImpl { |
| 61 | + virtual_method_docs, |
| 62 | + .. |
| 63 | + } => { |
| 64 | + map.entry(x.class_name.as_str()) |
| 65 | + .or_default() |
| 66 | + .virtual_methods = virtual_method_docs |
| 67 | + } |
| 68 | + PluginItem::Struct { |
| 69 | + docs: Some(docs), .. |
| 70 | + } => map.entry(x.class_name.as_str()).or_default().definition = docs, |
| 71 | + _ => (), |
| 72 | + }); |
| 73 | + map.into_iter().map(|(class, pieces)| { |
| 74 | + let StructDocs { |
| 75 | + base, |
| 76 | + description, |
| 77 | + members, |
| 78 | + } = pieces.definition; |
| 79 | + |
| 80 | + let InherentImplDocs { |
| 81 | + methods, |
| 82 | + signals, |
| 83 | + constants, |
| 84 | + } = pieces.inherent; |
| 85 | + |
| 86 | + let virtual_methods = pieces.virtual_methods; |
| 87 | + let brief = description.lines().next().unwrap_or_default(); |
| 88 | +format!(r#" |
| 89 | +<?xml version="1.0" encoding="UTF-8"?> |
| 90 | +<class name="{class}" inherits="{base}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 91 | +<brief_description>{brief}</brief_description> |
| 92 | +<description>{description}</description> |
| 93 | +<methods>{methods}{virtual_methods}</methods> |
| 94 | +<constants>{constants}</constants> |
| 95 | +<signals>{signals}</signals> |
| 96 | +<members>{members}</members> |
| 97 | +</class>"#) |
| 98 | + }, |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +/// # Safety |
| 103 | +/// |
| 104 | +/// The Godot binding must have been initialized before calling this function. |
| 105 | +/// |
| 106 | +/// If "experimental-threads" is not enabled, then this must be called from the same thread that the bindings were initialized from. |
| 107 | +pub unsafe fn register() { |
| 108 | + for xml in gather_xml_docs() { |
| 109 | + crate::sys::interface_fn!(editor_help_load_xml_from_utf8_chars_and_len)( |
| 110 | + xml.as_ptr() as *const std::ffi::c_char, |
| 111 | + xml.len() as i64, |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments