module docs

This commit is contained in:
faldor20 2024-03-10 21:15:36 +10:00
parent 0b56882785
commit 0c463555f4
No known key found for this signature in database
GPG key ID: F2216079B890CD57
4 changed files with 26 additions and 5 deletions

View file

@ -22,6 +22,7 @@ pub struct ModuleDocumentation {
pub enum DocEntry {
DocDef(DocDef),
DetachedDoc(String),
ModuleDoc(String),
}
#[derive(Debug, Clone)]
@ -179,7 +180,7 @@ fn generate_entry_docs(
if let Some(docs) = comments_or_new_lines_to_docs(header_comments) {
println!("<<docs:\n {:#?}\n docs>>", docs);
acc.push(DetachedDoc(docs));
acc.push(DocEntry::ModuleDoc(docs));
}
let mut before_comments_or_new_lines: Option<&[CommentOrNewline]> = None;

View file

@ -253,6 +253,7 @@ impl AnalyzedDocument {
declarations,
exposed_imports,
imports,
docs_by_module,
modules_info,
..
} = self.module()?;
@ -273,6 +274,7 @@ impl AnalyzedDocument {
interns,
imports,
modules_info,
docs_by_module,
true,
))
} else {
@ -298,6 +300,7 @@ impl AnalyzedDocument {
interns,
imports,
modules_info,
docs_by_module,
true,
);
Some(completions)

View file

@ -8,7 +8,8 @@ use roc_can::{
pattern::{ListPatterns, Pattern, RecordDestruct, TupleDestruct},
traverse::{walk_decl, walk_def, walk_expr, DeclarationInfo, Visitor},
};
use roc_collections::MutMap;
use roc_collections::{MutMap, VecMap};
use roc_load::docs::ModuleDocumentation;
use roc_module::symbol::{Interns, ModuleId, Symbol};
use roc_region::all::{Loc, Position, Region};
use roc_types::{
@ -326,6 +327,7 @@ pub(super) fn get_module_completion_items(
interns: &Interns,
imported_modules: &HashMap<ModuleId, Arc<Vec<(Symbol, Variable)>>>,
modules_info: &ModulesInfo,
docs: &VecMap<ModuleId, ModuleDocumentation>,
just_modules: bool,
) -> Vec<CompletionItem> {
let module_completions = imported_modules
@ -333,6 +335,7 @@ pub(super) fn get_module_completion_items(
.flat_map(|(mod_id, exposed_symbols)| {
let mod_name = mod_id.to_ident_str(interns).to_string();
//Completion for modules themselves
if mod_name.starts_with(&prefix) {
let item = CompletionItem {
label: mod_name.clone(),
@ -342,12 +345,13 @@ pub(super) fn get_module_completion_items(
mod_id,
interns,
exposed_symbols,
docs.get(mod_id),
modules_info,
)),
..Default::default()
};
vec![item]
//Complete dot completions
//Complete dot completions for module exports
} else if prefix.starts_with(&(mod_name + ".")) {
get_module_exposed_completion(exposed_symbols, modules_info, mod_id, interns)
} else {

View file

@ -1,3 +1,4 @@
use roc_load::docs::ModuleDocumentation;
use roc_module::symbol::{Interns, ModuleId, Symbol};
use roc_types::subs::Variable;
@ -33,18 +34,30 @@ fn md_doc(val: String) -> Documentation {
})
}
///Generates a nicely formatted block of text for the completionitem documentation field
pub(super) fn module_documentation(
description_type: DescriptionsType,
module_id: &ModuleId,
interns: &Interns,
exposed: &[(Symbol, Variable)],
module_docs: Option<&ModuleDocumentation>,
modules_info: &ModulesInfo,
) -> Documentation {
let exposed_string =
module_exposed_list(module_id, interns, modules_info, exposed).unwrap_or_default();
let module_doc = module_docs
.and_then(|docs| {
docs.entries.first().and_then(|first_doc| match first_doc {
roc_load::docs::DocEntry::ModuleDoc(str) => Some(str.clone()),
_ => None,
})
})
.unwrap_or_default();
match description_type {
DescriptionsType::Exposes => md_doc(format!("```roc\n{0}\n```", exposed_string)),
DescriptionsType::Exposes => md_doc(format!(
"{0}```roc\n{1}\n```",
module_doc + "\n",
exposed_string
)),
}
}