reovim-protocol 0.14.4

Wire protocol types for reovim client-server communication
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
//! Shared types for protocol messages.
//!
//! This module defines serializable types that represent editor state.
//! These are "snapshot" types designed for wire transmission, not
//! runtime manipulation.

use serde::{Deserialize, Serialize};

/// Cursor/text position in a buffer.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct Position {
    /// Line number (0-indexed).
    pub line: usize,
    /// Column number (0-indexed, byte offset).
    pub column: usize,
}

impl Position {
    /// Create a new position.
    #[must_use]
    pub const fn new(line: usize, column: usize) -> Self {
        Self { line, column }
    }
}

/// Buffer identifier.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct BufferId(pub usize);

impl From<usize> for BufferId {
    fn from(id: usize) -> Self {
        Self(id)
    }
}

impl From<BufferId> for usize {
    fn from(id: BufferId) -> Self {
        id.0
    }
}

/// Wire-format window identifier for serialization.
///
/// This is a thin wrapper for protocol serialization only.
/// Convert to/from kernel `WindowId` at RPC boundaries in the server layer.
///
/// `#[serde(transparent)]` ensures wire format is just `42`, not `{"0": 42}`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct WireWindowId(pub usize);

impl From<usize> for WireWindowId {
    fn from(id: usize) -> Self {
        Self(id)
    }
}

impl From<WireWindowId> for usize {
    fn from(id: WireWindowId) -> Self {
        id.0
    }
}

/// Wire-format layer identifier for serialization.
///
/// Represents a compositor layer in the protocol.
/// `#[serde(transparent)]` ensures wire format is just `42`, not `{"0": 42}`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct WireLayerId(pub usize);

impl From<usize> for WireLayerId {
    fn from(id: usize) -> Self {
        Self(id)
    }
}

impl From<WireLayerId> for usize {
    fn from(id: WireLayerId) -> Self {
        id.0
    }
}

/// Rectangle bounds for layout geometry.
///
/// Represents a window's position and size in terminal cells.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct WireRect {
    /// X position (column, 0-indexed).
    pub x: u16,
    /// Y position (row, 0-indexed).
    pub y: u16,
    /// Width in columns.
    pub width: u16,
    /// Height in rows.
    pub height: u16,
}

impl WireRect {
    /// Create a new rectangle.
    #[must_use]
    pub const fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Create a rectangle covering the full screen.
    #[must_use]
    pub const fn full_screen(width: u16, height: u16) -> Self {
        Self {
            x: 0,
            y: 0,
            width,
            height,
        }
    }
}

/// Zone type within a compositor layer.
///
/// Each layer has three zones with different stacking behavior.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum WireZone {
    /// Tiled zone - windows arranged in binary split tree.
    #[default]
    Tiled,
    /// Float zone - freely positioned floating windows.
    Float,
    /// Overlay zone - popups, menus, tooltips.
    Overlay,
}

/// Split direction for window splits.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum WireSplitDirection {
    /// Horizontal split (windows stacked vertically).
    Horizontal,
    /// Vertical split (windows side by side).
    Vertical,
}

/// Type of layout change that occurred.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum WireLayoutChangeKind {
    /// Window was split.
    Split {
        /// ID of the new window created by split.
        new_window: WireWindowId,
        /// Direction of the split.
        direction: WireSplitDirection,
    },
    /// Window was closed.
    Close {
        /// ID of the closed window.
        closed_window: WireWindowId,
        /// ID of the window that received focus (if any).
        new_focus: Option<WireWindowId>,
    },
    /// Focus changed to a different window.
    Focus {
        /// Previous focused window (if any).
        from: Option<WireWindowId>,
        /// New focused window.
        to: WireWindowId,
    },
    /// Window was resized.
    Resize {
        /// ID of the resized window.
        window: WireWindowId,
    },
    /// All windows were equalized.
    Equalize,
}

/// Window placement information in the layout.
///
/// Contains all information needed to render a window.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WireWindowPlacement {
    /// Window identifier.
    pub window_id: WireWindowId,
    /// Layer this window belongs to.
    pub layer_id: WireLayerId,
    /// Zone within the layer.
    pub zone: WireZone,
    /// Screen bounds (position and size).
    pub bounds: WireRect,
    /// Z-order for stacking (higher = on top).
    pub z_order: u16,
    /// Whether the window is visible.
    pub visible: bool,
    /// Whether the window can receive focus.
    pub focusable: bool,
    /// Buffer displayed in this window (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub buffer_id: Option<BufferId>,
}

/// Complete layout state for the screen.
///
/// Contains all windows with their positions, the focused window,
/// and screen dimensions.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct WireLayoutInfo {
    /// Screen dimensions.
    #[serde(default)]
    pub screen: WireRect,
    /// All windows with their placements.
    #[serde(default)]
    pub windows: Vec<WireWindowPlacement>,
    /// Currently focused window (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub focused_window: Option<WireWindowId>,
    /// Currently active layer (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_layer: Option<WireLayerId>,
    /// Total number of windows.
    #[serde(default)]
    pub window_count: usize,
}

impl WireLayoutInfo {
    /// Create a single-window layout covering the full screen.
    #[must_use]
    pub fn single_window(width: u16, height: u16, buffer_id: Option<BufferId>) -> Self {
        let window = WireWindowPlacement {
            window_id: WireWindowId(0),
            layer_id: WireLayerId(0),
            zone: WireZone::Tiled,
            bounds: WireRect::full_screen(width, height),
            z_order: 0,
            visible: true,
            focusable: true,
            buffer_id,
        };
        Self {
            screen: WireRect::full_screen(width, height),
            windows: vec![window],
            focused_window: Some(WireWindowId(0)),
            active_layer: Some(WireLayerId(0)),
            window_count: 1,
        }
    }

    /// Check if this is a multi-window layout.
    #[must_use]
    pub const fn is_multi_window(&self) -> bool {
        self.windows.len() > 1
    }

    /// Get the focused window placement.
    #[must_use]
    pub fn focused(&self) -> Option<&WireWindowPlacement> {
        self.focused_window
            .and_then(|id| self.windows.iter().find(|w| w.window_id == id))
    }
}

/// Selection mode.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SelectionMode {
    /// Character-wise selection (like vim's `v`).
    #[default]
    Character,
    /// Line-wise selection (like vim's `V`).
    Line,
    /// Block/rectangular selection (like vim's `Ctrl-V`).
    Block,
}

/// Screen content format for rendering.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ScreenFormat {
    /// Raw terminal output with ANSI escape codes.
    RawAnsi,
    /// Plain text with ANSI codes stripped.
    #[default]
    PlainText,
    /// Structured cell grid as JSON.
    CellGrid,
}

/// Mode information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModeInfo {
    /// Focus area (e.g., "Editor", "Explorer").
    pub focus: String,
    /// Edit mode (e.g., "Normal", "Insert", "Visual").
    pub edit_mode: String,
    /// Sub-mode (e.g., "None", "Command", "`OperatorPending`").
    pub sub_mode: String,
    /// Display string for statusline.
    pub display: String,
}

/// Cursor information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CursorInfo {
    /// Line number (0-indexed).
    pub line: usize,
    /// Column number (0-indexed).
    pub column: usize,
}

impl From<Position> for CursorInfo {
    fn from(pos: Position) -> Self {
        Self {
            line: pos.line,
            column: pos.column,
        }
    }
}

impl From<CursorInfo> for Position {
    fn from(info: CursorInfo) -> Self {
        Self {
            line: info.line,
            column: info.column,
        }
    }
}

/// Selection information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SelectionInfo {
    /// Whether selection is active.
    pub active: bool,
    /// Selection mode.
    pub mode: SelectionMode,
    /// Anchor position (where selection started).
    pub anchor: Position,
    /// Cursor position (current end of selection).
    pub cursor: Position,
}

/// Buffer information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BufferInfo {
    /// Buffer ID.
    pub id: usize,
    /// File path (if associated with a file).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_path: Option<String>,
    /// Whether the buffer has unsaved changes.
    pub modified: bool,
    /// Total line count.
    pub line_count: usize,
    /// Content type detected by codec pipeline (e.g., "text/utf-8", "binary/raw").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
    /// Whether the buffer is read-only (e.g., lossy codec decode).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub readonly: Option<bool>,
    /// Codec metadata for round-trip encoding awareness.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub codec_metadata: Option<CodecMetadataWire>,
}

/// Wire-format codec metadata for protocol transmission.
///
/// Contains the subset of codec metadata that clients may use for display
/// (e.g., showing line ending type or BOM status in the statusline).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CodecMetadataWire {
    /// Codec name that decoded this buffer (e.g., "utf-8").
    pub codec_name: String,
    /// Line ending style detected on open (e.g., "lf", "crlf").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub line_ending: Option<String>,
    /// Whether the file had a BOM (byte order mark).
    #[serde(default, skip_serializing_if = "is_false")]
    pub has_bom: bool,
}

/// Screen information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScreenInfo {
    /// Terminal width in columns.
    pub width: u16,
    /// Terminal height in rows.
    pub height: u16,
    /// Currently active buffer ID.
    pub active_buffer_id: BufferId,
    /// Currently active window ID (if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_window_id: Option<WireWindowId>,
    /// Total number of windows.
    pub window_count: usize,
}

/// Window information snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WindowInfo {
    /// Window ID.
    pub id: WireWindowId,
    /// Buffer displayed in this window.
    pub buffer_id: BufferId,
    /// Whether this window is active.
    pub is_active: bool,
    /// Cursor position within the window.
    pub cursor: Position,
}

/// Cell information for grid rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellInfo {
    /// Character to display.
    pub char: char,
    /// Foreground color (CSS hex like "#ff0000" or "ansi:9").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fg: Option<String>,
    /// Background color (CSS hex like "#ff0000" or "ansi:9").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bg: Option<String>,
    /// Bold attribute.
    #[serde(default, skip_serializing_if = "is_false")]
    pub bold: bool,
    /// Italic attribute.
    #[serde(default, skip_serializing_if = "is_false")]
    pub italic: bool,
    /// Underline attribute.
    #[serde(default, skip_serializing_if = "is_false")]
    pub underline: bool,
}

/// Helper for serde's `skip_serializing_if`.
/// Note: serde requires `&T` signature, hence the reference.
#[allow(clippy::trivially_copy_pass_by_ref)]
const fn is_false(b: &bool) -> bool {
    !*b
}

impl Default for CellInfo {
    fn default() -> Self {
        Self {
            char: ' ',
            fg: None,
            bg: None,
            bold: false,
            italic: false,
            underline: false,
        }
    }
}

#[cfg(test)]
#[path = "types_tests.rs"]
mod tests;