Skip to content

Commit 82db89b

Browse files
committed
Fix text component crash and improve color handling
1 parent d6713a4 commit 82db89b

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

crates/frontend/src/component/content_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ fn create_descriptions(name: Option<Arc<str>>, version: Arc<str>, authors: Arc<s
727727
.overflow_x_hidden()
728728
.line_height(relative(1.0))
729729
.child(SharedString::from(filename))
730-
.child(styled_text);
730+
.child(div().line_clamp(2).child(styled_text));
731731
return (description1, None);
732732
}
733733

crates/schema/src/text_component.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn append_flat(component: &mut FlatTextComponent, value: serde_json::Value) {
104104
style.strikethrough = Some(*value);
105105
}
106106

107-
let index = component.runs.len();
107+
let start_runs = component.runs.len();
108108
let start = component.content.len();
109109

110110
let text = if let Some(serde_json::Value::String(string)) = map.remove("text") {
@@ -126,23 +126,64 @@ fn append_flat(component: &mut FlatTextComponent, value: serde_json::Value) {
126126
let end = component.content.len();
127127

128128
if end > start && style != TextComponentStyle::default() {
129-
component.runs.insert(index, TextComponentRun {
130-
range: start..end,
131-
style
132-
});
129+
// Ideally we could just insert start..end, but gpui doesn't handle overlaps properly
130+
131+
let mut ix = start;
132+
let mut run_index = start_runs;
133+
134+
while let Some(run) = component.runs.get_mut(run_index) {
135+
if run.range.start > ix {
136+
let until = run.range.start;
137+
component.runs.insert(run_index, TextComponentRun {
138+
range: ix..until,
139+
style: style.clone(),
140+
});
141+
ix = until;
142+
run_index += 1;
143+
continue;
144+
}
145+
146+
if run.style.colour.is_none() {
147+
run.style.colour = style.colour;
148+
}
149+
if run.style.bold.is_none() {
150+
run.style.bold = style.bold;
151+
}
152+
if run.style.italic.is_none() {
153+
run.style.italic = style.italic;
154+
}
155+
if run.style.underlined.is_none() {
156+
run.style.underlined = style.underlined;
157+
}
158+
if run.style.strikethrough.is_none() {
159+
run.style.strikethrough = style.strikethrough;
160+
}
161+
162+
ix = run.range.end;
163+
run_index += 1;
164+
}
165+
166+
if ix < end {
167+
component.runs.push(TextComponentRun {
168+
range: ix..end,
169+
style
170+
});
171+
}
133172
}
134173
},
135174
}
136175
}
137176

138177
fn append_string(component: &mut FlatTextComponent, mut text: String) {
178+
let current_len = component.content.len();
179+
139180
let mut last_legacy_color = None;
140181
let mut current_style = TextComponentStyle::default();
141182
while let Some(pos) = text.find('\u{00a7}') {
142183
if let Some((from, style)) = last_legacy_color.take() {
143184
if pos > from {
144185
component.runs.push(TextComponentRun {
145-
range: from..pos,
186+
range: current_len+from..current_len+pos,
146187
style,
147188
});
148189
}
@@ -186,7 +227,7 @@ fn append_string(component: &mut FlatTextComponent, mut text: String) {
186227
let to = text.len();
187228
if to > from {
188229
component.runs.push(TextComponentRun {
189-
range: from..to,
230+
range: current_len+from..current_len+to,
190231
style,
191232
});
192233
}

0 commit comments

Comments
 (0)