| 1 |
#![allow(clippy::manual_map)] |
| 2 |
use std::{fmt::Write, sync::OnceLock}; |
| 3 |
|
| 4 |
use log::{info, warn}; |
| 5 |
use regex::{Captures, Regex}; |
| 6 |
|
| 7 |
use super::{LocationInObject, gi_docgen}; |
| 8 |
use crate::{ |
| 9 |
Env, |
| 10 |
analysis::functions::Info, |
| 11 |
library::{FunctionKind, TypeId}, |
| 12 |
nameutil, |
| 13 |
parser::DocFormat, |
| 14 |
}; |
| 15 |
|
| 16 |
const LANGUAGE_SEP_BEGIN: &str = "<!-- language=\""; |
| 17 |
const LANGUAGE_SEP_END: &str = "\" -->"; |
| 18 |
const LANGUAGE_BLOCK_BEGIN: &str = "|["; |
| 19 |
const LANGUAGE_BLOCK_END: &str = "\n]|"; |
| 20 |
|
| 21 |
// A list of function names that are ignored when warning about a "not found |
| 22 |
// function" |
| 23 |
const IGNORE_C_WARNING_FUNCS: [&str; 6] = [ |
| 24 |
"g_object_unref", |
| 25 |
"g_object_ref", |
| 26 |
"g_free", |
| 27 |
"g_list_free", |
| 28 |
"g_strfreev", |
| 29 |
"printf", |
| 30 |
]; |
| 31 |
|
| 32 |
pub fn reformat_doc( |
| 33 |
input: &str, |
| 34 |
env: &Env, |
| 35 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 36 |
) -> String { |
| 37 |
code_blocks_transformation(input, env, in_type) |
| 38 |
} |
| 39 |
|
| 40 |
fn try_split<'a>(src: &'a str, needle: &str) -> (&'a str, Option<&'a str>) { |
| 41 |
match src.find(needle) { |
| 42 |
Some(pos) => (&src[..pos], Some(&src[pos + needle.len()..])), |
| 43 |
None => (src, None), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
fn code_blocks_transformation( |
| 48 |
mut input: &str, |
| 49 |
env: &Env, |
| 50 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 51 |
) -> String { |
| 52 |
let mut out = String::with_capacity(input.len()); |
| 53 |
|
| 54 |
loop { |
| 55 |
input = match try_split(input, LANGUAGE_BLOCK_BEGIN) { |
| 56 |
(before, Some(after)) => { |
| 57 |
out.push_str(&format(before, env, in_type)); |
| 58 |
if let (before, Some(after)) = |
| 59 |
try_split(get_language(after, &mut out), LANGUAGE_BLOCK_END) |
| 60 |
{ |
| 61 |
out.push_str(before); |
| 62 |
out.push_str("\n```"); |
| 63 |
after |
| 64 |
} else { |
| 65 |
after |
| 66 |
} |
| 67 |
} |
| 68 |
(before, None) => { |
| 69 |
out.push_str(&format(before, env, in_type)); |
| 70 |
return out; |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
fn get_language<'a>(entry: &'a str, out: &mut String) -> &'a str { |
| 77 |
if let (_, Some(after)) = try_split(entry, LANGUAGE_SEP_BEGIN) |
| 78 |
&& let (before, Some(after)) = try_split(after, LANGUAGE_SEP_END) |
| 79 |
{ |
| 80 |
if !["text", "rust"].contains(&before) { |
| 81 |
write!(out, "\n\n**⚠️ The following code is in {before} ⚠️**").unwrap(); |
| 82 |
} |
| 83 |
write!(out, "\n\n```{before}").unwrap(); |
| 84 |
return after; |
| 85 |
} |
| 86 |
out.push_str("\n```text"); |
| 87 |
entry |
| 88 |
} |
| 89 |
|
| 90 |
// try to get the language if any is defined or fallback to text |
| 91 |
fn get_markdown_language(input: &str) -> (&str, &str) { |
| 92 |
let (lang, after) = if let Some((lang, after)) = input.split_once('\n') { |
| 93 |
let lang = if lang.is_empty() { None } else { Some(lang) }; |
| 94 |
(lang, after) |
| 95 |
} else { |
| 96 |
(None, input) |
| 97 |
}; |
| 98 |
(lang.unwrap_or("text"), after) |
| 99 |
} |
| 100 |
|
| 101 |
// Re-format codeblocks & replaces the C types and GI-docgen with proper links |
| 102 |
fn format( |
| 103 |
mut input: &str, |
| 104 |
env: &Env, |
| 105 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 106 |
) -> String { |
| 107 |
let mut ret = String::with_capacity(input.len()); |
| 108 |
loop { |
| 109 |
input = match try_split(input, "```") { |
| 110 |
(before, Some(after)) => { |
| 111 |
// if we are inside a codeblock |
| 112 |
ret.push_str(&replace_symbols(before, env, in_type)); |
| 113 |
|
| 114 |
let (lang, after) = get_markdown_language(after); |
| 115 |
if !["text", "rust", "xml", "css", "json", "html"].contains(&lang) |
| 116 |
&& after.lines().count() > 1 |
| 117 |
{ |
| 118 |
write!(ret, "**⚠️ The following code is in {lang} ⚠️**\n\n").unwrap(); |
| 119 |
} |
| 120 |
writeln!(ret, "```{lang}").unwrap(); |
| 121 |
|
| 122 |
if let (before, Some(after)) = try_split(after, "```") { |
| 123 |
ret.push_str(before); |
| 124 |
ret.push_str("```"); |
| 125 |
after |
| 126 |
} else { |
| 127 |
after |
| 128 |
} |
| 129 |
} |
| 130 |
(before, None) => { |
| 131 |
ret.push_str(&replace_symbols(before, env, in_type)); |
| 132 |
return ret; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
fn replace_symbols( |
| 139 |
input: &str, |
| 140 |
env: &Env, |
| 141 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 142 |
) -> String { |
| 143 |
if env.library.doc_format == DocFormat::GiDocgen { |
| 144 |
let out = gi_docgen::replace_c_types(input, env, in_type); |
| 145 |
let out = gi_docgen_symbol().replace_all(&out, |caps: &Captures<'_>| match &caps[2] { |
| 146 |
"TRUE" => "[`true`]".to_string(), |
| 147 |
"FALSE" => "[`false`]".to_string(), |
| 148 |
"NULL" => "[`None`]".to_string(), |
| 149 |
symbol_name => match &caps[1] { |
| 150 |
// Opt-in only for the %SYMBOLS, @/# causes breakages |
| 151 |
"%" => find_constant_or_variant_wrapper(symbol_name, env, in_type), |
| 152 |
s => panic!("Unknown symbol prefix `{s}`"), |
| 153 |
}, |
| 154 |
}); |
| 155 |
let out = gdk_gtk().replace_all(&out, |caps: &Captures<'_>| { |
| 156 |
find_type(&caps[2], env).unwrap_or_else(|| format!("`{}`", &caps[2])) |
| 157 |
}); |
| 158 |
|
| 159 |
out.to_string() |
| 160 |
} else { |
| 161 |
replace_c_types(input, env, in_type) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
fn symbol() -> &'static Regex { |
| 166 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 167 |
REGEX.get_or_init(|| Regex::new(r"([@#%])(\w+\b)([:.]+[\w-]+\b)?").unwrap()) |
| 168 |
} |
| 169 |
|
| 170 |
fn gi_docgen_symbol() -> &'static Regex { |
| 171 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 172 |
REGEX.get_or_init(|| Regex::new(r"([%])(\w+\b)([:.]+[\w-]+\b)?").unwrap()) |
| 173 |
} |
| 174 |
|
| 175 |
fn function() -> &'static Regex { |
| 176 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 177 |
REGEX.get_or_init(|| Regex::new(r"([@#%])?(\w+\b[:.]+)?(\b[a-z0-9_]+)\(\)").unwrap()) |
| 178 |
} |
| 179 |
|
| 180 |
fn gdk_gtk() -> &'static Regex { |
| 181 |
// **note** |
| 182 |
// The optional . at the end is to make the regex more relaxed for some weird |
| 183 |
// broken cases on gtk3's docs it doesn't hurt other docs so please don't drop |
| 184 |
// it |
| 185 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 186 |
REGEX.get_or_init(|| { |
| 187 |
Regex::new(r"`([^\(:])?((G[dts]k|Pango|cairo_|graphene_|Adw|Hdy|GtkSource)\w+\b)(\.)?`") |
| 188 |
.unwrap() |
| 189 |
}) |
| 190 |
} |
| 191 |
|
| 192 |
fn tags() -> &'static Regex { |
| 193 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 194 |
REGEX.get_or_init(|| Regex::new(r"<[\w/-]+>").unwrap()) |
| 195 |
} |
| 196 |
|
| 197 |
fn spaces() -> &'static Regex { |
| 198 |
static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 199 |
REGEX.get_or_init(|| Regex::new(r"[ ]{2,}").unwrap()) |
| 200 |
} |
| 201 |
|
| 202 |
fn replace_c_types( |
| 203 |
entry: &str, |
| 204 |
env: &Env, |
| 205 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 206 |
) -> String { |
| 207 |
let out = function().replace_all(entry, |caps: &Captures<'_>| { |
| 208 |
let name = &caps[3]; |
| 209 |
find_method_or_function_by_ctype(None, name, env, in_type).unwrap_or_else(|| { |
| 210 |
if !IGNORE_C_WARNING_FUNCS.contains(&name) { |
| 211 |
info!("No function found for `{name}()`"); |
| 212 |
} |
| 213 |
format!("`{}{}()`", caps.get(2).map_or("", |m| m.as_str()), name) |
| 214 |
}) |
| 215 |
}); |
| 216 |
|
| 217 |
let out = symbol().replace_all(&out, |caps: &Captures<'_>| match &caps[2] { |
| 218 |
"TRUE" => "[`true`]".to_string(), |
| 219 |
"FALSE" => "[`false`]".to_string(), |
| 220 |
"NULL" => "[`None`]".to_string(), |
| 221 |
symbol_name => match &caps[1] { |
| 222 |
"%" => find_constant_or_variant_wrapper(symbol_name, env, in_type), |
| 223 |
"#" => { |
| 224 |
if let Some(member_path) = caps.get(3).map(|m| m.as_str()) { |
| 225 |
let method_name = member_path.trim_start_matches('.'); |
| 226 |
find_member(symbol_name, method_name, env, in_type).unwrap_or_else(|| { |
| 227 |
info!("`#{symbol_name}` not found as method"); |
| 228 |
format!("`{symbol_name}{member_path}`") |
| 229 |
}) |
| 230 |
} else if let Some(type_) = find_type(symbol_name, env) { |
| 231 |
type_ |
| 232 |
} else if let Some(constant_or_variant) = |
| 233 |
find_constant_or_variant(symbol_name, env, in_type) |
| 234 |
{ |
| 235 |
warn!( |
| 236 |
"`{symbol_name}` matches a constant/variant and should use `%` prefix instead of `#`" |
| 237 |
); |
| 238 |
constant_or_variant |
| 239 |
} else { |
| 240 |
info!("Type `#{symbol_name}` not found"); |
| 241 |
format!("`{symbol_name}`") |
| 242 |
} |
| 243 |
} |
| 244 |
"@" => { |
| 245 |
// XXX: Theoretically this code should check if the resulting |
| 246 |
// symbol truly belongs to `in_type`! |
| 247 |
if let Some(type_) = find_type(symbol_name, env) { |
| 248 |
warn!( |
| 249 |
"`{symbol_name}` matches a type and should use `#` prefix instead of `%`" |
| 250 |
); |
| 251 |
type_ |
| 252 |
} else if let Some(constant_or_variant) = |
| 253 |
find_constant_or_variant(symbol_name, env, in_type) |
| 254 |
{ |
| 255 |
constant_or_variant |
| 256 |
} else if let Some(function) = |
| 257 |
find_method_or_function_by_ctype(None, symbol_name, env, in_type) |
| 258 |
{ |
| 259 |
function |
| 260 |
} else { |
| 261 |
// `@` is often used to refer to fields and function parameters. |
| 262 |
format!("`{symbol_name}`") |
| 263 |
} |
| 264 |
} |
| 265 |
s => panic!("Unknown symbol prefix `{s}`"), |
| 266 |
}, |
| 267 |
}); |
| 268 |
let out = gdk_gtk().replace_all(&out, |caps: &Captures<'_>| { |
| 269 |
find_type(&caps[2], env).unwrap_or_else(|| format!("`{}`", &caps[2])) |
| 270 |
}); |
| 271 |
let out = tags().replace_all(&out, "`$0`"); |
| 272 |
spaces().replace_all(&out, " ").into_owned() |
| 273 |
} |
| 274 |
|
| 275 |
/// Wrapper around [`find_constant_or_variant`] that fallbacks to returning |
| 276 |
/// the `symbol_name` |
| 277 |
fn find_constant_or_variant_wrapper( |
| 278 |
symbol_name: &str, |
| 279 |
env: &Env, |
| 280 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 281 |
) -> String { |
| 282 |
find_constant_or_variant(symbol_name, env, in_type).unwrap_or_else(|| { |
| 283 |
info!("Constant or variant `%{symbol_name}` not found"); |
| 284 |
format!("`{symbol_name}`") |
| 285 |
}) |
| 286 |
} |
| 287 |
|
| 288 |
fn find_member( |
| 289 |
type_: &str, |
| 290 |
method_name: &str, |
| 291 |
env: &Env, |
| 292 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 293 |
) -> Option<String> { |
| 294 |
let symbols = env.symbols.borrow(); |
| 295 |
let is_signal = method_name.starts_with("::"); |
| 296 |
let is_property = !is_signal && method_name.starts_with(':'); |
| 297 |
if !is_signal && !is_property { |
| 298 |
find_method_or_function_by_ctype(Some(type_), method_name, env, in_type) |
| 299 |
} else { |
| 300 |
env.analysis |
| 301 |
.objects |
| 302 |
.values() |
| 303 |
.find(|o| o.c_type == type_) |
| 304 |
.map(|info| { |
| 305 |
let sym = symbols.by_tid(info.type_id).unwrap(); // we are sure the object exists |
| 306 |
let name = method_name.trim_start_matches(':'); |
| 307 |
if is_property { |
| 308 |
gen_property_doc_link(&sym.full_rust_name(), name) |
| 309 |
} else { |
| 310 |
gen_signal_doc_link(&sym.full_rust_name(), name) |
| 311 |
} |
| 312 |
}) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
fn find_constant_or_variant( |
| 317 |
symbol: &str, |
| 318 |
env: &Env, |
| 319 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 320 |
) -> Option<String> { |
| 321 |
if let Some((flag_info, member_info)) = env.analysis.flags.iter().find_map(|f| { |
| 322 |
f.type_(&env.library) |
| 323 |
.members |
| 324 |
.iter() |
| 325 |
.find(|m| m.c_identifier == symbol && !m.status.ignored()) |
| 326 |
.map(|m| (f, m)) |
| 327 |
}) { |
| 328 |
Some(gen_member_doc_link( |
| 329 |
flag_info.type_id, |
| 330 |
&nameutil::bitfield_member_name(&member_info.name), |
| 331 |
env, |
| 332 |
in_type, |
| 333 |
)) |
| 334 |
} else if let Some((enum_info, member_info)) = env.analysis.enumerations.iter().find_map(|e| { |
| 335 |
e.type_(&env.library) |
| 336 |
.members |
| 337 |
.iter() |
| 338 |
.find(|m| m.c_identifier == symbol && !m.status.ignored()) |
| 339 |
.map(|m| (e, m)) |
| 340 |
}) { |
| 341 |
Some(gen_member_doc_link( |
| 342 |
enum_info.type_id, |
| 343 |
&nameutil::enum_member_name(&member_info.name), |
| 344 |
env, |
| 345 |
in_type, |
| 346 |
)) |
| 347 |
} else if let Some(const_info) = env |
| 348 |
.analysis |
| 349 |
.constants |
| 350 |
.iter() |
| 351 |
.find(|c| c.glib_name == symbol) |
| 352 |
{ |
| 353 |
Some(gen_const_doc_link(const_info)) |
| 354 |
} else { |
| 355 |
None |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
// A list of types that are automatically ignored by the `find_type` function |
| 360 |
const IGNORED_C_TYPES: [&str; 6] = [ |
| 361 |
"gconstpointer", |
| 362 |
"guint16", |
| 363 |
"guint", |
| 364 |
"gunicode", |
| 365 |
"gchararray", |
| 366 |
"GList", |
| 367 |
]; |
| 368 |
/// either an object/interface, record, enum or a flag |
| 369 |
fn find_type(type_: &str, env: &Env) -> Option<String> { |
| 370 |
if IGNORED_C_TYPES.contains(&type_) { |
| 371 |
return None; |
| 372 |
} |
| 373 |
|
| 374 |
let type_id = if let Some(obj) = env.analysis.objects.values().find(|o| o.c_type == type_) { |
| 375 |
Some(obj.type_id) |
| 376 |
} else if let Some(record) = env |
| 377 |
.analysis |
| 378 |
.records |
| 379 |
.values() |
| 380 |
.find(|r| r.type_(&env.library).c_type == type_) |
| 381 |
{ |
| 382 |
Some(record.type_id) |
| 383 |
} else if let Some(enum_) = env |
| 384 |
.analysis |
| 385 |
.enumerations |
| 386 |
.iter() |
| 387 |
.find(|e| e.type_(&env.library).c_type == type_) |
| 388 |
{ |
| 389 |
Some(enum_.type_id) |
| 390 |
} else if let Some(flag) = env |
| 391 |
.analysis |
| 392 |
.flags |
| 393 |
.iter() |
| 394 |
.find(|f| f.type_(&env.library).c_type == type_) |
| 395 |
{ |
| 396 |
Some(flag.type_id) |
| 397 |
} else { |
| 398 |
None |
| 399 |
}; |
| 400 |
|
| 401 |
type_id.map(|ty| gen_symbol_doc_link(ty, env)) |
| 402 |
} |
| 403 |
|
| 404 |
fn find_method_or_function_by_ctype( |
| 405 |
c_type: Option<&str>, |
| 406 |
name: &str, |
| 407 |
env: &Env, |
| 408 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 409 |
) -> Option<String> { |
| 410 |
find_method_or_function( |
| 411 |
env, |
| 412 |
in_type, |
| 413 |
|f| f.glib_name == name, |
| 414 |
|o| c_type.is_none_or(|t| o.c_type == t), |
| 415 |
|r| c_type.is_none_or(|t| r.type_(&env.library).c_type == t), |
| 416 |
|r| c_type.is_none_or(|t| r.type_(&env.library).c_type == t), |
| 417 |
|r| c_type.is_none_or(|t| r.type_(&env.library).c_type == t), |
| 418 |
c_type.is_some_and(|t| t.ends_with("Class")), |
| 419 |
false, |
| 420 |
) |
| 421 |
} |
| 422 |
|
| 423 |
/// Find a function in all the possible items, if not found return the original |
| 424 |
/// name surrounded with backticks. A function can either be a |
| 425 |
/// struct/interface/record method, a global function or maybe a virtual |
| 426 |
/// function |
| 427 |
/// |
| 428 |
/// This function is generic so it can be de-duplicated between a |
| 429 |
/// - [`find_method_or_function_by_ctype()`] where the object/records are looked |
| 430 |
/// by their C name |
| 431 |
/// - [`gi_docgen::find_method_or_function_by_name()`] where the object/records |
| 432 |
/// are looked by their name |
| 433 |
pub(crate) fn find_method_or_function( |
| 434 |
env: &Env, |
| 435 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 436 |
search_fn: impl Fn(&crate::analysis::functions::Info) -> bool + Copy, |
| 437 |
search_obj: impl Fn(&crate::analysis::object::Info) -> bool + Copy, |
| 438 |
search_record: impl Fn(&crate::analysis::record::Info) -> bool + Copy, |
| 439 |
search_enum: impl Fn(&crate::analysis::enums::Info) -> bool + Copy, |
| 440 |
search_flag: impl Fn(&crate::analysis::flags::Info) -> bool + Copy, |
| 441 |
is_class_method: bool, |
| 442 |
is_virtual_method: bool, |
| 443 |
) -> Option<String> { |
| 444 |
if is_virtual_method { |
| 445 |
if let Some((obj_info, fn_info)) = env |
| 446 |
.analysis |
| 447 |
.find_object_by_virtual_method(env, search_obj, search_fn) |
| 448 |
{ |
| 449 |
Some(gen_object_fn_doc_link( |
| 450 |
obj_info, |
| 451 |
fn_info, |
| 452 |
env, |
| 453 |
in_type, |
| 454 |
&obj_info.name, |
| 455 |
)) |
| 456 |
} else { |
| 457 |
None |
| 458 |
} |
| 459 |
} else if is_class_method { |
| 460 |
if let Some((record_info, fn_info)) = |
| 461 |
env.analysis |
| 462 |
.find_record_by_function(env, search_record, search_fn) |
| 463 |
{ |
| 464 |
let object = env.config.objects.get(&record_info.full_name); |
| 465 |
let visible_parent = object |
| 466 |
.and_then(|o| o.trait_name.clone()) |
| 467 |
.unwrap_or_else(|| format!("{}Ext", record_info.name)); |
| 468 |
let parent = format!("subclass::prelude::{visible_parent}"); |
| 469 |
let is_self = in_type == Some((&record_info.type_id, None)); |
| 470 |
Some(fn_info.doc_link(Some(&parent), Some(&visible_parent), is_self)) |
| 471 |
} else { |
| 472 |
None |
| 473 |
} |
| 474 |
// if we can find the function in an object |
| 475 |
} else if let Some((obj_info, fn_info)) = env |
| 476 |
.analysis |
| 477 |
.find_object_by_function(env, search_obj, search_fn) |
| 478 |
{ |
| 479 |
Some(gen_object_fn_doc_link( |
| 480 |
obj_info, |
| 481 |
fn_info, |
| 482 |
env, |
| 483 |
in_type, |
| 484 |
&obj_info.name, |
| 485 |
)) |
| 486 |
// or in a record |
| 487 |
} else if let Some((record_info, fn_info)) = |
| 488 |
env.analysis |
| 489 |
.find_record_by_function(env, search_record, search_fn) |
| 490 |
{ |
| 491 |
Some(gen_type_fn_doc_link( |
| 492 |
record_info.type_id, |
| 493 |
fn_info, |
| 494 |
env, |
| 495 |
in_type, |
| 496 |
)) |
| 497 |
} else if let Some((enum_info, fn_info)) = |
| 498 |
env.analysis |
| 499 |
.find_enum_by_function(env, search_enum, search_fn) |
| 500 |
{ |
| 501 |
Some(gen_type_fn_doc_link( |
| 502 |
enum_info.type_id, |
| 503 |
fn_info, |
| 504 |
env, |
| 505 |
in_type, |
| 506 |
)) |
| 507 |
} else if let Some((flag_info, fn_info)) = |
| 508 |
env.analysis |
| 509 |
.find_flag_by_function(env, search_flag, search_fn) |
| 510 |
{ |
| 511 |
Some(gen_type_fn_doc_link( |
| 512 |
flag_info.type_id, |
| 513 |
fn_info, |
| 514 |
env, |
| 515 |
in_type, |
| 516 |
)) |
| 517 |
// or as a global function |
| 518 |
} else if let Some(fn_info) = env.analysis.find_global_function(env, search_fn) { |
| 519 |
Some(fn_info.doc_link(None, None, false)) |
| 520 |
} else { |
| 521 |
None |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
pub(crate) fn gen_type_fn_doc_link( |
| 526 |
type_id: TypeId, |
| 527 |
fn_info: &Info, |
| 528 |
env: &Env, |
| 529 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 530 |
) -> String { |
| 531 |
let symbols = env.symbols.borrow(); |
| 532 |
let sym_name = symbols.by_tid(type_id).unwrap().full_rust_name(); |
| 533 |
let is_self = in_type == Some((&type_id, None)); |
| 534 |
|
| 535 |
fn_info.doc_link(Some(&sym_name), None, is_self) |
| 536 |
} |
| 537 |
|
| 538 |
pub(crate) fn gen_object_fn_doc_link( |
| 539 |
obj_info: &crate::analysis::object::Info, |
| 540 |
fn_info: &Info, |
| 541 |
env: &Env, |
| 542 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 543 |
visible_name: &str, |
| 544 |
) -> String { |
| 545 |
let symbols = env.symbols.borrow(); |
| 546 |
let sym = symbols.by_tid(obj_info.type_id).unwrap(); |
| 547 |
let is_self = in_type == Some((&obj_info.type_id, Some(obj_info.function_location(fn_info)))); |
| 548 |
|
| 549 |
if fn_info.kind == FunctionKind::VirtualMethod || fn_info.kind == FunctionKind::ClassMethod { |
| 550 |
let (type_name, visible_type_name) = obj_info.generate_doc_link_info(fn_info); |
| 551 |
fn_info.doc_link( |
| 552 |
Some(&sym.full_rust_name().replace(visible_name, &type_name)), |
| 553 |
Some(&visible_type_name), |
| 554 |
false, |
| 555 |
) |
| 556 |
} else if fn_info.kind == FunctionKind::Method { |
| 557 |
let (type_name, visible_type_name) = obj_info.generate_doc_link_info(fn_info); |
| 558 |
|
| 559 |
fn_info.doc_link( |
| 560 |
Some(&sym.full_rust_name().replace(visible_name, &type_name)), |
| 561 |
Some(&visible_type_name), |
| 562 |
is_self, |
| 563 |
) |
| 564 |
} else { |
| 565 |
fn_info.doc_link(Some(&sym.full_rust_name()), None, is_self) |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
// Helper function to generate a doc link for an enum member/bitfield variant |
| 570 |
pub(crate) fn gen_member_doc_link( |
| 571 |
type_id: TypeId, |
| 572 |
member_name: &str, |
| 573 |
env: &Env, |
| 574 |
in_type: Option<(&TypeId, Option<LocationInObject>)>, |
| 575 |
) -> String { |
| 576 |
let symbols = env.symbols.borrow(); |
| 577 |
let sym = symbols.by_tid(type_id).unwrap().full_rust_name(); |
| 578 |
let is_self = in_type == Some((&type_id, None)); |
| 579 |
|
| 580 |
if is_self { |
| 581 |
format!("[`{member_name}`][Self::{member_name}]") |
| 582 |
} else { |
| 583 |
format!("[`{sym}::{member_name}`][crate::{sym}::{member_name}]") |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
pub(crate) fn gen_const_doc_link(const_info: &crate::analysis::constants::Info) -> String { |
| 588 |
// for whatever reason constants are not part of the symbols list |
| 589 |
format!("[`{n}`][crate::{n}]", n = const_info.name) |
| 590 |
} |
| 591 |
|
| 592 |
pub(crate) fn gen_signal_doc_link(symbol: &str, signal: &str) -> String { |
| 593 |
format!("[`{signal}`][struct@crate::{symbol}#{signal}]") |
| 594 |
} |
| 595 |
|
| 596 |
pub(crate) fn gen_property_doc_link(symbol: &str, property: &str) -> String { |
| 597 |
format!("[`{property}`][struct@crate::{symbol}#{property}]") |
| 598 |
} |
| 599 |
|
| 600 |
pub(crate) fn gen_vfunc_doc_link(symbol: &str, vfunc: &str) -> String { |
| 601 |
format!("`vfunc::{symbol}::{vfunc}`") |
| 602 |
} |
| 603 |
|
| 604 |
pub(crate) fn gen_callback_doc_link(callback: &str) -> String { |
| 605 |
format!("`callback::{callback}") |
| 606 |
} |
| 607 |
|
| 608 |
pub(crate) fn gen_alias_doc_link(alias: &str) -> String { |
| 609 |
format!("`alias::{alias}`") |
| 610 |
} |
| 611 |
|
| 612 |
pub(crate) fn gen_symbol_doc_link(type_id: TypeId, env: &Env) -> String { |
| 613 |
let symbols = env.symbols.borrow(); |
| 614 |
let sym = symbols.by_tid(type_id).unwrap(); |
| 615 |
// Workaround the case of glib::Variant being a derive macro and a struct |
| 616 |
if sym.name() == "Variant" && (sym.crate_name().is_none() || sym.crate_name() == Some("glib")) { |
| 617 |
format!("[`{n}`][struct@crate::{n}]", n = sym.full_rust_name()) |
| 618 |
} else { |
| 619 |
format!("[`{n}`][crate::{n}]", n = sym.full_rust_name()) |
| 620 |
} |
| 621 |
} |