1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Styled text with multiple color spans
//!
//! This module provides structures for representing text with multiple styling spans,
//! enabling syntax highlighting and rich text rendering.
//!
//! # Example
//!
//! ```ignore
//! use blinc_layout::styled_text::{StyledText, TextSpan};
//! use blinc_core::Color;
//!
//! // Create styled text manually
//! let styled = StyledText::from_lines(vec![
//! StyledLine {
//! text: "fn main() {".to_string(),
//! spans: vec![
//! TextSpan::new(0, 2, Color::BLUE, true), // "fn" keyword
//! TextSpan::new(3, 7, Color::YELLOW, false), // "main" function name
//! ],
//! },
//! ]);
//! ```
use blinc_core::Color;
use crate::syntax::TokenType;
/// A span of styled text within a line
#[derive(Clone, Debug)]
pub struct TextSpan {
/// Start byte index in the line
pub start: usize,
/// End byte index in the line (exclusive)
pub end: usize,
/// Text color
pub color: Color,
/// Whether text is bold
pub bold: bool,
/// Whether text is italic
pub italic: bool,
/// Whether text has underline decoration
pub underline: bool,
/// Whether text has strikethrough decoration
pub strikethrough: bool,
/// Optional link URL (for clickable text spans)
pub link_url: Option<String>,
/// Token type (for intellisense callbacks)
pub token_type: Option<TokenType>,
}
impl TextSpan {
/// Create a new text span
pub fn new(start: usize, end: usize, color: Color, bold: bool) -> Self {
Self {
start,
end,
color,
bold,
italic: false,
underline: false,
strikethrough: false,
link_url: None,
token_type: None,
}
}
/// Create a span with just color (not bold)
pub fn colored(start: usize, end: usize, color: Color) -> Self {
Self::new(start, end, color, false)
}
/// Set the token type for this span
pub fn with_token_type(mut self, token_type: TokenType) -> Self {
self.token_type = Some(token_type);
self
}
/// Set italic style
pub fn with_italic(mut self, italic: bool) -> Self {
self.italic = italic;
self
}
/// Set underline decoration
pub fn with_underline(mut self, underline: bool) -> Self {
self.underline = underline;
self
}
/// Set strikethrough decoration
pub fn with_strikethrough(mut self, strikethrough: bool) -> Self {
self.strikethrough = strikethrough;
self
}
/// Set link URL for clickable span
pub fn with_link(mut self, url: impl Into<String>) -> Self {
self.link_url = Some(url.into());
self
}
/// Create an italic span
pub fn italic(start: usize, end: usize, color: Color) -> Self {
Self::new(start, end, color, false).with_italic(true)
}
/// Create a bold italic span
pub fn bold_italic(start: usize, end: usize, color: Color) -> Self {
Self::new(start, end, color, true).with_italic(true)
}
/// Create a link span (underlined by default)
pub fn link(start: usize, end: usize, color: Color, url: impl Into<String>) -> Self {
Self::new(start, end, color, false)
.with_underline(true)
.with_link(url)
}
}
/// A line with styled spans
#[derive(Clone, Debug)]
pub struct StyledLine {
/// The raw text content
pub text: String,
/// Style spans for this line (must cover entire line, sorted by start position)
pub spans: Vec<TextSpan>,
}
impl StyledLine {
/// Create a new styled line
pub fn new(text: impl Into<String>, spans: Vec<TextSpan>) -> Self {
Self {
text: text.into(),
spans,
}
}
/// Create a line with a single color for all text
pub fn plain(text: impl Into<String>, color: Color) -> Self {
let text = text.into();
let len = text.len();
Self {
spans: vec![TextSpan::colored(0, len, color)],
text,
}
}
}
/// Complete styled text with multiple lines
#[derive(Clone, Debug, Default)]
pub struct StyledText {
/// All lines with their styles
pub lines: Vec<StyledLine>,
}
impl StyledText {
/// Create empty styled text
pub fn new() -> Self {
Self::default()
}
/// Create from pre-built lines
pub fn from_lines(lines: Vec<StyledLine>) -> Self {
Self { lines }
}
/// Create from plain text with a single color
pub fn plain(text: &str, color: Color) -> Self {
let lines = text
.lines()
.map(|line| StyledLine::plain(line, color))
.collect();
Self { lines }
}
/// Get the total number of lines
pub fn line_count(&self) -> usize {
self.lines.len()
}
/// Get the raw text content (without styling)
pub fn raw_text(&self) -> String {
self.lines
.iter()
.map(|l| l.text.as_str())
.collect::<Vec<_>>()
.join("\n")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_plain_text() {
let styled = StyledText::plain("Hello\nWorld", Color::WHITE);
assert_eq!(styled.line_count(), 2);
assert_eq!(styled.lines[0].text, "Hello");
assert_eq!(styled.lines[1].text, "World");
assert_eq!(styled.lines[0].spans.len(), 1);
assert_eq!(styled.lines[0].spans[0].start, 0);
assert_eq!(styled.lines[0].spans[0].end, 5);
}
#[test]
fn test_raw_text() {
let styled = StyledText::plain("Line 1\nLine 2\nLine 3", Color::WHITE);
assert_eq!(styled.raw_text(), "Line 1\nLine 2\nLine 3");
}
#[test]
fn test_styled_line() {
let line = StyledLine::new(
"fn main()",
vec![
TextSpan::new(0, 2, Color::BLUE, true),
TextSpan::colored(3, 7, Color::YELLOW),
],
);
assert_eq!(line.text, "fn main()");
assert_eq!(line.spans.len(), 2);
assert!(line.spans[0].bold);
assert!(!line.spans[1].bold);
}
}