-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Shahar Dawn Or <[email protected]>
- Loading branch information
Showing
19 changed files
with
916 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Migration Guide | ||
|
||
Upgrading from nixdoc <= 2.x.x to >= 3.0.0 | ||
|
||
To leverage the new doc-comment features and prepare for the deprecation of the legacy format, follow these guidelines: | ||
|
||
## Documentation Comments | ||
|
||
- Use double asterisks `/** */` to mark comments intended as documentation. This differentiates them from internal comments and ensures they are properly processed as part of the documentation. | ||
|
||
**Example:** | ||
|
||
`lib/attrsets.nix (old format)` | ||
````nix | ||
/* Filter an attribute set by removing all attributes for which the | ||
given predicate return false. | ||
Example: | ||
filterAttrs (n: v: n == "foo") { foo = 1; bar = 2; } | ||
=> { foo = 1; } | ||
Type: | ||
filterAttrs :: (String -> Any -> Bool) -> AttrSet -> AttrSet | ||
*/ | ||
filterAttrs = | ||
# Predicate taking an attribute name and an attribute value, which returns `true` to include the attribute or `false` to exclude the attribute. | ||
pred: | ||
# The attribute set to filter | ||
set: | ||
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set)); | ||
```` | ||
|
||
-> | ||
|
||
`lib/attrsets.nix (new format)` | ||
````nix | ||
/** | ||
Filter an attribute set by removing all attributes for which the | ||
given predicate return false. | ||
# Example | ||
```nix | ||
filterAttrs (n: v: n == "foo") { foo = 1; bar = 2; } | ||
=> { foo = 1; } | ||
``` | ||
# Type | ||
``` | ||
filterAttrs :: (String -> Any -> Bool) -> AttrSet -> AttrSet | ||
``` | ||
# Arguments | ||
- **pred**: Predicate taking an attribute name and an attribute value, which returns `true` to include the attribute, or `false` to exclude the attribute. | ||
- **set**: The attribute set to filter | ||
*/ | ||
filterAttrs = | ||
pred: | ||
set: | ||
listToAttrs (concatMap (name: let v = set.${name}; in if pred name v then [(nameValuePair name v)] else []) (attrNames set)); | ||
```` | ||
|
||
## Documenting Arguments | ||
|
||
With the introduction of RFC145, there is a shift in how arguments are documented. While direct "argument" documentation is not specified, you can still document arguments effectively within your doc-comments by writing explicit markdown. | ||
|
||
**Example:** Migrating **Single Argument Documentation** | ||
|
||
The approach to documenting single arguments has evolved. Instead of individual argument comments, document the function and its arguments together. | ||
|
||
```nix | ||
{ | ||
/** | ||
The `id` function returns the provided value unchanged. | ||
# Arguments | ||
- **x**: The value to be returned. | ||
*/ | ||
id = x: x; | ||
} | ||
``` | ||
|
||
If arguments require more complex documentation consider starting an extra section per argument | ||
|
||
```nix | ||
{ | ||
/** | ||
The `id` function returns the provided value unchanged. | ||
# Arguments | ||
## **x** | ||
(...Some comprehensive documentation) | ||
*/ | ||
id = x: x; | ||
} | ||
``` | ||
|
||
**Example:** Documenting Structured Arguments | ||
Structured arguments can be documented (described in RFC145 as 'lambda formals'), using doc-comments. | ||
|
||
```nix | ||
{ | ||
/** | ||
* The `add` function calculates the sum of `a` and `b`. | ||
*/ | ||
add = { | ||
/** The first number to add. */ | ||
a, | ||
/** The second number to add. */ | ||
b | ||
}: a + b; | ||
} | ||
``` | ||
|
||
Ensure your documentation comments start with double asterisks to comply with the new standard. The legacy format remains supported for now but will not receive new features. It will be removed once important downstream projects have been migrated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use rnix::ast::{self, AstToken}; | ||
use rnix::{match_ast, SyntaxNode}; | ||
use rowan::ast::AstNode; | ||
|
||
/// Implements functions for doc-comments according to rfc145. | ||
pub trait DocComment { | ||
fn doc_text(&self) -> Option<&str>; | ||
} | ||
|
||
impl DocComment for ast::Comment { | ||
/// Function returns the contents of the doc-comment, if the [ast::Comment] is a | ||
/// doc-comment, or None otherwise. | ||
/// | ||
/// Note: [ast::Comment] holds both the single-line and multiline comment. | ||
/// | ||
/// /**{content}*/ | ||
/// -> {content} | ||
/// | ||
/// It is named `doc_text` to complement [ast::Comment::text]. | ||
fn doc_text(&self) -> Option<&str> { | ||
let text = self.syntax().text(); | ||
// Check whether this is a doc-comment | ||
if text.starts_with(r#"/**"#) && self.text().starts_with('*') { | ||
self.text().strip_prefix('*') | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
/// Function retrieves a doc-comment from the [ast::Expr] | ||
/// | ||
/// Returns an [Option<String>] of the first suitable doc-comment. | ||
/// Returns [None] in case no suitable comment was found. | ||
/// | ||
/// Doc-comments can appear in two places for any expression | ||
/// | ||
/// ```nix | ||
/// # (1) directly before the expression (anonymous) | ||
/// /** Doc */ | ||
/// bar: bar; | ||
/// | ||
/// # (2) when assigning a name. | ||
/// { | ||
/// /** Doc */ | ||
/// foo = bar: bar; | ||
/// } | ||
/// ``` | ||
/// | ||
/// 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<String> { | ||
if let Some(doc) = get_doc_comment(expr) { | ||
// Found in place (1) | ||
doc.doc_text().map(|v| v.to_owned()) | ||
} 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()) | ||
}else{ | ||
None | ||
} | ||
}, | ||
_ => { | ||
// Yet unhandled ast-nodes | ||
None | ||
} | ||
|
||
} | ||
} | ||
// None | ||
} else { | ||
// There is no parent; | ||
// No further places where a doc-comment could be. | ||
None | ||
} | ||
} | ||
|
||
/// Looks backwards from the given expression | ||
/// Only whitespace or non-doc-comments are allowed in between an expression and the doc-comment. | ||
/// Any other Node or Token stops the peek. | ||
fn get_doc_comment(expr: &SyntaxNode) -> Option<ast::Comment> { | ||
let mut prev = expr.prev_sibling_or_token(); | ||
loop { | ||
match prev { | ||
Some(rnix::NodeOrToken::Token(ref token)) => { | ||
match_ast! { match token { | ||
ast::Whitespace(_) => { | ||
prev = token.prev_sibling_or_token(); | ||
}, | ||
ast::Comment(it) => { | ||
if it.doc_text().is_some() { | ||
break Some(it); | ||
}else{ | ||
//Ignore non-doc comments. | ||
prev = token.prev_sibling_or_token(); | ||
} | ||
}, | ||
_ => { | ||
break None; | ||
} | ||
}} | ||
} | ||
_ => break None, | ||
}; | ||
} | ||
} |
Oops, something went wrong.