Skip to content

Commit

Permalink
Merge pull request #798 from EPashkin/function_doc_trait_name
Browse files Browse the repository at this point in the history
Add overriding for function trait for manual implemented functions
  • Loading branch information
EPashkin committed Jun 23, 2019
2 parents fab39bc + 31c723f commit 20feecf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ generate_builder = true
doc_hidden = true
# disable length_of autodetection
disable_length_detect = true
# write function docs to trait other than default "xxxExt",
# also works in [object.signal] and [object.property]
doc_trait_name = "SocketListenerExtManual"
# override for parameter
[[object.function.parameter]]
# filter by name
Expand Down
54 changes: 49 additions & 5 deletions src/codegen/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ fn create_object_doc(w: &mut dyn Write, env: &Env, info: &analysis::object::Info
let signals: &[Signal];
let properties: &[Property];

let obj = env
.config
.objects
.get(&info.full_name)
.expect("Object not found");

match *env.library.type_(info.type_id) {
Type::Class(ref cl) => {
doc = cl.doc.as_ref();
Expand Down Expand Up @@ -239,17 +245,54 @@ fn create_object_doc(w: &mut dyn Write, env: &Env, info: &analysis::object::Info

for function in functions {
let ty = if has_trait && function.parameters.iter().any(|p| p.instance_parameter) {
ty_ext.clone()
let configured_functions = obj.functions.matched(&function.name);
if let Some(trait_name) = configured_functions
.iter()
.filter_map(|f| f.doc_trait_name.as_ref())
.next()
{
TypeStruct::new(SType::Trait, trait_name)
} else {
ty_ext.clone()
}
} else {
ty.clone()
};
create_fn_doc(w, env, function, Some(Box::new(ty)))?;
}
for signal in signals {
create_fn_doc(w, env, signal, Some(Box::new(ty_ext.clone())))?;
let ty = if has_trait {
let configured_signals = obj.signals.matched(&signal.name);
if let Some(trait_name) = configured_signals
.iter()
.filter_map(|f| f.doc_trait_name.as_ref())
.next()
{
TypeStruct::new(SType::Trait, trait_name)
} else {
ty_ext.clone()
}
} else {
ty.clone()
};
create_fn_doc(w, env, signal, Some(Box::new(ty)))?;
}
for property in properties {
create_property_doc(w, env, property, Some(Box::new(ty_ext.clone())))?;
let ty = if has_trait {
let configured_properties = obj.properties.matched(&property.name);
if let Some(trait_name) = configured_properties
.iter()
.filter_map(|f| f.doc_trait_name.as_ref())
.next()
{
TypeStruct::new(SType::Trait, trait_name)
} else {
ty_ext.clone()
}
} else {
ty.clone()
};
create_property_doc(w, env, property, Some(Box::new(ty)))?;
}
Ok(())
}
Expand Down Expand Up @@ -447,19 +490,20 @@ fn create_property_doc(
{
return Ok(());
}
let name_for_func = nameutil::signal_to_snake(&property.name);
let mut v = Vec::with_capacity(2);

let symbols = env.symbols.borrow();
if property.readable {
v.push(TypeStruct {
parent: parent.clone(),
..TypeStruct::new(SType::Fn, &format!("get_property_{}", property.name))
..TypeStruct::new(SType::Fn, &format!("get_property_{}", name_for_func))
});
}
if property.writable {
v.push(TypeStruct {
parent,
..TypeStruct::new(SType::Fn, &format!("set_property_{}", property.name))
..TypeStruct::new(SType::Fn, &format!("set_property_{}", name_for_func))
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/config/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub struct Function {
pub doc_hidden: bool,
pub is_windows_utf8: bool,
pub disable_length_detect: bool,
pub doc_trait_name: Option<String>,
}

impl Parse for Function {
Expand All @@ -192,6 +193,7 @@ impl Parse for Function {
"is_windows_utf8",
"disable_length_detect",
"pattern",
"doc_trait_name",
],
&format!("function {}", object_name),
);
Expand Down Expand Up @@ -222,6 +224,10 @@ impl Parse for Function {
.lookup("disable_length_detect")
.and_then(Value::as_bool)
.unwrap_or(false);
let doc_trait_name = toml
.lookup("doc_trait_name")
.and_then(Value::as_str)
.map(ToOwned::to_owned);

Some(Function {
ident,
Expand All @@ -233,6 +239,7 @@ impl Parse for Function {
doc_hidden,
is_windows_utf8,
disable_length_detect,
doc_trait_name,
})
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/config/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Property {
pub ignore: bool,
pub version: Option<Version>,
pub generate: Option<PropertyGenerateFlags>,
pub doc_trait_name: Option<String>,
}

impl Parse for Property {
Expand All @@ -30,7 +31,14 @@ impl Parse for Property {
};

toml.check_unwanted(
&["ignore", "version", "name", "pattern", "generate"],
&[
"ignore",
"version",
"name",
"pattern",
"generate",
"doc_trait_name",
],
&format!("property {}", object_name),
);

Expand All @@ -47,12 +55,17 @@ impl Parse for Property {
.map_err(|e| error!("{} for object {}", e, object_name))
.ok()
});
let doc_trait_name = toml
.lookup("doc_trait_name")
.and_then(Value::as_str)
.map(ToOwned::to_owned);

Some(Property {
ident,
ignore,
version,
generate,
doc_trait_name,
})
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct Signal {
pub ret: Return,
pub concurrency: library::Concurrency,
pub doc_hidden: bool,
pub doc_trait_name: Option<String>,
}

impl Signal {
Expand Down Expand Up @@ -136,6 +137,7 @@ impl Signal {
"name",
"pattern",
"concurrency",
"doc_trait_name",
],
&format!("signal {}", object_name),
);
Expand Down Expand Up @@ -166,6 +168,10 @@ impl Signal {
.lookup("doc_hidden")
.and_then(Value::as_bool)
.unwrap_or(false);
let doc_trait_name = toml
.lookup("doc_trait_name")
.and_then(Value::as_str)
.map(ToOwned::to_owned);

Some(Signal {
ident,
Expand All @@ -176,6 +182,7 @@ impl Signal {
ret,
concurrency,
doc_hidden,
doc_trait_name,
})
}
}
Expand Down

0 comments on commit 20feecf

Please sign in to comment.