feat: support doc comments for functions args and validator params

- Add support to the formatter for these doc comments
- Add a new field to `Arg` `doc: Option<String>`
- Don't attach docs immediately after typechecking a module
  - instead we should do it on demand in docs, build, and lsp
  - the check command doesn't need to have any docs attached
  - doing it more lazily defers the computation until later making
    typechecking feedback a bit faster
- Add support for function arg and validator param docs in
  `attach_module_docs` methods
- Update some snapshots
- Add put_doc to Arg

closes #685
This commit is contained in:
rvcas
2023-10-16 13:33:14 -04:00
parent 10b9dc2042
commit e5801f9c19
16 changed files with 205 additions and 34 deletions

View File

@@ -181,14 +181,17 @@ where
output_path: destination.clone(),
});
let doc_files = docs::generate_all(
&self.root,
&self.config,
self.checked_modules
.values()
.filter(|CheckedModule { package, .. }| package == &self.config.name.to_string())
.collect(),
);
let modules = self
.checked_modules
.values_mut()
.filter(|CheckedModule { package, .. }| package == &self.config.name.to_string())
.map(|m| {
m.attach_doc_and_module_comments();
&*m
})
.collect();
let doc_files = docs::generate_all(&self.root, &self.config, modules);
for file in doc_files {
let path = destination.join(file.path);
@@ -270,6 +273,10 @@ where
path: self.blueprint_path(),
});
self.checked_modules.values_mut().for_each(|m| {
m.attach_doc_and_module_comments();
});
let mut generator = self.checked_modules.new_generator(
&self.functions,
&self.data_types,
@@ -641,7 +648,7 @@ where
self.module_types
.insert(name.clone(), ast.type_info.clone());
let mut checked_module = CheckedModule {
let checked_module = CheckedModule {
kind,
extra,
name: name.clone(),
@@ -651,8 +658,6 @@ where
input_path: path,
};
checked_module.attach_doc_and_module_comments();
self.checked_modules.insert(name, checked_module);
}
}

View File

@@ -1,8 +1,8 @@
use crate::error::Error;
use aiken_lang::{
ast::{
DataType, Definition, Located, ModuleKind, TypedDataType, TypedFunction, TypedModule,
TypedValidator, UntypedModule,
DataType, Definition, Function, Located, ModuleKind, TypedDataType, TypedFunction,
TypedModule, TypedValidator, UntypedModule, Validator,
},
gen_uplc::{
builder::{DataTypeKey, FunctionAccessKey},
@@ -208,24 +208,85 @@ impl CheckedModule {
def.put_doc(doc);
}
if let Definition::DataType(DataType { constructors, .. }) = def {
for constructor in constructors {
let docs: Vec<&str> =
comments_before(&mut doc_comments, constructor.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
constructor.put_doc(doc);
}
match def {
Definition::DataType(DataType { constructors, .. }) => {
for constructor in constructors {
let docs: Vec<&str> = comments_before(
&mut doc_comments,
constructor.location.start,
&self.code,
);
if !docs.is_empty() {
let doc = docs.join("\n");
constructor.put_doc(doc);
}
for argument in constructor.arguments.iter_mut() {
for argument in constructor.arguments.iter_mut() {
let docs: Vec<&str> = comments_before(
&mut doc_comments,
argument.location.start,
&self.code,
);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
}
}
Definition::Fn(Function { arguments, .. }) => {
for argument in arguments {
let docs: Vec<&str> =
comments_before(&mut doc_comments, argument.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
}
Definition::Validator(Validator {
params,
fun,
other_fun,
..
}) => {
for param in params {
let docs: Vec<&str> =
comments_before(&mut doc_comments, param.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
param.put_doc(doc);
}
}
for argument in fun.arguments.iter_mut() {
let docs: Vec<&str> =
comments_before(&mut doc_comments, argument.location.start, &self.code);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
if let Some(fun) = other_fun {
for argument in fun.arguments.iter_mut() {
let docs: Vec<&str> = comments_before(
&mut doc_comments,
argument.location.start,
&self.code,
);
if !docs.is_empty() {
let doc = docs.join("\n");
argument.put_doc(doc);
}
}
}
}
_ => (),
}
}
}