From 552d1c45cf69e672815b7382599c1b24c482879b Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 25 Feb 2022 12:52:12 -0500 Subject: [PATCH 1/7] feat: create some underline styles data types --- code_markup/src/underline_style.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 code_markup/src/underline_style.rs diff --git a/code_markup/src/underline_style.rs b/code_markup/src/underline_style.rs new file mode 100644 index 0000000000..2ced40aea2 --- /dev/null +++ b/code_markup/src/underline_style.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +use crate::colors::{from_hsb, RgbaTup}; + +#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug, Deserialize, Serialize)] +pub enum UnderlineStyle { + Error, + Warning, +} + +pub fn default_underline_colors() -> HashMap { + let mut underline_colors = HashMap::new(); + + underline_colors.insert(UnderlineStyle::Error, from_hsb(0, 50, 75)); + underline_colors.insert(UnderlineStyle::Warning, from_hsb(60, 50, 75)); + + underline_colors +} From 5e5446ef51b0d510bf037cfbeb865077481c5d4f Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 25 Feb 2022 12:52:52 -0500 Subject: [PATCH 2/7] feat: expose underline style --- code_markup/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/code_markup/src/lib.rs b/code_markup/src/lib.rs index 9e5d220855..1b140a9678 100644 --- a/code_markup/src/lib.rs +++ b/code_markup/src/lib.rs @@ -3,3 +3,4 @@ pub mod markup; pub mod markup_error; pub mod slow_pool; pub mod syntax_highlight; +pub mod underline_style; From 278bed1214a8dcc7bb5bc5a721c95981854c0814 Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 25 Feb 2022 12:53:50 -0500 Subject: [PATCH 3/7] feat: render an error underline on text --- editor/src/editor/render_ast.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/editor/src/editor/render_ast.rs b/editor/src/editor/render_ast.rs index 8dcda658e7..90f7de4ac2 100644 --- a/editor/src/editor/render_ast.rs +++ b/editor/src/editor/render_ast.rs @@ -5,7 +5,7 @@ use crate::graphics::primitives::text as gr_text; use cgmath::Vector2; use roc_code_markup::markup::nodes::{MarkupNode, BLANK_PLACEHOLDER}; use roc_code_markup::slow_pool::{MarkNodeId, SlowPool}; -use roc_code_markup::syntax_highlight::HighlightStyle; +use roc_code_markup::{syntax_highlight::HighlightStyle, underline_style::UnderlineStyle}; use winit::dpi::PhysicalSize; use crate::{editor::config::Config, graphics::colors}; @@ -94,6 +94,9 @@ fn markup_to_wgpu_helper<'a>( txt_row_col: &mut (usize, usize), mark_node_pool: &'a SlowPool, ) -> EdResult<()> { + let char_width = code_style.glyph_dim_rect.width; + let char_height = code_style.glyph_dim_rect.height; + match markup_node { MarkupNode::Nested { ast_node_id: _, @@ -132,10 +135,15 @@ fn markup_to_wgpu_helper<'a>( let full_content = markup_node.get_full_content().replace("\n", "\\n"); // any \n left here should be escaped so that it can be shown as \n - let glyph_text = glyph_brush::OwnedText::new(full_content) + let glyph_text = glyph_brush::OwnedText::new(&full_content) .with_color(colors::to_slice(*highlight_color)) .with_scale(code_style.font_size); + let top_left_coords = ( + code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width, + code_style.txt_coords.y + (txt_row_col.0 as f32) * char_height + 1.0 * char_height, + ); + txt_row_col.1 += content.len(); for _ in 0..*newlines_at_end { @@ -144,6 +152,19 @@ fn markup_to_wgpu_helper<'a>( } wgpu_texts.push(glyph_text); + + let underline_rect = Rect { + top_left_coords: top_left_coords.into(), + width: char_width * (full_content.len() as f32), + height: 5.0, + color: *code_style + .ed_theme + .underline_colors + .get(&UnderlineStyle::Error) + .unwrap(), + }; + + rects.push(underline_rect); } MarkupNode::Blank { ast_node_id: _, @@ -160,9 +181,6 @@ fn markup_to_wgpu_helper<'a>( let highlight_color = map_get(&code_style.ed_theme.syntax_high_map, &HighlightStyle::Blank)?; - let char_width = code_style.glyph_dim_rect.width; - let char_height = code_style.glyph_dim_rect.height; - let blank_rect = Rect { top_left_coords: ( code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width, From 0853c0bdb7d5e1ba9711ccd96491b7aa0635a650 Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 25 Feb 2022 12:54:19 -0500 Subject: [PATCH 4/7] feat: add underline colors to theme --- editor/src/editor/theme.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/editor/src/editor/theme.rs b/editor/src/editor/theme.rs index 30e45a47a9..2c21121f02 100644 --- a/editor/src/editor/theme.rs +++ b/editor/src/editor/theme.rs @@ -1,5 +1,8 @@ use gr_colors::{from_hsb, RgbaTup}; -use roc_code_markup::syntax_highlight::{default_highlight_map, HighlightStyle}; +use roc_code_markup::{ + syntax_highlight::{default_highlight_map, HighlightStyle}, + underline_style::{default_underline_colors, UnderlineStyle}, +}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -12,6 +15,7 @@ pub struct EdTheme { pub subtle_text: RgbaTup, pub syntax_high_map: HashMap, pub ui_theme: UITheme, + pub underline_colors: HashMap, } impl Default for EdTheme { @@ -21,6 +25,7 @@ impl Default for EdTheme { subtle_text: from_hsb(240, 5, 60), syntax_high_map: default_highlight_map(), ui_theme: UITheme::default(), + underline_colors: default_underline_colors(), } } } From 6e752645a6180ee30108c5531b73e22019e02a1b Mon Sep 17 00:00:00 2001 From: rvcas Date: Fri, 25 Feb 2022 13:02:35 -0500 Subject: [PATCH 5/7] feat: only render unline if attribute is there --- code_markup/src/markup/attribute.rs | 26 +++++++++---- editor/src/editor/render_ast.rs | 60 +++++++++++++++++++---------- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/code_markup/src/markup/attribute.rs b/code_markup/src/markup/attribute.rs index 90b4801f18..9cd63cd79b 100644 --- a/code_markup/src/markup/attribute.rs +++ b/code_markup/src/markup/attribute.rs @@ -47,16 +47,28 @@ pub struct UnderlineEnd { #[derive(Debug)] pub enum Attribute { // Rust does not yet support types for enum variants so we have to do it like this - Caret { caret: Caret }, + Caret { + caret: Caret, + }, - SelectionStart { selection_start: SelectionStart }, - SelectionEnd { selection_end: SelectionEnd }, + SelectionStart { + selection_start: SelectionStart, + }, + SelectionEnd { + selection_end: SelectionEnd, + }, - HighlightStart { highlight_start: HighlightStart }, - HighlightEnd { highlight_end: HighlightEnd }, + HighlightStart { + highlight_start: HighlightStart, + }, + HighlightEnd { + highlight_end: HighlightEnd, + }, - UnderlineStart { underline_start: UnderlineStart }, - UnderlineEnd { underline_end: UnderlineEnd }, + Underline { + underline_start: UnderlineStart, + underline_end: UnderlineEnd, + }, } #[derive(Debug, Default)] diff --git a/editor/src/editor/render_ast.rs b/editor/src/editor/render_ast.rs index 90f7de4ac2..0b83561d13 100644 --- a/editor/src/editor/render_ast.rs +++ b/editor/src/editor/render_ast.rs @@ -3,9 +3,15 @@ use crate::editor::{ed_error::EdResult, theme::EdTheme, util::map_get}; use crate::graphics::primitives::rect::Rect; use crate::graphics::primitives::text as gr_text; use cgmath::Vector2; -use roc_code_markup::markup::nodes::{MarkupNode, BLANK_PLACEHOLDER}; -use roc_code_markup::slow_pool::{MarkNodeId, SlowPool}; -use roc_code_markup::{syntax_highlight::HighlightStyle, underline_style::UnderlineStyle}; +use roc_code_markup::{ + markup::{ + attribute::Attribute, + nodes::{MarkupNode, BLANK_PLACEHOLDER}, + }, + slow_pool::{MarkNodeId, SlowPool}, + syntax_highlight::HighlightStyle, + underline_style::UnderlineStyle, +}; use winit::dpi::PhysicalSize; use crate::{editor::config::Config, graphics::colors}; @@ -127,7 +133,7 @@ fn markup_to_wgpu_helper<'a>( content, ast_node_id: _, syn_high_style, - attributes: _, + attributes, parent_id_opt: _, newlines_at_end, } => { @@ -139,10 +145,35 @@ fn markup_to_wgpu_helper<'a>( .with_color(colors::to_slice(*highlight_color)) .with_scale(code_style.font_size); - let top_left_coords = ( - code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width, - code_style.txt_coords.y + (txt_row_col.0 as f32) * char_height + 1.0 * char_height, - ); + for attribute in &attributes.all { + match attribute { + Attribute::Underline { + underline_start: _, + underline_end: _, + } => { + let top_left_coords = ( + code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width, + code_style.txt_coords.y + + (txt_row_col.0 as f32) * char_height + + 1.0 * char_height, + ); + + let underline_rect = Rect { + top_left_coords: top_left_coords.into(), + width: char_width * (full_content.len() as f32), + height: 5.0, + color: *code_style + .ed_theme + .underline_colors + .get(&UnderlineStyle::Error) + .unwrap(), + }; + + rects.push(underline_rect); + } + rest => todo!("handle Attribute: {:?}", rest), + } + } txt_row_col.1 += content.len(); @@ -152,19 +183,6 @@ fn markup_to_wgpu_helper<'a>( } wgpu_texts.push(glyph_text); - - let underline_rect = Rect { - top_left_coords: top_left_coords.into(), - width: char_width * (full_content.len() as f32), - height: 5.0, - color: *code_style - .ed_theme - .underline_colors - .get(&UnderlineStyle::Error) - .unwrap(), - }; - - rects.push(underline_rect); } MarkupNode::Blank { ast_node_id: _, From 26556c631b35fa7678286cbf0096077043acf756 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Sat, 26 Feb 2022 10:59:38 +0100 Subject: [PATCH 6/7] refactor, allowed to specify full underline --- code_markup/src/markup/attribute.rs | 12 ++++++++++-- code_markup/src/underline_style.rs | 2 +- editor/src/editor/render_ast.rs | 6 +++--- editor/src/editor/theme.rs | 6 +++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/code_markup/src/markup/attribute.rs b/code_markup/src/markup/attribute.rs index 9cd63cd79b..0ac1e558a4 100644 --- a/code_markup/src/markup/attribute.rs +++ b/code_markup/src/markup/attribute.rs @@ -66,11 +66,19 @@ pub enum Attribute { }, Underline { - underline_start: UnderlineStart, - underline_end: UnderlineEnd, + underline_spec: UnderlineSpec, }, } +#[derive(Debug)] +pub enum UnderlineSpec { + Partial{ + start: usize, + end: usize, + }, + Full +} + #[derive(Debug, Default)] pub struct Attributes { pub all: Vec, diff --git a/code_markup/src/underline_style.rs b/code_markup/src/underline_style.rs index 2ced40aea2..4eb81ee73c 100644 --- a/code_markup/src/underline_style.rs +++ b/code_markup/src/underline_style.rs @@ -10,7 +10,7 @@ pub enum UnderlineStyle { Warning, } -pub fn default_underline_colors() -> HashMap { +pub fn default_underline_color_map() -> HashMap { let mut underline_colors = HashMap::new(); underline_colors.insert(UnderlineStyle::Error, from_hsb(0, 50, 75)); diff --git a/editor/src/editor/render_ast.rs b/editor/src/editor/render_ast.rs index 0b83561d13..ab44e2a5b7 100644 --- a/editor/src/editor/render_ast.rs +++ b/editor/src/editor/render_ast.rs @@ -148,9 +148,9 @@ fn markup_to_wgpu_helper<'a>( for attribute in &attributes.all { match attribute { Attribute::Underline { - underline_start: _, - underline_end: _, + underline_spec: _, } => { + // TODO use underline_spec let top_left_coords = ( code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width, code_style.txt_coords.y @@ -164,7 +164,7 @@ fn markup_to_wgpu_helper<'a>( height: 5.0, color: *code_style .ed_theme - .underline_colors + .underline_color_map .get(&UnderlineStyle::Error) .unwrap(), }; diff --git a/editor/src/editor/theme.rs b/editor/src/editor/theme.rs index 2c21121f02..d3482f2cbe 100644 --- a/editor/src/editor/theme.rs +++ b/editor/src/editor/theme.rs @@ -1,7 +1,7 @@ use gr_colors::{from_hsb, RgbaTup}; use roc_code_markup::{ syntax_highlight::{default_highlight_map, HighlightStyle}, - underline_style::{default_underline_colors, UnderlineStyle}, + underline_style::{default_underline_color_map, UnderlineStyle}, }; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -15,7 +15,7 @@ pub struct EdTheme { pub subtle_text: RgbaTup, pub syntax_high_map: HashMap, pub ui_theme: UITheme, - pub underline_colors: HashMap, + pub underline_color_map: HashMap, } impl Default for EdTheme { @@ -25,7 +25,7 @@ impl Default for EdTheme { subtle_text: from_hsb(240, 5, 60), syntax_high_map: default_highlight_map(), ui_theme: UITheme::default(), - underline_colors: default_underline_colors(), + underline_color_map: default_underline_color_map(), } } } From b46ef4d68d00c14cb93484ee46bf1fccf15323e1 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Sat, 26 Feb 2022 11:05:40 +0100 Subject: [PATCH 7/7] fmt --- cli_utils/Cargo.lock | 10 ++++++++++ code_markup/src/markup/attribute.rs | 31 ++++++++--------------------- editor/src/editor/render_ast.rs | 4 +--- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/cli_utils/Cargo.lock b/cli_utils/Cargo.lock index c385582180..86ed98ed87 100644 --- a/cli_utils/Cargo.lock +++ b/cli_utils/Cargo.lock @@ -897,6 +897,12 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" +[[package]] +name = "dunce" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541" + [[package]] name = "either" version = "1.6.1" @@ -2504,6 +2510,7 @@ dependencies = [ name = "roc_builtins" version = "0.1.0" dependencies = [ + "dunce", "roc_collections", "roc_module", "roc_region", @@ -2518,6 +2525,7 @@ dependencies = [ "bumpalo", "roc_builtins", "roc_collections", + "roc_error_macros", "roc_module", "roc_parse", "roc_problem", @@ -2587,6 +2595,7 @@ dependencies = [ "roc_builtins", "roc_can", "roc_collections", + "roc_error_macros", "roc_module", "roc_parse", "roc_region", @@ -2760,6 +2769,7 @@ dependencies = [ "roc_can", "roc_collections", "roc_constrain", + "roc_error_macros", "roc_module", "roc_mono", "roc_parse", diff --git a/code_markup/src/markup/attribute.rs b/code_markup/src/markup/attribute.rs index 0ac1e558a4..58f57bc206 100644 --- a/code_markup/src/markup/attribute.rs +++ b/code_markup/src/markup/attribute.rs @@ -47,36 +47,21 @@ pub struct UnderlineEnd { #[derive(Debug)] pub enum Attribute { // Rust does not yet support types for enum variants so we have to do it like this - Caret { - caret: Caret, - }, + Caret { caret: Caret }, - SelectionStart { - selection_start: SelectionStart, - }, - SelectionEnd { - selection_end: SelectionEnd, - }, + SelectionStart { selection_start: SelectionStart }, + SelectionEnd { selection_end: SelectionEnd }, - HighlightStart { - highlight_start: HighlightStart, - }, - HighlightEnd { - highlight_end: HighlightEnd, - }, + HighlightStart { highlight_start: HighlightStart }, + HighlightEnd { highlight_end: HighlightEnd }, - Underline { - underline_spec: UnderlineSpec, - }, + Underline { underline_spec: UnderlineSpec }, } #[derive(Debug)] pub enum UnderlineSpec { - Partial{ - start: usize, - end: usize, - }, - Full + Partial { start: usize, end: usize }, + Full, } #[derive(Debug, Default)] diff --git a/editor/src/editor/render_ast.rs b/editor/src/editor/render_ast.rs index ab44e2a5b7..d66457a79e 100644 --- a/editor/src/editor/render_ast.rs +++ b/editor/src/editor/render_ast.rs @@ -147,9 +147,7 @@ fn markup_to_wgpu_helper<'a>( for attribute in &attributes.all { match attribute { - Attribute::Underline { - underline_spec: _, - } => { + Attribute::Underline { underline_spec: _ } => { // TODO use underline_spec let top_left_coords = ( code_style.txt_coords.x + (txt_row_col.1 as f32) * char_width,