Fixed problems with docs failing to render and included test to validate that behavior

This commit is contained in:
Chadtech 2021-07-03 14:51:40 -04:00
parent 1c06e280b0
commit 62d5633546
8 changed files with 120 additions and 79 deletions

1
Cargo.lock generated
View file

@ -3139,6 +3139,7 @@ dependencies = [
"roc_load", "roc_load",
"roc_module", "roc_module",
"roc_region", "roc_region",
"roc_types",
] ]
[[package]] [[package]]

View file

@ -20,7 +20,7 @@ use roc_types::types::Alias;
pub struct Module { pub struct Module {
pub module_id: ModuleId, pub module_id: ModuleId,
pub exposed_imports: MutMap<Symbol, Variable>, pub exposed_imports: MutMap<Symbol, Variable>,
pub exposed_symbols: Vec<Symbol>, pub exposed_symbols: MutSet<Symbol>,
pub references: MutSet<Symbol>, pub references: MutSet<Symbol>,
pub aliases: MutMap<Symbol, Alias>, pub aliases: MutMap<Symbol, Alias>,
pub rigid_variables: MutMap<Variable, Lowercase>, pub rigid_variables: MutMap<Variable, Lowercase>,
@ -50,7 +50,7 @@ pub fn canonicalize_module_defs<'a, F>(
dep_idents: MutMap<ModuleId, IdentIds>, dep_idents: MutMap<ModuleId, IdentIds>,
aliases: MutMap<Symbol, Alias>, aliases: MutMap<Symbol, Alias>,
exposed_imports: MutMap<Ident, (Symbol, Region)>, exposed_imports: MutMap<Ident, (Symbol, Region)>,
exposed_symbols: &Vec<Symbol>, exposed_symbols: &MutSet<Symbol>,
var_store: &mut VarStore, var_store: &mut VarStore,
look_up_builtin: F, look_up_builtin: F,
) -> Result<ModuleOutput, RuntimeError> ) -> Result<ModuleOutput, RuntimeError>
@ -203,11 +203,7 @@ where
// we can see if there were any // we can see if there were any
// exposed symbols which did not have // exposed symbols which did not have
// corresponding defs. // corresponding defs.
if let Some(index) = exposed_but_not_defined.remove(symbol);
exposed_but_not_defined.iter().position(|s| *s == *symbol)
{
exposed_but_not_defined.remove(index);
}
} }
} }
} }
@ -220,11 +216,7 @@ where
// we can see if there were any // we can see if there were any
// exposed symbols which did not have // exposed symbols which did not have
// corresponding defs. // corresponding defs.
if let Some(index) = exposed_but_not_defined.remove(symbol);
exposed_but_not_defined.iter().position(|s| *s == *symbol)
{
exposed_but_not_defined.remove(index);
}
} }
} }
} }
@ -252,9 +244,7 @@ where
// we can see if there were any // we can see if there were any
// exposed symbols which did not have // exposed symbols which did not have
// corresponding defs. // corresponding defs.
if let Some(index) = exposed_but_not_defined.iter().position(|s| *s == symbol) { exposed_but_not_defined.remove(&symbol);
exposed_but_not_defined.remove(index);
}
aliases.insert(symbol, alias); aliases.insert(symbol, alias);
} }

View file

@ -123,7 +123,10 @@ fn detached_docs_from_comments_and_new_lines<'a>(
} }
CommentOrNewline::LineComment(_) | CommentOrNewline::Newline => { CommentOrNewline::LineComment(_) | CommentOrNewline::Newline => {
detached_docs.push(docs.clone()); if !docs.is_empty() {
detached_docs.push(docs.clone());
}
docs = String::new(); docs = String::new();
} }
} }

View file

@ -48,7 +48,7 @@ pub fn build_effect_builtins(
scope: &mut Scope, scope: &mut Scope,
effect_symbol: Symbol, effect_symbol: Symbol,
var_store: &mut VarStore, var_store: &mut VarStore,
exposed_symbols: &mut Vec<Symbol>, exposed_symbols: &mut MutSet<Symbol>,
declarations: &mut Vec<Declaration>, declarations: &mut Vec<Declaration>,
) { ) {
for (_, f) in BUILTIN_EFFECT_FUNCTIONS.iter() { for (_, f) in BUILTIN_EFFECT_FUNCTIONS.iter() {
@ -60,7 +60,7 @@ pub fn build_effect_builtins(
var_store, var_store,
); );
exposed_symbols.push(symbol); exposed_symbols.insert(symbol);
declarations.push(Declaration::Declare(def)); declarations.push(Declaration::Declare(def));
} }
} }

View file

@ -487,9 +487,7 @@ fn start_phase<'a>(
let exposed_symbols = state let exposed_symbols = state
.exposed_symbols_by_module .exposed_symbols_by_module
.remove(&module_id) .remove(&module_id)
.expect("Could not find listener ID in exposed_symbols_by_module") .expect("Could not find listener ID in exposed_symbols_by_module");
.into_iter()
.collect::<Vec<Symbol>>();
let mut aliases = MutMap::default(); let mut aliases = MutMap::default();
@ -974,7 +972,7 @@ enum BuildTask<'a> {
parsed: ParsedModule<'a>, parsed: ParsedModule<'a>,
module_ids: ModuleIds, module_ids: ModuleIds,
dep_idents: MutMap<ModuleId, IdentIds>, dep_idents: MutMap<ModuleId, IdentIds>,
exposed_symbols: Vec<Symbol>, exposed_symbols: MutSet<Symbol>,
aliases: MutMap<Symbol, Alias>, aliases: MutMap<Symbol, Alias>,
}, },
Solve { Solve {
@ -3503,8 +3501,8 @@ fn fabricate_effects_module<'a>(
let mut declarations = Vec::new(); let mut declarations = Vec::new();
let exposed_symbols: Vec<Symbol> = { let exposed_symbols: MutSet<Symbol> = {
let mut exposed_symbols = Vec::new(); let mut exposed_symbols = MutSet::default();
{ {
for (ident, ann) in effect_entries { for (ident, ann) in effect_entries {
@ -3536,8 +3534,7 @@ fn fabricate_effects_module<'a>(
&mut var_store, &mut var_store,
annotation, annotation,
); );
exposed_symbols.insert(symbol);
exposed_symbols.push(symbol);
declarations.push(Declaration::Declare(def)); declarations.push(Declaration::Declare(def));
} }
@ -3651,7 +3648,7 @@ fn canonicalize_and_constrain<'a, F>(
arena: &'a Bump, arena: &'a Bump,
module_ids: &ModuleIds, module_ids: &ModuleIds,
dep_idents: MutMap<ModuleId, IdentIds>, dep_idents: MutMap<ModuleId, IdentIds>,
exposed_symbols: Vec<Symbol>, exposed_symbols: MutSet<Symbol>,
aliases: MutMap<Symbol, Alias>, aliases: MutMap<Symbol, Alias>,
parsed: ParsedModule<'a>, parsed: ParsedModule<'a>,
look_up_builtins: F, look_up_builtins: F,

View file

@ -14,6 +14,7 @@ roc_builtins = { path = "../compiler/builtins" }
roc_can = { path = "../compiler/can" } roc_can = { path = "../compiler/can" }
roc_module = { path = "../compiler/module" } roc_module = { path = "../compiler/module" }
roc_region = { path = "../compiler/region" } roc_region = { path = "../compiler/region" }
roc_types = { path = "../compiler/types" }
roc_collections = { path = "../compiler/collections" } roc_collections = { path = "../compiler/collections" }
bumpalo = { version = "3.2", features = ["collections"] } bumpalo = { version = "3.2", features = ["collections"] }

View file

@ -163,9 +163,8 @@ fn render_main_content(
} }
} }
DocEntry::DetachedDoc(docs) => { DocEntry::DetachedDoc(docs) => {
buf.push_str( let markdown = markdown_to_html(&mut module.scope, interns, docs.to_string());
markdown_to_html(&mut module.scope, interns, docs.to_string()).as_str(), buf.push_str(markdown.as_str());
);
} }
}; };
} }
@ -526,7 +525,7 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
} }
} }
fn insert_doc_links(scope: &mut Scope, interns: &Interns, markdown: String) -> String { pub fn insert_doc_links(scope: &mut Scope, interns: &Interns, markdown: String) -> String {
let buf = &markdown; let buf = &markdown;
let mut result = String::new(); let mut result = String::new();
@ -572,7 +571,11 @@ fn insert_doc_links(scope: &mut Scope, interns: &Interns, markdown: String) -> S
} }
} }
result if chomping_from == None {
markdown
} else {
result
}
} }
fn make_doc_link(scope: &mut Scope, interns: &Interns, doc_item: &str) -> String { fn make_doc_link(scope: &mut Scope, interns: &Interns, doc_item: &str) -> String {
@ -619,62 +622,65 @@ fn markdown_to_html(scope: &mut Scope, interns: &Interns, markdown: String) -> S
let mut docs_parser = vec![]; let mut docs_parser = vec![];
let (_, _) = pulldown_cmark::Parser::new_ext(&markdown_with_links, markdown_options).fold( let (_, _) = pulldown_cmark::Parser::new_ext(&markdown_with_links, markdown_options).fold(
(0, 0), (0, 0),
|(start_quote_count, end_quote_count), event| match event { |(start_quote_count, end_quote_count), event| {
// Replace this sequence (`>>>` syntax): match event {
// Start(BlockQuote) // Replace this sequence (`>>>` syntax):
// Start(BlockQuote) // Start(BlockQuote)
// Start(BlockQuote) // Start(BlockQuote)
// Start(Paragraph) // Start(BlockQuote)
// For `Start(CodeBlock(Fenced(Borrowed("roc"))))` // Start(Paragraph)
Event::Start(BlockQuote) => { // For `Start(CodeBlock(Fenced(Borrowed("roc"))))`
docs_parser.push(event); Event::Start(BlockQuote) => {
(start_quote_count + 1, 0)
}
Event::Start(Paragraph) => {
if start_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(Event::Start(CodeBlock(CodeBlockKind::Fenced(
CowStr::Borrowed("roc"),
))));
} else {
docs_parser.push(event); docs_parser.push(event);
(start_quote_count + 1, 0)
} }
(0, 0) Event::Start(Paragraph) => {
} if start_quote_count == 3 {
// Replace this sequence (`>>>` syntax): docs_parser.pop();
// End(Paragraph) docs_parser.pop();
// End(BlockQuote) docs_parser.pop();
// End(BlockQuote) docs_parser.push(Event::Start(CodeBlock(CodeBlockKind::Fenced(
// End(BlockQuote) CowStr::Borrowed("roc"),
// For `End(CodeBlock(Fenced(Borrowed("roc"))))` ))));
Event::End(Paragraph) => { } else {
docs_parser.push(event); docs_parser.push(event);
(0, 1) }
} (0, 0)
Event::End(BlockQuote) => { }
if end_quote_count == 3 { // Replace this sequence (`>>>` syntax):
docs_parser.pop(); // End(Paragraph)
docs_parser.pop(); // End(BlockQuote)
docs_parser.pop(); // End(BlockQuote)
docs_parser.push(Event::End(CodeBlock(CodeBlockKind::Fenced( // End(BlockQuote)
CowStr::Borrowed("roc"), // For `End(CodeBlock(Fenced(Borrowed("roc"))))`
)))); Event::End(Paragraph) => {
docs_parser.push(event);
(0, 1)
}
Event::End(BlockQuote) => {
if end_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(Event::End(CodeBlock(CodeBlockKind::Fenced(
CowStr::Borrowed("roc"),
))));
(0, 0)
} else {
docs_parser.push(event);
(0, end_quote_count + 1)
}
}
_ => {
docs_parser.push(event);
(0, 0) (0, 0)
} else {
docs_parser.push(event);
(0, end_quote_count + 1)
} }
}
_ => {
docs_parser.push(event);
(0, 0)
} }
}, },
); );
let mut docs_html = String::new(); let mut docs_html = String::new();
pulldown_cmark::html::push_html(&mut docs_html, docs_parser.into_iter()); pulldown_cmark::html::push_html(&mut docs_html, docs_parser.into_iter());
docs_html docs_html

View file

@ -0,0 +1,43 @@
#[macro_use]
extern crate pretty_assertions;
#[cfg(test)]
mod insert_doc_links {
use roc_can::env::Env;
use roc_can::scope::Scope;
use roc_collections::all::MutMap;
use roc_docs::insert_doc_links;
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
use roc_types::subs::VarStore;
#[test]
fn no_doc_links() {
let home = ModuleIds::default().get_or_insert(&"Test".into());
let module_ids = ModuleIds::default();
let dep_idents = IdentIds::exposed_builtins(0);
let env = Env::new(home, dep_idents, &module_ids, IdentIds::default());
let all_ident_ids = MutMap::default();
let interns = Interns {
module_ids: env.module_ids.clone(),
all_ident_ids,
};
let var_store = &mut VarStore::default();
let scope = &mut Scope::new(home, var_store);
let markdown = r#"
# Hello
Hello thanks for using my package
"#;
assert_eq!(
markdown,
insert_doc_links(scope, &interns, markdown.to_string()),
);
}
}