Skip to content

Commit 37109f1

Browse files
authored
[Fix] incorrect code highlight component width (#91)
1 parent 7025332 commit 37109f1

File tree

9 files changed

+31
-23
lines changed

9 files changed

+31
-23
lines changed

cli/src/code_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn create_code_config(cli: &CLI, code_config: CodeConfig) -> anyhow::Result<
1616

1717
fn map_breadcrumbs(cli: &CLI, breadcrumbs_config: Breadcrumbs) -> Breadcrumbs {
1818
Breadcrumbs {
19+
enable: cli.has_breadcrumbs.unwrap_or(breadcrumbs_config.enable),
1920
separator: cli
2021
.breadcrumbs_separator
2122
.clone()

cli/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ struct CLI {
9393
/// Breadcrumbs is a useful and unique feature in CodeSnap, it shows the path of the file
9494
/// so that users can know where the code snippet comes from.
9595
#[arg(long)]
96-
has_breadcrumbs: bool,
96+
has_breadcrumbs: Option<bool>,
9797

98-
#[arg(long)]
98+
#[arg(long, default_value = "false")]
9999
has_line_number: bool,
100100

101101
/// Breadcrumbs separator is the character to separate the path in breadcrumbs

core/assets/theme_configs/bamboo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"code_config": {
2323
"font_family": "CaskaydiaCove Nerd Font",
2424
"breadcrumbs": {
25+
"enable": false,
2526
"separator": "/",
2627
"color": "#80848b",
2728
"font_family": "CaskaydiaCove Nerd Font"

core/src/components/breadcrumbs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const LINE_HEIGHT: f32 = 15.;
1717

1818
pub struct Breadcrumbs {
1919
children: Vec<Box<dyn Component>>,
20-
has_breadcrumbs: bool,
2120
path: Option<String>,
2221
}
2322

@@ -30,14 +29,14 @@ impl Component for Breadcrumbs {
3029
&self.children
3130
}
3231

33-
fn render_condition(&self, _context: &ComponentContext) -> bool {
34-
self.has_breadcrumbs
32+
fn render_condition(&self, context: &ComponentContext) -> bool {
33+
context.take_snapshot_params.code_config.breadcrumbs.enable
3534
}
3635

37-
fn style(&self, _context: &ComponentContext) -> RawComponentStyle {
36+
fn style(&self, context: &ComponentContext) -> RawComponentStyle {
3837
let style = RawComponentStyle::default();
3938

40-
if !self.has_breadcrumbs {
39+
if !context.take_snapshot_params.code_config.breadcrumbs.enable {
4140
return style;
4241
}
4342

@@ -95,11 +94,10 @@ impl Component for Breadcrumbs {
9594
}
9695

9796
impl Breadcrumbs {
98-
pub fn from(has_breadcrumbs: bool, file_path: Option<String>) -> Breadcrumbs {
97+
pub fn from(file_path: Option<String>) -> Breadcrumbs {
9998
Breadcrumbs {
10099
children: vec![],
101100
path: file_path,
102-
has_breadcrumbs,
103101
}
104102
}
105103
}

core/src/components/highlight_code_block.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::{
22
config::HighlightLine,
33
edges::{edge::Edge, padding::Padding},
4-
utils::color::RgbaColor,
4+
snapshot::image_snapshot::DEFAULT_WINDOW_MIN_WIDTH,
5+
utils::{code::min_width, color::RgbaColor},
56
};
67

78
use super::{
89
editor::code::CODE_LINE_HEIGHT,
910
interface::{
10-
component::{Component, ComponentContext, RenderParams},
11+
component::{query_style, Component, ComponentContext, RenderParams},
1112
style::ComponentStyle,
1213
},
1314
};
@@ -102,14 +103,15 @@ impl HighlightCodeBlock {
102103
);
103104
}
104105

106+
let editor_style = query_style("RectInnerLayer").unwrap();
105107
let end_line_number = end_line_number.min(self.code_line_count as u32);
106108
let mut paint = Paint::default();
107109
// If the start line number is start at n, the y offset should be (n - 1) * line_height
108110
let start_y_offset = (start_line_number - 1) as f32 * CODE_LINE_HEIGHT;
109111
let rect = Rect::from_xywh(
110112
render_params.x - self.editor_padding.left,
111113
render_params.y + start_y_offset,
112-
parent_style.width + self.editor_padding.horizontal(),
114+
editor_style.width,
113115
// If end_line_number is equal to start_line_number, the height should be line_height
114116
(end_line_number - start_line_number + 1) as f32 * CODE_LINE_HEIGHT,
115117
)

core/src/components/interface/component.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ lazy_static! {
2121
static ref STYLE_MAP: Mutex<HashMap<&'static str, Style<f32>>> = Mutex::new(HashMap::new());
2222
}
2323

24+
pub(crate) fn query_style(name: &'static str) -> Option<Style<f32>> {
25+
let style_map = STYLE_MAP.lock().unwrap();
26+
27+
style_map.get(name).cloned()
28+
}
29+
2430
pub struct ComponentContext {
2531
pub scale_factor: f32,
2632
pub take_snapshot_params: Arc<SnapshotConfig>,
@@ -144,12 +150,13 @@ pub trait Component {
144150
let width = self.parse_size(style.width, width, parent_style.map(|s| s.width))
145151
+ style.padding.horizontal()
146152
+ style.margin.horizontal();
153+
147154
let style = Style {
148155
min_width: style.min_width,
149-
width: if width > style.min_width {
150-
width
151-
} else {
156+
width: if width < style.min_width {
152157
style.min_width
158+
} else {
159+
width
153160
},
154161
height: self.parse_size(style.height, height, parent_style.map(|s| s.height))
155162
+ style.padding.vertical()

core/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ pub struct Margin {
127127

128128
#[derive(Clone, Builder, Serialize, Deserialize, Debug, JsonSchema, Default)]
129129
pub struct Breadcrumbs {
130+
#[builder(default = false)]
131+
pub enable: bool,
132+
130133
#[builder(setter(into, strip_option), default = String::from("/"))]
131134
pub separator: String,
132135

@@ -206,9 +209,6 @@ pub struct Code {
206209
#[builder(setter(into))]
207210
pub content: String,
208211

209-
#[builder(default = false)]
210-
pub has_breadcrumbs: bool,
211-
212212
#[builder(setter(into, strip_option), default = None)]
213213
pub start_line_number: Option<u32>,
214214

core/src/snapshot/ascii_snapshot.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const SPACE_BOTH_SIDE: usize = 2;
1111

1212
pub struct ASCIISnapshot {
1313
code: Code,
14+
has_breadcrumbs: bool,
1415
}
1516

1617
fn optional(component: String, is_view: bool) -> String {
@@ -32,6 +33,7 @@ impl ASCIISnapshot {
3233
match config.content {
3334
Content::Code(ref raw_code) => Ok(ASCIISnapshot {
3435
code: raw_code.clone(),
36+
has_breadcrumbs: config.code_config.breadcrumbs.enable,
3537
}),
3638
_ => Err(anyhow::anyhow!("The code content is not raw")),
3739
}
@@ -84,7 +86,7 @@ impl ASCIISnapshot {
8486
"{}{line}",
8587
text_line(&self.code.file_path.clone().unwrap_or(String::from("")))
8688
),
87-
self.code.has_breadcrumbs,
89+
self.has_breadcrumbs,
8890
);
8991

9092
format!("{top_frame}{breadcrumbs}{code}{bottom_frame}")

core/src/snapshot/image_snapshot.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ impl ImageSnapshot {
159159
) -> anyhow::Result<Vec<Box<dyn Component>>> {
160160
let code_lines = code_content.content.lines().collect::<Vec<&str>>();
161161
let view: Vec<Box<dyn Component>> = vec![
162-
Box::new(Breadcrumbs::from(
163-
code_content.has_breadcrumbs,
164-
code_content.file_path.clone(),
165-
)),
162+
Box::new(Breadcrumbs::from(code_content.file_path.clone())),
166163
Box::new(CodeBlock::from_children(vec![
167164
Box::new(HighlightCodeBlock::from(
168165
code_content.highlight_lines.clone(),

0 commit comments

Comments
 (0)