rsvim_core 0.1.2

The core library for RSVIM text editor.
Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Command-line widget.

use crate::content::TextContentsWk;
use crate::prelude::*;
use crate::ui::canvas::Canvas;
use crate::ui::tree::*;
use crate::ui::viewport::{
  CursorViewport, CursorViewportArc, Viewport, ViewportArc,
};
use crate::ui::widget::cursor::Cursor;
use crate::ui::widget::window::opt::{WindowOptions, WindowOptionsBuilder};
use crate::ui::widget::{EditableWidgetable, Widgetable};
use crate::{
  geo_rect_as, inode_enum_dispatcher, inode_itree_impl, widget_enum_dispatcher,
};
use indicator::{Indicator, IndicatorSymbol};
use input::Input;
use message::Message;
use root::RootContainer;

use std::sync::Arc;

pub mod indicator;
pub mod input;
pub mod message;
pub mod root;

#[cfg(test)]
pub mod indicator_tests;

#[derive(Debug, Clone)]
/// The value holder for each window widget.
pub enum CommandLineNode {
  RootContainer(RootContainer),
  Indicator(Indicator),
  Input(Input),
  Cursor(Cursor),
  Message(Message),
}

inode_enum_dispatcher!(
  CommandLineNode,
  RootContainer,
  Indicator,
  Input,
  Cursor,
  Message
);

widget_enum_dispatcher!(
  CommandLineNode,
  RootContainer,
  Indicator,
  Input,
  Cursor,
  Message
);

#[derive(Debug, Clone)]
/// The Vim command-line.
pub struct CommandLine {
  base: Itree<CommandLineNode>,
  options: WindowOptions,

  indicator_id: TreeNodeId,
  input_id: TreeNodeId,
  cursor_id: Option<TreeNodeId>,
  message_id: TreeNodeId,

  input_viewport: ViewportArc,
  input_cursor_viewport: CursorViewportArc,
  message_viewport: ViewportArc,
}

impl CommandLine {
  pub fn new(shape: IRect, text_contents: TextContentsWk) -> Self {
    // Force cmdline window options.
    let options = WindowOptionsBuilder::default()
      .wrap(false)
      .line_break(false)
      .scroll_off(0_u16)
      .build()
      .unwrap();

    let root = RootContainer::new(shape);
    let root_id = root.id();
    let root_node = CommandLineNode::RootContainer(root);

    let mut base = Itree::new(root_node);

    let indicator_shape =
      IRect::new(shape.min().into(), (shape.min().x + 1, shape.max().y));
    let indicator = Indicator::new(indicator_shape, IndicatorSymbol::Empty);
    let indicator_id = indicator.id();
    let mut indicator_node = CommandLineNode::Indicator(indicator);
    // Indicator by default is invisible
    indicator_node.set_visible(false);
    base.bounded_insert(root_id, indicator_node);

    let input_shape =
      IRect::new((shape.min().x + 1, shape.min().y), shape.max().into());

    let (input_viewport, input_cursor_viewport, message_viewport) = {
      let input_actual_shape = geo_rect_as!(input_shape, u16);
      let text_contents = text_contents.upgrade().unwrap();
      let text_contents = lock!(text_contents);
      let input_viewport = Viewport::view(
        &options,
        text_contents.command_line_input(),
        &input_actual_shape,
        0,
        0,
      );
      let input_cursor_viewport = CursorViewport::from_top_left(
        &input_viewport,
        text_contents.command_line_input(),
      );

      let message_actual_shape = geo_rect_as!(shape, u16);
      let message_viewport = Viewport::view(
        &options,
        text_contents.command_line_message(),
        &message_actual_shape,
        0,
        0,
      );
      (input_viewport, input_cursor_viewport, message_viewport)
    };

    let input_viewport = Viewport::to_arc(input_viewport);
    let input_cursor_viewport = CursorViewport::to_arc(input_cursor_viewport);
    let message_viewport = Viewport::to_arc(message_viewport);

    let input = Input::new(
      input_shape,
      text_contents.clone(),
      Arc::downgrade(&input_viewport),
    );
    let input_id = input.id();
    let mut input_node = CommandLineNode::Input(input);
    // Input by default is invisible
    input_node.set_visible(false);
    base.bounded_insert(root_id, input_node);

    let message = Message::new(
      shape,
      text_contents.clone(),
      Arc::downgrade(&message_viewport),
    );
    let message_id = message.id();
    let message_node = CommandLineNode::Message(message);
    base.bounded_insert(root_id, message_node);

    Self {
      base,
      options,
      indicator_id,
      input_id,
      message_id,
      cursor_id: None,
      input_viewport,
      input_cursor_viewport,
      message_viewport,
    }
  }
}

inode_itree_impl!(CommandLine, base);

impl Widgetable for CommandLine {
  fn draw(&self, canvas: &mut Canvas) {
    for node in self.base.iter() {
      // trace!("Draw window:{:?}", node);
      if !node.visible() {
        continue;
      }
      node.draw(canvas);
    }
  }
}

impl CommandLine {
  /// Get window local options.
  pub fn options(&self) -> &WindowOptions {
    &self.options
  }

  /// Set window local options.
  pub fn set_options(&mut self, options: &WindowOptions) {
    self.options = *options;
  }

  /// Cursor widget ID.
  pub fn cursor_id(&self) -> Option<TreeNodeId> {
    self.cursor_id
  }

  /// Command-line indicator widget ID.
  pub fn indicator_id(&self) -> TreeNodeId {
    self.indicator_id
  }

  /// Command-line input widget ID.
  pub fn input_id(&self) -> TreeNodeId {
    self.input_id
  }

  /// Command-line message widget ID.
  pub fn message_id(&self) -> TreeNodeId {
    self.message_id
  }
}

// Viewport {
impl CommandLine {
  /// Get input viewport.
  pub fn input_viewport(&self) -> ViewportArc {
    self.input_viewport.clone()
  }

  /// Get message viewport.
  pub fn message_viewport(&self) -> ViewportArc {
    self.message_viewport.clone()
  }

  /// Set viewport for input.
  pub fn set_input_viewport(&mut self, viewport: ViewportArc) {
    self.input_viewport = viewport.clone();
    if let Some(CommandLineNode::Input(input)) =
      self.base.node_mut(self.input_id)
    {
      input.set_viewport(Arc::downgrade(&viewport));
    }
  }

  /// Set viewport for message.
  pub fn set_message_viewport(&mut self, viewport: ViewportArc) {
    self.message_viewport = viewport.clone();
    if let Some(CommandLineNode::Message(message)) =
      self.base.node_mut(self.message_id)
    {
      message.set_viewport(Arc::downgrade(&viewport));
    }
  }

  /// Get cursor viewport for input.
  pub fn input_cursor_viewport(&self) -> CursorViewportArc {
    self.input_cursor_viewport.clone()
  }

  /// Set cursor viewport for input.
  pub fn set_input_cursor_viewport(
    &mut self,
    cursor_viewport: CursorViewportArc,
  ) {
    self.input_cursor_viewport = cursor_viewport;
  }
}
// Viewport }

// Editable Viewport {
impl EditableWidgetable for CommandLine {
  fn editable_viewport(&self) -> ViewportArc {
    self.input_viewport()
  }

  fn set_editable_viewport(&mut self, viewport: ViewportArc) {
    self.set_input_viewport(viewport);
  }

  fn editable_cursor_viewport(&self) -> CursorViewportArc {
    self.input_cursor_viewport()
  }

  fn set_editable_cursor_viewport(
    &mut self,
    cursor_viewport: CursorViewportArc,
  ) {
    self.set_input_cursor_viewport(cursor_viewport);
  }

  fn editable_options(&self) -> &WindowOptions {
    self.options()
  }

  fn editable_actual_shape(&self) -> &U16Rect {
    self.input().actual_shape()
  }

  fn move_editable_cursor_to(&mut self, x: isize, y: isize) -> Option<IRect> {
    self.move_cursor_to(x, y)
  }

  fn editable_cursor_id(&self) -> Option<TreeNodeId> {
    self.cursor_id()
  }
}
// Editable Viewport }

// Show/Hide switch {
impl CommandLine {
  pub fn show_message(&mut self) {
    self.indicator_mut().set_visible(false);
    self.input_mut().set_visible(false);
    self.message_mut().set_visible(true);
  }

  pub fn show_input(&mut self) {
    self.indicator_mut().set_visible(true);
    self.input_mut().set_visible(true);
    self.message_mut().set_visible(false);
  }
}
// Show/Hide switch }

// Widgets {
impl CommandLine {
  /// Command-line input widget.
  pub fn input(&self) -> &Input {
    debug_assert!(self.base.node(self.input_id).is_some());
    debug_assert!(matches!(
      self.base.node(self.input_id).unwrap(),
      CommandLineNode::Input(_)
    ));

    match self.base.node(self.input_id).unwrap() {
      CommandLineNode::Input(w) => {
        debug_assert_eq!(w.id(), self.input_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Mutable command-line input widget.
  pub fn input_mut(&mut self) -> &mut Input {
    debug_assert!(self.base.node_mut(self.input_id).is_some());
    debug_assert!(matches!(
      self.base.node_mut(self.input_id).unwrap(),
      CommandLineNode::Input(_)
    ));

    match self.base.node_mut(self.input_id).unwrap() {
      CommandLineNode::Input(w) => {
        debug_assert_eq!(w.id(), self.input_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Command-line message widget
  pub fn message(&self) -> &Message {
    debug_assert!(self.base.node(self.message_id).is_some());
    debug_assert!(matches!(
      self.base.node(self.message_id).unwrap(),
      CommandLineNode::Message(_)
    ));

    match self.base.node(self.message_id).unwrap() {
      CommandLineNode::Message(w) => {
        debug_assert_eq!(w.id(), self.message_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Mutable command-line message widget.
  pub fn message_mut(&mut self) -> &mut Message {
    debug_assert!(self.base.node_mut(self.message_id).is_some());
    debug_assert!(matches!(
      self.base.node_mut(self.message_id).unwrap(),
      CommandLineNode::Message(_)
    ));

    match self.base.node_mut(self.message_id).unwrap() {
      CommandLineNode::Message(w) => {
        debug_assert_eq!(w.id(), self.message_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Command-line indicator widget.
  pub fn indicator(&self) -> &Indicator {
    debug_assert!(self.base.node(self.indicator_id).is_some());
    debug_assert!(matches!(
      self.base.node(self.indicator_id).unwrap(),
      CommandLineNode::Indicator(_)
    ));

    match self.base.node(self.indicator_id).unwrap() {
      CommandLineNode::Indicator(w) => {
        debug_assert_eq!(w.id(), self.indicator_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Mutable command-line indicator widget.
  pub fn indicator_mut(&mut self) -> &mut Indicator {
    debug_assert!(self.base.node_mut(self.indicator_id).is_some());
    debug_assert!(matches!(
      self.base.node_mut(self.indicator_id).unwrap(),
      CommandLineNode::Indicator(_)
    ));

    match self.base.node_mut(self.indicator_id).unwrap() {
      CommandLineNode::Indicator(w) => {
        debug_assert_eq!(w.id(), self.indicator_id);
        w
      }
      _ => unreachable!(),
    }
  }

  /// Command-line cursor widget.
  pub fn cursor(&self) -> Option<&Cursor> {
    match self.cursor_id {
      Some(cursor_id) => {
        debug_assert!(self.base.node(cursor_id).is_some());
        debug_assert!(matches!(
          self.base.node(cursor_id).unwrap(),
          CommandLineNode::Cursor(_)
        ));

        match self.base.node(cursor_id).unwrap() {
          CommandLineNode::Cursor(w) => {
            debug_assert_eq!(w.id(), cursor_id);
            Some(w)
          }
          _ => unreachable!(),
        }
      }
      None => None,
    }
  }

  /// Mutable command-line cursor widget.
  pub fn cursor_mut(&mut self) -> Option<&mut Cursor> {
    match self.cursor_id {
      Some(cursor_id) => {
        debug_assert!(self.base.node_mut(cursor_id).is_some());
        debug_assert!(matches!(
          self.base.node_mut(cursor_id).unwrap(),
          CommandLineNode::Cursor(_)
        ));

        match self.base.node_mut(cursor_id).unwrap() {
          CommandLineNode::Cursor(w) => {
            debug_assert_eq!(w.id(), cursor_id);
            Some(w)
          }
          _ => unreachable!(),
        }
      }
      None => None,
    }
  }
}
// Attributes }

// Cursor {
impl CommandLine {
  /// Enable/insert cursor widget in commandline, i.e. when user start command-line mode, the
  /// cursor moves to the command-line widget and allow receive user ex command or search patterns.
  ///
  /// # Returns
  /// It returns the old cursor widget if there's any, otherwise it returns `None`.
  pub fn insert_cursor(&mut self, cursor: Cursor) -> Option<CommandLineNode> {
    self.cursor_id = Some(cursor.id());
    self
      .base
      .bounded_insert(self.input_id, CommandLineNode::Cursor(cursor))
  }

  /// Disable/remove cursor widget from commandline, i.e. when user leaves command-line mode, the
  /// command-line content widget doesn't contain cursor any longer.
  ///
  /// # Returns
  /// It returns the removed cursor widget if exists, otherwise it returns `None`.
  pub fn remove_cursor(&mut self) -> Option<CommandLineNode> {
    match self.cursor_id {
      Some(cursor_id) => {
        debug_assert!(self.base.node(cursor_id).is_some());
        debug_assert!(self.base.parent_id(cursor_id).is_some());
        debug_assert_eq!(
          self.base.parent_id(cursor_id).unwrap(),
          self.input_id
        );
        self.cursor_id = None;
        let cursor_node = self.base.remove(cursor_id);
        debug_assert!(cursor_node.is_some());
        debug_assert!(matches!(
          cursor_node.as_ref().unwrap(),
          CommandLineNode::Cursor(_)
        ));
        cursor_node
      }
      None => {
        debug_assert!(self.cursor_id.is_none());
        debug_assert!(self.base.node(self.input_id).is_some());
        debug_assert!(self.base.children_ids(self.input_id).is_empty());
        None
      }
    }
  }

  /// Bounded move cursor by x(columns) and y(rows).
  ///
  /// # Panics
  /// It panics if cursor not exist.
  pub fn move_cursor_by(&mut self, x: isize, y: isize) -> Option<IRect> {
    let cursor_id = self.cursor_id.unwrap();
    self.base.bounded_move_by(cursor_id, x, y)
  }

  /// Bounded move cursor to position x(columns) and y(rows).
  ///
  /// # Panics
  /// It panics if cursor not exist.
  pub fn move_cursor_to(&mut self, x: isize, y: isize) -> Option<IRect> {
    let cursor_id = self.cursor_id.unwrap();
    self.base.bounded_move_to(cursor_id, x, y)
  }
}
// Cursor }