mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 15:51:12 +00:00
bug fixes
This commit is contained in:
parent
37ba50e746
commit
810c7dce80
12 changed files with 171 additions and 63 deletions
|
@ -29,7 +29,6 @@ pub fn new_comma_mn(expr_id: ExprId, parent_id_opt: Option<MarkNodeId>) -> Marku
|
|||
pub fn new_blank_mn(ast_node_id: ASTNodeId, parent_id_opt: Option<MarkNodeId>) -> MarkupNode {
|
||||
MarkupNode::Blank {
|
||||
ast_node_id,
|
||||
syn_high_style: HighlightStyle::Blank,
|
||||
attributes: Attributes::default(),
|
||||
parent_id_opt,
|
||||
newlines_at_end: 0,
|
||||
|
@ -43,7 +42,6 @@ pub fn new_blank_mn_w_nls(
|
|||
) -> MarkupNode {
|
||||
MarkupNode::Blank {
|
||||
ast_node_id,
|
||||
syn_high_style: HighlightStyle::Blank,
|
||||
attributes: Attributes::default(),
|
||||
parent_id_opt,
|
||||
newlines_at_end: nr_of_newlines,
|
||||
|
@ -130,3 +128,14 @@ pub fn new_arg_name_mn(content: String, expr_id: ExprId) -> MarkupNode {
|
|||
newlines_at_end: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_arrow_mn(ast_node_id: ASTNodeId, newlines_at_end: usize) -> MarkupNode {
|
||||
MarkupNode::Text {
|
||||
content: nodes::ARROW.to_owned(),
|
||||
ast_node_id,
|
||||
syn_high_style: HighlightStyle::Operator,
|
||||
attributes: Attributes::default(),
|
||||
parent_id_opt: None,
|
||||
newlines_at_end,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ pub fn def2_to_markup<'a, 'b>(
|
|||
*expr_id,
|
||||
mark_node_pool,
|
||||
interns,
|
||||
0
|
||||
)?;
|
||||
|
||||
let tld_mn = tld_mark_node(
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
|
||||
use crate::{markup::{attribute::Attributes, common_nodes::{new_arg_name_mn, new_blank_mn, new_colon_mn, new_comma_mn, new_equals_mn, new_left_accolade_mn, new_left_square_mn, new_operator_mn, new_right_accolade_mn, new_right_square_mn}, nodes::{MarkupNode, get_string, new_markup_node}}, slow_pool::{MarkNodeId, SlowPool}, syntax_highlight::HighlightStyle};
|
||||
use crate::{markup::{attribute::Attributes, common_nodes::{new_arg_name_mn, new_arrow_mn, new_blank_mn, new_colon_mn, new_comma_mn, new_equals_mn, new_left_accolade_mn, new_left_square_mn, new_operator_mn, new_right_accolade_mn, new_right_square_mn}, nodes::{MarkupNode, get_string, new_markup_node}}, slow_pool::{MarkNodeId, SlowPool}, syntax_highlight::HighlightStyle};
|
||||
|
||||
use bumpalo::Bump;
|
||||
use itertools::Itertools;
|
||||
use roc_ast::{ast_error::ASTResult, lang::{core::{ast::ASTNodeId, expr::{
|
||||
use roc_ast::{ast_error::{ASTResult, IdentIdNotFound}, lang::{core::{ast::ASTNodeId, expr::{
|
||||
expr2::{Expr2, ExprId},
|
||||
record_field::RecordField,
|
||||
}, pattern::{Pattern2, get_identifier_string}, val_def::ValueDef}, env::Env}};
|
||||
use roc_module::symbol::Interns;
|
||||
use roc_module::{ident::Ident, symbol::Interns};
|
||||
use snafu::OptionExt;
|
||||
|
||||
// make Markup Nodes: generate String representation, assign Highlighting Style
|
||||
pub fn expr2_to_markup<'a, 'b>(
|
||||
|
@ -17,11 +18,12 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
expr2_node_id: ExprId,
|
||||
mark_node_pool: &mut SlowPool,
|
||||
interns: &Interns,
|
||||
indent_level: usize,
|
||||
) -> ASTResult<MarkNodeId> {
|
||||
|
||||
let ast_node_id = ASTNodeId::AExprId(expr2_node_id);
|
||||
|
||||
println!("{:?}", expr2);
|
||||
println!("EXPR2 {:?}", expr2);
|
||||
|
||||
let mark_node_id = match expr2 {
|
||||
Expr2::SmallInt { text, .. }
|
||||
|
@ -30,28 +32,46 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
| Expr2::Float { text, .. } => {
|
||||
let num_str = get_string(env, text);
|
||||
|
||||
new_markup_node(num_str, ast_node_id, HighlightStyle::Number, mark_node_pool)
|
||||
new_markup_node(
|
||||
with_indent(indent_level, &num_str),
|
||||
ast_node_id,
|
||||
HighlightStyle::Number,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
)
|
||||
}
|
||||
Expr2::Str(text) => new_markup_node(
|
||||
"\"".to_owned() + text.as_str(env.pool) + "\"",
|
||||
ast_node_id,
|
||||
HighlightStyle::String,
|
||||
mark_node_pool,
|
||||
),
|
||||
Expr2::Str(text) => {
|
||||
let content = format!("\"{}\"", text.as_str(env.pool));
|
||||
|
||||
new_markup_node(
|
||||
with_indent(indent_level, &content),
|
||||
ast_node_id,
|
||||
HighlightStyle::String,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
)
|
||||
},
|
||||
Expr2::GlobalTag { name, .. } => new_markup_node(
|
||||
get_string(env, name),
|
||||
with_indent(indent_level, &get_string(env, name)),
|
||||
ast_node_id,
|
||||
HighlightStyle::Type,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
),
|
||||
Expr2::Call { expr: expr_id, .. } => {
|
||||
let expr = env.pool.get(*expr_id);
|
||||
expr2_to_markup(arena, env, expr, *expr_id, mark_node_pool, interns)?
|
||||
expr2_to_markup(arena, env, expr, *expr_id, mark_node_pool, interns, indent_level)?
|
||||
}
|
||||
Expr2::Var(symbol) => {
|
||||
//TODO make bump_format with arena
|
||||
let text = format!("{:?}", symbol);
|
||||
new_markup_node(text, ast_node_id, HighlightStyle::Variable, mark_node_pool)
|
||||
let text = env.get_name_for_ident_id(symbol.ident_id())?;
|
||||
|
||||
new_markup_node(
|
||||
text.to_string(),
|
||||
ast_node_id,
|
||||
HighlightStyle::Value,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
)
|
||||
}
|
||||
Expr2::List { elems, .. } => {
|
||||
let mut children_ids =
|
||||
|
@ -70,6 +90,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
*node_id,
|
||||
mark_node_pool,
|
||||
interns,
|
||||
indent_level
|
||||
)?);
|
||||
|
||||
if idx + 1 < elems.len() {
|
||||
|
@ -116,6 +137,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
ast_node_id,
|
||||
HighlightStyle::RecordField,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
));
|
||||
|
||||
match record_field {
|
||||
|
@ -132,6 +154,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
*sub_expr2_node_id,
|
||||
mark_node_pool,
|
||||
interns,
|
||||
indent_level
|
||||
)?);
|
||||
}
|
||||
}
|
||||
|
@ -167,7 +190,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
let val_name_mn = MarkupNode::Text {
|
||||
content: val_name,
|
||||
ast_node_id,
|
||||
syn_high_style: HighlightStyle::Variable,
|
||||
syn_high_style: HighlightStyle::Value,
|
||||
attributes: Attributes::default(),
|
||||
parent_id_opt: None,
|
||||
newlines_at_end: 0,
|
||||
|
@ -192,6 +215,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
*expr_id,
|
||||
mark_node_pool,
|
||||
interns,
|
||||
indent_level
|
||||
)?;
|
||||
|
||||
let body_mn = mark_node_pool.get_mut(body_mn_id);
|
||||
|
@ -225,24 +249,39 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
let backslash_mn = new_operator_mn("\\".to_string(), expr2_node_id, None);
|
||||
let backslash_mn_id = mark_node_pool.add(backslash_mn);
|
||||
|
||||
let arg_names: Vec<&str> =
|
||||
args.iter(env.pool).map(
|
||||
| (_, arg_node_id) | {
|
||||
let arg_pattern2 = env.pool.get(*arg_node_id);
|
||||
let arg_idents: Vec<&Ident> =
|
||||
args.iter(env.pool).map(
|
||||
| (_, arg_node_id) | {
|
||||
let arg_pattern2 = env.pool.get(*arg_node_id);
|
||||
|
||||
match arg_pattern2 {
|
||||
Pattern2::Identifier(id_symbol) => {
|
||||
id_symbol.ident_str(interns).as_str()
|
||||
},
|
||||
other => {
|
||||
todo!("TODO: support the following pattern2 as function arg: {:?}", other);
|
||||
match arg_pattern2 {
|
||||
Pattern2::Identifier(id_symbol) => {
|
||||
let ident_id = id_symbol.ident_id();
|
||||
let ident =
|
||||
env
|
||||
.ident_ids
|
||||
.get_name(ident_id)
|
||||
.with_context(|| IdentIdNotFound {
|
||||
ident_id,
|
||||
env_ident_ids_str: format!("{:?}", env.ident_ids),
|
||||
});
|
||||
|
||||
ident
|
||||
},
|
||||
other => {
|
||||
todo!("TODO: support the following pattern2 as function arg: {:?}", other);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
.collect();
|
||||
)
|
||||
.collect::<ASTResult<Vec<&Ident>>>()?;
|
||||
|
||||
let arg_mark_nodes = arg_names.iter().map(
|
||||
let arg_names =
|
||||
arg_idents.iter().map(|ident| {
|
||||
ident.as_inline_str().as_str()
|
||||
});
|
||||
|
||||
let arg_mark_nodes = arg_names.map(
|
||||
|arg_name|
|
||||
new_arg_name_mn(arg_name.to_string(), expr2_node_id)
|
||||
);
|
||||
|
@ -261,8 +300,16 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
mark_node_pool.add(mark_node)
|
||||
).collect();
|
||||
|
||||
let arrow_mn =
|
||||
new_arrow_mn(
|
||||
ASTNodeId::AExprId(expr2_node_id),
|
||||
1
|
||||
);
|
||||
let arrow_mn_id = mark_node_pool.add(arrow_mn);
|
||||
|
||||
let mut children_ids = vec![backslash_mn_id];
|
||||
children_ids.append(&mut args_with_commas_ids);
|
||||
children_ids.push(arrow_mn_id);
|
||||
|
||||
let args_mn = MarkupNode::Nested {
|
||||
ast_node_id: ASTNodeId::AExprId(expr2_node_id),
|
||||
|
@ -280,6 +327,7 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
*body_id,
|
||||
mark_node_pool,
|
||||
interns,
|
||||
indent_level + 1
|
||||
)?;
|
||||
|
||||
let function_node = MarkupNode::Nested {
|
||||
|
@ -296,9 +344,19 @@ pub fn expr2_to_markup<'a, 'b>(
|
|||
ast_node_id,
|
||||
HighlightStyle::Blank,
|
||||
mark_node_pool,
|
||||
indent_level,
|
||||
),
|
||||
rest => todo!("implement expr2_to_markup for {:?}", rest),
|
||||
};
|
||||
|
||||
Ok(mark_node_id)
|
||||
}
|
||||
|
||||
fn with_indent(indent_level: usize, some_str: &str) -> String {
|
||||
let full_indent = std::iter::repeat(" ").take(indent_level * 4);
|
||||
let mut full_string: String = full_indent.collect();
|
||||
|
||||
full_string.push_str(some_str);
|
||||
|
||||
full_string
|
||||
}
|
|
@ -32,10 +32,14 @@ pub enum MarkupNode {
|
|||
Blank {
|
||||
ast_node_id: ASTNodeId,
|
||||
attributes: Attributes,
|
||||
syn_high_style: HighlightStyle, // TODO remove HighlightStyle, this is always HighlightStyle::Blank
|
||||
parent_id_opt: Option<MarkNodeId>,
|
||||
newlines_at_end: usize,
|
||||
},
|
||||
Indent {
|
||||
ast_node_id: ASTNodeId,
|
||||
indent_level: usize,
|
||||
parent_id_opt: Option<MarkNodeId>,
|
||||
}
|
||||
}
|
||||
|
||||
impl MarkupNode {
|
||||
|
@ -44,6 +48,7 @@ impl MarkupNode {
|
|||
MarkupNode::Nested { ast_node_id, .. } => *ast_node_id,
|
||||
MarkupNode::Text { ast_node_id, .. } => *ast_node_id,
|
||||
MarkupNode::Blank { ast_node_id, .. } => *ast_node_id,
|
||||
MarkupNode::Indent { ast_node_id, .. } => *ast_node_id,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +57,7 @@ impl MarkupNode {
|
|||
MarkupNode::Nested { parent_id_opt, .. } => *parent_id_opt,
|
||||
MarkupNode::Text { parent_id_opt, .. } => *parent_id_opt,
|
||||
MarkupNode::Blank { parent_id_opt, .. } => *parent_id_opt,
|
||||
MarkupNode::Indent { parent_id_opt, .. } => *parent_id_opt,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,6 +66,7 @@ impl MarkupNode {
|
|||
MarkupNode::Nested { children_ids, .. } => children_ids.to_vec(),
|
||||
MarkupNode::Text { .. } => vec![],
|
||||
MarkupNode::Blank { .. } => vec![],
|
||||
MarkupNode::Indent { .. } => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,6 +161,7 @@ impl MarkupNode {
|
|||
MarkupNode::Nested { .. } => "".to_owned(),
|
||||
MarkupNode::Text { content, .. } => content.clone(),
|
||||
MarkupNode::Blank { .. } => BLANK_PLACEHOLDER.to_owned(),
|
||||
MarkupNode::Indent { indent_level, .. } => std::iter::repeat( SINGLE_INDENT).take(*indent_level).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,13 +178,8 @@ impl MarkupNode {
|
|||
|
||||
pub fn get_content_mut(&mut self) -> MarkResult<&mut String> {
|
||||
match self {
|
||||
MarkupNode::Nested { .. } => ExpectedTextNode {
|
||||
function_name: "set_content".to_owned(),
|
||||
node_type: self.node_type_as_string(),
|
||||
}
|
||||
.fail(),
|
||||
MarkupNode::Text { content, .. } => Ok(content),
|
||||
MarkupNode::Blank { .. } => ExpectedTextNode {
|
||||
_ => ExpectedTextNode {
|
||||
function_name: "set_content".to_owned(),
|
||||
node_type: self.node_type_as_string(),
|
||||
}
|
||||
|
@ -208,6 +211,7 @@ impl MarkupNode {
|
|||
MarkupNode::Nested { .. } => "Nested",
|
||||
MarkupNode::Text { .. } => "Text",
|
||||
MarkupNode::Blank { .. } => "Blank",
|
||||
MarkupNode::Indent { .. } => "Indent",
|
||||
};
|
||||
|
||||
type_str.to_owned()
|
||||
|
@ -232,6 +236,7 @@ impl MarkupNode {
|
|||
MarkupNode::Blank {
|
||||
newlines_at_end, ..
|
||||
} => *newlines_at_end,
|
||||
MarkupNode::Indent { .. } => 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,6 +251,7 @@ impl MarkupNode {
|
|||
MarkupNode::Blank {
|
||||
newlines_at_end, ..
|
||||
} => *newlines_at_end += 1,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -263,14 +269,17 @@ pub const COLON: &str = ": ";
|
|||
pub const COMMA: &str = ", ";
|
||||
pub const STRING_QUOTES: &str = "\"\"";
|
||||
pub const EQUALS: &str = " = ";
|
||||
pub const ARROW: &str = " -> ";
|
||||
pub const SINGLE_INDENT: &str = " "; // 4 spaces
|
||||
|
||||
pub fn new_markup_node(
|
||||
text: String,
|
||||
node_id: ASTNodeId,
|
||||
highlight_style: HighlightStyle,
|
||||
mark_node_pool: &mut SlowPool,
|
||||
indent_level: usize,
|
||||
) -> MarkNodeId {
|
||||
let node = MarkupNode::Text {
|
||||
let content_node = MarkupNode::Text {
|
||||
content: text,
|
||||
ast_node_id: node_id,
|
||||
syn_high_style: highlight_style,
|
||||
|
@ -279,7 +288,28 @@ pub fn new_markup_node(
|
|||
newlines_at_end: 0,
|
||||
};
|
||||
|
||||
mark_node_pool.add(node)
|
||||
let content_node_id = mark_node_pool.add(content_node);
|
||||
|
||||
if indent_level > 0 {
|
||||
let indent_node = MarkupNode::Indent {
|
||||
ast_node_id: node_id,
|
||||
indent_level,
|
||||
parent_id_opt: None
|
||||
};
|
||||
|
||||
let indent_node_id = mark_node_pool.add(indent_node);
|
||||
|
||||
let nested_node = MarkupNode::Nested {
|
||||
ast_node_id: node_id,
|
||||
children_ids: vec![indent_node_id, content_node_id],
|
||||
parent_id_opt: None,
|
||||
newlines_at_end: 0,
|
||||
};
|
||||
|
||||
mark_node_pool.add(nested_node)
|
||||
} else {
|
||||
content_node_id
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_parent_for_all(markup_node_id: MarkNodeId, mark_node_pool: &mut SlowPool) {
|
||||
|
@ -325,6 +355,7 @@ pub fn set_parent_for_all_helper(
|
|||
}
|
||||
MarkupNode::Text { parent_id_opt, .. } => *parent_id_opt = Some(parent_node_id),
|
||||
MarkupNode::Blank { parent_id_opt, .. } => *parent_id_opt = Some(parent_node_id),
|
||||
MarkupNode::Indent { parent_id_opt, .. } => *parent_id_opt = Some(parent_node_id),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ pub fn tld_mark_node<'a>(
|
|||
let val_name_mn = MarkupNode::Text {
|
||||
content: val_name.to_owned(),
|
||||
ast_node_id,
|
||||
syn_high_style: HighlightStyle::Variable,
|
||||
syn_high_style: HighlightStyle::Value,
|
||||
attributes: Attributes::default(),
|
||||
parent_id_opt: None,
|
||||
newlines_at_end: 0,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue