Skip to content

Commit 0a8b0eb

Browse files
samooyozerosnacksgregorsternat
authored andcommitted
feat(forge): add params natspec for enums (foundry-rs#10022)
* feat(forge): add params natspec for enums * chore: cargo fmt * fix: clippy errors * add @gregorsternat as co-author given their earlier work on foundry-rs#9905 Co-authored-by: gregorsternat <[email protected]> * refactor: add variant constructor * style: clippy issue * docs: remove formating * refactor(doc): remove the custom variant natspec for enum --------- Co-authored-by: zerosnacks <[email protected]> Co-authored-by: gregorsternat <[email protected]>
1 parent 78c19e1 commit 0a8b0eb

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

crates/doc/src/writer/as_doc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ impl AsDoc for Document {
229229
writer.write_subtitle("Enums")?;
230230
enums.into_iter().try_for_each(|(item, comments, code)| {
231231
writer.write_heading(&item.name.safe_unwrap().name)?;
232-
writer.write_section(comments, code)
232+
writer.write_section(comments, code)?;
233+
writer.try_write_variant_table(item, comments)
233234
})?;
234235
}
235236
}

crates/doc/src/writer/buf_writer.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{AsDoc, CommentTag, Comments, Deployment, Markdown, writer::traits::ParamLike};
22
use itertools::Itertools;
3-
use solang_parser::pt::{ErrorParameter, EventParameter, Parameter, VariableDeclaration};
3+
use solang_parser::pt::{
4+
EnumDefinition, ErrorParameter, EventParameter, Parameter, VariableDeclaration,
5+
};
46
use std::{
57
fmt::{self, Display, Write},
68
sync::LazyLock,
@@ -19,6 +21,11 @@ const DEPLOYMENTS_TABLE_HEADERS: &[&str] = &["Network", "Address"];
1921
static DEPLOYMENTS_TABLE_SEPARATOR: LazyLock<String> =
2022
LazyLock::new(|| DEPLOYMENTS_TABLE_HEADERS.iter().map(|h| "-".repeat(h.len())).join("|"));
2123

24+
/// Headers and separator for rendering the variants table.
25+
const VARIANTS_TABLE_HEADERS: &[&str] = &["Name", "Description"];
26+
static VARIANTS_TABLE_SEPARATOR: LazyLock<String> =
27+
LazyLock::new(|| VARIANTS_TABLE_HEADERS.iter().map(|h| "-".repeat(h.len())).join("|"));
28+
2229
/// The buffered writer.
2330
/// Writes various display items into the internal buffer.
2431
#[derive(Debug, Default)]
@@ -177,6 +184,45 @@ impl BufWriter {
177184
self.try_write_table(CommentTag::Param, params, comments, "Properties")
178185
}
179186

187+
/// Tries to write the variant table to the buffer.
188+
/// Doesn't write anything if either params or comments are empty.
189+
pub fn try_write_variant_table(
190+
&mut self,
191+
params: &EnumDefinition,
192+
comments: &Comments,
193+
) -> fmt::Result {
194+
let comments = comments.include_tags(&[CommentTag::Param]);
195+
196+
// There is nothing to write.
197+
if comments.is_empty() {
198+
return Ok(());
199+
}
200+
201+
self.write_bold("Variants")?;
202+
self.writeln()?;
203+
204+
self.write_piped(&VARIANTS_TABLE_HEADERS.join("|"))?;
205+
self.write_piped(&VARIANTS_TABLE_SEPARATOR)?;
206+
207+
for value in &params.values {
208+
let param_name = value.as_ref().map(|v| v.name.clone());
209+
210+
let comment = param_name.as_ref().and_then(|name| {
211+
comments.iter().find_map(|comment| comment.match_first_word(name))
212+
});
213+
214+
let row = [
215+
Markdown::Code(&param_name.unwrap_or("<none>".to_string())).as_doc()?,
216+
comment.unwrap_or_default().replace('\n', " "),
217+
];
218+
self.write_piped(&row.join("|"))?;
219+
}
220+
221+
self.writeln()?;
222+
223+
Ok(())
224+
}
225+
180226
/// Tries to write the parameters table to the buffer.
181227
/// Doesn't write anything if either params or comments are empty.
182228
pub fn try_write_events_table(

0 commit comments

Comments
 (0)