diff --git a/Cargo.lock b/Cargo.lock index f493e55a6b..0af4e03703 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2672,6 +2672,8 @@ dependencies = [ "log", "maplit", "page_size", + "pest", + "pest_derive", "pretty_assertions", "quickcheck", "quickcheck_macros", diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index ecb40b74ea..884909a0c8 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -484,6 +484,34 @@ fn get_inplace_from_layout(layout: &Layout<'_>) -> InPlace { } } +pub fn int_with_precision<'a, 'ctx, 'env>( + env: &Env<'a, 'ctx, 'env>, + value: i128, + precision: &Builtin, +) -> IntValue<'ctx> { + match precision { + Builtin::Usize => ptr_int(env.context, env.ptr_bytes).const_int(value as u64, false), + Builtin::Int128 => const_i128(env, value), + Builtin::Int64 => env.context.i64_type().const_int(value as u64, false), + Builtin::Int32 => env.context.i32_type().const_int(value as u64, false), + Builtin::Int16 => env.context.i16_type().const_int(value as u64, false), + Builtin::Int8 => env.context.i8_type().const_int(value as u64, false), + _ => panic!("Invalid layout for int literal = {:?}", precision), + } +} + +pub fn float_with_precision<'a, 'ctx, 'env>( + env: &Env<'a, 'ctx, 'env>, + value: f64, + precision: &Builtin, +) -> FloatValue<'ctx> { + match precision { + Builtin::Float64 => env.context.f64_type().const_float(value), + Builtin::Float32 => env.context.f32_type().const_float(value), + _ => panic!("Invalid layout for float literal = {:?}", precision), + } +} + pub fn build_exp_literal<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, layout: &Layout<'_>, @@ -492,22 +520,16 @@ pub fn build_exp_literal<'a, 'ctx, 'env>( use roc_mono::ir::Literal::*; match literal { - Int(int) => - (match layout { - Layout::Builtin(Builtin::Usize) => ptr_int(env.context, env.ptr_bytes), - Layout::Builtin(Builtin::Int128) => env.context.i128_type(), /* TODO file an issue: you can't currently have an int literal bigger than 64 bits long, and also (as we see here), you can't currently have (at least in Inkwell) a when-branch with an i128 literal in its pattren */ - Layout::Builtin(Builtin::Int64) => env.context.i64_type(), - Layout::Builtin(Builtin::Int32) => env.context.i32_type(), - Layout::Builtin(Builtin::Int16) => env.context.i16_type(), - Layout::Builtin(Builtin::Int8) => env.context.i8_type(), - _ => panic!("Invalid layout for int literal = {:?}", layout), - }).const_int(*int as u64, false).into(), - Float(num) => - (match layout { - Layout::Builtin(Builtin::Float64) => env.context.f64_type(), - Layout::Builtin(Builtin::Float32) => env.context.f32_type(), - _ => panic!("Invalid layout for float literal = {:?}", layout), - }).const_float(*num).into(), + Int(int) => match layout { + Layout::Builtin(builtin) => int_with_precision(env, *int as i128, builtin).into(), + _ => panic!("Invalid layout for int literal = {:?}", layout), + }, + + Float(float) => match layout { + Layout::Builtin(builtin) => float_with_precision(env, *float, builtin).into(), + _ => panic!("Invalid layout for float literal = {:?}", layout), + }, + Bool(b) => env.context.bool_type().const_int(*b as u64, false).into(), Byte(b) => env.context.i8_type().const_int(*b as u64, false).into(), Str(str_literal) => { @@ -1494,9 +1516,9 @@ pub fn build_exp_stmt<'a, 'ctx, 'env>( call, layout, pass, - fail: roc_mono::ir::Stmt::Unreachable, + fail: roc_mono::ir::Stmt::Rethrow, } => { - // when the fail case is just Unreachable, there is no cleanup work to do + // when the fail case is just Rethrow, there is no cleanup work to do // so we can just treat this invoke as a normal call let stmt = roc_mono::ir::Stmt::Let(*symbol, Expr::Call(call.clone()), layout.clone(), pass); @@ -1561,7 +1583,7 @@ pub fn build_exp_stmt<'a, 'ctx, 'env>( } }, - Unreachable => { + Rethrow => { cxa_rethrow_exception(env); // used in exception handling @@ -1783,6 +1805,22 @@ struct SwitchArgsIr<'a, 'ctx> { pub ret_type: BasicTypeEnum<'ctx>, } +fn const_i128<'a, 'ctx, 'env>(env: &Env<'a, 'ctx, 'env>, value: i128) -> IntValue<'ctx> { + // TODO verify the order [a, b] is correct for larger numbers when we can parse them + debug_assert!(value <= i64::MAX as i128); + + // truncate the lower 64 bits + let value = value as u128; + let a = value as u64; + + // get the upper 64 bits + let b = (value >> 64) as u64; + + env.context + .i128_type() + .const_int_arbitrary_precision(&[a, b]) +} + fn build_switch_ir<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, layout_ids: &mut LayoutIds<'a>, @@ -1860,7 +1898,7 @@ fn build_switch_ir<'a, 'ctx, 'env>( ptr_int(env.context, env.ptr_bytes).const_int(*int as u64, false) } Layout::Builtin(Builtin::Int64) => context.i64_type().const_int(*int as u64, false), - Layout::Builtin(Builtin::Int128) => context.i128_type().const_int(*int as u64, false), /* TODO file an issue: you can't currently have an int literal bigger than 64 bits long, and also (as we see here), you can't currently have (at least in Inkwell) a when-branch with an i128 literal in its pattren */ + Layout::Builtin(Builtin::Int128) => const_i128(env, *int as i128), Layout::Builtin(Builtin::Int32) => context.i32_type().const_int(*int as u64, false), Layout::Builtin(Builtin::Int16) => context.i16_type().const_int(*int as u64, false), Layout::Builtin(Builtin::Int8) => context.i8_type().const_int(*int as u64, false), diff --git a/compiler/gen/tests/gen_list.rs b/compiler/gen/tests/gen_list.rs index 8916d07586..cb01dc7760 100644 --- a/compiler/gen/tests/gen_list.rs +++ b/compiler/gen/tests/gen_list.rs @@ -1273,7 +1273,7 @@ mod gen_list { app "quicksort" provides [ main ] to "./platform" - swap : Int *, Int *, List a -> List a + swap : Nat, Nat, List a -> List a swap = \i, j, list -> when Pair (List.get list i) (List.get list j) is Pair (Ok atI) (Ok atJ) -> @@ -1396,7 +1396,7 @@ mod gen_list { quicksortHelp list 0 (n - 1) - quicksortHelp : List (Num a), Int *, Int * -> List (Num a) + quicksortHelp : List (Num a), Nat, Nat -> List (Num a) quicksortHelp = \list, low, high -> if low < high then when partition low high list is @@ -1408,7 +1408,7 @@ mod gen_list { list - swap : Int *, Int *, List a -> List a + swap : Nat, Nat, List a -> List a swap = \i, j, list -> when Pair (List.get list i) (List.get list j) is Pair (Ok atI) (Ok atJ) -> @@ -1419,7 +1419,7 @@ mod gen_list { _ -> [] - partition : Int *, Int *, List (Num a) -> [ Pair (Int *) (List (Num a)) ] + partition : Nat, Nat, List (Num a) -> [ Pair Nat (List (Num a)) ] partition = \low, high, initialList -> when List.get initialList high is Ok pivot -> @@ -1431,7 +1431,7 @@ mod gen_list { Pair (low - 1) initialList - partitionHelp : Int *, Int *, List (Num a), Int *, (Num a) -> [ Pair (Int *) (List (Num a)) ] + partitionHelp : Nat, Nat, List (Num a), Nat, (Num a) -> [ Pair Nat (List (Num a)) ] partitionHelp = \i, j, list, high, pivot -> if j < high then when List.get list j is @@ -1466,7 +1466,7 @@ mod gen_list { quicksortHelp list 0 (List.len list - 1) - quicksortHelp : List (Num a), Int *, Int * -> List (Num a) + quicksortHelp : List (Num a), Nat, Nat -> List (Num a) quicksortHelp = \list, low, high -> if low < high then when partition low high list is @@ -1478,7 +1478,7 @@ mod gen_list { list - swap : Int *, Int *, List a -> List a + swap : Nat, Nat, List a -> List a swap = \i, j, list -> when Pair (List.get list i) (List.get list j) is Pair (Ok atI) (Ok atJ) -> @@ -1489,7 +1489,7 @@ mod gen_list { _ -> [] - partition : Int *, Int *, List (Num a) -> [ Pair (Int *) (List (Num a)) ] + partition : Nat, Nat, List (Num a) -> [ Pair Nat (List (Num a)) ] partition = \low, high, initialList -> when List.get initialList high is Ok pivot -> @@ -1501,7 +1501,7 @@ mod gen_list { Pair (low - 1) initialList - partitionHelp : Int *, Int *, List (Num a), Int *, Num a -> [ Pair (Int *) (List (Num a)) ] + partitionHelp : Nat, Nat, List (Num a), Nat, Num a -> [ Pair Nat (List (Num a)) ] partitionHelp = \i, j, list, high, pivot -> # if j < high then if False then @@ -1539,7 +1539,7 @@ mod gen_list { quicksortHelp list 0 (List.len list - 1) - quicksortHelp : List (Num a), Int *, Int * -> List (Num a) + quicksortHelp : List (Num a), Nat, Nat -> List (Num a) quicksortHelp = \list, low, high -> if low < high then when partition low high list is @@ -1551,7 +1551,7 @@ mod gen_list { list - swap : Int *, Int *, List a -> List a + swap : Nat, Nat, List a -> List a swap = \i, j, list -> when Pair (List.get list i) (List.get list j) is Pair (Ok atI) (Ok atJ) -> @@ -1562,7 +1562,7 @@ mod gen_list { _ -> [] - partition : Int *, Int *, List (Num a) -> [ Pair (Int *) (List (Num a)) ] + partition : Nat, Nat, List (Num a) -> [ Pair Nat (List (Num a)) ] partition = \low, high, initialList -> when List.get initialList high is Ok pivot -> @@ -1574,7 +1574,7 @@ mod gen_list { Pair (low - 1) initialList - partitionHelp : Int *, Int *, List (Num a), Int *, Num a -> [ Pair (Int *) (List (Num a)) ] + partitionHelp : Nat, Nat, List (Num a), Nat, Num a -> [ Pair Nat (List (Num a)) ] partitionHelp = \i, j, list, high, pivot -> if j < high then when List.get list j is @@ -1740,6 +1740,7 @@ mod gen_list { assert_evals_to!("[[2]] != [[1]]", true, bool); } + #[test] #[should_panic(expected = r#"Roc failed with message: "integer addition overflowed!"#)] fn cleanup_because_exception() { assert_evals_to!( diff --git a/compiler/gen_dev/src/lib.rs b/compiler/gen_dev/src/lib.rs index 0a5a922a1a..a4df102b59 100644 --- a/compiler/gen_dev/src/lib.rs +++ b/compiler/gen_dev/src/lib.rs @@ -339,7 +339,7 @@ where Stmt::Ret(sym) => { self.set_last_seen(*sym, stmt); } - Stmt::Unreachable => {} + Stmt::Rethrow => {} Stmt::Inc(sym, following) => { self.set_last_seen(*sym, stmt); self.scan_ast(following); diff --git a/compiler/mono/src/borrow.rs b/compiler/mono/src/borrow.rs index 019a2249c2..8fe05e3e7e 100644 --- a/compiler/mono/src/borrow.rs +++ b/compiler/mono/src/borrow.rs @@ -170,7 +170,7 @@ impl<'a> ParamMap<'a> { } Inc(_, _) | Dec(_, _) => unreachable!("these have not been introduced yet"), - Ret(_) | Unreachable | Jump(_, _) | RuntimeError(_) => { + Ret(_) | Rethrow | Jump(_, _) | RuntimeError(_) => { // these are terminal, do nothing } } @@ -515,7 +515,7 @@ impl<'a> BorrowInfState<'a> { } Inc(_, _) | Dec(_, _) => unreachable!("these have not been introduced yet"), - Ret(_) | RuntimeError(_) | Unreachable => { + Ret(_) | RuntimeError(_) | Rethrow => { // these are terminal, do nothing } } diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index 6c0015129f..f0107b78ce 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -66,7 +66,7 @@ pub enum Test<'a> { union: crate::exhaustive::Union, arguments: Vec<(Pattern<'a>, Layout<'a>)>, }, - IsInt(i64), + IsInt(i128), // float patterns are stored as u64 so they are comparable/hashable IsFloat(u64), IsStr(Box), @@ -445,10 +445,10 @@ fn test_at_path<'a>(selected_path: &Path, branch: &Branch<'a>, all_tests: &mut V num_alts: union.alternatives.len(), }); } - IntLiteral(_, v) => { + IntLiteral(v) => { all_tests.push(guarded(IsInt(*v))); } - FloatLiteral(_, v) => { + FloatLiteral(v) => { all_tests.push(IsFloat(*v)); } StrLiteral(v) => { @@ -636,7 +636,7 @@ fn to_relevant_branch_help<'a>( _ => None, }, - IntLiteral(_, int) => match test { + IntLiteral(int) => match test { IsInt(is_int) if int == *is_int => { start.extend(end); Some(Branch { @@ -647,7 +647,7 @@ fn to_relevant_branch_help<'a>( _ => None, }, - FloatLiteral(_, float) => match test { + FloatLiteral(float) => match test { IsFloat(test_float) if float == *test_float => { start.extend(end); Some(Branch { @@ -740,8 +740,8 @@ fn needs_tests(pattern: &Pattern) -> bool { | AppliedTag { .. } | BitLiteral { .. } | EnumLiteral { .. } - | IntLiteral(_, _) - | FloatLiteral(_, _) + | IntLiteral(_) + | FloatLiteral(_) | StrLiteral(_) => true, } } @@ -1092,7 +1092,9 @@ fn test_to_equality<'a>( ) } Test::IsInt(test_int) => { - let lhs = Expr::Literal(Literal::Int(test_int)); + // TODO don't downcast i128 here + debug_assert!(test_int <= i64::MAX as i128); + let lhs = Expr::Literal(Literal::Int(test_int as i64)); let lhs_symbol = env.unique_symbol(); stores.push((lhs_symbol, Layout::Builtin(Builtin::Int64), lhs)); diff --git a/compiler/mono/src/exhaustive.rs b/compiler/mono/src/exhaustive.rs index 8e6442cf08..f3726960e8 100644 --- a/compiler/mono/src/exhaustive.rs +++ b/compiler/mono/src/exhaustive.rs @@ -37,7 +37,7 @@ pub enum Pattern { #[derive(Clone, Debug, PartialEq)] pub enum Literal { - Int(i64), + Int(i128), Bit(bool), Byte(u8), Float(u64), @@ -48,8 +48,8 @@ fn simplify(pattern: &crate::ir::Pattern) -> Pattern { use crate::ir::Pattern::*; match pattern { - IntLiteral(_, v) => Literal(Literal::Int(*v)), - FloatLiteral(_, v) => Literal(Literal::Float(*v)), + IntLiteral(v) => Literal(Literal::Int(*v)), + FloatLiteral(v) => Literal(Literal::Float(*v)), StrLiteral(v) => Literal(Literal::Str(v.clone())), // To make sure these are exhaustive, we have to "fake" a union here diff --git a/compiler/mono/src/inc_dec.rs b/compiler/mono/src/inc_dec.rs index 219dba7607..27503a35e1 100644 --- a/compiler/mono/src/inc_dec.rs +++ b/compiler/mono/src/inc_dec.rs @@ -50,7 +50,7 @@ pub fn occuring_variables(stmt: &Stmt<'_>) -> (MutSet, MutSet) { result.insert(*symbol); } - Unreachable => {} + Rethrow => {} Inc(symbol, cont) | Dec(symbol, cont) => { result.insert(*symbol); @@ -792,7 +792,7 @@ impl<'a> Context<'a> { } } - Unreachable => (stmt, MutSet::default()), + Rethrow => (stmt, MutSet::default()), Jump(j, xs) => { let empty = MutSet::default(); @@ -953,7 +953,7 @@ pub fn collect_stmt( vars } - Unreachable => vars, + Rethrow => vars, RuntimeError(_) => vars, } diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index 5942e55e2d..f011c2857e 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -762,7 +762,7 @@ pub enum Stmt<'a> { ret_layout: Layout<'a>, }, Ret(Symbol), - Unreachable, + Rethrow, Inc(Symbol, &'a Stmt<'a>), Dec(Symbol, &'a Stmt<'a>), Join { @@ -911,11 +911,6 @@ pub enum CallType<'a> { }, } -// x = f a b c; S -// -// -// invoke x = f a b c in S else Unreachable - #[derive(Clone, Debug, PartialEq)] pub enum Expr<'a> { Literal(Literal<'a>), @@ -1133,7 +1128,7 @@ impl<'a> Stmt<'a> { symbol, call, pass, - fail: Stmt::Unreachable, + fail: Stmt::Rethrow, .. } => alloc .text("let ") @@ -1166,7 +1161,7 @@ impl<'a> Stmt<'a> { .append(symbol_to_doc(alloc, *symbol)) .append(";"), - Unreachable => alloc.text("unreachable;"), + Rethrow => alloc.text("unreachable;"), Switch { cond_symbol, @@ -4572,7 +4567,7 @@ fn substitute_in_stmt_help<'a>( } } - Unreachable => None, + Rethrow => None, RuntimeError(_) => None, } @@ -4774,8 +4769,8 @@ fn store_pattern<'a>( Underscore => { // do nothing } - IntLiteral(_, _) - | FloatLiteral(_, _) + IntLiteral(_) + | FloatLiteral(_) | EnumLiteral { .. } | BitLiteral { .. } | StrLiteral(_) => {} @@ -4814,8 +4809,8 @@ fn store_pattern<'a>( Underscore => { // ignore } - IntLiteral(_, _) - | FloatLiteral(_, _) + IntLiteral(_) + | FloatLiteral(_) | EnumLiteral { .. } | BitLiteral { .. } | StrLiteral(_) => {} @@ -4909,8 +4904,8 @@ fn store_record_destruct<'a>( // // internally. But `y` is never used, so we must make sure it't not stored/loaded. } - IntLiteral(_, _) - | FloatLiteral(_, _) + IntLiteral(_) + | FloatLiteral(_) | EnumLiteral { .. } | BitLiteral { .. } | StrLiteral(_) => {} @@ -5293,7 +5288,7 @@ fn build_call<'a>( hole: &'a Stmt<'a>, ) -> Stmt<'a> { if can_throw_exception(&call) { - let fail = env.arena.alloc(Stmt::Unreachable); + let fail = env.arena.alloc(Stmt::Rethrow); Stmt::Invoke { symbol: assigned, call, @@ -5654,8 +5649,8 @@ fn call_by_name<'a>( pub enum Pattern<'a> { Identifier(Symbol), Underscore, - IntLiteral(Variable, i64), - FloatLiteral(Variable, u64), + IntLiteral(i128), + FloatLiteral(u64), BitLiteral { value: bool, tag_name: TagName, @@ -5728,10 +5723,8 @@ fn from_can_pattern_help<'a>( match can_pattern { Underscore => Ok(Pattern::Underscore), Identifier(symbol) => Ok(Pattern::Identifier(*symbol)), - IntLiteral(precision_var, int) => Ok(Pattern::IntLiteral(*precision_var, *int)), - FloatLiteral(precision_var, float) => { - Ok(Pattern::FloatLiteral(*precision_var, f64::to_bits(*float))) - } + IntLiteral(_, int) => Ok(Pattern::IntLiteral(*int as i128)), + FloatLiteral(_, float) => Ok(Pattern::FloatLiteral(f64::to_bits(*float))), StrLiteral(v) => Ok(Pattern::StrLiteral(v.clone())), Shadowed(region, ident) => Err(RuntimeError::Shadowing { original_region: *region, @@ -5744,10 +5737,10 @@ fn from_can_pattern_help<'a>( } NumLiteral(var, num) => { match num_argument_to_int_or_float(env.subs, env.ptr_bytes, *var, false) { - IntOrFloat::SignedIntType(_) => Ok(Pattern::IntLiteral(*var, *num)), - IntOrFloat::UnsignedIntType(_) => Ok(Pattern::IntLiteral(*var, *num)), - IntOrFloat::BinaryFloatType(_) => Ok(Pattern::FloatLiteral(*var, *num as u64)), - IntOrFloat::DecimalFloatType(_) => Ok(Pattern::FloatLiteral(*var, *num as u64)), + IntOrFloat::SignedIntType(_) => Ok(Pattern::IntLiteral(*num as i128)), + IntOrFloat::UnsignedIntType(_) => Ok(Pattern::IntLiteral(*num as i128)), + IntOrFloat::BinaryFloatType(_) => Ok(Pattern::FloatLiteral(*num as u64)), + IntOrFloat::DecimalFloatType(_) => Ok(Pattern::FloatLiteral(*num as u64)), } } diff --git a/compiler/mono/src/tail_recursion.rs b/compiler/mono/src/tail_recursion.rs index 4998c5fd84..87aa45038c 100644 --- a/compiler/mono/src/tail_recursion.rs +++ b/compiler/mono/src/tail_recursion.rs @@ -102,7 +102,7 @@ fn insert_jumps<'a>( pass: Stmt::Ret(rsym), .. } if needle == *fsym && symbol == rsym => { - debug_assert_eq!(fail, &&Stmt::Unreachable); + debug_assert_eq!(fail, &&Stmt::Rethrow); // replace the call and return with a jump @@ -237,7 +237,7 @@ fn insert_jumps<'a>( None => None, }, - Unreachable => None, + Rethrow => None, Ret(_) => None, Jump(_, _) => None, RuntimeError(_) => None, diff --git a/editor/Cargo.toml b/editor/Cargo.toml index fd0b8f6d6e..fcfda19a8a 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -66,6 +66,8 @@ cgmath = "0.17.0" itertools = "0.9.0" snafu = { version = "0.6", features = ["backtraces"] } colored = "2" +pest = "2.1" +pest_derive = "2.1" [dependencies.bytemuck] diff --git a/editor/editor-ideas.md b/editor/editor-ideas.md index ab10bb43d1..55e00d488a 100644 --- a/editor/editor-ideas.md +++ b/editor/editor-ideas.md @@ -55,8 +55,8 @@ These are potentially inspirational resources for the editor's design. ### Productivity features * When refactoring; - - cutting and pasting code to a new file should automatically add imports to the new file and delete them from the old file. - - When renaming a function for example, references in comments should be brought to the user's attention. + - Cutting and pasting code to a new file should automatically add imports to the new file and delete them from the old file. + - Ability to link e.g. variable name in comments to actual variable name. Comment is automatically updated when variable name is changed. * Automatically create all "arms" when pattern matching after entering `when var is` based on the type. - All `when ... is` should be updated if the type is changed, e.g. adding Indigo to the Color type should add an arm everywhere where `when color is` is used. diff --git a/editor/src/graphics/lowlevel/pipelines.rs b/editor/src/graphics/lowlevel/pipelines.rs new file mode 100644 index 0000000000..98b692fe30 --- /dev/null +++ b/editor/src/graphics/lowlevel/pipelines.rs @@ -0,0 +1,71 @@ +use super::ortho::{init_ortho, OrthoResources}; +use super::vertex::Vertex; + +pub struct RectResources { + pub pipeline: wgpu::RenderPipeline, + pub ortho: OrthoResources, +} + +pub fn make_rect_pipeline( + gpu_device: &wgpu::Device, + swap_chain_descr: &wgpu::SwapChainDescriptor, +) -> RectResources { + let ortho = init_ortho(swap_chain_descr.width, swap_chain_descr.height, gpu_device); + + let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + bind_group_layouts: &[&ortho.bind_group_layout], + push_constant_ranges: &[], + label: Some("Rectangle pipeline layout"), + }); + let pipeline = create_render_pipeline( + &gpu_device, + &pipeline_layout, + swap_chain_descr.format, + &[Vertex::DESC], + wgpu::include_spirv!("../shaders/rect.vert.spv"), + wgpu::include_spirv!("../shaders/rect.frag.spv"), + ); + + RectResources { pipeline, ortho } +} + +pub fn create_render_pipeline( + device: &wgpu::Device, + layout: &wgpu::PipelineLayout, + color_format: wgpu::TextureFormat, + vertex_descs: &[wgpu::VertexBufferDescriptor], + vs_src: wgpu::ShaderModuleSource, + fs_src: wgpu::ShaderModuleSource, +) -> wgpu::RenderPipeline { + let vs_module = device.create_shader_module(vs_src); + let fs_module = device.create_shader_module(fs_src); + + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("Render pipeline"), + layout: Some(&layout), + vertex_stage: wgpu::ProgrammableStageDescriptor { + module: &vs_module, + entry_point: "main", + }, + fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + module: &fs_module, + entry_point: "main", + }), + rasterization_state: None, + primitive_topology: wgpu::PrimitiveTopology::TriangleList, + color_states: &[wgpu::ColorStateDescriptor { + format: color_format, + color_blend: wgpu::BlendDescriptor::REPLACE, + alpha_blend: wgpu::BlendDescriptor::REPLACE, + write_mask: wgpu::ColorWrite::ALL, + }], + depth_stencil_state: None, + sample_count: 1, + sample_mask: !0, + alpha_to_coverage_enabled: false, + vertex_state: wgpu::VertexStateDescriptor { + index_format: wgpu::IndexFormat::Uint32, + vertex_buffers: vertex_descs, + }, + }) +} diff --git a/editor/src/lib.rs b/editor/src/lib.rs index 69c72eace6..33ad9a16a3 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -8,6 +8,11 @@ // See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/ +extern crate pest; +#[cfg(test)] +#[macro_use] +extern crate pest_derive; + use crate::error::EdError::MissingGlyphDims; use crate::error::{print_err, EdResult}; use crate::graphics::colors::{CARET_COLOR, CODE_COLOR, TXT_COLOR}; diff --git a/editor/src/selection.rs b/editor/src/selection.rs index b8976d31da..ef7a212e78 100644 --- a/editor/src/selection.rs +++ b/editor/src/selection.rs @@ -150,3 +150,2033 @@ pub fn create_selection_rects<'a>( Ok(all_rects) } } + +#[cfg(test)] +mod test_parse { + use crate::error::{EdResult, OutOfBounds}; + use crate::tea::model::{Position, RawSelection}; + use crate::tea::update::{move_caret_down, move_caret_left, move_caret_right, move_caret_up}; + use crate::vec_result::get_res; + use core::cmp::Ordering; + use pest::Parser; + use snafu::OptionExt; + use std::collections::HashMap; + use std::slice::SliceIndex; + + #[derive(Parser)] + #[grammar = "../tests/selection.pest"] + pub struct LineParser; + + // show selection and caret position as symbols in lines for easy testing + fn convert_selection_to_dsl( + raw_sel_opt: Option, + caret_pos: Position, + lines: &mut [String], + ) -> EdResult<&[String]> { + if let Some(raw_sel) = raw_sel_opt { + let mut to_insert = vec![ + (raw_sel.start_pos, '['), + (raw_sel.end_pos, ']'), + (caret_pos, '|'), + ]; + let symbol_map: HashMap = + [('[', 2), (']', 0), ('|', 1)].iter().cloned().collect(); + + // sort for nice printing + to_insert.sort_by(|a, b| { + let pos_cmp = a.0.cmp(&b.0); + if pos_cmp == Ordering::Equal { + symbol_map.get(&a.1).cmp(&symbol_map.get(&b.1)) + } else { + pos_cmp + } + }); + + // insert symbols into text lines + for i in 0..to_insert.len() { + let (pos, insert_char) = *get_res(i, &to_insert)?; + + insert_at_pos(lines, pos, insert_char)?; + + // shift position of following symbols now that symbol is inserted + for j in i..to_insert.len() { + let (old_pos, _) = get_mut_res(j, &mut to_insert)?; + + if old_pos.line == pos.line { + old_pos.column += 1; + } + } + } + } else { + insert_at_pos(lines, caret_pos, '|')?; + } + + Ok(lines) + } + + fn insert_at_pos(lines: &mut [String], pos: Position, insert_char: char) -> EdResult<()> { + let line = get_mut_res(pos.line, lines)?; + line.insert(pos.column, insert_char); + + Ok(()) + } + + // It's much nicer to have get_mut return a Result with clear error than an Option + fn get_mut_res( + index: usize, + vec: &mut [T], + ) -> EdResult<&mut >::Output> { + let vec_len = vec.len(); + + let elt_ref = vec.get_mut(index).context(OutOfBounds { index, vec_len })?; + + Ok(elt_ref) + } + + // Retrieve selection and position from formatted string + fn convert_dsl_to_selection( + lines: &[String], + ) -> Result<(Option, Position), String> { + let lines_str: String = lines.join(""); + + let parsed = LineParser::parse(Rule::linesWithSelect, &lines_str) + .expect("Selection test DSL parsing failed"); + + let mut caret_opt: Option<(usize, usize)> = None; + let mut sel_start_opt: Option<(usize, usize)> = None; + let mut sel_end_opt: Option<(usize, usize)> = None; + let mut line_nr = 0; + let mut col_nr = 0; + + for line in parsed { + for elt in line.into_inner() { + match elt.as_rule() { + Rule::optCaret => { + if elt.as_span().as_str() == "|" { + if caret_opt.is_some() { + return Err( + "Multiple carets found, there should be only one".to_owned() + ); + } else { + caret_opt = Some((line_nr, col_nr)); + } + } + } + Rule::optSelStart => { + if sel_start_opt.is_some() { + if elt.as_span().as_str() == "[" { + return Err("Found start of selection more than once, there should be only one".to_owned()); + } + } else if elt.as_span().as_str() == "[" { + sel_start_opt = Some((line_nr, col_nr)); + } + } + Rule::optSelEnd => { + if sel_end_opt.is_some() { + if elt.as_span().as_str() == "]" { + return Err("Found end of selection more than once, there should be only one".to_owned()); + } + } else if elt.as_span().as_str() == "]" { + sel_end_opt = Some((line_nr, col_nr)); + } + } + Rule::text => { + let split_str = elt + .as_span() + .as_str() + .split('\n') + .into_iter() + .collect::>(); + + if split_str.len() > 1 { + line_nr += split_str.len() - 1; + col_nr = 0 + } + if let Some(last_str) = split_str.last() { + col_nr += last_str.len() + } + } + _ => {} + } + } + } + + // Make sure return makes sense + if let Some((line, column)) = caret_opt { + let caret_pos = Position { line, column }; + if sel_start_opt.is_none() && sel_end_opt.is_none() { + Ok((None, caret_pos)) + } else if let Some((start_line, start_column)) = sel_start_opt { + if let Some((end_line, end_column)) = sel_end_opt { + Ok(( + Some(RawSelection { + start_pos: Position { + line: start_line, + column: start_column, + }, + end_pos: Position { + line: end_line, + column: end_column, + }, + }), + caret_pos, + )) + } else { + Err("Selection end ']' was not found, but selection start '[' was. Bad input string.".to_owned()) + } + } else { + Err("Selection start '[' was not found, but selection end ']' was. Bad input string.".to_owned()) + } + } else { + Err("No caret was found in lines.".to_owned()) + } + } + + pub type MoveCaretFun = + fn(Position, Option, bool, &[String]) -> (Position, Option); + + // Convert nice string representations and compare results + fn assert_move( + pre_lines_str: &[&str], + expected_post_lines_str: &[&str], + shift_pressed: bool, + move_fun: MoveCaretFun, + ) -> Result<(), String> { + let pre_lines: Vec = pre_lines_str.iter().map(|l| l.to_string()).collect(); + let expected_post_lines: Vec = expected_post_lines_str + .iter() + .map(|l| l.to_string()) + .collect(); + + let (sel_opt, caret_pos) = convert_dsl_to_selection(&pre_lines)?; + + let mut clean_lines = pre_lines + .into_iter() + .map(|line| line.replace(&['[', ']', '|'][..], "")) + .collect::>(); + + let (new_caret_pos, new_sel_opt) = + move_fun(caret_pos, sel_opt, shift_pressed, &clean_lines); + + let post_lines_res = convert_selection_to_dsl(new_sel_opt, new_caret_pos, &mut clean_lines); + + match post_lines_res { + Ok(post_lines) => { + assert_eq!(expected_post_lines, post_lines); + Ok(()) + } + Err(e) => Err(format!("{:?}", e)), + } + } + + #[test] + fn move_right() -> Result<(), String> { + assert_move(&["|"], &["|"], false, move_caret_right)?; + assert_move(&["a|"], &["a|"], false, move_caret_right)?; + assert_move(&["|A"], &["A|"], false, move_caret_right)?; + assert_move(&["|abc"], &["a|bc"], false, move_caret_right)?; + assert_move(&["a|bc"], &["ab|c"], false, move_caret_right)?; + assert_move(&["abc|"], &["abc|"], false, move_caret_right)?; + assert_move(&["| abc"], &[" |abc"], false, move_caret_right)?; + assert_move(&["abc| "], &["abc |"], false, move_caret_right)?; + assert_move(&["abc|\n", "d"], &["abc\n", "|d"], false, move_caret_right)?; + assert_move(&["abc|\n", ""], &["abc\n", "|"], false, move_caret_right)?; + assert_move( + &["abc\n", "|def"], + &["abc\n", "d|ef"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def| "], + &["abc\n", "def |"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def |\n", "ghi"], + &["abc\n", "def \n", "|ghi"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def|\n", ""], + &["abc\n", "def\n", "|"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "ghi|\n", "jkl"], + &["abc\n", "def\n", "ghi\n", "|jkl"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "|ghi\n", "jkl"], + &["abc\n", "def\n", "g|hi\n", "jkl"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "g|hi\n", "jkl"], + &["abc\n", "def\n", "gh|i\n", "jkl"], + false, + move_caret_right, + )?; + + Ok(()) + } + + #[test] + fn move_left() -> Result<(), String> { + assert_move(&["|"], &["|"], false, move_caret_left)?; + assert_move(&["|a"], &["|a"], false, move_caret_left)?; + assert_move(&["|A"], &["|A"], false, move_caret_left)?; + assert_move(&["a|bc"], &["|abc"], false, move_caret_left)?; + assert_move(&["ab|c"], &["a|bc"], false, move_caret_left)?; + assert_move(&["abc|"], &["ab|c"], false, move_caret_left)?; + assert_move(&[" |abc"], &["| abc"], false, move_caret_left)?; + assert_move(&["abc |"], &["abc| "], false, move_caret_left)?; + assert_move(&["abc\n", "|d"], &["abc|\n", "d"], false, move_caret_left)?; + assert_move(&["abc\n", "|"], &["abc|\n", ""], false, move_caret_left)?; + assert_move( + &["abc\n", "d|ef"], + &["abc\n", "|def"], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def |"], + &["abc\n", "def| "], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def \n", "|ghi"], + &["abc\n", "def |\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "|"], + &["abc\n", "def|\n", ""], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "ghi\n", "|jkl"], + &["abc\n", "def\n", "ghi|\n", "jkl"], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "g|hi\n", "jkl"], + &["abc\n", "def\n", "|ghi\n", "jkl"], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "gh|i\n", "jkl"], + &["abc\n", "def\n", "g|hi\n", "jkl"], + false, + move_caret_left, + )?; + + Ok(()) + } + + #[test] + fn move_up() -> Result<(), String> { + assert_move(&["|"], &["|"], false, move_caret_up)?; + assert_move(&["|a"], &["|a"], false, move_caret_up)?; + assert_move(&["A|"], &["|A"], false, move_caret_up)?; + assert_move(&["a|bc"], &["|abc"], false, move_caret_up)?; + assert_move(&["ab|c"], &["|abc"], false, move_caret_up)?; + assert_move(&["abc|"], &["|abc"], false, move_caret_up)?; + assert_move(&["|abc\n", "def"], &["|abc\n", "def"], false, move_caret_up)?; + assert_move(&["abc\n", "|def"], &["|abc\n", "def"], false, move_caret_up)?; + assert_move(&["abc\n", "d|ef"], &["a|bc\n", "def"], false, move_caret_up)?; + assert_move(&["abc\n", "de|f"], &["ab|c\n", "def"], false, move_caret_up)?; + assert_move(&["abc\n", "def|"], &["abc|\n", "def"], false, move_caret_up)?; + assert_move( + &["abc\n", "def \n", "|ghi"], + &["abc\n", "|def \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "g|hi"], + &["abc\n", "d|ef \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "gh|i"], + &["abc\n", "de|f \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "ghi|"], + &["abc\n", "def| \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "de\n", "ghi|"], + &["abc\n", "de|\n", "ghi"], + false, + move_caret_up, + )?; + assert_move(&["abc\n", "de|"], &["ab|c\n", "de"], false, move_caret_up)?; + assert_move(&["abc\n", "d|e"], &["a|bc\n", "de"], false, move_caret_up)?; + assert_move(&["abc\n", "|de"], &["|abc\n", "de"], false, move_caret_up)?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "mnopqrst|"], + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + &["ab\n", "cdef|\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "|mnopqrst"], + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &[" ab\n", " |cdef\n", "ghijkl\n", "mnopqrst"], + &[" |ab\n", " cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "mnopqr|st"], + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cde|f\n", "ghijkl\n", "mnopqrst"], + &["ab|\n", "cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + &["abcdefgh\n", "ijklmn\n", "op|qr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + &["abcdefgh\n", "ijkl|mn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + &["abcdef|gh\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh|\n", "ijklmn\n", "opqr\n", "st"], + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefg|h\n", "ijklmn\n", "opqr\n", "st"], + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["a|bcdefgh\n", "ijklmn\n", "opqr\n", "st"], + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move(&["abc def gh |"], &["|abc def gh "], false, move_caret_up)?; + assert_move(&["abc de|f gh "], &["|abc def gh "], false, move_caret_up)?; + assert_move(&["ab|c def gh "], &["|abc def gh "], false, move_caret_up)?; + assert_move(&["a|bc def gh "], &["|abc def gh "], false, move_caret_up)?; + + Ok(()) + } + + #[test] + fn move_down() -> Result<(), String> { + assert_move(&["|"], &["|"], false, move_caret_down)?; + assert_move(&["|a"], &["a|"], false, move_caret_down)?; + assert_move(&["A|"], &["A|"], false, move_caret_down)?; + assert_move(&["a|bc"], &["abc|"], false, move_caret_down)?; + assert_move(&["ab|c"], &["abc|"], false, move_caret_down)?; + assert_move(&["abc|"], &["abc|"], false, move_caret_down)?; + assert_move(&["abc| "], &["abc |"], false, move_caret_down)?; + assert_move( + &["abc\n", "|def"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "d|ef"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|f"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "def|"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["|abc\n", "def"], + &["abc\n", "|def"], + false, + move_caret_down, + )?; + assert_move( + &["a|bc\n", "def"], + &["abc\n", "d|ef"], + false, + move_caret_down, + )?; + assert_move( + &["ab|c\n", "def"], + &["abc\n", "de|f"], + false, + move_caret_down, + )?; + assert_move( + &["abc|\n", "def"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "|def \n", "ghi"], + &["abc\n", "def \n", "|ghi"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "d|ef \n", "ghi"], + &["abc\n", "def \n", "g|hi"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|f \n", "ghi"], + &["abc\n", "def \n", "gh|i"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "def| \n", "ghi"], + &["abc\n", "def \n", "ghi|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "def |\n", "ghi"], + &["abc\n", "def \n", "ghi|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|\n", "ghi"], + &["abc\n", "de\n", "gh|i"], + false, + move_caret_down, + )?; + assert_move(&["abc|\n", "de"], &["abc\n", "de|"], false, move_caret_down)?; + assert_move(&["ab|c\n", "de"], &["abc\n", "de|"], false, move_caret_down)?; + assert_move(&["a|bc\n", "de"], &["abc\n", "d|e"], false, move_caret_down)?; + assert_move(&["|abc\n", "de"], &["abc\n", "|de"], false, move_caret_down)?; + assert_move( + &["ab|\n", "cdef\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "cd|ef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef|\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "cdef\n", "ghij|kl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + &["ab\n", "cdef\n", "ghijkl\n", "mnopqr|st"], + false, + move_caret_down, + )?; + assert_move( + &[" |ab\n", " cdef\n", "ghijkl\n", "mnopqrst"], + &[" ab\n", " |cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "|cdef\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + &["ab\n", "cdef\n", "ghijkl\n", "|mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh|\n", "ijklmn\n", "opqr\n", "st"], + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "|st"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + false, + move_caret_down, + )?; + assert_move(&["abc def gh |"], &["abc def gh |"], false, move_caret_down)?; + assert_move(&["abc de|f gh "], &["abc def gh |"], false, move_caret_down)?; + assert_move(&["ab|c def gh "], &["abc def gh |"], false, move_caret_down)?; + assert_move(&["a|bc def gh "], &["abc def gh |"], false, move_caret_down)?; + assert_move(&["|abc def gh "], &["abc def gh |"], false, move_caret_down)?; + + Ok(()) + } + + #[test] + fn start_selection_right() -> Result<(), String> { + assert_move(&["|"], &["|"], true, move_caret_right)?; + assert_move(&["a|"], &["a|"], true, move_caret_right)?; + assert_move(&["|A"], &["[A]|"], true, move_caret_right)?; + assert_move(&["|abc"], &["[a]|bc"], true, move_caret_right)?; + assert_move(&["a|bc"], &["a[b]|c"], true, move_caret_right)?; + assert_move(&["abc|"], &["abc|"], true, move_caret_right)?; + assert_move(&["| abc"], &["[ ]|abc"], true, move_caret_right)?; + assert_move(&["abc| "], &["abc[ ]|"], true, move_caret_right)?; + assert_move(&["abc|\n", "d"], &["abc[\n", "]|d"], true, move_caret_right)?; + assert_move(&["abc|\n", ""], &["abc[\n", "]|"], true, move_caret_right)?; + assert_move( + &["abc\n", "|def"], + &["abc\n", "[d]|ef"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def| "], + &["abc\n", "def[ ]|"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def |\n", "ghi"], + &["abc\n", "def [\n", "]|ghi"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def|\n", ""], + &["abc\n", "def[\n", "]|"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "ghi|\n", "jkl"], + &["abc\n", "def\n", "ghi[\n", "]|jkl"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "|ghi\n", "jkl"], + &["abc\n", "def\n", "[g]|hi\n", "jkl"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "g|hi\n", "jkl"], + &["abc\n", "def\n", "g[h]|i\n", "jkl"], + true, + move_caret_right, + )?; + + Ok(()) + } + + #[test] + fn start_selection_left() -> Result<(), String> { + assert_move(&["|"], &["|"], true, move_caret_left)?; + assert_move(&["a|"], &["|[a]"], true, move_caret_left)?; + assert_move(&["|A"], &["|A"], true, move_caret_left)?; + assert_move(&["|abc"], &["|abc"], true, move_caret_left)?; + assert_move(&["a|bc"], &["|[a]bc"], true, move_caret_left)?; + assert_move(&["abc|"], &["ab|[c]"], true, move_caret_left)?; + assert_move(&[" |abc"], &["|[ ]abc"], true, move_caret_left)?; + assert_move(&["abc |"], &["abc|[ ]"], true, move_caret_left)?; + assert_move(&["abc|\n", "d"], &["ab|[c]\n", "d"], true, move_caret_left)?; + assert_move(&["abc\n", "|d"], &["abc|[\n", "]d"], true, move_caret_left)?; + assert_move(&["abc\n", "|"], &["abc|[\n", "]"], true, move_caret_left)?; + assert_move( + &["abc\n", " |def"], + &["abc\n", "|[ ]def"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "d|ef"], + &["abc\n", "|[d]ef"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "de|f "], + &["abc\n", "d|[e]f "], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "|"], + &["abc\n", "def|[\n", "]"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "|ghi\n", "jkl"], + &["abc\n", "def|[\n", "]ghi\n", "jkl"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "g|hi\n", "jkl"], + &["abc\n", "def\n", "|[g]hi\n", "jkl"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "gh|i\n", "jkl"], + &["abc\n", "def\n", "g|[h]i\n", "jkl"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "ghi|\n", "jkl"], + &["abc\n", "def\n", "gh|[i]\n", "jkl"], + true, + move_caret_left, + )?; + + Ok(()) + } + + #[test] + fn start_selection_down() -> Result<(), String> { + assert_move(&["|"], &["|"], true, move_caret_down)?; + assert_move(&["|a"], &["[a]|"], true, move_caret_down)?; + assert_move(&["A|"], &["A|"], true, move_caret_down)?; + assert_move(&["a|bc"], &["a[bc]|"], true, move_caret_down)?; + assert_move(&["ab|c"], &["ab[c]|"], true, move_caret_down)?; + assert_move(&["abc|"], &["abc|"], true, move_caret_down)?; + assert_move(&["abc| "], &["abc[ ]|"], true, move_caret_down)?; + assert_move( + &["abc\n", "|def"], + &["abc\n", "[def]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "d|ef"], + &["abc\n", "d[ef]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|f"], + &["abc\n", "de[f]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "def|"], + &["abc\n", "def|"], + true, + move_caret_down, + )?; + assert_move( + &["|abc\n", "def"], + &["[abc\n", "]|def"], + true, + move_caret_down, + )?; + assert_move( + &["a|bc\n", "def"], + &["a[bc\n", "d]|ef"], + true, + move_caret_down, + )?; + assert_move( + &["ab|c\n", "def"], + &["ab[c\n", "de]|f"], + true, + move_caret_down, + )?; + assert_move( + &["abc|\n", "def"], + &["abc[\n", "def]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "|def \n", "ghi"], + &["abc\n", "[def \n", "]|ghi"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "d|ef \n", "ghi"], + &["abc\n", "d[ef \n", "g]|hi"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|f \n", "ghi"], + &["abc\n", "de[f \n", "gh]|i"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "def| \n", "ghi"], + &["abc\n", "def[ \n", "ghi]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "def |\n", "ghi"], + &["abc\n", "def [\n", "ghi]|"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|\n", "ghi"], + &["abc\n", "de[\n", "gh]|i"], + true, + move_caret_down, + )?; + assert_move( + &["abc|\n", "de"], + &["abc[\n", "de]|"], + true, + move_caret_down, + )?; + assert_move( + &["ab|c\n", "de"], + &["ab[c\n", "de]|"], + true, + move_caret_down, + )?; + assert_move( + &["a|bc\n", "de"], + &["a[bc\n", "d]|e"], + true, + move_caret_down, + )?; + assert_move( + &["|abc\n", "de"], + &["[abc\n", "]|de"], + true, + move_caret_down, + )?; + assert_move( + &["ab|\n", "cdef\n", "ghijkl\n", "mnopqrst"], + &["ab[\n", "cd]|ef\n", "ghijkl\n", "mnopqrst"], + true, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef|\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "cdef[\n", "ghij]|kl\n", "mnopqrst"], + true, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + &["ab\n", "cdef\n", "ghijkl[\n", "mnopqr]|st"], + true, + move_caret_down, + )?; + assert_move( + &[" |ab\n", " cdef\n", "ghijkl\n", "mnopqrst"], + &[" [ab\n", " ]|cdef\n", "ghijkl\n", "mnopqrst"], + true, + move_caret_down, + )?; + assert_move( + &["ab\n", "|cdef\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "[cdef\n", "]|ghijkl\n", "mnopqrst"], + true, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + &["ab\n", "cdef\n", "[ghijkl\n", "]|mnopqrst"], + true, + move_caret_down, + )?; + assert_move( + &["abcdefgh|\n", "ijklmn\n", "opqr\n", "st"], + &["abcdefgh[\n", "ijklmn]|\n", "opqr\n", "st"], + true, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + &["abcdefgh\n", "ijklmn[\n", "opqr]|\n", "st"], + true, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + &["abcdefgh\n", "ijklmn\n", "opqr[\n", "st]|"], + true, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "|st"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "[st]|"], + true, + move_caret_down, + )?; + assert_move(&["abc def gh |"], &["abc def gh |"], true, move_caret_down)?; + assert_move( + &["abc de|f gh "], + &["abc de[f gh ]|"], + true, + move_caret_down, + )?; + assert_move( + &["ab|c def gh "], + &["ab[c def gh ]|"], + true, + move_caret_down, + )?; + assert_move( + &["a|bc def gh "], + &["a[bc def gh ]|"], + true, + move_caret_down, + )?; + assert_move( + &["|abc def gh "], + &["[abc def gh ]|"], + true, + move_caret_down, + )?; + + Ok(()) + } + + #[test] + fn start_selection_up() -> Result<(), String> { + assert_move(&["|"], &["|"], true, move_caret_up)?; + assert_move(&["|a"], &["|a"], true, move_caret_up)?; + assert_move(&["A|"], &["|[A]"], true, move_caret_up)?; + assert_move(&["a|bc"], &["|[a]bc"], true, move_caret_up)?; + assert_move(&["ab|c"], &["|[ab]c"], true, move_caret_up)?; + assert_move(&["abc|"], &["|[abc]"], true, move_caret_up)?; + assert_move(&["|abc\n", "def"], &["|abc\n", "def"], true, move_caret_up)?; + assert_move( + &["abc\n", "|def"], + &["|[abc\n", "]def"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "d|ef"], + &["a|[bc\n", "d]ef"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "de|f"], + &["ab|[c\n", "de]f"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def|"], + &["abc|[\n", "def]"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "|ghi"], + &["abc\n", "|[def \n", "]ghi"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "g|hi"], + &["abc\n", "d|[ef \n", "g]hi"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "gh|i"], + &["abc\n", "de|[f \n", "gh]i"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def \n", "ghi|"], + &["abc\n", "def|[ \n", "ghi]"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "de\n", "ghi|"], + &["abc\n", "de|[\n", "ghi]"], + true, + move_caret_up, + )?; + assert_move(&["abc\n", "de|"], &["ab|[c\n", "de]"], true, move_caret_up)?; + assert_move(&["abc\n", "d|e"], &["a|[bc\n", "d]e"], true, move_caret_up)?; + assert_move(&["abc\n", "|de"], &["|[abc\n", "]de"], true, move_caret_up)?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "mnopqrst|"], + &["ab\n", "cdef\n", "ghijkl|[\n", "mnopqrst]"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + &["ab\n", "cdef|[\n", "ghijkl]\n", "mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "|mnopqrst"], + &["ab\n", "cdef\n", "|[ghijkl\n", "]mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &[" ab\n", " |cdef\n", "ghijkl\n", "mnopqrst"], + &[" |[ab\n", " ]cdef\n", "ghijkl\n", "mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "mnopqr|st"], + &["ab\n", "cdef\n", "ghijkl|[\n", "mnopqr]st"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cde|f\n", "ghijkl\n", "mnopqrst"], + &["ab|[\n", "cde]f\n", "ghijkl\n", "mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + &["abcdefgh\n", "ijklmn\n", "op|[qr\n", "st]"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + &["abcdefgh\n", "ijkl|[mn\n", "opqr]\n", "st"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + &["abcdef|[gh\n", "ijklmn]\n", "opqr\n", "st"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefgh|\n", "ijklmn\n", "opqr\n", "st"], + &["|[abcdefgh]\n", "ijklmn\n", "opqr\n", "st"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefg|h\n", "ijklmn\n", "opqr\n", "st"], + &["|[abcdefg]h\n", "ijklmn\n", "opqr\n", "st"], + true, + move_caret_up, + )?; + assert_move( + &["a|bcdefgh\n", "ijklmn\n", "opqr\n", "st"], + &["|[a]bcdefgh\n", "ijklmn\n", "opqr\n", "st"], + true, + move_caret_up, + )?; + assert_move( + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + &["|abcdefgh\n", "ijklmn\n", "opqr\n", "st"], + true, + move_caret_up, + )?; + assert_move(&["abc def gh |"], &["|[abc def gh ]"], true, move_caret_up)?; + assert_move(&["abc de|f gh "], &["|[abc de]f gh "], true, move_caret_up)?; + assert_move(&["ab|c def gh "], &["|[ab]c def gh "], true, move_caret_up)?; + assert_move(&["a|bc def gh "], &["|[a]bc def gh "], true, move_caret_up)?; + + Ok(()) + } + + #[test] + fn end_selection_right() -> Result<(), String> { + assert_move(&["[A]|"], &["A|"], false, move_caret_right)?; + assert_move(&["[a]|bc"], &["a|bc"], false, move_caret_right)?; + assert_move(&["a[b]|c"], &["ab|c"], false, move_caret_right)?; + assert_move(&["ab[c]|"], &["abc|"], false, move_caret_right)?; + assert_move(&["[ ]|abc"], &[" |abc"], false, move_caret_right)?; + assert_move(&["|[ ]abc"], &[" |abc"], false, move_caret_right)?; + assert_move(&["a|[b]c"], &["ab|c"], false, move_caret_right)?; + assert_move( + &["abc[\n", "]|d"], + &["abc\n", "|d"], + false, + move_caret_right, + )?; + assert_move( + &["abc|[\n", "]d"], + &["abc\n", "|d"], + false, + move_caret_right, + )?; + assert_move(&["abc|[\n", "]"], &["abc\n", "|"], false, move_caret_right)?; + assert_move( + &["abc\n", "[d]|ef"], + &["abc\n", "d|ef"], + false, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "ghi[\n", "]|jkl"], + &["abc\n", "def\n", "ghi\n", "|jkl"], + false, + move_caret_right, + )?; + assert_move(&["[ab]|c"], &["ab|c"], false, move_caret_right)?; + assert_move(&["[abc]|"], &["abc|"], false, move_caret_right)?; + assert_move( + &["ab|[c\n", "]def\n", "ghi"], + &["abc\n", "|def\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["ab[c\n", "]|def\n", "ghi"], + &["abc\n", "|def\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["a|[bc\n", "]def\n", "ghi"], + &["abc\n", "|def\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["|[abc\n", "]def\n", "ghi"], + &["abc\n", "|def\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["a|[bc\n", "d]ef\n", "ghi"], + &["abc\n", "d|ef\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["|[abc\n", "def]\n", "ghi"], + &["abc\n", "def|\n", "ghi"], + false, + move_caret_right, + )?; + assert_move( + &["[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]|"], + &["ab\n", "cdef\n", "ghijkl\n", "mnopqrst|"], + false, + move_caret_right, + )?; + assert_move( + &["|[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]"], + &["ab\n", "cdef\n", "ghijkl\n", "mnopqrst|"], + false, + move_caret_right, + )?; + assert_move( + &["ab\n", "c[def\n", "ghijkl\n", "mno]|pqrst"], + &["ab\n", "cdef\n", "ghijkl\n", "mno|pqrst"], + false, + move_caret_right, + )?; + assert_move( + &["ab\n", "c|[def\n", "ghijkl\n", "mno]pqrst"], + &["ab\n", "cdef\n", "ghijkl\n", "mno|pqrst"], + false, + move_caret_right, + )?; + + Ok(()) + } + + #[test] + fn end_selection_left() -> Result<(), String> { + assert_move(&["[A]|"], &["|A"], false, move_caret_left)?; + assert_move(&["[a]|bc"], &["|abc"], false, move_caret_left)?; + assert_move(&["a[b]|c"], &["a|bc"], false, move_caret_left)?; + assert_move(&["ab[c]|"], &["ab|c"], false, move_caret_left)?; + assert_move(&["[ ]|abc"], &["| abc"], false, move_caret_left)?; + assert_move(&["|[ ]abc"], &["| abc"], false, move_caret_left)?; + assert_move(&["a|[b]c"], &["a|bc"], false, move_caret_left)?; + assert_move(&["abc[\n", "]|d"], &["abc|\n", "d"], false, move_caret_left)?; + assert_move(&["abc|[\n", "]d"], &["abc|\n", "d"], false, move_caret_left)?; + assert_move(&["abc|[\n", "]"], &["abc|\n", ""], false, move_caret_left)?; + assert_move( + &["abc\n", "[d]|ef"], + &["abc\n", "|def"], + false, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "ghi[\n", "]|jkl"], + &["abc\n", "def\n", "ghi|\n", "jkl"], + false, + move_caret_left, + )?; + assert_move(&["[ab]|c"], &["|abc"], false, move_caret_left)?; + assert_move(&["[abc]|"], &["|abc"], false, move_caret_left)?; + assert_move( + &["ab|[c\n", "]def\n", "ghi"], + &["ab|c\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["ab[c\n", "]|def\n", "ghi"], + &["ab|c\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["a|[bc\n", "]def\n", "ghi"], + &["a|bc\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["|[abc\n", "]def\n", "ghi"], + &["|abc\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["a|[bc\n", "d]ef\n", "ghi"], + &["a|bc\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["|[abc\n", "def]\n", "ghi"], + &["|abc\n", "def\n", "ghi"], + false, + move_caret_left, + )?; + assert_move( + &["[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]|"], + &["|ab\n", "cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_left, + )?; + assert_move( + &["|[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]"], + &["|ab\n", "cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_left, + )?; + assert_move( + &["ab\n", "c[def\n", "ghijkl\n", "mno]|pqrst"], + &["ab\n", "c|def\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_left, + )?; + assert_move( + &["ab\n", "c|[def\n", "ghijkl\n", "mno]pqrst"], + &["ab\n", "c|def\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_left, + )?; + + Ok(()) + } + + #[test] + fn end_selection_down() -> Result<(), String> { + assert_move(&["[a]|"], &["a|"], false, move_caret_down)?; + assert_move(&["|[a]"], &["a|"], false, move_caret_down)?; + assert_move(&["a|[bc]"], &["abc|"], false, move_caret_down)?; + assert_move(&["ab[c]|"], &["abc|"], false, move_caret_down)?; + assert_move(&["abc|[ ]"], &["abc |"], false, move_caret_down)?; + assert_move( + &["abc\n", "|[def]"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "d|[ef]"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|[f]"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["[abc\n", "]|def"], + &["abc\n", "|def"], + false, + move_caret_down, + )?; + assert_move( + &["a[bc\n", "d]|ef"], + &["abc\n", "d|ef"], + false, + move_caret_down, + )?; + assert_move( + &["ab|[c\n", "de]f"], + &["abc\n", "de|f"], + false, + move_caret_down, + )?; + assert_move( + &["abc[\n", "def]|"], + &["abc\n", "def|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "|[def \n", "]ghi"], + &["abc\n", "def \n", "|ghi"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "d[ef \n", "g]|hi"], + &["abc\n", "def \n", "g|hi"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de[f \n", "gh]|i"], + &["abc\n", "def \n", "gh|i"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "def[ \n", "ghi]|"], + &["abc\n", "def \n", "ghi|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "def [\n", "ghi]|"], + &["abc\n", "def \n", "ghi|"], + false, + move_caret_down, + )?; + assert_move( + &["abc\n", "de[\n", "gh]|i"], + &["abc\n", "de\n", "gh|i"], + false, + move_caret_down, + )?; + assert_move( + &["abc|[\n", "de]"], + &["abc\n", "de|"], + false, + move_caret_down, + )?; + assert_move( + &["ab[c\n", "de]|"], + &["abc\n", "de|"], + false, + move_caret_down, + )?; + assert_move( + &["a|[bc\n", "d]e"], + &["abc\n", "d|e"], + false, + move_caret_down, + )?; + assert_move( + &["[abc\n", "]|de"], + &["abc\n", "|de"], + false, + move_caret_down, + )?; + assert_move( + &["ab[\n", "cd]|ef\n", "ghijkl\n", "mnopqrst"], + &["ab\n", "cd|ef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef|[\n", "ghij]kl\n", "mnopqrst"], + &["ab\n", "cdef\n", "ghij|kl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl[\n", "mnopqr]|st"], + &["ab\n", "cdef\n", "ghijkl\n", "mnopqr|st"], + false, + move_caret_down, + )?; + assert_move( + &[" [ab\n", " ]|cdef\n", "ghijkl\n", "mnopqrst"], + &[" ab\n", " |cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "|[cdef\n", "]ghijkl\n", "mnopqrst"], + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["ab\n", "cdef\n", "[ghijkl\n", "]|mnopqrst"], + &["ab\n", "cdef\n", "ghijkl\n", "|mnopqrst"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh[\n", "ijklmn]|\n", "opqr\n", "st"], + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn[\n", "opqr]|\n", "st"], + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr[\n", "st]|"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + false, + move_caret_down, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "[st]|"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "st|"], + false, + move_caret_down, + )?; + assert_move( + &["abc de[f gh ]|"], + &["abc def gh |"], + false, + move_caret_down, + )?; + assert_move( + &["ab|[c def gh ]"], + &["abc def gh |"], + false, + move_caret_down, + )?; + assert_move( + &["a[bc def gh ]|"], + &["abc def gh |"], + false, + move_caret_down, + )?; + assert_move( + &["[abc def gh ]|"], + &["abc def gh |"], + false, + move_caret_down, + )?; + + Ok(()) + } + + #[test] + fn end_selection_up() -> Result<(), String> { + assert_move(&["[a]|"], &["|a"], false, move_caret_up)?; + assert_move(&["|[a]"], &["|a"], false, move_caret_up)?; + assert_move(&["a|[bc]"], &["a|bc"], false, move_caret_up)?; + assert_move(&["ab[c]|"], &["ab|c"], false, move_caret_up)?; + assert_move(&["abc|[ ]"], &["abc| "], false, move_caret_up)?; + assert_move( + &["abc\n", "|[def]"], + &["abc\n", "|def"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "d|[ef]"], + &["abc\n", "d|ef"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "de|[f]"], + &["abc\n", "de|f"], + false, + move_caret_up, + )?; + assert_move( + &["[abc\n", "]|def"], + &["|abc\n", "def"], + false, + move_caret_up, + )?; + assert_move( + &["a[bc\n", "d]|ef"], + &["a|bc\n", "def"], + false, + move_caret_up, + )?; + assert_move( + &["ab|[c\n", "de]f"], + &["ab|c\n", "def"], + false, + move_caret_up, + )?; + assert_move( + &["abc[\n", "def]|"], + &["abc|\n", "def"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "|[def \n", "]ghi"], + &["abc\n", "|def \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "d[ef \n", "g]|hi"], + &["abc\n", "d|ef \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "de|[f \n", "gh]i"], + &["abc\n", "de|f \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "def[ \n", "ghi]|"], + &["abc\n", "def| \n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "def [\n", "ghi]|"], + &["abc\n", "def |\n", "ghi"], + false, + move_caret_up, + )?; + assert_move( + &["abc\n", "de[\n", "gh]|i"], + &["abc\n", "de|\n", "ghi"], + false, + move_caret_up, + )?; + assert_move(&["abc|[\n", "de]"], &["abc|\n", "de"], false, move_caret_up)?; + assert_move(&["ab[c\n", "de]|"], &["ab|c\n", "de"], false, move_caret_up)?; + assert_move(&["a|[bc\n", "d]e"], &["a|bc\n", "de"], false, move_caret_up)?; + assert_move(&["[abc\n", "]|de"], &["|abc\n", "de"], false, move_caret_up)?; + assert_move( + &["ab[\n", "cd]|ef\n", "ghijkl\n", "mnopqrst"], + &["ab|\n", "cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef|[\n", "ghij]kl\n", "mnopqrst"], + &["ab\n", "cdef|\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl[\n", "mnopqr]|st"], + &["ab\n", "cdef\n", "ghijkl|\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &[" [ab\n", " ]|cdef\n", "ghijkl\n", "mnopqrst"], + &[" |ab\n", " cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "|[cdef\n", "]ghijkl\n", "mnopqrst"], + &["ab\n", "|cdef\n", "ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "[ghijkl\n", "]|mnopqrst"], + &["ab\n", "cdef\n", "|ghijkl\n", "mnopqrst"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh[\n", "ijklmn]|\n", "opqr\n", "st"], + &["abcdefgh|\n", "ijklmn\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn[\n", "opqr]|\n", "st"], + &["abcdefgh\n", "ijklmn|\n", "opqr\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr[\n", "st]|"], + &["abcdefgh\n", "ijklmn\n", "opqr|\n", "st"], + false, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "opqr\n", "[st]|"], + &["abcdefgh\n", "ijklmn\n", "opqr\n", "|st"], + false, + move_caret_up, + )?; + assert_move(&["abc de[f gh ]|"], &["abc de|f gh "], false, move_caret_up)?; + assert_move(&["ab|[c def gh ]"], &["ab|c def gh "], false, move_caret_up)?; + assert_move(&["a[bc def gh ]|"], &["a|bc def gh "], false, move_caret_up)?; + assert_move(&["[abc def gh ]|"], &["|abc def gh "], false, move_caret_up)?; + + Ok(()) + } + + #[test] + fn extend_selection_right() -> Result<(), String> { + assert_move(&["[a]|bc"], &["[ab]|c"], true, move_caret_right)?; + assert_move(&["a[b]|c"], &["a[bc]|"], true, move_caret_right)?; + assert_move(&["[ab]|c"], &["[abc]|"], true, move_caret_right)?; + assert_move(&["[ ]|abc"], &["[ a]|bc"], true, move_caret_right)?; + assert_move(&["[abc]|"], &["[abc]|"], true, move_caret_right)?; + assert_move(&["a[bc]|"], &["a[bc]|"], true, move_caret_right)?; + assert_move(&["ab[c]|"], &["ab[c]|"], true, move_caret_right)?; + assert_move( + &["abc[\n", "]|d"], + &["abc[\n", "d]|"], + true, + move_caret_right, + )?; + assert_move(&["ab[c]|\n", ""], &["ab[c\n", "]|"], true, move_caret_right)?; + assert_move( + &["ab[c]|\n", "d"], + &["ab[c\n", "]|d"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "def\n", "ghi[\n", "]|jkl"], + &["abc\n", "def\n", "ghi[\n", "j]|kl"], + true, + move_caret_right, + )?; + assert_move( + &["ab[c\n", "def\n", "ghi\n", "]|jkl"], + &["ab[c\n", "def\n", "ghi\n", "j]|kl"], + true, + move_caret_right, + )?; + assert_move( + &["ab[c\n", "def\n", "]|ghi\n", "jkl"], + &["ab[c\n", "def\n", "g]|hi\n", "jkl"], + true, + move_caret_right, + )?; + assert_move( + &["[abc\n", "def\n", "ghi\n", "jk]|l"], + &["[abc\n", "def\n", "ghi\n", "jkl]|"], + true, + move_caret_right, + )?; + assert_move( + &["[abc\n", "def\n", "ghi\n", "jkl]|"], + &["[abc\n", "def\n", "ghi\n", "jkl]|"], + true, + move_caret_right, + )?; + + Ok(()) + } + + #[test] + fn extend_selection_left() -> Result<(), String> { + assert_move(&["ab|[c]"], &["a|[bc]"], true, move_caret_left)?; + assert_move(&["a|[bc]"], &["|[abc]"], true, move_caret_left)?; + assert_move(&["|[abc]"], &["|[abc]"], true, move_caret_left)?; + assert_move(&["|[ab]c"], &["|[ab]c"], true, move_caret_left)?; + assert_move(&["|[a]bc"], &["|[a]bc"], true, move_caret_left)?; + assert_move(&[" |[a]bc"], &["|[ a]bc"], true, move_caret_left)?; + assert_move( + &["abc|[\n", "]d"], + &["ab|[c\n", "]d"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "|[d]"], + &["abc|[\n", "d]"], + true, + move_caret_left, + )?; + assert_move(&["ab|[c\n", "]"], &["a|[bc\n", "]"], true, move_caret_left)?; + assert_move( + &["abc\n", "def|[\n", "ghi\n", "j]kl"], + &["abc\n", "de|[f\n", "ghi\n", "j]kl"], + true, + move_caret_left, + )?; + assert_move( + &["a|[bc\n", "def\n", "ghi\n", "jkl]"], + &["|[abc\n", "def\n", "ghi\n", "jkl]"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def\n", "ghi\n", "|[jkl]"], + &["abc\n", "def\n", "ghi|[\n", "jkl]"], + true, + move_caret_left, + )?; + + Ok(()) + } + + #[test] + fn extend_selection_up() -> Result<(), String> { + assert_move(&["ab|[c]"], &["|[abc]"], true, move_caret_up)?; + assert_move(&["a|[bc]"], &["|[abc]"], true, move_caret_up)?; + assert_move(&["|[abc]"], &["|[abc]"], true, move_caret_up)?; + assert_move(&["|[ab]c"], &["|[ab]c"], true, move_caret_up)?; + assert_move(&["|[a]bc"], &["|[a]bc"], true, move_caret_up)?; + assert_move(&[" |[a]bc"], &["|[ a]bc"], true, move_caret_up)?; + assert_move(&["ab[c]|"], &["|[ab]c"], true, move_caret_up)?; + assert_move(&["[a]|"], &["|a"], true, move_caret_up)?; + assert_move(&["[a]|bc"], &["|abc"], true, move_caret_up)?; + assert_move(&["[a]|bc\n", "d"], &["|abc\n", "d"], true, move_caret_up)?; + assert_move( + &["abc\n", "de[f]|"], + &["abc|[\n", "de]f"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "de|[f]"], + &["ab|[c\n", "def]"], + true, + move_caret_up, + )?; + assert_move( + &["ab|[c\n", "def]"], + &["|[abc\n", "def]"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "[mnopqr]|st"], + &["ab\n", "cdef\n", "ghijkl|[\n", "]mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "[mnopqrs]|t"], + &["ab\n", "cdef\n", "ghijkl|[\n", "]mnopqrst"], + true, + move_caret_up, + )?; + assert_move( + &["abcdefgh\n", "ijklmn\n", "|[o]pqr\n", "st"], + &["abcdefgh\n", "|[ijklmn\n", "o]pqr\n", "st"], + true, + move_caret_up, + )?; + + Ok(()) + } + + #[test] + fn extend_selection_down() -> Result<(), String> { + assert_move(&["[ab]|c"], &["[abc]|"], true, move_caret_down)?; + assert_move(&["[a]|bc"], &["[abc]|"], true, move_caret_down)?; + assert_move(&["[abc]|"], &["[abc]|"], true, move_caret_down)?; + assert_move(&["|[ab]c"], &["ab[c]|"], true, move_caret_down)?; + assert_move(&["|[a]bc"], &["a[bc]|"], true, move_caret_down)?; + assert_move( + &["[a]|bc\n", "d"], + &["[abc\n", "d]|"], + true, + move_caret_down, + )?; + assert_move( + &["[a]|bc\n", "de"], + &["[abc\n", "d]|e"], + true, + move_caret_down, + )?; + assert_move( + &["[abc\n", "d]|e"], + &["[abc\n", "de]|"], + true, + move_caret_down, + )?; + assert_move(&["[a]|bc\n", ""], &["[abc\n", "]|"], true, move_caret_down)?; + assert_move( + &["ab\n", "cdef\n", "ghijkl\n", "[mnopqr]|st"], + &["ab\n", "cdef\n", "ghijkl\n", "[mnopqrst]|"], + true, + move_caret_down, + )?; + assert_move( + &["a[b\n", "cdef\n", "ghijkl\n", "mnopqr]|st"], + &["a[b\n", "cdef\n", "ghijkl\n", "mnopqrst]|"], + true, + move_caret_down, + )?; + assert_move( + &["[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]|"], + &["[ab\n", "cdef\n", "ghijkl\n", "mnopqrst]|"], + true, + move_caret_down, + )?; + assert_move( + &["abcd[efgh]|\n", "ijklmn\n", "opqr\n", "st"], + &["abcd[efgh\n", "ijklmn]|\n", "opqr\n", "st"], + true, + move_caret_down, + )?; + assert_move( + &["abcd[e]|fgh\n", "ijklmn\n", "opqr\n", "st"], + &["abcd[efgh\n", "ijklm]|n\n", "opqr\n", "st"], + true, + move_caret_down, + )?; + + Ok(()) + } + + #[test] + fn shrink_selection_right() -> Result<(), String> { + assert_move(&["ab|[c]"], &["abc|"], true, move_caret_right)?; + assert_move(&["a|[bc]"], &["ab|[c]"], true, move_caret_right)?; + assert_move(&["|[abc]"], &["a|[bc]"], true, move_caret_right)?; + assert_move( + &["|[abc\n", "def\n", "ghi\n", "jkl]"], + &["a|[bc\n", "def\n", "ghi\n", "jkl]"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "d|[ef\n", "]ghi\n", "jkl"], + &["abc\n", "de|[f\n", "]ghi\n", "jkl"], + true, + move_caret_right, + )?; + assert_move( + &["abc\n", "de|[f]\n", "ghi\n", "jkl"], + &["abc\n", "def|\n", "ghi\n", "jkl"], + true, + move_caret_right, + )?; + + Ok(()) + } + + #[test] + fn shrink_selection_left() -> Result<(), String> { + assert_move(&["ab[c]|"], &["ab|c"], true, move_caret_left)?; + assert_move(&["a[bc]|"], &["a[b]|c"], true, move_caret_left)?; + assert_move(&["[abc]|"], &["[ab]|c"], true, move_caret_left)?; + assert_move( + &["[abc\n", "def\n", "ghi\n", "jkl]|"], + &["[abc\n", "def\n", "ghi\n", "jk]|l"], + true, + move_caret_left, + )?; + assert_move( + &["|[abc\n", "def\n", "ghi\n", "jkl]"], + &["|[abc\n", "def\n", "ghi\n", "jkl]"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "def[\n", "]|ghi\n", "jkl"], + &["abc\n", "def|\n", "ghi\n", "jkl"], + true, + move_caret_left, + )?; + assert_move( + &["abc\n", "d[ef\n", "gh]|i\n", "jkl"], + &["abc\n", "d[ef\n", "g]|hi\n", "jkl"], + true, + move_caret_left, + )?; + + Ok(()) + } + + #[test] + fn shrink_selection_up() -> Result<(), String> { + assert_move(&["[abc]|"], &["|abc"], true, move_caret_up)?; + assert_move(&["[ab]|c"], &["|abc"], true, move_caret_up)?; + assert_move(&["[a]|bc"], &["|abc"], true, move_caret_up)?; + assert_move(&["|abc"], &["|abc"], true, move_caret_up)?; + assert_move( + &["[abc\n", "def]|"], + &["[abc]|\n", "def"], + true, + move_caret_up, + )?; + assert_move( + &["[abc\n", "de]|f"], + &["[ab]|c\n", "def"], + true, + move_caret_up, + )?; + assert_move( + &["[abc\n", "def\n", "ghi\n", "jkl]|"], + &["[abc\n", "def\n", "ghi]|\n", "jkl"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "def\n", "ghi[\n", "jkl]|"], + &["abc\n", "def\n", "ghi|\n", "jkl"], + true, + move_caret_up, + )?; + assert_move( + &["abc\n", "d[ef\n", "ghi\n", "jk]|l"], + &["abc\n", "d[ef\n", "gh]|i\n", "jkl"], + true, + move_caret_up, + )?; + assert_move( + &["[abc\n", "d]|ef\n", "ghi\n", "jkl"], + &["[a]|bc\n", "def\n", "ghi\n", "jkl"], + true, + move_caret_up, + )?; + + Ok(()) + } + + #[test] + fn shrink_selection_down() -> Result<(), String> { + assert_move(&["|[abc]"], &["abc|"], true, move_caret_down)?; + assert_move( + &["|[abc\n", "def]"], + &["abc\n", "|[def]"], + true, + move_caret_down, + )?; + assert_move( + &["a|[bc\n", "def]"], + &["abc\n", "d|[ef]"], + true, + move_caret_down, + )?; + assert_move( + &["|[abc\n", "def\n", "ghi]"], + &["abc\n", "|[def\n", "ghi]"], + true, + move_caret_down, + )?; + assert_move( + &["ab|[c\n", "def\n", "ghi]"], + &["abc\n", "de|[f\n", "ghi]"], + true, + move_caret_down, + )?; + assert_move( + &["abc\n", "de|[f\n", "ghi]"], + &["abc\n", "def\n", "gh|[i]"], + true, + move_caret_down, + )?; + assert_move( + &["abcdef|[\n", "ghij\n", "kl]"], + &["abcdef\n", "ghij|[\n", "kl]"], + true, + move_caret_down, + )?; + assert_move( + &["abcde|[f\n", "ghij\n", "kl]"], + &["abcdef\n", "ghij|[\n", "kl]"], + true, + move_caret_down, + )?; + assert_move( + &["ab|[cdef\n", "ghij\n", "kl]"], + &["abcdef\n", "gh|[ij\n", "kl]"], + true, + move_caret_down, + )?; + + Ok(()) + } +} diff --git a/editor/src/tea/ed_model.rs b/editor/src/tea/ed_model.rs new file mode 100644 index 0000000000..a864a2f698 --- /dev/null +++ b/editor/src/tea/ed_model.rs @@ -0,0 +1,52 @@ +use crate::graphics::primitives::rect::Rect; +use std::cmp::Ordering; + +#[derive(Debug)] +pub struct EdModel { + pub lines: Vec, + pub caret_pos: Position, + pub selection_opt: Option, + pub glyph_dim_rect_opt: Option, +} + +pub fn init_model() -> EdModel { + EdModel { + lines: vec![String::new()], + caret_pos: Position { line: 0, column: 0 }, + selection_opt: None, + glyph_dim_rect_opt: None, + } +} + +//Is model.rs the right place for these structs? +#[derive(Debug, Copy, Clone)] +pub struct Position { + pub line: usize, + pub column: usize, +} + +impl Ord for Position { + fn cmp(&self, other: &Self) -> Ordering { + (self.line, self.column).cmp(&(other.line, other.column)) + } +} + +impl PartialOrd for Position { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for Position { + fn eq(&self, other: &Self) -> bool { + (self.line, self.column) == (other.line, other.column) + } +} + +impl Eq for Position {} + +#[derive(Debug, Copy, Clone)] +pub struct RawSelection { + pub start_pos: Position, + pub end_pos: Position, +} diff --git a/editor/src/tea/update.rs b/editor/src/tea/update.rs index a667a8043d..f83dd677a5 100644 --- a/editor/src/tea/update.rs +++ b/editor/src/tea/update.rs @@ -12,7 +12,12 @@ pub fn move_caret_left( let old_line_nr = old_caret_pos.line; let old_col_nr = old_caret_pos.column; - let (line_nr, col_nr) = if old_col_nr == 0 { + let (line_nr, col_nr) = if old_selection_opt.is_some() && !shift_pressed { + match old_selection_opt { + Some(old_selection) => (old_selection.start_pos.line, old_selection.start_pos.column), + None => unreachable!(), + } + } else if old_col_nr == 0 { if old_line_nr == 0 { (0, 0) } else if let Some(curr_line) = lines.get(old_line_nr - 1) { @@ -31,13 +36,24 @@ pub fn move_caret_left( let new_selection_opt = if shift_pressed { if let Some(old_selection) = old_selection_opt { - Some(RawSelection { - start_pos: Position { - line: line_nr, - column: col_nr, - }, - end_pos: old_selection.end_pos, - }) + if old_caret_pos >= old_selection.end_pos { + if new_caret_pos == old_selection.start_pos { + None + } else { + Some(RawSelection { + start_pos: old_selection.start_pos, + end_pos: new_caret_pos, + }) + } + } else { + Some(RawSelection { + start_pos: Position { + line: line_nr, + column: col_nr, + }, + end_pos: old_selection.end_pos, + }) + } } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { Some(RawSelection { start_pos: Position { @@ -68,7 +84,12 @@ pub fn move_caret_right( let old_line_nr = old_caret_pos.line; let old_col_nr = old_caret_pos.column; - let (line_nr, col_nr) = if let Some(curr_line) = lines.get(old_line_nr) { + let (line_nr, col_nr) = if old_selection_opt.is_some() && !shift_pressed { + match old_selection_opt { + Some(old_selection) => (old_selection.end_pos.line, old_selection.end_pos.column), + None => unreachable!(), + } + } else if let Some(curr_line) = lines.get(old_line_nr) { if let Some(last_char) = curr_line.chars().last() { if is_newline(&last_char) { if old_col_nr + 1 > curr_line.len() - 1 { @@ -95,13 +116,24 @@ pub fn move_caret_right( let new_selection_opt = if shift_pressed { if let Some(old_selection) = old_selection_opt { - Some(RawSelection { - start_pos: old_selection.start_pos, - end_pos: Position { - line: line_nr, - column: col_nr, - }, - }) + if old_caret_pos <= old_selection.start_pos { + if new_caret_pos == old_selection.end_pos { + None + } else { + Some(RawSelection { + start_pos: new_caret_pos, + end_pos: old_selection.end_pos, + }) + } + } else { + Some(RawSelection { + start_pos: old_selection.start_pos, + end_pos: Position { + line: line_nr, + column: col_nr, + }, + }) + } } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { Some(RawSelection { start_pos: Position { @@ -132,10 +164,15 @@ pub fn move_caret_up( let old_line_nr = old_caret_pos.line; let old_col_nr = old_caret_pos.column; - let (line_nr, col_nr) = if old_line_nr == 0 { - (old_line_nr, old_col_nr) + let (line_nr, col_nr) = if old_selection_opt.is_some() && !shift_pressed { + match old_selection_opt { + Some(old_selection) => (old_selection.start_pos.line, old_selection.start_pos.column), + None => unreachable!(), + } + } else if old_line_nr == 0 { + (old_line_nr, 0) } else if let Some(prev_line) = lines.get(old_line_nr - 1) { - if prev_line.len() < old_col_nr { + if prev_line.len() <= old_col_nr { (old_line_nr - 1, prev_line.len() - 1) } else { (old_line_nr - 1, old_col_nr) @@ -151,10 +188,21 @@ pub fn move_caret_up( let new_selection_opt = if shift_pressed { if let Some(old_selection) = old_selection_opt { - Some(RawSelection { - start_pos: new_caret_pos, - end_pos: old_selection.end_pos, - }) + if old_selection.end_pos <= old_caret_pos { + if new_caret_pos == old_selection.start_pos { + None + } else { + Some(RawSelection { + start_pos: min(old_selection.start_pos, new_caret_pos), + end_pos: max(old_selection.start_pos, new_caret_pos), + }) + } + } else { + Some(RawSelection { + start_pos: new_caret_pos, + end_pos: old_selection.end_pos, + }) + } } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { Some(RawSelection { start_pos: min(old_caret_pos, new_caret_pos), @@ -179,10 +227,19 @@ pub fn move_caret_down( let old_line_nr = old_caret_pos.line; let old_col_nr = old_caret_pos.column; - let (line_nr, col_nr) = if old_line_nr + 1 >= lines.len() { - (old_line_nr, old_col_nr) + let (line_nr, col_nr) = if old_selection_opt.is_some() && !shift_pressed { + match old_selection_opt { + Some(old_selection) => (old_selection.end_pos.line, old_selection.end_pos.column), + None => unreachable!(), + } + } else if old_line_nr + 1 >= lines.len() { + if let Some(curr_line) = lines.get(old_line_nr) { + (old_line_nr, curr_line.len()) + } else { + unreachable!() + } } else if let Some(next_line) = lines.get(old_line_nr + 1) { - if next_line.len() < old_col_nr { + if next_line.len() <= old_col_nr { if let Some(last_char) = next_line.chars().last() { if is_newline(&last_char) { (old_line_nr + 1, next_line.len() - 1) @@ -206,10 +263,21 @@ pub fn move_caret_down( let new_selection_opt = if shift_pressed { if let Some(old_selection) = old_selection_opt { - Some(RawSelection { - start_pos: old_selection.start_pos, - end_pos: new_caret_pos, - }) + if old_caret_pos <= old_selection.start_pos { + if new_caret_pos == old_selection.end_pos { + None + } else { + Some(RawSelection { + start_pos: min(old_selection.end_pos, new_caret_pos), + end_pos: max(old_selection.end_pos, new_caret_pos), + }) + } + } else { + Some(RawSelection { + start_pos: old_selection.start_pos, + end_pos: new_caret_pos, + }) + } } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { Some(RawSelection { start_pos: min(old_caret_pos, new_caret_pos), diff --git a/editor/src/util.rs b/editor/src/util.rs index 3f49f32c79..8fba081a08 100644 --- a/editor/src/util.rs +++ b/editor/src/util.rs @@ -1,5 +1,5 @@ pub fn is_newline(char_ref: &char) -> bool { - let newline_codes = vec!['\u{d}']; + let newline_codes = vec!['\u{d}', '\n']; newline_codes.contains(char_ref) } diff --git a/editor/tests/selection.pest b/editor/tests/selection.pest new file mode 100644 index 0000000000..5bd1b4c5b7 --- /dev/null +++ b/editor/tests/selection.pest @@ -0,0 +1,11 @@ +text = { (ASCII_ALPHANUMERIC | " " | "\t" | "\n")* } + +caret = {"|"} + +optSelStart = { "["{0,1} } + +optSelEnd = { "]"{0,1} } + +optCaret = { caret{0,1} } + +linesWithSelect = { SOI ~ text ~ optCaret ~ text ~ optSelStart ~ text ~ optCaret ~ text ~ optCaret ~ text ~ optSelEnd ~ text ~ optCaret ~ text ~ EOI}