From 5962fa2ce27c3133c8cf2097a8d8a1b1305971d9 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Sun, 13 Oct 2024 12:37:44 +0200 Subject: [PATCH] Expressions: display implementation code --- pesto/src/bulk.rs | 5 +++- pesto/src/comment.rs | 8 +++--- pesto/src/main.rs | 4 +++ pesto/src/pasta.rs | 4 +++ pesto/src/position.rs | 15 +++++++--- website/src/app/f/[...path]/page.tsx | 42 ++++++++++++++++++++++------ website/src/models/data/index.ts | 2 ++ 7 files changed, 62 insertions(+), 18 deletions(-) diff --git a/pesto/src/bulk.rs b/pesto/src/bulk.rs index 4fbe270..e49174a 100644 --- a/pesto/src/bulk.rs +++ b/pesto/src/bulk.rs @@ -93,6 +93,7 @@ fn fill_docs( if let Some(position) = &item.docs.attr.position { if let Some(Some(doc_comment)) = pos_doc_map.get(&position) { item.docs.attr.content = doc_comment.content.clone(); + item.docs.attr.expr = doc_comment.expr.as_ref().map(|nix| nix.text().to_string() ); } } if let Some(lambda) = item.docs.lambda.as_mut() { @@ -100,6 +101,7 @@ fn fill_docs( if let Some(Some(doc_comment)) = pos_doc_map.get(&position) { lambda.content = doc_comment.content.clone(); lambda.countApplied = doc_comment.count_applied; + lambda.expr = doc_comment.expr.as_ref().map(|nix| nix.text().to_string() ); } } } @@ -150,10 +152,11 @@ impl<'a> BulkProcessing for Pasta { if let Some(orig_file) = when_overridable_lambda(doc, &file_idx_map) { let orig_lambda = get_overridable_fn(&orig_file); let orig_pos = - orig_lambda.map(|n| seek_file_position(&orig_file, &n.text_range().start())); + orig_lambda.as_ref().map(|n| seek_file_position(&orig_file, &n.text_range().start())); if let Some(orig_pos) = orig_pos { if let Some(l) = &mut doc.docs.lambda { l.position = orig_pos; + l.expr = orig_lambda.as_ref().map(|n| n.text().to_string()); restores.push(format!("{:?}", doc.path.join("."))); } } diff --git a/pesto/src/comment.rs b/pesto/src/comment.rs index 304166f..bf93b86 100644 --- a/pesto/src/comment.rs +++ b/pesto/src/comment.rs @@ -53,18 +53,18 @@ impl DocComment for ast::Comment { /// If the doc-comment is not found in place (1) the search continues at place (2) /// More precisely before the NODE_ATTRPATH_VALUE (ast) /// If no doc-comment was found in place (1) or (2) this function returns None. -pub fn get_expr_docs(expr: &SyntaxNode) -> Option { +pub fn get_expr_docs(expr: &SyntaxNode) -> Option<(String, SyntaxNode)> { if let Some(doc) = get_doc_comment(expr) { // Found in place (1) - doc.doc_text().map(|v| v.to_owned()) + doc.doc_text().map(|v| (v.to_owned(), expr.clone())) } else if let Some(ref parent) = expr.parent() { match_ast! { match parent { ast::AttrpathValue(_) => { if let Some(doc_comment) = get_doc_comment(parent) { - doc_comment.doc_text().map(|v| v.to_owned()) + doc_comment.doc_text().map(|v| (v.to_owned(), parent.clone())) }else if let Some(comment) = get_comment(parent) { - Some(comment.text().to_owned()) + Some((comment.text().to_owned(),parent.clone())) } else { None } diff --git a/pesto/src/main.rs b/pesto/src/main.rs index 491bf0b..dd9371f 100644 --- a/pesto/src/main.rs +++ b/pesto/src/main.rs @@ -174,8 +174,10 @@ struct DocumentFrontmatter<'a> { is_functor: Option, /// Where the attribute is defined at. attr_position: Option<&'a FilePosition>, + attr_expr: Option<&'a String>, /// Where the original lambda is defined at. lambda_position: Option<&'a FilePosition>, + lambda_expr: Option<&'a String>, /// How many times the function is applied. count_applied: Option, content_meta: Option>, @@ -201,12 +203,14 @@ impl<'a> FromDocs<'a> for Document<'a> { path: &item.path, aliases: item.aliases.as_ref(), attr_position: item.docs.attr.position.as_ref(), + attr_expr: item.docs.attr.expr.as_ref(), lambda_position: item .docs .lambda .as_ref() .map(|i| i.position.as_ref()) .flatten(), + lambda_expr: item.docs.lambda.as_ref().map(|i| i.expr.as_ref()).flatten(), is_primop: item.docs.lambda.as_ref().map(|i| i.isPrimop), is_functor: item.docs.lambda.as_ref().map(|i| i.isFunctor).flatten(), count_applied: item.docs.lambda.as_ref().map(|i| i.countApplied).flatten(), diff --git a/pesto/src/pasta.rs b/pesto/src/pasta.rs index ae004cf..ce73d86 100644 --- a/pesto/src/pasta.rs +++ b/pesto/src/pasta.rs @@ -30,12 +30,16 @@ pub struct LambdaMeta { pub content: Option, #[allow(non_snake_case)] pub countApplied: Option, + // Serialized AST + pub expr: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AttrMeta { pub position: Option, /// I want to add this pub content: Option, + // Serialized AST + pub expr: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct DocsMeta { diff --git a/pesto/src/position.rs b/pesto/src/position.rs index 6231312..d106193 100644 --- a/pesto/src/position.rs +++ b/pesto/src/position.rs @@ -498,15 +498,21 @@ impl<'a> DocComment<'a> for DocIndex<'a> { let doc = match expr.kind() { rnix::SyntaxKind::NODE_LAMBDA => { let (_outer_lambda, count_applied) = get_parent_lambda(&expr); + let res = get_expr_docs(&expr); + NixDocComment { - content: get_expr_docs(&expr), + content: res.as_ref().map(|v| v.0.to_owned()), count_applied: Some(count_applied), + expr: res.as_ref().map(|v| v.1.to_owned()), } } - _ => NixDocComment { - content: get_expr_docs(&expr), + _ => { + let res = get_expr_docs(&expr); + NixDocComment { + content: res.as_ref().map(|v| v.0.to_owned()), count_applied: None, - }, + expr: res.as_ref().map(|v| v.1.to_owned()), + }}, }; return Some(doc); } @@ -535,6 +541,7 @@ impl<'a> DocComment<'a> for DocIndex<'a> { pub struct NixDocComment { pub content: Option, pub count_applied: Option, + pub expr: Option, } fn get_parent_lambda(expr: &SyntaxNode) -> (SyntaxNode, usize) { diff --git a/website/src/app/f/[...path]/page.tsx b/website/src/app/f/[...path]/page.tsx index a805b48..681e0dc 100644 --- a/website/src/app/f/[...path]/page.tsx +++ b/website/src/app/f/[...path]/page.tsx @@ -177,6 +177,8 @@ export default async function Page(props: { params: { path: string[] } }) { return undefined; } + const expr_code = (meta?.lambda_expr || meta?.attr_expr) ?? null; + return ( <> @@ -315,7 +317,8 @@ export default async function Page(props: { params: { path: string[] } }) { {meta && }
- {(!!meta?.aliases?.length || + {(expr_code || + !!meta?.aliases?.length || (!!signature && !meta?.signature) || meta?.is_functor) && ( <> @@ -330,7 +333,7 @@ export default async function Page(props: { params: { path: string[] } }) { textAlign: "center", }} > - Noogle also knows + Noogle detected )} @@ -347,13 +350,7 @@ export default async function Page(props: { params: { path: string[] } }) { )} {!!meta?.aliases?.length && ( <> - - Aliases - +
    {meta?.aliases?.map((a) => (
  • @@ -377,6 +374,33 @@ export default async function Page(props: { params: { path: string[] } }) { )} + {meta?.is_primop && ( + + + + )} + {expr_code && ( + + + + )}
diff --git a/website/src/models/data/index.ts b/website/src/models/data/index.ts index 7b0557e..1ff01b6 100644 --- a/website/src/models/data/index.ts +++ b/website/src/models/data/index.ts @@ -38,7 +38,9 @@ export type DocMeta = { is_functor?: boolean; primop_meta?: PrimopMatter; attr_position?: FilePosition; + attr_expr?: string; lambda_position?: FilePosition; + lambda_expr?: string; count_applied?: number; content_meta?: SourceOrigin; signature?: string;