From bae3db0616adfe44d7565c73c34580acaa60a368 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Tue, 29 Dec 2020 20:30:40 +0100 Subject: [PATCH] rustfmt, renamed caret to txt_cursor --- editor/src/buffer.rs | 6 +- editor/src/colors.rs | 3 +- editor/src/error.rs | 38 ++- editor/src/keyboard_input.rs | 52 ++-- editor/src/lib.rs | 105 ++++---- editor/src/selection.rs | 184 ++++++-------- editor/src/tea/mod.rs | 3 +- editor/src/tea/model.rs | 26 +- editor/src/tea/update.rs | 453 +++++++++++++++-------------------- editor/src/text.rs | 59 ++--- editor/src/vec_result.rs | 10 +- 11 files changed, 430 insertions(+), 509 deletions(-) diff --git a/editor/src/buffer.rs b/editor/src/buffer.rs index 6be73a691a..f6d84866b8 100644 --- a/editor/src/buffer.rs +++ b/editor/src/buffer.rs @@ -87,7 +87,7 @@ pub struct RectBuffers { pub fn create_rect_buffers( gpu_device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, - rects: &[Rect] + rects: &[Rect], ) -> RectBuffers { let nr_of_rects = rects.len() as u64; @@ -113,9 +113,7 @@ pub fn create_rect_buffers( quad_buffer_builder = quad_buffer_builder.push_rect(&rect); } - - let (stg_vertex, stg_index, num_indices) = - quad_buffer_builder.build(&gpu_device); + let (stg_vertex, stg_index, num_indices) = quad_buffer_builder.build(&gpu_device); stg_vertex.copy_to_buffer(encoder, &vertex_buffer); stg_index.copy_to_buffer(encoder, &index_buffer); diff --git a/editor/src/colors.rs b/editor/src/colors.rs index 304ccd7f5e..063f215776 100644 --- a/editor/src/colors.rs +++ b/editor/src/colors.rs @@ -1,2 +1 @@ - -pub const WHITE: [f32; 3] = [1.0, 1.0, 1.0]; \ No newline at end of file +pub const WHITE: [f32; 3] = [1.0, 1.0, 1.0]; diff --git a/editor/src/error.rs b/editor/src/error.rs index 23bde669ee..dadbe7b141 100644 --- a/editor/src/error.rs +++ b/editor/src/error.rs @@ -1,5 +1,5 @@ -use snafu::{Backtrace, Snafu, ErrorCompat}; use colored::*; +use snafu::{Backtrace, ErrorCompat, Snafu}; //import errors as follows: // `use crate::error::OutOfBounds;` @@ -14,18 +14,15 @@ pub enum EdError { index, vec_len ))] - OutOfBounds { + OutOfBounds { index: usize, vec_len: usize, - backtrace: Backtrace + backtrace: Backtrace, }, - #[snafu(display( - "InvalidSelection: {}", - err_msg - ))] - InvalidSelection { + #[snafu(display("InvalidSelection: {}", err_msg))] + InvalidSelection { err_msg: String, - backtrace: Backtrace + backtrace: Backtrace, }, } @@ -48,27 +45,23 @@ fn color_backtrace(backtrace: &snafu::Backtrace) -> String { let mut prev_line_opt: Option = None; for line in backtrace_split { - - let new_line = - if line.contains("src") { - if !contains_one_of(&line, &irrelevant_src) { - if let Some(prev_line) = prev_line_opt { - prev_line_opt = Some(format!("{}", prev_line.truecolor(255, 30, 30))); - } - format!("{}\n", line.truecolor(255, 100, 100)) - } else { - format!("{}\n", line) + let new_line = if line.contains("src") { + if !contains_one_of(&line, &irrelevant_src) { + if let Some(prev_line) = prev_line_opt { + prev_line_opt = Some(format!("{}", prev_line.truecolor(255, 30, 30))); } + format!("{}\n", line.truecolor(255, 100, 100)) } else { format!("{}\n", line) - }; - + } + } else { + format!("{}\n", line) + }; if let Some(prev_line) = prev_line_opt { ret_str.push_str(&prev_line); } prev_line_opt = Some(new_line); - } ret_str @@ -83,4 +76,3 @@ fn contains_one_of(main_str: &str, contain_slice: &[&str]) -> bool { false } - diff --git a/editor/src/keyboard_input.rs b/editor/src/keyboard_input.rs index df01638c0d..00ef60ab11 100644 --- a/editor/src/keyboard_input.rs +++ b/editor/src/keyboard_input.rs @@ -1,6 +1,8 @@ +use crate::tea::model::Model; +use crate::tea::update::{ + move_txt_cursor_down, move_txt_cursor_left, move_txt_cursor_right, move_txt_cursor_up, +}; use winit::event::{ElementState, ModifiersState, VirtualKeyCode}; -use crate::tea::model::{Model}; -use crate::tea::update::{move_caret_left, move_caret_right, move_caret_down, move_caret_up}; pub fn handle_keydown( elem_state: ElementState, @@ -16,25 +18,45 @@ pub fn handle_keydown( match virtual_keycode { Left => { - let (new_caret_pos, new_selection_opt) = move_caret_left(model.caret_pos, model.selection_opt, modifiers.shift(), &model.lines); - model.caret_pos = new_caret_pos; + let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_left( + model.txt_cursor_pos, + model.selection_opt, + modifiers.shift(), + &model.lines, + ); + model.txt_cursor_pos = new_txt_cursor_pos; model.selection_opt = new_selection_opt; - }, + } Up => { - let (new_caret_pos, new_selection_opt) = move_caret_up(model.caret_pos, model.selection_opt, modifiers.shift(), &model.lines); - model.caret_pos = new_caret_pos; + let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_up( + model.txt_cursor_pos, + model.selection_opt, + modifiers.shift(), + &model.lines, + ); + model.txt_cursor_pos = new_txt_cursor_pos; model.selection_opt = new_selection_opt; - }, + } Right => { - let (new_caret_pos, new_selection_opt) = move_caret_right(model.caret_pos, model.selection_opt, modifiers.shift(), &model.lines); - model.caret_pos = new_caret_pos; + let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_right( + model.txt_cursor_pos, + model.selection_opt, + modifiers.shift(), + &model.lines, + ); + model.txt_cursor_pos = new_txt_cursor_pos; model.selection_opt = new_selection_opt; - }, + } Down => { - let (new_caret_pos, new_selection_opt) = move_caret_down(model.caret_pos, model.selection_opt, modifiers.shift(), &model.lines); - model.caret_pos = new_caret_pos; + let (new_txt_cursor_pos, new_selection_opt) = move_txt_cursor_down( + model.txt_cursor_pos, + model.selection_opt, + modifiers.shift(), + &model.lines, + ); + model.txt_cursor_pos = new_txt_cursor_pos; model.selection_opt = new_selection_opt; - }, + } Copy => { todo!("copy"); } @@ -100,7 +122,7 @@ pub fn handle_keydown( // Escape | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | F13 | F14 | F15 // | F16 | F17 | F18 | F19 | F20 | F21 | F22 | F23 | F24 | Snapshot | Scroll | Pause // | Insert | Home | Delete | End | PageDown | PageUp | Left | Up | Right | Down | Compose -// | Caret | Numlock | AbntC1 | AbntC2 | Ax | Calculator | Capital | Convert | Kana +// | txt_cursor | Numlock | AbntC1 | AbntC2 | Ax | Calculator | Capital | Convert | Kana // | Kanji | LAlt | LBracket | LControl | LShift | LWin | Mail | MediaSelect | PlayPause // | Power | PrevTrack | MediaStop | Mute | MyComputer | NavigateForward // | NavigateBackward | NextTrack | NoConvert | OEM102 | RAlt | Sysrq | RBracket diff --git a/editor/src/lib.rs b/editor/src/lib.rs index b526e7804b..c4eb314674 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -9,25 +9,26 @@ // See this link to learn wgpu: https://sotrh.github.io/learn-wgpu/ use crate::buffer::create_rect_buffers; -use crate::text::{build_glyph_brush, Text, is_newline}; -use crate::vertex::Vertex; -use crate::rect::{Rect}; -use crate::error::{print_err}; +use crate::error::print_err; use crate::ortho::{init_ortho, update_ortho_buffer, OrthoResources}; -use crate::selection::{create_selection_rects}; +use crate::rect::Rect; +use crate::selection::create_selection_rects; use crate::tea::{model, update}; -use model::{Position}; +use crate::text::{build_glyph_brush, is_newline, Text}; +use crate::vertex::Vertex; +use model::Position; use std::error::Error; use std::io; use std::path::Path; +use wgpu::{CommandEncoder, RenderPass, TextureView}; use winit::event; use winit::event::{Event, ModifiersState}; use winit::event_loop::ControlFlow; -use wgpu::{TextureView, CommandEncoder, RenderPass}; - pub mod ast; mod buffer; +mod colors; +pub mod error; pub mod expr; pub mod file; mod keyboard_input; @@ -36,15 +37,13 @@ mod pattern; pub mod pool; mod rect; mod scope; +mod selection; +mod tea; pub mod text; mod types; mod util; -mod vertex; -mod colors; -pub mod error; mod vec_result; -mod selection; -mod tea; +mod vertex; /// The editor is actually launched from the CLI if you pass it zero arguments, /// or if you provide it 1 or more files or directories to open on launch. @@ -183,7 +182,7 @@ fn run_event_loop() -> Result<(), Box> { input.state, virtual_keycode, keyboard_modifiers, - &mut ed_model + &mut ed_model, ); } } @@ -202,7 +201,7 @@ fn run_event_loop() -> Result<(), Box> { label: Some("Redraw"), }); - let frame = swap_chain + let frame = swap_chain .get_current_frame() .expect("Failed to acquire next SwapChainFrame") .output; @@ -210,30 +209,33 @@ fn run_event_loop() -> Result<(), Box> { let glyph_bounds_rects = queue_all_text( &size, &ed_model.lines, - ed_model.caret_pos, + ed_model.txt_cursor_pos, &mut glyph_brush, ); if let Some(selection) = ed_model.selection_opt { - let selection_rects_res = create_selection_rects(selection, &glyph_bounds_rects); + let selection_rects_res = + create_selection_rects(selection, &glyph_bounds_rects); match selection_rects_res { - Ok(selection_rects) => + Ok(selection_rects) => { if !selection_rects.is_empty() { let rect_buffers = create_rect_buffers( &gpu_device, - &mut encoder, + &mut encoder, &selection_rects, ); - - let mut render_pass = begin_render_pass(&mut encoder, &frame.view); - + + let mut render_pass = begin_render_pass(&mut encoder, &frame.view); + render_pass.set_pipeline(&rect_pipeline); render_pass.set_bind_group(0, &ortho.bind_group, &[]); - render_pass.set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..)); + render_pass + .set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..)); render_pass.set_index_buffer(rect_buffers.index_buffer.slice(..)); render_pass.draw_indexed(0..rect_buffers.num_rects, 0, 0..1); - }, + } + } Err(e) => { begin_render_pass(&mut encoder, &frame.view); print_err(&e) //TODO draw error text on screen @@ -245,15 +247,15 @@ fn run_event_loop() -> Result<(), Box> { // draw all text glyph_brush - .draw_queued( - &gpu_device, - &mut staging_belt, - &mut encoder, - &frame.view, - size.width, - size.height, - ) - .expect("Draw queued"); + .draw_queued( + &gpu_device, + &mut staging_belt, + &mut encoder, + &frame.view, + size.width, + size.height, + ) + .expect("Draw queued"); staging_belt.finish(); cmd_queue.submit(Some(encoder.finish())); @@ -274,10 +276,9 @@ fn run_event_loop() -> Result<(), Box> { }) } - fn begin_render_pass<'a>( encoder: &'a mut CommandEncoder, - texture_view: &'a TextureView + texture_view: &'a TextureView, ) -> RenderPass<'a> { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { @@ -365,7 +366,7 @@ fn create_render_pipeline( fn queue_all_text( size: &winit::dpi::PhysicalSize, lines: &[String], - caret_pos: Position, + txt_cursor_pos: Position, glyph_brush: &mut wgpu_glyph::GlyphBrush<()>, ) -> Vec> { let area_bounds = (size.width as f32, size.height as f32).into(); @@ -388,18 +389,18 @@ fn queue_all_text( ..Default::default() }; - let caret_pos_label = Text { + let txt_cursor_pos_label = Text { position: (30.0, 530.0).into(), area_bounds, color: (0.4666, 0.2, 1.0, 1.0).into(), - text: format!("Ln {}, Col {}", caret_pos.line, caret_pos.column), + text: format!("Ln {}, Col {}", txt_cursor_pos.line, txt_cursor_pos.column), size: 30.0, ..Default::default() }; text::queue_text_draw(&main_label, glyph_brush); - text::queue_text_draw(&caret_pos_label, glyph_brush); + text::queue_text_draw(&txt_cursor_pos_label, glyph_brush); text::queue_text_draw(&code_text, glyph_brush) } @@ -417,23 +418,28 @@ fn update_text_state(ed_model: &mut model::Model, received_char: &char) { } else if ed_model.lines.len() > 1 { ed_model.lines.pop(); } - ed_model.caret_pos = update::move_caret_left(ed_model.caret_pos, None, false, &ed_model.lines).0; + ed_model.txt_cursor_pos = update::move_txt_cursor_left( + ed_model.txt_cursor_pos, + None, + false, + &ed_model.lines, + ) + .0; } } '\u{e000}'..='\u{f8ff}' | '\u{f0000}'..='\u{ffffd}' | '\u{100000}'..='\u{10fffd}' => { // These are private use characters; ignore them. // See http://www.unicode.org/faq/private_use.html } - ch if is_newline(ch) => { + ch if is_newline(ch) => { if let Some(last_line) = ed_model.lines.last_mut() { last_line.push(*received_char) } ed_model.lines.push(String::new()); - ed_model.caret_pos = - Position { - line: ed_model.caret_pos.line + 1, - column: 0 - }; + ed_model.txt_cursor_pos = Position { + line: ed_model.txt_cursor_pos.line + 1, + column: 0, + }; ed_model.selection_opt = None; } @@ -442,11 +448,10 @@ fn update_text_state(ed_model: &mut model::Model, received_char: &char) { if let Some(last_line) = ed_model.lines.last_mut() { last_line.push(*received_char); - - ed_model.caret_pos = - Position { + + ed_model.txt_cursor_pos = Position { line: nr_lines - 1, - column: last_line.len() + column: last_line.len(), }; ed_model.selection_opt = None; diff --git a/editor/src/selection.rs b/editor/src/selection.rs index 28549bfde2..2a30b873d7 100644 --- a/editor/src/selection.rs +++ b/editor/src/selection.rs @@ -1,34 +1,32 @@ - -use crate::rect::{Rect}; -use crate::vec_result::{get_res}; -use crate::error::{EdResult, InvalidSelection}; use crate::colors; -use crate::tea::model::{RawSelection}; +use crate::error::{EdResult, InvalidSelection}; +use crate::rect::Rect; +use crate::tea::model::RawSelection; +use crate::vec_result::get_res; use snafu::ensure; //using the "parse don't validate" pattern struct ValidSelection { - selection: RawSelection + selection: RawSelection, } -fn validate_selection( selection: RawSelection) -> EdResult { - let RawSelection {start_pos, end_pos} = selection; +fn validate_selection(selection: RawSelection) -> EdResult { + let RawSelection { start_pos, end_pos } = selection; ensure!( start_pos.line <= end_pos.line, - InvalidSelection { err_msg: - format!( + InvalidSelection { + err_msg: format!( "start_pos.line ({}) should be smaller than or equal to end_pos.line ({})", - start_pos.line, - end_pos.line + start_pos.line, end_pos.line ) } ); ensure!( !(start_pos.line == end_pos.line && start_pos.column > end_pos.column), - InvalidSelection { err_msg: - format!( + InvalidSelection { + err_msg: format!( "start_pos.column ({}) should be smaller than or equal to end_pos.column ({}) when start_pos.line equals end_pos.line", start_pos.column, end_pos.column @@ -36,150 +34,116 @@ fn validate_selection( selection: RawSelection) -> EdResult { } ); - Ok( - ValidSelection { - selection: RawSelection {start_pos, end_pos} - } - ) + Ok(ValidSelection { + selection: RawSelection { start_pos, end_pos }, + }) } - pub fn create_selection_rects( raw_sel: RawSelection, - glyph_bound_rects: &Vec> -) -> EdResult> { + glyph_bound_rects: &Vec>, +) -> EdResult> { let valid_sel = validate_selection(raw_sel)?; - let RawSelection {start_pos, end_pos} = valid_sel.selection; + let RawSelection { start_pos, end_pos } = valid_sel.selection; let mut all_rects = Vec::new(); if start_pos.line == end_pos.line { - let start_glyph_rect = - get_res( - start_pos.column, - get_res(start_pos.line, glyph_bound_rects)?, - )?; + let start_glyph_rect = get_res( + start_pos.column, + get_res(start_pos.line, glyph_bound_rects)?, + )?; - let stop_glyph_rect = - get_res( - end_pos.column - 1, - get_res(end_pos.line, glyph_bound_rects)? - )?; + let stop_glyph_rect = get_res( + end_pos.column - 1, + get_res(end_pos.line, glyph_bound_rects)?, + )?; - let top_left_coords = - start_glyph_rect.top_left_coords; + let top_left_coords = start_glyph_rect.top_left_coords; let height = start_glyph_rect.height; - let width = (stop_glyph_rect.top_left_coords.x - start_glyph_rect.top_left_coords.x) + stop_glyph_rect.width; + let width = (stop_glyph_rect.top_left_coords.x - start_glyph_rect.top_left_coords.x) + + stop_glyph_rect.width; - all_rects.push( - Rect { - top_left_coords, - width, - height, - color: colors::WHITE - } - ); + all_rects.push(Rect { + top_left_coords, + width, + height, + color: colors::WHITE, + }); Ok(all_rects) } else { // first line let start_line = get_res(start_pos.line, glyph_bound_rects)?; - let start_glyph_rect = - get_res( - start_pos.column, - start_line - )?; + let start_glyph_rect = get_res(start_pos.column, start_line)?; - let start_line_last_glyph_rect = - get_res( - start_line.len() - 1, - start_line - )?; + let start_line_last_glyph_rect = get_res(start_line.len() - 1, start_line)?; - let top_left_coords = - start_glyph_rect.top_left_coords; + let top_left_coords = start_glyph_rect.top_left_coords; let height = start_glyph_rect.height; - let width = (start_line_last_glyph_rect.top_left_coords.x - start_glyph_rect.top_left_coords.x) + start_line_last_glyph_rect.width; + let width = (start_line_last_glyph_rect.top_left_coords.x + - start_glyph_rect.top_left_coords.x) + + start_line_last_glyph_rect.width; - all_rects.push( - Rect { - top_left_coords, - width, - height, - color: colors::WHITE - } - ); + all_rects.push(Rect { + top_left_coords, + width, + height, + color: colors::WHITE, + }); //middle lines let nr_mid_lines = (end_pos.line - start_pos.line) - 1; let first_mid_line = start_pos.line + 1; - for i in first_mid_line..(first_mid_line + nr_mid_lines) { + for i in first_mid_line..(first_mid_line + nr_mid_lines) { let mid_line = get_res(i, glyph_bound_rects)?; - let mid_line_first_glyph_rect = - get_res( - 0, - mid_line - )?; + let mid_line_first_glyph_rect = get_res(0, mid_line)?; - let mid_line_last_glyph_rect = - get_res( - mid_line.len() - 1, - mid_line - )?; + let mid_line_last_glyph_rect = get_res(mid_line.len() - 1, mid_line)?; - let top_left_coords = - mid_line_first_glyph_rect.top_left_coords; + let top_left_coords = mid_line_first_glyph_rect.top_left_coords; let height = mid_line_first_glyph_rect.height; - let width = (mid_line_last_glyph_rect.top_left_coords.x - mid_line_first_glyph_rect.top_left_coords.x) + mid_line_last_glyph_rect.width; + let width = (mid_line_last_glyph_rect.top_left_coords.x + - mid_line_first_glyph_rect.top_left_coords.x) + + mid_line_last_glyph_rect.width; - all_rects.push( - Rect { - top_left_coords, - width, - height, - color: colors::WHITE - } - ); + all_rects.push(Rect { + top_left_coords, + width, + height, + color: colors::WHITE, + }); } //last line if end_pos.column > 0 { let stop_line = get_res(end_pos.line, glyph_bound_rects)?; - let stop_line_first_glyph_rect = - get_res( - 0, - stop_line - )?; + let stop_line_first_glyph_rect = get_res(0, stop_line)?; - let stop_glyph_rect = - get_res( - end_pos.column - 1, - stop_line - )?; + let stop_glyph_rect = get_res(end_pos.column - 1, stop_line)?; - let top_left_coords = - stop_line_first_glyph_rect.top_left_coords; + let top_left_coords = stop_line_first_glyph_rect.top_left_coords; let height = stop_glyph_rect.height; - let width = (stop_glyph_rect.top_left_coords.x - stop_line_first_glyph_rect.top_left_coords.x) + stop_glyph_rect.width; + let width = (stop_glyph_rect.top_left_coords.x + - stop_line_first_glyph_rect.top_left_coords.x) + + stop_glyph_rect.width; - all_rects.push( - Rect { - top_left_coords, - width, - height, - color: colors::WHITE - } - ); + all_rects.push(Rect { + top_left_coords, + width, + height, + color: colors::WHITE, + }); } - + Ok(all_rects) } -} \ No newline at end of file +} diff --git a/editor/src/tea/mod.rs b/editor/src/tea/mod.rs index df645364fb..e90336f8d3 100644 --- a/editor/src/tea/mod.rs +++ b/editor/src/tea/mod.rs @@ -1,3 +1,2 @@ - pub mod model; -pub mod update; \ No newline at end of file +pub mod update; diff --git a/editor/src/tea/model.rs b/editor/src/tea/model.rs index 9670588ceb..e54bfaa8c9 100644 --- a/editor/src/tea/model.rs +++ b/editor/src/tea/model.rs @@ -1,23 +1,17 @@ - -use std::cmp::{Ordering}; +use std::cmp::Ordering; #[derive(Debug)] pub struct Model { pub lines: Vec, - pub caret_pos: Position, - pub selection_opt: Option + pub txt_cursor_pos: Position, + pub selection_opt: Option, } pub fn init_model() -> Model { Model { - lines: - vec![String::new()], - caret_pos: - Position { - line: 0, column: 0 - }, - selection_opt: - None + lines: vec![String::new()], + txt_cursor_pos: Position { line: 0, column: 0 }, + selection_opt: None, } } @@ -25,7 +19,7 @@ pub fn init_model() -> Model { #[derive(Debug, Copy, Clone)] pub struct Position { pub line: usize, - pub column: usize + pub column: usize, } impl Ord for Position { @@ -46,10 +40,10 @@ impl PartialEq for Position { } } -impl Eq for Position { } +impl Eq for Position {} #[derive(Debug, Copy, Clone)] pub struct RawSelection { pub start_pos: Position, - pub end_pos: Position -} \ No newline at end of file + pub end_pos: Position, +} diff --git a/editor/src/tea/update.rs b/editor/src/tea/update.rs index 8e5fa92be2..48d52abe7a 100644 --- a/editor/src/tea/update.rs +++ b/editor/src/tea/update.rs @@ -1,280 +1,225 @@ - use crate::tea::model::{Position, RawSelection}; -use crate::text::{is_newline}; -use std::cmp::{min, max}; +use crate::text::is_newline; +use std::cmp::{max, min}; -pub fn move_caret_left(old_caret_pos: Position, old_selection_opt: Option, shift_pressed: bool, lines: &[String]) -> (Position, Option) { - let old_line_nr = old_caret_pos.line; - let old_col_nr = old_caret_pos.column; +pub fn move_txt_cursor_left( + old_txt_cursor_pos: Position, + old_selection_opt: Option, + shift_pressed: bool, + lines: &[String], +) -> (Position, Option) { + let old_line_nr = old_txt_cursor_pos.line; + let old_col_nr = old_txt_cursor_pos.column; - let (line_nr, col_nr) = - if old_caret_pos.column == 0 { - if old_caret_pos.line == 0 { - (0, 0) - } else if let Some(curr_line) = lines.get(old_line_nr - 1) { - (old_line_nr - 1, curr_line.len() - 1) - } else { - (0, 0) // this should never happen, should this method return Result? - } + let (line_nr, col_nr) = if old_txt_cursor_pos.column == 0 { + if old_txt_cursor_pos.line == 0 { + (0, 0) + } else if let Some(curr_line) = lines.get(old_line_nr - 1) { + (old_line_nr - 1, curr_line.len() - 1) } else { - (old_line_nr, old_col_nr - 1) - }; - - let new_caret_pos = Position { - line: line_nr, - column: col_nr + (0, 0) // this should never happen, should this method return Result? + } + } else { + (old_line_nr, old_col_nr - 1) }; - 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 - , - } - ) - } else if !(old_line_nr == line_nr && old_col_nr == col_nr){ - Some( - RawSelection { - start_pos: - Position { - line: line_nr, - column: col_nr - } - , - end_pos: - Position { - line: old_line_nr, - column: old_col_nr - } - , - } - ) - } else { - None - } - + let new_txt_cursor_pos = Position { + line: line_nr, + column: col_nr, + }; + + 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, + }) + } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { + Some(RawSelection { + start_pos: Position { + line: line_nr, + column: col_nr, + }, + end_pos: Position { + line: old_line_nr, + column: old_col_nr, + }, + }) } else { None - }; + } + } else { + None + }; - - ( - new_caret_pos, - new_selection_opt - ) + (new_txt_cursor_pos, new_selection_opt) } -pub fn move_caret_right(old_caret_pos: Position, old_selection_opt: Option, shift_pressed: bool, lines: &[String]) -> (Position, Option) { - let old_line_nr = old_caret_pos.line; - let old_col_nr = old_caret_pos.column; +pub fn move_txt_cursor_right( + old_txt_cursor_pos: Position, + old_selection_opt: Option, + shift_pressed: bool, + lines: &[String], +) -> (Position, Option) { + let old_line_nr = old_txt_cursor_pos.line; + let old_col_nr = old_txt_cursor_pos.column; - let (line_nr, col_nr) = - 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 { - (old_line_nr + 1, 0) - } else { - (old_line_nr, old_col_nr + 1) - } - } else if old_col_nr < curr_line.len() { - (old_line_nr, old_col_nr + 1) + let (line_nr, col_nr) = 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 { + (old_line_nr + 1, 0) } else { - (old_line_nr, old_col_nr) + (old_line_nr, old_col_nr + 1) } + } else if old_col_nr < curr_line.len() { + (old_line_nr, old_col_nr + 1) } else { (old_line_nr, old_col_nr) } } else { - (0, 0) // this should never happen, should this method return Result? - }; - - let new_caret_pos = Position { - line: line_nr, - column: col_nr + (old_line_nr, old_col_nr) + } + } else { + (0, 0) // this should never happen, should this method return Result? }; - 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 - } - , - } - ) - } else if !(old_line_nr == line_nr && old_col_nr == col_nr){ - Some( - RawSelection { - start_pos: - Position { - line: old_line_nr, - column: old_col_nr - } - , - end_pos: - Position { - line: line_nr, - column: col_nr - } - , - } - ) - } else { - None - } - - } else { - None - }; - - ( - new_caret_pos, - new_selection_opt - ) -} - -pub fn move_caret_up(old_caret_pos: Position, old_selection_opt: Option, shift_pressed: bool, lines: &[String]) -> (Position, Option) { - 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) - } else if let Some(prev_line) = lines.get(old_line_nr - 1) { - if prev_line.len() < old_col_nr { - (old_line_nr - 1, prev_line.len() - 1) - } else { - (old_line_nr - 1, old_col_nr) - } - } else { - (0, 0) // this should never happen, should this method return Result? - }; - - let new_caret_pos = Position { - line: line_nr, - column: col_nr - }; - - 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 - , - } - ) - } else if !(old_line_nr == line_nr && old_col_nr == col_nr){ - Some( - RawSelection { - start_pos: - min(old_caret_pos, new_caret_pos) - , - end_pos: - max(old_caret_pos, new_caret_pos) - , - } - ) - } else { - None - } - - } else { - None - }; - - ( - new_caret_pos, - new_selection_opt - ) -} - -pub fn move_caret_down(old_caret_pos: Position, old_selection_opt: Option, shift_pressed: bool, lines: &[String]) -> (Position, Option) { - 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) - } else if let Some(next_line) = lines.get(old_line_nr + 1) { - 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) - } else { - (old_line_nr + 1, next_line.len()) - } - } else { - (old_line_nr + 1, 0) - } - - } else { - (old_line_nr + 1, old_col_nr) - } - } else { - (0, 0) // this should never happen, should this method return Result? - }; - - let new_caret_pos = Position { + let new_txt_cursor_pos = Position { line: line_nr, - column: col_nr + column: col_nr, }; - 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 - , - } - ) - } else if !(old_line_nr == line_nr && old_col_nr == col_nr){ - Some( - RawSelection { - start_pos: - min(old_caret_pos, new_caret_pos) - , - end_pos: - max(old_caret_pos, new_caret_pos) - , - } - ) - } else { - None - } - + 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, + }, + }) + } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { + Some(RawSelection { + start_pos: Position { + line: old_line_nr, + column: old_col_nr, + }, + end_pos: Position { + line: line_nr, + column: col_nr, + }, + }) } else { None - }; + } + } else { + None + }; - ( - new_caret_pos, - new_selection_opt - ) -} \ No newline at end of file + (new_txt_cursor_pos, new_selection_opt) +} + +pub fn move_txt_cursor_up( + old_txt_cursor_pos: Position, + old_selection_opt: Option, + shift_pressed: bool, + lines: &[String], +) -> (Position, Option) { + let old_line_nr = old_txt_cursor_pos.line; + let old_col_nr = old_txt_cursor_pos.column; + + let (line_nr, col_nr) = if old_line_nr == 0 { + (old_line_nr, old_col_nr) + } else if let Some(prev_line) = lines.get(old_line_nr - 1) { + if prev_line.len() < old_col_nr { + (old_line_nr - 1, prev_line.len() - 1) + } else { + (old_line_nr - 1, old_col_nr) + } + } else { + (0, 0) // this should never happen, should this method return Result? + }; + + let new_txt_cursor_pos = Position { + line: line_nr, + column: col_nr, + }; + + let new_selection_opt = if shift_pressed { + if let Some(old_selection) = old_selection_opt { + Some(RawSelection { + start_pos: new_txt_cursor_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_txt_cursor_pos, new_txt_cursor_pos), + end_pos: max(old_txt_cursor_pos, new_txt_cursor_pos), + }) + } else { + None + } + } else { + None + }; + + (new_txt_cursor_pos, new_selection_opt) +} + +pub fn move_txt_cursor_down( + old_txt_cursor_pos: Position, + old_selection_opt: Option, + shift_pressed: bool, + lines: &[String], +) -> (Position, Option) { + let old_line_nr = old_txt_cursor_pos.line; + let old_col_nr = old_txt_cursor_pos.column; + + let (line_nr, col_nr) = if old_line_nr + 1 >= lines.len() { + (old_line_nr, old_col_nr) + } else if let Some(next_line) = lines.get(old_line_nr + 1) { + 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) + } else { + (old_line_nr + 1, next_line.len()) + } + } else { + (old_line_nr + 1, 0) + } + } else { + (old_line_nr + 1, old_col_nr) + } + } else { + (0, 0) // this should never happen, should this method return Result? + }; + + let new_txt_cursor_pos = Position { + line: line_nr, + column: col_nr, + }; + + 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_txt_cursor_pos, + }) + } else if !(old_line_nr == line_nr && old_col_nr == col_nr) { + Some(RawSelection { + start_pos: min(old_txt_cursor_pos, new_txt_cursor_pos), + end_pos: max(old_txt_cursor_pos, new_txt_cursor_pos), + }) + } else { + None + } + } else { + None + }; + + (new_txt_cursor_pos, new_selection_opt) +} diff --git a/editor/src/text.rs b/editor/src/text.rs index fff12effb5..f1f008b309 100644 --- a/editor/src/text.rs +++ b/editor/src/text.rs @@ -1,11 +1,11 @@ // Adapted from https://github.com/sotrh/learn-wgpu // by Benjamin Hansen, licensed under the MIT license -use ab_glyph::{FontArc, InvalidFont, Glyph}; -use cgmath::{Vector2, Vector4}; -use wgpu_glyph::{ab_glyph, GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section}; use crate::rect::Rect; +use ab_glyph::{FontArc, Glyph, InvalidFont}; +use cgmath::{Vector2, Vector4}; use itertools::Itertools; +use wgpu_glyph::{ab_glyph, GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section}; #[derive(Debug)] pub struct Text { @@ -45,7 +45,8 @@ pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec) -> Vec) -> Vec>(); - let last_rect_opt = rects_vec.last().cloned(); - // add extra rect to make it easy to highlight the newline character - if let Some(last_rect) = last_rect_opt { - rects_vec.push(Rect { - top_left_coords: [last_rect.top_left_coords.x + last_rect.width, last_rect.top_left_coords.y].into(), - width: last_rect.width, - height: last_rect.height, - color: last_rect.color - }); - } - rects_vec - }) - .collect() + }) + .group_by(|rect| rect.top_left_coords.y) + .into_iter() + .map(|(_y_coord, rect_group)| { + let mut rects_vec = rect_group.collect::>(); + let last_rect_opt = rects_vec.last().cloned(); + // add extra rect to make it easy to highlight the newline character + if let Some(last_rect) = last_rect_opt { + rects_vec.push(Rect { + top_left_coords: [ + last_rect.top_left_coords.x + last_rect.width, + last_rect.top_left_coords.y, + ] + .into(), + width: last_rect.width, + height: last_rect.height, + color: last_rect.color, + }); + } + rects_vec + }) + .collect() } pub fn glyph_top_y(glyph: &Glyph) -> f32 { let height = glyph.scale.y; - + glyph.position.y - height * 0.75 } @@ -112,4 +117,4 @@ pub fn is_newline(char_ref: &char) -> bool { let newline_codes = vec!['\u{d}']; newline_codes.contains(char_ref) -} \ No newline at end of file +} diff --git a/editor/src/vec_result.rs b/editor/src/vec_result.rs index a06ce22a8b..202af28a96 100644 --- a/editor/src/vec_result.rs +++ b/editor/src/vec_result.rs @@ -1,17 +1,15 @@ - +use crate::error::EdResult; use crate::error::OutOfBounds; -use crate::error::{EdResult}; +use snafu::OptionExt; use std::slice::SliceIndex; -use snafu::{OptionExt}; // replace vec methods that return Option with ones that return Result and proper Error pub fn get_res(index: usize, vec: &[T]) -> EdResult<&>::Output> { - let elt_ref = vec.get(index).context(OutOfBounds { index, - vec_len: vec.len() + vec_len: vec.len(), })?; Ok(elt_ref) -} \ No newline at end of file +}