Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly dedent doc strings after parsing in the proc-macro #1899

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/docstring-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ enum AssociatedErrorTest {
}

/// <docstring-object>
///
/// <docstring-object-indented-1>
/// <docstring-object-indented-2>
#[derive(uniffi::Object)]
pub struct ObjectTest {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
assert AssociatedErrorTest.Test2.__doc__ == "<docstring-associated-error-variant-2>"

# Test objects
assert ObjectTest.__doc__ == "<docstring-object>"
expected_object_doc = """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasn't immediately clear to me why this was expected. Can you please see if a test in fixtures/procmacros isn't too difficult, and there you can test the string without the language-specific indents added by languages, and also comment about exactly why the value is what it is (ie, describe in words the intent of the dedent). Ideally here you'd also explain the same (but in this case, IIUC, Python has added an indent to match the indent of the object definition in the generated .py code?)

<docstring-object>

<docstring-object-indented-1>
<docstring-object-indented-2>
"""
assert ObjectTest.__doc__ == expected_object_doc
assert ObjectTest.__init__.__doc__ == "<docstring-primary-constructor>"
assert ObjectTest.new_alternate.__doc__ == "<docstring-alternate-constructor>"
assert ObjectTest.test.__doc__ == "<docstring-method>"
Expand Down
3 changes: 3 additions & 0 deletions fixtures/docstring/src/docstring.udl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ interface AssociatedErrorTest {
};

/// <docstring-object>
///
/// <docstring-object-indented-1>
/// <docstring-object-indented-2>
interface ObjectTest {
/// <docstring-primary-constructor>
constructor();
Expand Down
8 changes: 7 additions & 1 deletion fixtures/docstring/tests/bindings/test_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
assert AssociatedErrorTest.Test2.__doc__ == "<docstring-associated-error-variant-2>"

# Test objects
assert ObjectTest.__doc__ == "<docstring-object>"
expected_object_doc = """
<docstring-object>

<docstring-object-indented-1>
<docstring-object-indented-2>
"""
assert ObjectTest.__doc__ == expected_object_doc
assert ObjectTest.__init__.__doc__ == "<docstring-primary-constructor>"
assert ObjectTest.new_alternate.__doc__ == "<docstring-alternate-constructor>"
assert ObjectTest.test.__doc__ == "<docstring-method>"
Expand Down
1 change: 1 addition & 0 deletions uniffi_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ proc-macro2 = "1.0"
quote = "1.0"
serde = "1.0.136"
syn = { version = "2.0", features = ["full", "visit-mut"] }
textwrap = "0.16.0"
toml = "0.5.9"
uniffi_build = { path = "../uniffi_build", version = "=0.25.3" }
uniffi_meta = { path = "../uniffi_meta", version = "=0.25.3" }
Expand Down
4 changes: 2 additions & 2 deletions uniffi_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ pub(crate) fn extract_docstring(attrs: &[Attribute]) -> syn::Result<String> {
let name_value = attr.meta.require_name_value()?;
if let Expr::Lit(expr) = &name_value.value {
if let Lit::Str(lit_str) = &expr.lit {
return Ok(lit_str.value().trim().to_owned());
return Ok(lit_str.value());
}
}
Err(syn::Error::new_spanned(attr, "Cannot parse doc attribute"))
})
.collect::<syn::Result<Vec<_>>>()
.map(|lines| lines.join("\n"));
.map(|lines| textwrap::dedent(&lines.join("\n")));
}