debugging inline comment bug

This commit is contained in:
Anton-4 2021-12-01 16:57:28 +01:00
parent 30c1d218a7
commit 5bd776f972
17 changed files with 206 additions and 30 deletions

1
Cargo.lock generated
View file

@ -3260,6 +3260,7 @@ name = "roc_docs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"indoc",
"pretty_assertions", "pretty_assertions",
"pulldown-cmark", "pulldown-cmark",
"roc_ast", "roc_ast",

View file

@ -1,9 +1,6 @@
use roc_module::symbol::IdentId; use roc_module::symbol::IdentId;
use crate::{ use crate::{lang::core::expr::{expr2::Expr2, expr2_to_string::expr2_to_string}, mem_pool::{pool::{NodeId, Pool}}};
lang::core::expr::{expr2::Expr2, expr2_to_string::expr2_to_string},
mem_pool::pool::{NodeId, Pool},
};
// A top level definition, not inside a function. For example: `main = "Hello, world!"` // A top level definition, not inside a function. For example: `main = "Hello, world!"`
#[derive(Debug)] #[derive(Debug)]
@ -14,6 +11,14 @@ pub enum Def2 {
expr_id: NodeId<Expr2>, expr_id: NodeId<Expr2>,
}, },
Blank, Blank,
CommentsBefore {
comments: String,
def_id: DefId
},
CommentsAfter {
comments: String,
def_id: DefId
},
} }
pub type DefId = NodeId<Def2>; pub type DefId = NodeId<Def2>;
@ -36,7 +41,15 @@ pub fn def2_to_string(node_id: DefId, pool: &Pool) -> String {
Def2::Blank => { Def2::Blank => {
full_string.push_str("Def2::Blank"); full_string.push_str("Def2::Blank");
} }
Def2::CommentsBefore {
comments,
def_id:_
} => full_string.push_str(comments),
Def2::CommentsAfter {
comments,
def_id:_
} => full_string.push_str(comments),
} }
full_string full_string
} }

View file

@ -30,9 +30,51 @@ pub fn def_to_def2<'a>(
) -> Def2 { ) -> Def2 {
use roc_parse::ast::Def::*; use roc_parse::ast::Def::*;
dbg!(parsed_def);
match parsed_def { match parsed_def {
SpaceBefore(inner_def, _) => def_to_def2(arena, env, scope, inner_def, region), SpaceBefore(inner_def, comments) => {
SpaceAfter(inner_def, _) => def_to_def2(arena, env, scope, inner_def, region), if comments.len() > 0 {
let inner_def = def_to_def2(arena, env, scope, inner_def, region);
let inner_def_id = env.pool.add(inner_def);
let mut all_comments_str = String::new();
for comment in comments.iter() {
all_comments_str.push_str(&comment.to_string());
}
all_comments_str = all_comments_str.trim().to_string();
Def2::CommentsBefore {
comments: all_comments_str,
def_id: inner_def_id,
}
} else {
def_to_def2(arena, env, scope, inner_def, region)
}
},
SpaceAfter(inner_def, comments) => {
if comments.len() > 0 {
let inner_def = def_to_def2(arena, env, scope, inner_def, region);
let inner_def_id = env.pool.add(inner_def);
let mut all_comments_str = String::new();
for comment in comments.iter() {
all_comments_str.push_str(&comment.to_string());
}
all_comments_str = all_comments_str.trim().to_string();
Def2::CommentsAfter {
def_id: inner_def_id,
comments: all_comments_str,
}
} else {
def_to_def2(arena, env, scope, inner_def, region)
}
},
Body(&loc_pattern, &loc_expr) => { Body(&loc_pattern, &loc_expr) => {
let expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0; let expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0;
let expr_id = env.pool.add(expr2); let expr_id = env.pool.add(expr2);

View file

@ -161,13 +161,6 @@ pub enum Expr2 {
}, },
Blank, // Rendered as empty box in editor Blank, // Rendered as empty box in editor
Comment(PoolStr),
WithComment {
expr_id: ExprId,
comment_id: ExprId
},
Comments(PoolVec<ExprId>),
// Compiles, but will crash if reached // Compiles, but will crash if reached
RuntimeError(/* TODO make a version of RuntimeError that fits in 15B */), RuntimeError(/* TODO make a version of RuntimeError that fits in 15B */),
} }

View file

@ -1,4 +1,4 @@
use roc_ast::lang::core::{ast::ASTNodeId, expr::expr2::ExprId}; use roc_ast::{lang::core::{ast::ASTNodeId, expr::expr2::ExprId}};
use crate::{slow_pool::MarkNodeId, syntax_highlight::HighlightStyle}; use crate::{slow_pool::MarkNodeId, syntax_highlight::HighlightStyle};
@ -147,3 +147,14 @@ pub fn new_arrow_mn(ast_node_id: ASTNodeId, newlines_at_end: usize) -> MarkupNod
newlines_at_end, newlines_at_end,
} }
} }
pub fn new_comments_mn(comments: String, ast_node_id: ASTNodeId, newlines_at_end: usize) -> MarkupNode {
MarkupNode::Text {
content: comments,
ast_node_id,
syn_high_style: HighlightStyle::Comment,
attributes: Attributes::default(),
parent_id_opt: None,
newlines_at_end,
}
}

View file

@ -1,7 +1,4 @@
use crate::{ use crate::{markup::{common_nodes::new_blank_mn_w_nls, top_level_def::{tld_mark_node, tld_w_comments_mark_node}}, slow_pool::{MarkNodeId, SlowPool}};
markup::{common_nodes::new_blank_mn_w_nls, top_level_def::tld_mark_node},
slow_pool::{MarkNodeId, SlowPool},
};
use super::from_expr2::expr2_to_markup; use super::from_expr2::expr2_to_markup;
@ -46,6 +43,40 @@ pub fn def2_to_markup<'a>(
mark_node_pool.add(tld_mn) mark_node_pool.add(tld_mn)
} }
Def2::Blank => mark_node_pool.add(new_blank_mn_w_nls(ast_node_id, None, 2)), Def2::Blank => mark_node_pool.add(new_blank_mn_w_nls(ast_node_id, None, 2)),
Def2::CommentsBefore {
comments,
def_id
} => {
let inner_def = env.pool.get(*def_id);
let inner_def_mark_node_id = def2_to_markup(env, inner_def, *def_id, mark_node_pool, interns)?;
let full_mark_node = tld_w_comments_mark_node(
comments.clone(),
inner_def_mark_node_id,
ast_node_id,
mark_node_pool,
true
)?;
mark_node_pool.add(full_mark_node)
},
Def2::CommentsAfter {
def_id,
comments
} => {
let inner_def = env.pool.get(*def_id);
let inner_def_mark_node_id = def2_to_markup(env, inner_def, *def_id, mark_node_pool, interns)?;
let full_mark_node = tld_w_comments_mark_node(
comments.clone(),
inner_def_mark_node_id,
ast_node_id,
mark_node_pool,
false
)?;
mark_node_pool.add(full_mark_node)
},
}; };
Ok(mark_node_id) Ok(mark_node_id)

View file

@ -4,11 +4,7 @@ use roc_ast::{
}; };
use roc_module::symbol::IdentId; use roc_module::symbol::IdentId;
use crate::{ use crate::{markup::{attribute::Attributes, common_nodes::{new_comments_mn, new_equals_mn}, nodes::MarkupNode}, slow_pool::{MarkNodeId, SlowPool}, syntax_highlight::HighlightStyle};
markup::{attribute::Attributes, common_nodes::new_equals_mn, nodes::MarkupNode},
slow_pool::{MarkNodeId, SlowPool},
syntax_highlight::HighlightStyle,
};
pub fn tld_mark_node<'a>( pub fn tld_mark_node<'a>(
identifier_id: IdentId, identifier_id: IdentId,
@ -41,3 +37,33 @@ pub fn tld_mark_node<'a>(
Ok(full_let_node) Ok(full_let_node)
} }
pub fn tld_w_comments_mark_node(
comments: String,
def_mark_node_id: MarkNodeId,
ast_node_id: ASTNodeId,
mark_node_pool: &mut SlowPool,
comments_before: bool,
) -> ASTResult<MarkupNode> {
let comment_mn_id =
mark_node_pool.add(
new_comments_mn(comments, ast_node_id, 1)
);
let children_ids =
if comments_before {
vec![comment_mn_id, def_mark_node_id]
} else {
vec![def_mark_node_id, comment_mn_id]
};
let tld_w_comment_node = MarkupNode::Nested {
ast_node_id,
children_ids: children_ids,
parent_id_opt: None,
newlines_at_end: 2,
};
Ok(tld_w_comment_node)
}

View file

@ -321,6 +321,15 @@ impl<'a> CommentOrNewline<'a> {
DocComment(_) => false, DocComment(_) => false,
} }
} }
pub fn to_string(&self) -> std::string::String {
use CommentOrNewline::*;
match self {
Newline => "\n".to_owned(),
LineComment(comment_str) => format!("#{}",comment_str),
DocComment(comment_str) => format!("##{}",comment_str),
}
}
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]

View file

@ -1580,7 +1580,7 @@ pub fn defs<'a>(min_indent: u16) -> impl Parser<'a, Vec<'a, Located<Def<'a>>>, E
.alloc(def.value) .alloc(def.value)
.with_spaces_after(final_space, def.region) .with_spaces_after(final_space, def.region)
} }
dbg!(def);
output.push(def); output.push(def);
} }
} }

View file

@ -1147,7 +1147,10 @@ macro_rules! skip_second {
match $p1.parse(arena, state) { match $p1.parse(arena, state) {
Ok((p1, out1, state)) => match $p2.parse(arena, state) { Ok((p1, out1, state)) => match $p2.parse(arena, state) {
Ok((p2, _, state)) => Ok((p1.or(p2), out1, state)), Ok((p2, _, state)) => {
dbg!(&out1);
Ok((p1.or(p2), out1, state))
},
Err((p2, fail, _)) => Err((p1.or(p2), fail, original_state)), Err((p2, fail, _)) => Err((p1.or(p2), fail, original_state)),
}, },
Err((progress, fail, _)) => Err((progress, fail, original_state)), Err((progress, fail, _)) => Err((progress, fail, original_state)),

View file

@ -34,7 +34,10 @@ pub fn parse_defs_with<'a>(
let state = State::new(input.trim().as_bytes()); let state = State::new(input.trim().as_bytes());
match module_defs().parse(arena, state) { match module_defs().parse(arena, state) {
Ok(tuple) => Ok(tuple.1), Ok(tuple) => {
dbg!(&tuple);
Ok(tuple.1)
},
Err(tuple) => Err(tuple.1), Err(tuple) => Err(tuple.1),
} }
} }

View file

@ -0,0 +1,20 @@
[
|L 0-0, C 0-7| SpaceAfter(
SpaceBefore(
Body(
|L 0-0, C 0-3| Identifier(
"foo",
),
|L 0-0, C 6-7| Num(
"1",
),
),
[],
),
[
LineComment(
" comment after",
),
],
),
]

View file

@ -0,0 +1 @@
foo = 1 # comment after

View file

@ -98,6 +98,7 @@ mod test_parse {
}; };
} }
// see tests/snapshots/pass to see test input(.roc) and expected output(.result-ast)
snapshot_tests! { snapshot_tests! {
expr => { expr => {
add_var_with_spaces, add_var_with_spaces,
@ -232,6 +233,7 @@ mod test_parse {
nonempty_platform_header nonempty_platform_header
} }
module => { module => {
comment_after_def,
standalone_module_defs, standalone_module_defs,
module_def_newline, module_def_newline,
nested_def_annotation nested_def_annotation

View file

@ -26,3 +26,4 @@ snafu = { version = "0.6.10", features = ["backtraces"] }
pretty_assertions = "1.0.0" pretty_assertions = "1.0.0"
tempfile = "3.2.0" tempfile = "3.2.0"
uuid = { version = "0.8.2", features = ["v4"] } uuid = { version = "0.8.2", features = ["v4"] }
indoc = "1.0.3"

View file

@ -143,6 +143,7 @@ pub fn syntax_highlight_top_level_defs<'a>(
match roc_parse::test_helpers::parse_defs_with(arena, trimmed_code_str) { match roc_parse::test_helpers::parse_defs_with(arena, trimmed_code_str) {
Ok(vec_loc_def) => { Ok(vec_loc_def) => {
dbg!(&vec_loc_def);
let vec_def = vec_loc_def.iter().map(|loc| loc.value).collect(); let vec_def = vec_loc_def.iter().map(|loc| loc.value).collect();
defs_to_html(buf, vec_def, env_module_id, interns)?; defs_to_html(buf, vec_def, env_module_id, interns)?;

View file

@ -1,5 +1,7 @@
#[macro_use] #[macro_use]
extern crate pretty_assertions; extern crate pretty_assertions;
#[macro_use]
extern crate indoc;
#[cfg(test)] #[cfg(test)]
mod insert_doc_syntax_highlighting { mod insert_doc_syntax_highlighting {
@ -190,10 +192,27 @@ main = "Hello, world!"
} }
#[test] #[test]
fn tld_with_comment() { fn tld_with_comment_before() {
expect_html_def( expect_html_def(
r#"myVal = "Hello, World!" # COMMENT"#, indoc!(
"<span class=\"syntax-value\">myVal</span><span class=\"syntax-operator\"> = </span><span class=\"syntax-string\">\"Hello, World!\"</span><span class=\"syntax-comment\"> # COMMENT</span>\n\n", r#"
# COMMENT
myVal = "Hello, World!"
"#,
),
"<span class=\"syntax-comment\"># COMMENT</span>\n<span class=\"syntax-value\">myVal</span><span class=\"syntax-operator\"> = </span><span class=\"syntax-string\">\"Hello, World!\"</span>\n\n\n\n",
);
}
#[test]
fn tld_with_comment_after() {
expect_html_def(
indoc!(
r#"
myVal = "Hello, World!" # COMMENT
"#,
),
"<span class=\"syntax-value\">myVal</span><span class=\"syntax-operator\"> = </span><span class=\"syntax-string\">\"Hello, World!\"</span><span class=\"syntax-comment\"># COMMENT</span>\n\n\n\n",
); );
} }
} }