@@ -7,15 +7,11 @@ use tokio::sync::{mpsc, oneshot, watch};
77
88use crate :: {
99 api:: {
10- Controller , Cursor , Selection ,
11- controller:: { AsyncReceiver , AsyncSender , ControllerCallback } ,
10+ controller:: { AsyncReceiver , AsyncSender , ControllerCallback } , cursor:: CursorEvent , Controller , Cursor
1211 } ,
1312 errors:: ControllerResult ,
1413} ;
15- use codemp_proto:: {
16- cursor:: { CursorPosition , CursorUpdate , RowCol } ,
17- files:: BufferNode ,
18- } ;
14+ use codemp_proto:: cursor:: { CursorPosition , CursorUpdate , RowCol } ;
1915
2016/// A [Controller] for asynchronously sending and receiving [Cursor] event.
2117///
@@ -34,44 +30,49 @@ impl CursorController {
3430#[ derive( Debug ) ]
3531pub ( crate ) struct CursorControllerInner {
3632 pub ( crate ) op : mpsc:: UnboundedSender < CursorUpdate > ,
37- pub ( crate ) stream : mpsc:: Sender < oneshot:: Sender < Option < Cursor > > > ,
33+ pub ( crate ) stream : mpsc:: Sender < oneshot:: Sender < Option < CursorEvent > > > ,
3834 pub ( crate ) poll : mpsc:: UnboundedSender < oneshot:: Sender < ( ) > > ,
3935 pub ( crate ) callback : watch:: Sender < Option < ControllerCallback < CursorController > > > ,
4036 pub ( crate ) workspace_id : String ,
4137}
4238
4339#[ cfg_attr( feature = "async-trait" , async_trait:: async_trait) ]
44- impl Controller < Selection , Cursor > for CursorController { }
40+ impl Controller < Cursor , CursorEvent > for CursorController { }
4541
4642#[ cfg_attr( feature = "async-trait" , async_trait:: async_trait) ]
47- impl AsyncSender < Selection > for CursorController {
48- fn send ( & self , mut cursor : Selection ) -> ControllerResult < ( ) > {
49- if cursor. start_row > cursor. end_row
50- || ( cursor. start_row == cursor. end_row && cursor. start_col > cursor. end_col )
51- {
52- std:: mem:: swap ( & mut cursor. start_row , & mut cursor. end_row ) ;
53- std:: mem:: swap ( & mut cursor. start_col , & mut cursor. end_col ) ;
43+ impl AsyncSender < Cursor > for CursorController {
44+ fn send ( & self , mut cursor : Cursor ) -> ControllerResult < ( ) > {
45+ for sel in cursor. sel . iter_mut ( ) {
46+ if sel. start_row > sel. end_row
47+ || ( sel. start_row == sel. end_row && sel. start_col > sel. end_col )
48+ {
49+ std:: mem:: swap ( & mut sel. start_row , & mut sel. end_row ) ;
50+ std:: mem:: swap ( & mut sel. start_col , & mut sel. end_col ) ;
51+ }
5452 }
5553
56- Ok ( self . 0 . op . send ( CursorPosition {
57- buffer : BufferNode {
58- path : cursor. buffer ,
59- } ,
60- start : RowCol {
61- row : cursor. start_row ,
62- col : cursor. start_col ,
63- } ,
64- end : RowCol {
65- row : cursor. end_row ,
66- col : cursor. end_col ,
67- } ,
54+ Ok ( self . 0 . op . send ( CursorUpdate {
55+ buffer : cursor. buffer ,
56+ cursors : cursor. sel
57+ . into_iter ( )
58+ . map ( |x| CursorPosition {
59+ start : RowCol {
60+ row : x. start_row ,
61+ col : x. start_col ,
62+ } ,
63+ end : RowCol {
64+ row : x. end_row ,
65+ col : x. end_col ,
66+ }
67+ } )
68+ . collect ( )
6869 } ) ?)
6970 }
7071}
7172
7273#[ cfg_attr( feature = "async-trait" , async_trait:: async_trait) ]
73- impl AsyncReceiver < Cursor > for CursorController {
74- async fn try_recv ( & self ) -> ControllerResult < Option < Cursor > > {
74+ impl AsyncReceiver < CursorEvent > for CursorController {
75+ async fn try_recv ( & self ) -> ControllerResult < Option < CursorEvent > > {
7576 let ( tx, rx) = oneshot:: channel ( ) ;
7677 self . 0 . stream . send ( tx) . await ?;
7778 Ok ( rx. await ?)
0 commit comments