@@ -27,9 +27,10 @@ use cosmic_text::{
27
27
Attrs ,
28
28
AttrsList ,
29
29
Buffer ,
30
- Editor ,
31
30
FontSystem ,
32
31
Metrics ,
32
+ SyntaxEditor ,
33
+ SyntaxSystem ,
33
34
} ;
34
35
use std:: {
35
36
env,
@@ -38,6 +39,9 @@ use std::{
38
39
sync:: Mutex ,
39
40
} ;
40
41
42
+ use self :: syntax_text_box:: syntax_text_box;
43
+ mod syntax_text_box;
44
+
41
45
use self :: text:: text;
42
46
mod text;
43
47
@@ -46,6 +50,7 @@ mod text_box;
46
50
47
51
lazy_static:: lazy_static! {
48
52
static ref FONT_SYSTEM : FontSystem = FontSystem :: new( ) ;
53
+ static ref SYNTAX_SYSTEM : SyntaxSystem = SyntaxSystem :: new( ) ;
49
54
}
50
55
51
56
static FONT_SIZES : & ' static [ Metrics ] = & [
@@ -69,7 +74,7 @@ pub struct Window {
69
74
theme : Theme ,
70
75
path_opt : Option < PathBuf > ,
71
76
attrs : Attrs < ' static > ,
72
- editor : Mutex < Editor < ' static > > ,
77
+ editor : Mutex < SyntaxEditor < ' static > > ,
73
78
}
74
79
75
80
#[ allow( dead_code) ]
@@ -87,15 +92,13 @@ pub enum Message {
87
92
impl Window {
88
93
pub fn open ( & mut self , path : PathBuf ) {
89
94
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 ( ( ) ) => {
92
97
log:: info!( "opened '{}'" , path. display( ) ) ;
93
- editor. buffer . set_text ( & text, self . attrs ) ;
94
98
self . path_opt = Some ( path) ;
95
99
} ,
96
100
Err ( err) => {
97
101
log:: error!( "failed to open '{}': {}" , path. display( ) , err) ;
98
- editor. buffer . set_text ( "" , self . attrs ) ;
99
102
self . path_opt = None ;
100
103
}
101
104
}
@@ -113,10 +116,11 @@ impl Application for Window {
113
116
. monospaced ( true )
114
117
. family ( cosmic_text:: Family :: Monospace ) ;
115
118
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 ( ) ;
120
124
update_attrs ( & mut editor, attrs) ;
121
125
122
126
let mut window = Window {
@@ -154,7 +158,7 @@ impl Application for Window {
154
158
if let Some ( path) = & self . path_opt {
155
159
let editor = self . editor . lock ( ) . unwrap ( ) ;
156
160
let mut text = String :: new ( ) ;
157
- for line in editor. buffer . lines . iter ( ) {
161
+ for line in editor. buffer ( ) . lines . iter ( ) {
158
162
text. push_str ( line. text ( ) ) ;
159
163
text. push ( '\n' ) ;
160
164
}
@@ -202,7 +206,7 @@ impl Application for Window {
202
206
} ,
203
207
Message :: MetricsChanged ( metrics) => {
204
208
let mut editor = self . editor . lock ( ) . unwrap ( ) ;
205
- editor. buffer . set_metrics ( metrics) ;
209
+ editor. buffer_mut ( ) . set_metrics ( metrics) ;
206
210
} ,
207
211
Message :: ThemeChanged ( theme) => {
208
212
self . theme = match theme {
@@ -238,7 +242,7 @@ impl Application for Window {
238
242
let editor = self . editor . lock ( ) . unwrap ( ) ;
239
243
pick_list (
240
244
FONT_SIZES ,
241
- Some ( editor. buffer . metrics ( ) ) ,
245
+ Some ( editor. buffer ( ) . metrics ( ) ) ,
242
246
Message :: MetricsChanged
243
247
)
244
248
} ;
@@ -262,7 +266,7 @@ impl Application for Window {
262
266
. align_items( Alignment :: Center )
263
267
. spacing( 8 )
264
268
,
265
- text_box ( & self . editor)
269
+ syntax_text_box ( & self . editor)
266
270
]
267
271
. spacing ( 8 )
268
272
. padding ( 16 )
@@ -273,8 +277,8 @@ impl Application for Window {
273
277
}
274
278
}
275
279
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| {
278
282
line. set_attrs_list ( AttrsList :: new ( attrs) ) ;
279
283
} ) ;
280
284
}
0 commit comments