Expressions: display implementation code

This commit is contained in:
Johannes Kirschbauer 2024-10-13 12:37:44 +02:00
parent e873b68ed8
commit 5962fa2ce2
No known key found for this signature in database
7 changed files with 62 additions and 18 deletions

View File

@ -93,6 +93,7 @@ fn fill_docs(
if let Some(position) = &item.docs.attr.position { if let Some(position) = &item.docs.attr.position {
if let Some(Some(doc_comment)) = pos_doc_map.get(&position) { if let Some(Some(doc_comment)) = pos_doc_map.get(&position) {
item.docs.attr.content = doc_comment.content.clone(); 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() { 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) { if let Some(Some(doc_comment)) = pos_doc_map.get(&position) {
lambda.content = doc_comment.content.clone(); lambda.content = doc_comment.content.clone();
lambda.countApplied = doc_comment.count_applied; 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) { if let Some(orig_file) = when_overridable_lambda(doc, &file_idx_map) {
let orig_lambda = get_overridable_fn(&orig_file); let orig_lambda = get_overridable_fn(&orig_file);
let orig_pos = 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(orig_pos) = orig_pos {
if let Some(l) = &mut doc.docs.lambda { if let Some(l) = &mut doc.docs.lambda {
l.position = orig_pos; l.position = orig_pos;
l.expr = orig_lambda.as_ref().map(|n| n.text().to_string());
restores.push(format!("{:?}", doc.path.join("."))); restores.push(format!("{:?}", doc.path.join(".")));
} }
} }

View File

@ -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) /// If the doc-comment is not found in place (1) the search continues at place (2)
/// More precisely before the NODE_ATTRPATH_VALUE (ast) /// More precisely before the NODE_ATTRPATH_VALUE (ast)
/// If no doc-comment was found in place (1) or (2) this function returns None. /// If no doc-comment was found in place (1) or (2) this function returns None.
pub fn get_expr_docs(expr: &SyntaxNode) -> Option<String> { pub fn get_expr_docs(expr: &SyntaxNode) -> Option<(String, SyntaxNode)> {
if let Some(doc) = get_doc_comment(expr) { if let Some(doc) = get_doc_comment(expr) {
// Found in place (1) // 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() { } else if let Some(ref parent) = expr.parent() {
match_ast! { match_ast! {
match parent { match parent {
ast::AttrpathValue(_) => { ast::AttrpathValue(_) => {
if let Some(doc_comment) = get_doc_comment(parent) { 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) { }else if let Some(comment) = get_comment(parent) {
Some(comment.text().to_owned()) Some((comment.text().to_owned(),parent.clone()))
} else { } else {
None None
} }

View File

@ -174,8 +174,10 @@ struct DocumentFrontmatter<'a> {
is_functor: Option<bool>, is_functor: Option<bool>,
/// Where the attribute is defined at. /// Where the attribute is defined at.
attr_position: Option<&'a FilePosition>, attr_position: Option<&'a FilePosition>,
attr_expr: Option<&'a String>,
/// Where the original lambda is defined at. /// Where the original lambda is defined at.
lambda_position: Option<&'a FilePosition>, lambda_position: Option<&'a FilePosition>,
lambda_expr: Option<&'a String>,
/// How many times the function is applied. /// How many times the function is applied.
count_applied: Option<usize>, count_applied: Option<usize>,
content_meta: Option<SourceOrigin<'a>>, content_meta: Option<SourceOrigin<'a>>,
@ -201,12 +203,14 @@ impl<'a> FromDocs<'a> for Document<'a> {
path: &item.path, path: &item.path,
aliases: item.aliases.as_ref(), aliases: item.aliases.as_ref(),
attr_position: item.docs.attr.position.as_ref(), attr_position: item.docs.attr.position.as_ref(),
attr_expr: item.docs.attr.expr.as_ref(),
lambda_position: item lambda_position: item
.docs .docs
.lambda .lambda
.as_ref() .as_ref()
.map(|i| i.position.as_ref()) .map(|i| i.position.as_ref())
.flatten(), .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_primop: item.docs.lambda.as_ref().map(|i| i.isPrimop),
is_functor: item.docs.lambda.as_ref().map(|i| i.isFunctor).flatten(), is_functor: item.docs.lambda.as_ref().map(|i| i.isFunctor).flatten(),
count_applied: item.docs.lambda.as_ref().map(|i| i.countApplied).flatten(), count_applied: item.docs.lambda.as_ref().map(|i| i.countApplied).flatten(),

View File

@ -30,12 +30,16 @@ pub struct LambdaMeta {
pub content: Option<String>, pub content: Option<String>,
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub countApplied: Option<usize>, pub countApplied: Option<usize>,
// Serialized AST
pub expr: Option<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AttrMeta { pub struct AttrMeta {
pub position: Option<FilePosition>, pub position: Option<FilePosition>,
/// I want to add this /// I want to add this
pub content: Option<String>, pub content: Option<String>,
// Serialized AST
pub expr: Option<String>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DocsMeta { pub struct DocsMeta {

View File

@ -498,15 +498,21 @@ impl<'a> DocComment<'a> for DocIndex<'a> {
let doc = match expr.kind() { let doc = match expr.kind() {
rnix::SyntaxKind::NODE_LAMBDA => { rnix::SyntaxKind::NODE_LAMBDA => {
let (_outer_lambda, count_applied) = get_parent_lambda(&expr); let (_outer_lambda, count_applied) = get_parent_lambda(&expr);
let res = get_expr_docs(&expr);
NixDocComment { NixDocComment {
content: get_expr_docs(&expr), content: res.as_ref().map(|v| v.0.to_owned()),
count_applied: Some(count_applied), 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, count_applied: None,
}, expr: res.as_ref().map(|v| v.1.to_owned()),
}},
}; };
return Some(doc); return Some(doc);
} }
@ -535,6 +541,7 @@ impl<'a> DocComment<'a> for DocIndex<'a> {
pub struct NixDocComment { pub struct NixDocComment {
pub content: Option<String>, pub content: Option<String>,
pub count_applied: Option<usize>, pub count_applied: Option<usize>,
pub expr: Option<SyntaxNode>,
} }
fn get_parent_lambda(expr: &SyntaxNode) -> (SyntaxNode, usize) { fn get_parent_lambda(expr: &SyntaxNode) -> (SyntaxNode, usize) {

View File

@ -177,6 +177,8 @@ export default async function Page(props: { params: { path: string[] } }) {
return undefined; return undefined;
} }
const expr_code = (meta?.lambda_expr || meta?.attr_expr) ?? null;
return ( return (
<> <>
<Toc mdxSource={source + externManualSrc} title={item?.meta.title} /> <Toc mdxSource={source + externManualSrc} title={item?.meta.title} />
@ -315,7 +317,8 @@ export default async function Page(props: { params: { path: string[] } }) {
<MDX source={source} /> <MDX source={source} />
{meta && <PositionLink meta={meta} content={item?.content} />} {meta && <PositionLink meta={meta} content={item?.content} />}
<div data-pagefind-ignore="all"> <div data-pagefind-ignore="all">
{(!!meta?.aliases?.length || {(expr_code ||
!!meta?.aliases?.length ||
(!!signature && !meta?.signature) || (!!signature && !meta?.signature) ||
meta?.is_functor) && ( meta?.is_functor) && (
<> <>
@ -330,7 +333,7 @@ export default async function Page(props: { params: { path: string[] } }) {
textAlign: "center", textAlign: "center",
}} }}
> >
Noogle also knows Noogle detected
</Typography> </Typography>
</> </>
)} )}
@ -347,13 +350,7 @@ export default async function Page(props: { params: { path: string[] } }) {
)} )}
{!!meta?.aliases?.length && ( {!!meta?.aliases?.length && (
<> <>
<Typography <MDX source={`## Aliases`} />
variant="h5"
component={"h5"}
sx={{ color: "text.secondary" }}
>
Aliases
</Typography>
<ul> <ul>
{meta?.aliases?.map((a) => ( {meta?.aliases?.map((a) => (
<li key={a.join(".")}> <li key={a.join(".")}>
@ -377,6 +374,33 @@ export default async function Page(props: { params: { path: string[] } }) {
<MDX source={`\`\`\`haskell\n${signature.trim()}\n\`\`\`\n`} /> <MDX source={`\`\`\`haskell\n${signature.trim()}\n\`\`\`\n`} />
</> </>
)} )}
{meta?.is_primop && (
<Box>
<MDX
source={`## Implementation
:::{.tip}
This function is implemented in c++ and is part of the native nix runtime.
:::
`}
/>
</Box>
)}
{expr_code && (
<Box>
<MDX
source={`## Implementation
:::{.tip}
The following is the current implementation of this function.
\`\`\`nix
${expr_code.trim()}
\`\`\`
:::
`}
/>
</Box>
)}
</div> </div>
</Box> </Box>
<Divider flexItem sx={{ mt: 2 }} /> <Divider flexItem sx={{ mt: 2 }} />

View File

@ -38,7 +38,9 @@ export type DocMeta = {
is_functor?: boolean; is_functor?: boolean;
primop_meta?: PrimopMatter; primop_meta?: PrimopMatter;
attr_position?: FilePosition; attr_position?: FilePosition;
attr_expr?: string;
lambda_position?: FilePosition; lambda_position?: FilePosition;
lambda_expr?: string;
count_applied?: number; count_applied?: number;
content_meta?: SourceOrigin; content_meta?: SourceOrigin;
signature?: string; signature?: string;