1
0
Fork 0

[Fix] replace all tab character with space for correct render text (#77)

main
The Mist 2024-04-07 16:17:24 +08:00 committed by GitHub
parent 8d210e272e
commit 69ecdace3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View File

@ -11,7 +11,7 @@ fn min_width(width: f32) -> f32 {
}
pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) {
let trimmed_text = trim_space(text);
let trimmed_text = prepare_code(text);
let lines = trimmed_text.lines();
let max_length_line = lines.clone().into_iter().fold("", |max_length_line, cur| {
if cur.len() > max_length_line.len() {
@ -26,7 +26,16 @@ pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) {
(min_width(width), height)
}
pub fn trim_space(text: &str) -> String {
// The tab character is incorrectly render using cosmic, need to replace all tab with space
// before render the code
fn replace_tab_to_space(text: &str) -> String {
let spaces = " ".repeat(2);
str::replace(text, "\t", &spaces)
}
// If the first line have indention, remove the same indention from subsequent lines
fn trim_space(text: &str) -> String {
let lines = text.split("\n").collect::<Vec<&str>>();
let first_line = lines.first().unwrap();
let head_spaces = Regex::new(r"^(\s*)").unwrap().find(first_line);
@ -47,3 +56,7 @@ pub fn trim_space(text: &str) -> String {
None => text.to_string(),
}
}
pub fn prepare_code(code: &str) -> String {
replace_tab_to_space(&trim_space(&code))
}

View File

@ -1,5 +1,5 @@
use crate::{
code::{calc_wh, trim_space},
code::{calc_wh, prepare_code},
components::interface::{
component::{Component, ComponentContext, RenderParams},
render_error,
@ -71,7 +71,7 @@ impl Component for Code {
impl Code {
pub fn new(value: String, line_height: f32, font_size: f32) -> Code {
Code {
value: trim_space(&value),
value: prepare_code(&value),
line_height,
font_size,
children: vec![],