1
1
use std:: collections:: BTreeMap ;
2
2
use std:: fmt:: Debug ;
3
+ use std:: sync:: Arc ;
4
+
5
+ use parking_lot:: RwLock ;
3
6
4
7
use crate :: cursor:: Cursor ;
5
8
use crate :: editor:: Editor ;
6
9
use crate :: window:: WindowId ;
7
10
8
11
#[ derive( Debug ) ]
9
12
pub struct Context < ' ctx > {
10
- pub editor : & ' ctx mut Editor ,
13
+ pub editor : Arc < RwLock < Editor > > ,
11
14
pub cursors : & ' ctx mut BTreeMap < WindowId , Cursor > ,
12
15
}
13
16
@@ -61,14 +64,16 @@ impl Debug for MappableCommand {
61
64
62
65
fn move_left ( ctx : & mut Context ) {
63
66
{
64
- let tab = ctx. editor . focused_tab ( ) ;
67
+ let editor = ctx. editor . read ( ) ;
68
+ let tab = editor. focused_tab ( ) ;
65
69
let window = tab. tree . focus ( ) ;
66
70
let window = tab. tree . window ( window) ;
67
71
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
68
72
cursor. move_left ( ) ;
69
73
}
70
74
71
- let tab = ctx. editor . focused_tab_mut ( ) ;
75
+ let mut editor = ctx. editor . write ( ) ;
76
+ let tab = editor. focused_tab_mut ( ) ;
72
77
let window = tab. tree . focus ( ) ;
73
78
let window = tab. tree . window_mut ( window) ;
74
79
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
@@ -80,15 +85,17 @@ fn move_left(ctx: &mut Context) {
80
85
81
86
fn move_down ( ctx : & mut Context ) {
82
87
{
83
- let tab = ctx. editor . focused_tab ( ) ;
88
+ let editor = ctx. editor . read ( ) ;
89
+ let tab = editor. focused_tab ( ) ;
84
90
let window = tab. tree . focus ( ) ;
85
91
let window = tab. tree . window ( window) ;
86
- let document = ctx . editor . document ( & window. document ) ;
92
+ let document = editor. document ( & window. document ) ;
87
93
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
88
94
cursor. move_down ( document) ;
89
95
}
90
96
91
- let tab = ctx. editor . focused_tab_mut ( ) ;
97
+ let mut editor = ctx. editor . write ( ) ;
98
+ let tab = editor. focused_tab_mut ( ) ;
92
99
let window = tab. tree . focus ( ) ;
93
100
let window = tab. tree . window_mut ( window) ;
94
101
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
@@ -100,14 +107,16 @@ fn move_down(ctx: &mut Context) {
100
107
101
108
fn move_up ( ctx : & mut Context ) {
102
109
{
103
- let tab = ctx. editor . focused_tab ( ) ;
110
+ let editor = ctx. editor . read ( ) ;
111
+ let tab = editor. focused_tab ( ) ;
104
112
let window = tab. tree . focus ( ) ;
105
113
let window = tab. tree . window ( window) ;
106
114
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
107
115
cursor. move_up ( ) ;
108
116
}
109
117
110
- let tab = ctx. editor . focused_tab_mut ( ) ;
118
+ let mut editor = ctx. editor . write ( ) ;
119
+ let tab = editor. focused_tab_mut ( ) ;
111
120
let window = tab. tree . focus ( ) ;
112
121
let window = tab. tree . window_mut ( window) ;
113
122
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
@@ -119,15 +128,17 @@ fn move_up(ctx: &mut Context) {
119
128
120
129
pub fn move_right ( ctx : & mut Context ) {
121
130
{
122
- let tab = ctx. editor . focused_tab ( ) ;
131
+ let editor = ctx. editor . read ( ) ;
132
+ let tab = editor. focused_tab ( ) ;
123
133
let window = tab. tree . focus ( ) ;
124
134
let window = tab. tree . window ( window) ;
125
- let document = ctx . editor . document ( & window. document ) ;
135
+ let document = editor. document ( & window. document ) ;
126
136
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
127
137
cursor. move_right ( document) ;
128
138
}
129
139
130
- let tab = ctx. editor . focused_tab_mut ( ) ;
140
+ let mut editor = ctx. editor . write ( ) ;
141
+ let tab = editor. focused_tab_mut ( ) ;
131
142
let window = tab. tree . focus ( ) ;
132
143
let window = tab. tree . window_mut ( window) ;
133
144
let cursor = ctx. cursors . get_mut ( & window. id ) . unwrap ( ) ;
0 commit comments