Skip to content

Commit bbe7d77

Browse files
committed
Improvements to syntax editor, integrate with libcosmic editor
1 parent 1663bfc commit bbe7d77

File tree

9 files changed

+386
-69
lines changed

9 files changed

+386
-69
lines changed

examples/editor-libcosmic/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
77
publish = false
88

99
[dependencies]
10-
cosmic-text = { path = "../../" }
10+
cosmic-text = { path = "../../", features = ["syntect"] }
1111
env_logger = "0.9"
1212
fontdb = "0.9"
1313
lazy_static = "1.4"

examples/editor-libcosmic/src/main.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use cosmic_text::{
2727
Attrs,
2828
AttrsList,
2929
Buffer,
30-
Editor,
3130
FontSystem,
3231
Metrics,
32+
SyntaxEditor,
33+
SyntaxSystem,
3334
};
3435
use std::{
3536
env,
@@ -38,6 +39,9 @@ use std::{
3839
sync::Mutex,
3940
};
4041

42+
use self::syntax_text_box::syntax_text_box;
43+
mod syntax_text_box;
44+
4145
use self::text::text;
4246
mod text;
4347

@@ -46,6 +50,7 @@ mod text_box;
4650

4751
lazy_static::lazy_static! {
4852
static ref FONT_SYSTEM: FontSystem = FontSystem::new();
53+
static ref SYNTAX_SYSTEM: SyntaxSystem = SyntaxSystem::new();
4954
}
5055

5156
static FONT_SIZES: &'static [Metrics] = &[
@@ -69,7 +74,7 @@ pub struct Window {
6974
theme: Theme,
7075
path_opt: Option<PathBuf>,
7176
attrs: Attrs<'static>,
72-
editor: Mutex<Editor<'static>>,
77+
editor: Mutex<SyntaxEditor<'static>>,
7378
}
7479

7580
#[allow(dead_code)]
@@ -87,15 +92,13 @@ pub enum Message {
8792
impl Window {
8893
pub fn open(&mut self, path: PathBuf) {
8994
let mut editor = self.editor.lock().unwrap();
90-
match fs::read_to_string(&path) {
91-
Ok(text) => {
95+
match editor.load_text(&path, self.attrs) {
96+
Ok(()) => {
9297
log::info!("opened '{}'", path.display());
93-
editor.buffer.set_text(&text, self.attrs);
9498
self.path_opt = Some(path);
9599
},
96100
Err(err) => {
97101
log::error!("failed to open '{}': {}", path.display(), err);
98-
editor.buffer.set_text("", self.attrs);
99102
self.path_opt = None;
100103
}
101104
}
@@ -113,10 +116,11 @@ impl Application for Window {
113116
.monospaced(true)
114117
.family(cosmic_text::Family::Monospace);
115118

116-
let mut editor = Editor::new(Buffer::new(
117-
&FONT_SYSTEM,
118-
FONT_SIZES[1 /* Body */],
119-
));
119+
let mut editor = SyntaxEditor::new(
120+
Buffer::new(&FONT_SYSTEM, FONT_SIZES[1 /* Body */]),
121+
&SYNTAX_SYSTEM,
122+
"base16-eighties.dark"
123+
).unwrap();
120124
update_attrs(&mut editor, attrs);
121125

122126
let mut window = Window {
@@ -154,7 +158,7 @@ impl Application for Window {
154158
if let Some(path) = &self.path_opt {
155159
let editor = self.editor.lock().unwrap();
156160
let mut text = String::new();
157-
for line in editor.buffer.lines.iter() {
161+
for line in editor.buffer().lines.iter() {
158162
text.push_str(line.text());
159163
text.push('\n');
160164
}
@@ -202,7 +206,7 @@ impl Application for Window {
202206
},
203207
Message::MetricsChanged(metrics) => {
204208
let mut editor = self.editor.lock().unwrap();
205-
editor.buffer.set_metrics(metrics);
209+
editor.buffer_mut().set_metrics(metrics);
206210
},
207211
Message::ThemeChanged(theme) => {
208212
self.theme = match theme {
@@ -238,7 +242,7 @@ impl Application for Window {
238242
let editor = self.editor.lock().unwrap();
239243
pick_list(
240244
FONT_SIZES,
241-
Some(editor.buffer.metrics()),
245+
Some(editor.buffer().metrics()),
242246
Message::MetricsChanged
243247
)
244248
};
@@ -262,7 +266,7 @@ impl Application for Window {
262266
.align_items(Alignment::Center)
263267
.spacing(8)
264268
,
265-
text_box(&self.editor)
269+
syntax_text_box(&self.editor)
266270
]
267271
.spacing(8)
268272
.padding(16)
@@ -273,8 +277,8 @@ impl Application for Window {
273277
}
274278
}
275279

276-
fn update_attrs<'a>(editor: &mut Editor<'a>, attrs: Attrs<'a>) {
277-
editor.buffer.lines.iter_mut().for_each(|line| {
280+
fn update_attrs<'a>(editor: &mut SyntaxEditor<'a>, attrs: Attrs<'a>) {
281+
editor.buffer_mut().lines.iter_mut().for_each(|line| {
278282
line.set_attrs_list(AttrsList::new(attrs));
279283
});
280284
}

0 commit comments

Comments
 (0)