let value progress

This commit is contained in:
Anton-4 2021-07-10 20:13:43 +02:00
parent 9801de6be4
commit 9ceab03b42
10 changed files with 264 additions and 44 deletions

View File

@ -0,0 +1,39 @@
use crate::{editor::{slow_pool::{MarkNodeId, SlowPool}, syntax_highlight::HighlightStyle}, lang::ast::ExprId};
use super::{attribute::Attributes, nodes::MarkupNode, nodes};
pub fn new_equals_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>, mn_pool: &mut SlowPool) -> MarkNodeId {
let equals_mark_node = MarkupNode::Text {
content: nodes::EQUALS.to_owned(),
ast_node_id,
syn_high_style: HighlightStyle::Operator,
attributes: Attributes::new(),
parent_id_opt,
};
mn_pool.add(equals_mark_node)
}
pub fn new_comma_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>, mn_pool: &mut SlowPool) -> MarkNodeId {
let comma_mark_node = MarkupNode::Text {
content: nodes::COMMA.to_owned(),
ast_node_id,
syn_high_style: HighlightStyle::Blank,
attributes: Attributes::new(),
parent_id_opt,
};
mn_pool.add(comma_mark_node)
}
pub fn new_blank_mn(ast_node_id: ExprId, parent_id_opt: Option<MarkNodeId>, mn_pool: &mut SlowPool) -> MarkNodeId {
let blank_mark_node = MarkupNode::Blank {
ast_node_id,
syn_high_style: HighlightStyle::Blank,
attributes: Attributes::new(),
parent_id_opt,
};
mn_pool.add(blank_mark_node)
}

View File

@ -1,2 +1,3 @@
pub mod attribute;
pub mod nodes;
pub mod common_nodes;

View File

@ -1,7 +1,6 @@
use super::attribute::Attributes;
use crate::editor::ed_error::EdResult;
use crate::editor::ed_error::ExpectedTextNode;
use crate::editor::ed_error::GetContentOnNestedNode;
use crate::editor::ed_error::{NestedNodeMissingChild, NestedNodeRequired};
use crate::editor::slow_pool::MarkNodeId;
use crate::editor::slow_pool::SlowPool;
@ -151,11 +150,23 @@ impl MarkupNode {
}
// can't be &str, this creates borrowing issues
pub fn get_content(&self) -> EdResult<String> {
pub fn get_content(&self, markup_node_pool: &SlowPool) -> String {
match self {
MarkupNode::Nested { .. } => GetContentOnNestedNode {}.fail(),
MarkupNode::Text { content, .. } => Ok(content.clone()),
MarkupNode::Blank { .. } => Ok(BLANK_PLACEHOLDER.to_owned()),
MarkupNode::Nested { children_ids, .. } => {
let mut full_str = String::new();
for child_id in children_ids.iter() {
let child_node = markup_node_pool.get(*child_id);
full_str.push_str(
&child_node.get_content(markup_node_pool)
);
}
full_str
},
MarkupNode::Text { content, .. } => content.clone(),
MarkupNode::Blank { .. } => BLANK_PLACEHOLDER.to_owned(),
}
}
@ -175,11 +186,11 @@ impl MarkupNode {
}
}
pub fn is_all_alphanumeric(&self) -> EdResult<bool> {
Ok(self
.get_content()?
pub fn is_all_alphanumeric(&self, markup_node_pool: &SlowPool) -> bool {
self
.get_content(markup_node_pool)
.chars()
.all(|chr| chr.is_ascii_alphanumeric()))
.all(|chr| chr.is_ascii_alphanumeric())
}
pub fn add_child_at_index(&mut self, index: usize, child_id: MarkNodeId) -> EdResult<()> {
@ -226,6 +237,7 @@ pub const RIGHT_SQUARE_BR: &str = " ]";
pub const COLON: &str = ": ";
pub const COMMA: &str = ", ";
pub const STRING_QUOTES: &str = "\"\"";
pub const EQUALS: &str = " = ";
fn new_markup_node(
text: String,

View File

@ -1,12 +1,11 @@
use crate::editor::code_lines::CodeLines;
use crate::editor::grid_node_map::GridNodeMap;
use crate::editor::markup::common_nodes::new_blank_mn;
use crate::editor::slow_pool::{MarkNodeId, SlowPool};
use crate::editor::syntax_highlight::HighlightStyle;
use crate::editor::{
ed_error::EdError::ParseError,
ed_error::{EdResult, MissingParent, NoNodeAtCaretPosition},
markup::attribute::Attributes,
markup::nodes::{expr2_to_markup, set_parent_for_all, MarkupNode},
markup::nodes::{expr2_to_markup, set_parent_for_all},
};
use crate::graphics::primitives::rect::Rect;
use crate::lang::ast::Expr2;
@ -65,14 +64,7 @@ pub fn init_model<'a>(
let mut markup_node_pool = SlowPool::new();
let markup_root_id = if code_str.is_empty() {
let blank_root = MarkupNode::Blank {
ast_node_id: ast_root_id,
attributes: Attributes::new(),
syn_high_style: HighlightStyle::Blank,
parent_id_opt: None,
};
markup_node_pool.add(blank_root)
new_blank_mn(ast_root_id, None, &mut markup_node_pool)
} else {
let ast_root = &module.env.pool.get(ast_root_id);

View File

@ -582,6 +582,9 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
if let Expr2::Blank {..} = ast_node_ref {
match ch {
/*'a'..='z' => {
start_new_let_value(ed_model, ch)?
}*/
'"' => {
start_new_string(ed_model)?
},

View File

@ -98,7 +98,7 @@ pub fn update_int(
} else {
content_str_mut.insert(node_caret_offset, *ch);
let content_str = int_mark_node.get_content()?;
let content_str = int_mark_node.get_content(&ed_model.markup_node_pool);
// update GridNodeMap and CodeLines
ed_model.insert_between_line(

View File

@ -0,0 +1,187 @@
use crate::editor::ed_error::EdResult;
use crate::editor::ed_error::StringParseError;
use crate::editor::markup::attribute::Attributes;
use crate::editor::markup::common_nodes::new_blank_mn;
use crate::editor::markup::common_nodes::new_equals_mn;
use crate::editor::markup::nodes::MarkupNode;
use crate::editor::mvc::app_update::InputOutcome;
use crate::editor::mvc::ed_model::EdModel;
use crate::editor::mvc::ed_update::get_node_context;
use crate::editor::mvc::ed_update::NodeContext;
use crate::editor::slow_pool::MarkNodeId;
use crate::editor::syntax_highlight::HighlightStyle;
use crate::lang::ast::ArrString;
use crate::lang::ast::{Expr2, ValueDef};
use crate::lang::ast::Expr2::SmallInt;
use crate::lang::ast::IntVal;
use crate::lang::ast::{IntStyle, IntVal::*};
use crate::lang::pool::PoolStr;
use crate::ui::text::lines::SelectableLines;
pub fn start_new_let_value(ed_model: &mut EdModel, new_char: &char) -> EdResult<InputOutcome> {
let NodeContext {
old_caret_pos,
curr_mark_node_id,
curr_mark_node,
parent_id_opt,
ast_node_id,
} = get_node_context(&ed_model)?;
let is_blank_node = curr_mark_node.is_blank();
let val_name_string = new_char.to_string();
// safe unwrap because our ArrString has a 30B capacity
let mut val_name_string_container = ArrString::try_from_str(val_name_string).unwrap();
let val_name_expr2_node =
Expr2::SmallStr(
val_name_string_container
);
let val_name_expr_id = ed_model.module.env.pool.add(val_name_expr2_node);
let body_placeholder = Expr2::Blank;
let body_id = ed_model.module.env.pool.add(body_placeholder);
let value_def =
ValueDef::NoAnnotation {
pattern_id: Pattern2::Identifier(val_name_string),
expr_id: val_name_expr_id,
expr_var: ed_model.module.env.var_store.fresh()
};
let def_id = ed_model.module.env.pool.add(value_def);
let expr2_node = Expr2::LetValue {
def_id,
body_id,
body_var: ed_model.module.env.var_store.fresh(),
};
ed_model.module.env.pool.set(ast_node_id, expr2_node);
let val_name_mark_node = MarkupNode::Text {
content: val_name_string,
ast_node_id,
syn_high_style: HighlightStyle::Variable,
attributes: Attributes::new(),
parent_id_opt: Some(curr_mark_node_id),
};
let val_name_mn_id = ed_model.markup_node_pool.add(val_name_mark_node);
let equals_mn_id = new_equals_mn(ast_node_id, Some(curr_mark_node_id), &mut ed_model.markup_node_pool);
let val_mn_id = new_blank_mn(ast_node_id, Some(curr_mark_node_id), &mut ed_model.markup_node_pool);
let val_mark_node = MarkupNode::Nested {
ast_node_id,
children_ids: vec![val_name_mn_id, equals_mn_id, val_mn_id],
parent_id_opt,
};
if is_blank_node {
ed_model
.markup_node_pool
.replace_node(curr_mark_node_id, val_mark_node);
// remove data corresponding to Blank node
ed_model.del_at_line(old_caret_pos.line, old_caret_pos.column)?;
let char_len = 1;
ed_model.simple_move_carets_right(char_len);
// update GridNodeMap and CodeLines
ed_model.insert_between_line(
old_caret_pos.line,
old_caret_pos.column,
&digit_char.to_string(),
curr_mark_node_id,
)?;
Ok(InputOutcome::Accepted)
} else {
Ok(InputOutcome::Ignored)
}
}
// TODO check if new int needs more than e.g. 64 bits
pub fn update_int(
ed_model: &mut EdModel,
int_mark_node_id: MarkNodeId,
ch: &char,
) -> EdResult<InputOutcome> {
if ch.is_ascii_digit() {
let old_caret_pos = ed_model.get_caret();
let node_caret_offset = ed_model
.grid_node_map
.get_offset_to_node_id(old_caret_pos, int_mark_node_id)?;
let int_mark_node = ed_model.markup_node_pool.get_mut(int_mark_node_id);
let int_ast_node_id = int_mark_node.get_ast_node_id();
let content_str_mut = int_mark_node.get_content_mut()?;
// 00, 01 are not valid ints
if (content_str_mut == "0" && (node_caret_offset == 1 || *ch == '0'))
|| (*ch == '0' && node_caret_offset == 0)
{
Ok(InputOutcome::Ignored)
} else {
content_str_mut.insert(node_caret_offset, *ch);
let content_str = int_mark_node.get_content()?;
// update GridNodeMap and CodeLines
ed_model.insert_between_line(
old_caret_pos.line,
old_caret_pos.column,
&ch.to_string(),
int_mark_node_id,
)?;
// update ast
let new_pool_str = PoolStr::new(&content_str, ed_model.module.env.pool);
let int_ast_node = ed_model.module.env.pool.get_mut(int_ast_node_id);
match int_ast_node {
SmallInt { number, text, .. } => {
update_small_int_num(number, &content_str)?;
*text = new_pool_str;
}
_ => unimplemented!("TODO implement updating this type of Number"),
}
// update caret
ed_model.simple_move_carets_right(1);
Ok(InputOutcome::Accepted)
}
} else {
Ok(InputOutcome::Ignored)
}
}
fn update_small_int_num(number: &mut IntVal, updated_str: &str) -> EdResult<()> {
*number = match number {
I64(_) => I64(check_parse_res(updated_str.parse::<i64>())?),
U64(_) => U64(check_parse_res(updated_str.parse::<u64>())?),
I32(_) => I32(check_parse_res(updated_str.parse::<i32>())?),
U32(_) => U32(check_parse_res(updated_str.parse::<u32>())?),
I16(_) => I16(check_parse_res(updated_str.parse::<i16>())?),
U16(_) => U16(check_parse_res(updated_str.parse::<u16>())?),
I8(_) => I8(check_parse_res(updated_str.parse::<i8>())?),
U8(_) => U8(check_parse_res(updated_str.parse::<u8>())?),
};
Ok(())
}
fn check_parse_res<T, E: std::fmt::Debug>(parse_res: Result<T, E>) -> EdResult<T> {
match parse_res {
Ok(some_type) => Ok(some_type),
Err(parse_err) => StringParseError {
msg: format!("{:?}", parse_err),
}
.fail(),
}
}

View File

@ -1,6 +1,7 @@
use crate::editor::ed_error::EdResult;
use crate::editor::ed_error::{MissingParent, UnexpectedASTNode};
use crate::editor::markup::attribute::Attributes;
use crate::editor::markup::common_nodes::{new_blank_mn, new_comma_mn};
use crate::editor::markup::nodes;
use crate::editor::markup::nodes::MarkupNode;
use crate::editor::mvc::app_update::InputOutcome;
@ -191,27 +192,16 @@ pub fn update_mark_children(
parent_id_opt: Option<MarkNodeId>,
ed_model: &mut EdModel,
) -> EdResult<Vec<MarkNodeId>> {
let blank_mark_node = MarkupNode::Blank {
ast_node_id: blank_elt_id,
syn_high_style: HighlightStyle::Blank,
attributes: Attributes::new(),
parent_id_opt,
};
let blank_mark_node_id = ed_model.markup_node_pool.add(blank_mark_node);
let blank_mark_node_id =
new_blank_mn(blank_elt_id, parent_id_opt, &mut ed_model.markup_node_pool);
let mut children: Vec<MarkNodeId> = vec![];
if new_child_index > 1 {
let comma_mark_node = MarkupNode::Text {
content: nodes::COMMA.to_owned(),
ast_node_id: list_ast_node_id,
syn_high_style: HighlightStyle::Blank,
attributes: Attributes::new(),
parent_id_opt,
};
let comma_mark_node_id = ed_model.markup_node_pool.add(comma_mark_node);
let comma_mark_node_id =
new_comma_mn(list_ast_node_id, parent_id_opt, &mut ed_model.markup_node_pool);
ed_model.simple_move_carets_right(nodes::COMMA.len());

View File

@ -5,6 +5,7 @@ pub mod ed_update;
pub mod ed_view;
mod int_update;
mod list_update;
mod let_update;
mod lookup_update;
mod record_update;
mod string_update;

View File

@ -2,6 +2,7 @@ use crate::editor::ed_error::EdResult;
use crate::editor::ed_error::MissingParent;
use crate::editor::ed_error::RecordWithoutFields;
use crate::editor::markup::attribute::Attributes;
use crate::editor::markup::common_nodes::new_blank_mn;
use crate::editor::markup::nodes;
use crate::editor::markup::nodes::MarkupNode;
use crate::editor::mvc::app_update::InputOutcome;
@ -249,15 +250,9 @@ pub fn update_record_colon(
.get_mut(parent_id)
.add_child_at_index(new_child_index, record_colon_node_id)?;
let record_blank_node = MarkupNode::Blank {
ast_node_id: new_field_val_id,
syn_high_style: HighlightStyle::Blank,
attributes: Attributes::new(),
parent_id_opt: Some(parent_id),
};
let record_blank_node_id =
ed_model.markup_node_pool.add(record_blank_node);
new_blank_mn(new_field_val_id, Some(parent_id), &mut ed_model.markup_node_pool);
ed_model
.markup_node_pool
.get_mut(parent_id)