progress on starting with existing file to enable using LoadedModule

This commit is contained in:
Anton-4 2021-08-09 19:50:27 +02:00
parent 334f91392a
commit c6e816dfbe
4 changed files with 141 additions and 109 deletions

View file

@ -68,8 +68,6 @@ impl Symbol {
pub fn ident_string(self, interns: &Interns) -> &InlinableString { pub fn ident_string(self, interns: &Interns) -> &InlinableString {
dbg!(&interns.all_ident_ids); dbg!(&interns.all_ident_ids);
let stop = "here";
let ident_ids = interns let ident_ids = interns
.all_ident_ids .all_ident_ids
.get(&self.module_id()) .get(&self.module_id())

View file

@ -1,4 +1,5 @@
use super::keyboard_input; use super::keyboard_input;
use super::mvc::ed_model::init_model_refs;
use super::style::CODE_TXT_XY; use super::style::CODE_TXT_XY;
use crate::editor::ed_error::print_ui_err; use crate::editor::ed_error::print_ui_err;
use crate::editor::mvc::ed_view; use crate::editor::mvc::ed_view;
@ -29,7 +30,10 @@ use pipelines::RectResources;
use roc_module::symbol::Interns; use roc_module::symbol::Interns;
use roc_module::symbol::{IdentIds, ModuleIds}; use roc_module::symbol::{IdentIds, ModuleIds};
use roc_types::subs::VarStore; use roc_types::subs::VarStore;
use std::fs::File;
use std::os::unix::fs;
use std::{error::Error, io, path::Path}; use std::{error::Error, io, path::Path};
use std::io::{Write};
use wgpu::{CommandEncoder, RenderPass, TextureView}; use wgpu::{CommandEncoder, RenderPass, TextureView};
use wgpu_glyph::GlyphBrush; use wgpu_glyph::GlyphBrush;
use winit::{ use winit::{
@ -130,55 +134,17 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box<dyn Error>> {
let is_animating = true; let is_animating = true;
let mut env_pool = Pool::with_capacity(1024); let mut ed_model_refs = init_model_refs();
let env_arena = Bump::new();
let code_arena = Bump::new();
let mut var_store = VarStore::default(); let (file_path, code_bump_str) = read_file(file_path_opt, &ed_model_refs.code_arena);
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
let mut module_ids = ModuleIds::default();
let mod_id = module_ids.get_or_insert(&"ModId123".into());
let interns = Interns {
module_ids,
all_ident_ids: IdentIds::exposed_builtins(8),
};
let env = Env::new(
mod_id,
&env_arena,
&mut env_pool,
&mut var_store,
dep_idents,
&interns.module_ids,
exposed_ident_ids,
);
let mut code_str = BumpString::from_str_in("", &code_arena);
let file_path = if let Some(file_path) = file_path_opt {
match std::fs::read_to_string(file_path) {
Ok(file_as_str) => {
code_str = BumpString::from_str_in(&file_as_str, &code_arena);
file_path
}
Err(e) => {
print_ui_err(&FileOpenFailed {
path_str: file_path.to_string_lossy().to_string(),
err_msg: e.to_string(),
});
Path::new("")
}
}
} else {
Path::new("")
};
let ed_model_opt = { let ed_model_opt = {
let ed_model_res = ed_model::init_model(&code_str, file_path, env, &interns, &code_arena); let ed_model_res =
ed_model::init_model_and_env(
&code_bump_str,
file_path,
"HelloApp".to_owned(),
&mut ed_model_refs);
match ed_model_res { match ed_model_res {
Ok(mut ed_model) => { Ok(mut ed_model) => {
@ -417,6 +383,56 @@ fn begin_render_pass<'a>(
}) })
} }
fn read_file<'a>(file_path_opt: Option<&'a Path>, code_arena: &'a Bump) -> (&'a Path, BumpString<'a>) {
if let Some(file_path) = file_path_opt {
let file_as_str =
std::fs::read_to_string(file_path)
.expect(
&format!("Failed to read from provided file path: {:?}", file_path)
);
let code_str = BumpString::from_str_in(&file_as_str, code_arena);
(file_path, code_str)
} else {
let untitled_path = Path::new("UntitledApp.roc");
let code_str =
if !untitled_path.exists() {
let mut untitled_file =
File::create(untitled_path)
.expect(
&format!("I wanted to create {:?}, but it failed.", untitled_path)
);
let hello_world_roc = r#"
app "untitled-app"
packages { base: "platform" }
imports []
provides [ main ] to base
main = "Hello, world!"
"#;
write!(untitled_file, "{}", hello_world_roc)?;
BumpString::from_str_in(hello_world_roc, code_arena)
} else {
let file_as_str =
std::fs::read_to_string(untitled_path)
.expect(
&format!("I detected an existing {:?}, but I failed to read from it.", untitled_path)
);
BumpString::from_str_in(&file_as_str, code_arena)
};
(untitled_path, code_str)
}
}
fn queue_no_file_text( fn queue_no_file_text(
size: &PhysicalSize<u32>, size: &PhysicalSize<u32>,
text: &str, text: &str,

View file

@ -10,7 +10,7 @@ use crate::editor::{
use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::rect::Rect;
use crate::lang::ast::Expr2; use crate::lang::ast::Expr2;
use crate::lang::expr::{str_to_expr2, Env}; use crate::lang::expr::{str_to_expr2, Env};
use crate::lang::pool::NodeId; use crate::lang::pool::{NodeId, Pool};
use crate::lang::pool::PoolStr; use crate::lang::pool::PoolStr;
use crate::lang::scope::Scope; use crate::lang::scope::Scope;
use crate::ui::text::caret_w_select::CaretWSelect; use crate::ui::text::caret_w_select::CaretWSelect;
@ -20,8 +20,10 @@ use crate::ui::ui_error::UIResult;
use bumpalo::collections::String as BumpString; use bumpalo::collections::String as BumpString;
use bumpalo::Bump; use bumpalo::Bump;
use nonempty::NonEmpty; use nonempty::NonEmpty;
use roc_module::symbol::Interns; use roc_collections::all::MutMap;
use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds};
use roc_region::all::Region; use roc_region::all::Region;
use roc_types::subs::VarStore;
use std::path::Path; use std::path::Path;
#[derive(Debug)] #[derive(Debug)]
@ -38,7 +40,7 @@ pub struct EdModel<'a> {
pub has_focus: bool, pub has_focus: bool,
pub caret_w_select_vec: NonEmpty<(CaretWSelect, Option<MarkNodeId>)>, pub caret_w_select_vec: NonEmpty<(CaretWSelect, Option<MarkNodeId>)>,
pub selected_expr_opt: Option<SelectedExpression>, pub selected_expr_opt: Option<SelectedExpression>,
pub interns: &'a Interns, // this should eventually come from LoadedModule, see #1442 pub interns: Interns, // this should eventually come from LoadedModule, see #1442
pub show_debug_view: bool, pub show_debug_view: bool,
// EdModel is dirty if it has changed since the previous render. // EdModel is dirty if it has changed since the previous render.
pub dirty: bool, pub dirty: bool,
@ -51,14 +53,37 @@ pub struct SelectedExpression {
pub type_str: PoolStr, pub type_str: PoolStr,
} }
pub fn init_model<'a>( pub fn init_model_and_env<'a>(
code_str: &'a BumpString, code_str: &'a BumpString,
file_path: &'a Path, file_path: &'a Path,
env: Env<'a>, module_name: String,
interns: &'a Interns, ed_model_refs: &'a mut EdModelRefs,
code_arena: &'a Bump,
) -> EdResult<EdModel<'a>> { ) -> EdResult<EdModel<'a>> {
let mut module = EdModule::new(&code_str, env, code_arena)?;
let dep_idents = IdentIds::exposed_builtins(8);
let exposed_ident_ids = IdentIds::default();
let mut interns = Interns {
module_ids: ModuleIds::default(),
all_ident_ids: IdentIds::exposed_builtins(8),
};
let mod_id =
interns
.module_ids
.get_or_insert(&module_name.into());
let env = Env::new(
mod_id,
&ed_model_refs.env_arena,
&mut ed_model_refs.env_pool,
&mut ed_model_refs.var_store,
dep_idents,
&interns.module_ids,
exposed_ident_ids,
);
let mut module = EdModule::new(&code_str, env, &mut interns.all_ident_ids, &ed_model_refs.code_arena)?;
let ast_root_id = module.ast_root_id; let ast_root_id = module.ast_root_id;
let mut markup_node_pool = SlowPool::new(); let mut markup_node_pool = SlowPool::new();
@ -72,12 +97,12 @@ pub fn init_model<'a>(
let ast_root = &module.env.pool.get(ast_root_id); let ast_root = &module.env.pool.get(ast_root_id);
let temp_markup_root_id = expr2_to_markup( let temp_markup_root_id = expr2_to_markup(
code_arena, &ed_model_refs.code_arena,
&mut module.env, &mut module.env,
ast_root, ast_root,
ast_root_id, ast_root_id,
&mut markup_node_pool, &mut markup_node_pool,
interns &interns
)?; )?;
set_parent_for_all(temp_markup_root_id, &mut markup_node_pool); set_parent_for_all(temp_markup_root_id, &mut markup_node_pool);
@ -155,6 +180,22 @@ impl<'a> EdModel<'a> {
} }
} }
pub struct EdModelRefs {
pub code_arena: Bump,
pub env_arena: Bump,
pub env_pool: Pool,
pub var_store: VarStore,
}
pub fn init_model_refs() -> EdModelRefs {
EdModelRefs {
code_arena: Bump::new(),
env_arena: Bump::new(),
env_pool: Pool::with_capacity(1024),
var_store: VarStore::default(),
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct EdModule<'a> { pub struct EdModule<'a> {
pub env: Env<'a>, pub env: Env<'a>,
@ -165,7 +206,7 @@ pub struct EdModule<'a> {
use crate::lang::ast::expr2_to_string; use crate::lang::ast::expr2_to_string;
impl<'a> EdModule<'a> { impl<'a> EdModule<'a> {
pub fn new(code_str: &'a str, mut env: Env<'a>, ast_arena: &'a Bump) -> EdResult<EdModule<'a>> { pub fn new(code_str: &'a str, mut env: Env<'a>, all_ident_ids: &mut MutMap<ModuleId, IdentIds>, ast_arena: &'a Bump) -> EdResult<EdModule<'a>> {
if !code_str.is_empty() { if !code_str.is_empty() {
let mut scope = Scope::new(env.home, env.pool, env.var_store); let mut scope = Scope::new(env.home, env.pool, env.var_store);
@ -174,7 +215,26 @@ impl<'a> EdModule<'a> {
let expr2_result = str_to_expr2(&ast_arena, &code_str, &mut env, &mut scope, region); let expr2_result = str_to_expr2(&ast_arena, &code_str, &mut env, &mut scope, region);
match expr2_result { match expr2_result {
Ok((expr2, _output)) => { Ok((expr2, output)) => {
let bound_symbols = output.references.bound_symbols;
let mut module_ident_ids =
all_ident_ids
.get_mut(&env.home)
.unwrap_or_else(|| {
panic!(
"Could not find env.home (ModuleId: {:?}) in interns.all_ident_ids.keys: {:?}",
&env.home,
all_ident_ids.keys()
)
});
all_ident_ids
.insert(env.home, env.ident_ids);
let ast_root_id = env.pool.add(expr2); let ast_root_id = env.pool.add(expr2);
// for debugging // for debugging
@ -198,72 +258,30 @@ impl<'a> EdModule<'a> {
pub mod test_ed_model { pub mod test_ed_model {
use crate::editor::ed_error::EdResult; use crate::editor::ed_error::EdResult;
use crate::editor::mvc::ed_model; use crate::editor::mvc::ed_model;
use crate::lang::expr::Env;
use crate::lang::pool::Pool;
use crate::ui::text::caret_w_select::test_caret_w_select::convert_dsl_to_selection; use crate::ui::text::caret_w_select::test_caret_w_select::convert_dsl_to_selection;
use crate::ui::text::caret_w_select::test_caret_w_select::convert_selection_to_dsl; use crate::ui::text::caret_w_select::test_caret_w_select::convert_selection_to_dsl;
use crate::ui::text::lines::SelectableLines; use crate::ui::text::lines::SelectableLines;
use crate::ui::ui_error::UIResult; use crate::ui::ui_error::UIResult;
use bumpalo::collections::String as BumpString; use bumpalo::collections::String as BumpString;
use bumpalo::Bump;
use ed_model::EdModel; use ed_model::EdModel;
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
use roc_types::subs::VarStore;
use std::path::Path; use std::path::Path;
use super::EdModelRefs;
pub fn init_dummy_model<'a>( pub fn init_dummy_model<'a>(
code_str: &'a BumpString, code_str: &'a BumpString,
ed_model_refs: &'a mut EdModelRefs, ed_model_refs: &'a mut EdModelRefs,
) -> EdResult<EdModel<'a>> { ) -> EdResult<EdModel<'a>> {
let file_path = Path::new(""); let file_path = Path::new("");
let dep_idents = IdentIds::exposed_builtins(8); ed_model::init_model_and_env(
let exposed_ident_ids = IdentIds::default();
let mod_id = ed_model_refs
.interns
.module_ids
.get_or_insert(&"ModId123".into());
let env = Env::new(
mod_id,
&ed_model_refs.env_arena,
&mut ed_model_refs.env_pool,
&mut ed_model_refs.var_store,
dep_idents,
&ed_model_refs.interns.module_ids,
exposed_ident_ids,
);
ed_model::init_model(
&code_str, &code_str,
file_path, file_path,
env, "TestApp".to_owned(),
&ed_model_refs.interns, ed_model_refs,
&ed_model_refs.code_arena,
) )
} }
pub struct EdModelRefs {
code_arena: Bump,
env_arena: Bump,
env_pool: Pool,
var_store: VarStore,
interns: Interns,
}
pub fn init_model_refs() -> EdModelRefs {
EdModelRefs {
code_arena: Bump::new(),
env_arena: Bump::new(),
env_pool: Pool::with_capacity(1024),
var_store: VarStore::default(),
interns: Interns {
module_ids: ModuleIds::default(),
all_ident_ids: IdentIds::exposed_builtins(8),
},
}
}
pub fn ed_model_from_dsl<'a>( pub fn ed_model_from_dsl<'a>(
clean_code_str: &'a BumpString, clean_code_str: &'a BumpString,
code_lines: &[&str], code_lines: &[&str],

View file

@ -305,7 +305,7 @@ impl<'a> EdModel<'a> {
let content = subs.get(var).content; let content = subs.get(var).content;
PoolStr::new( PoolStr::new(
&content_to_string(content, &subs, self.module.env.home, self.interns), &content_to_string(content, &subs, self.module.env.home, &self.interns),
self.module.env.pool, self.module.env.pool,
) )
} }
@ -871,7 +871,7 @@ pub fn handle_new_char(received_char: &char, ed_model: &mut EdModel) -> EdResult
pub mod test_ed_update { pub mod test_ed_update {
use crate::editor::mvc::ed_model::test_ed_model::ed_model_from_dsl; use crate::editor::mvc::ed_model::test_ed_model::ed_model_from_dsl;
use crate::editor::mvc::ed_model::test_ed_model::ed_model_to_dsl; use crate::editor::mvc::ed_model::test_ed_model::ed_model_to_dsl;
use crate::editor::mvc::ed_model::test_ed_model::init_model_refs; use crate::editor::mvc::ed_model::init_model_refs;
use crate::editor::mvc::ed_update::handle_new_char; use crate::editor::mvc::ed_update::handle_new_char;
use crate::editor::mvc::ed_update::EdModel; use crate::editor::mvc::ed_update::EdModel;
use crate::editor::mvc::ed_update::EdResult; use crate::editor::mvc::ed_update::EdResult;