Skip to main content

fresh_core/
api.rs

1//! Plugin API: Safe interface for plugins to interact with the editor
2//!
3//! This module provides a safe, controlled API for plugins (Lua, WASM, etc.)
4//! to interact with the editor without direct access to internal state.
5//!
6//! # Type Safety Architecture
7//!
8//! Rust structs in this module serve as the **single source of truth** for the
9//! TypeScript plugin API. The type safety system works as follows:
10//!
11//! ```text
12//! Rust struct                  Generated TypeScript
13//! ───────────                  ────────────────────
14//! #[derive(TS, Deserialize)]   type ActionPopupOptions = {
15//! #[serde(deny_unknown_fields)]    id: string;
16//! struct ActionPopupOptions {      title: string;
17//!     id: String,                  message: string;
18//!     title: String,               actions: TsActionPopupAction[];
19//!     ...                      };
20//! }
21//! ```
22//!
23//! ## Key Patterns
24//!
25//! 1. **`#[derive(TS)]`** - Generates TypeScript type definitions via ts-rs
26//! 2. **`#[serde(deny_unknown_fields)]`** - Rejects typos/unknown fields at runtime
27//! 3. **`impl FromJs`** - Bridges rquickjs values to typed Rust structs
28//!
29//! ## Validation Layers
30//!
31//! | Layer                  | What it catches                          |
32//! |------------------------|------------------------------------------|
33//! | TypeScript compile     | Wrong field names, missing required fields |
34//! | Rust runtime (serde)   | Typos like `popup_id` instead of `id`    |
35//! | Rust compile           | Type mismatches in method signatures     |
36//!
37//! ## Limitations & Tradeoffs
38//!
39//! - **Manual parsing for complex types**: Some methods (e.g., `submitViewTransform`)
40//!   still use manual object parsing due to enum serialization complexity
41//! - **Two-step deserialization**: Complex nested structs may need
42//!   `rquickjs::Value → serde_json::Value → typed struct` due to rquickjs_serde limits
43//! - **Duplicate attributes**: Both `#[serde(...)]` and `#[ts(...)]` needed since
44//!   they control different things (runtime serialization vs compile-time codegen)
45
46use crate::command::{Command, Suggestion};
47use crate::file_explorer::FileExplorerDecoration;
48use crate::hooks::{HookCallback, HookRegistry};
49use crate::menu::{Menu, MenuItem};
50use crate::overlay::{OverlayHandle, OverlayNamespace};
51use crate::text_property::{TextProperty, TextPropertyEntry};
52use crate::BufferId;
53use crate::SplitId;
54use lsp_types;
55use serde::{Deserialize, Serialize};
56use serde_json::Value as JsonValue;
57use std::collections::HashMap;
58use std::ops::Range;
59use std::path::PathBuf;
60use std::sync::{Arc, RwLock};
61use ts_rs::TS;
62
63/// Minimal command registry for PluginApi.
64/// This is a stub that provides basic command storage for plugin use.
65/// The editor's full CommandRegistry lives in fresh-editor.
66pub struct CommandRegistry {
67    commands: std::sync::RwLock<Vec<Command>>,
68}
69
70impl CommandRegistry {
71    /// Create a new empty command registry
72    pub fn new() -> Self {
73        Self {
74            commands: std::sync::RwLock::new(Vec::new()),
75        }
76    }
77
78    /// Register a command
79    pub fn register(&self, command: Command) {
80        let mut commands = self.commands.write().unwrap();
81        commands.retain(|c| c.name != command.name);
82        commands.push(command);
83    }
84
85    /// Unregister a command by name  
86    pub fn unregister(&self, name: &str) {
87        let mut commands = self.commands.write().unwrap();
88        commands.retain(|c| c.name != name);
89    }
90}
91
92impl Default for CommandRegistry {
93    fn default() -> Self {
94        Self::new()
95    }
96}
97
98/// A callback ID for JavaScript promises in the plugin runtime.
99///
100/// This newtype distinguishes JS promise callbacks (resolved via `resolve_callback`)
101/// from Rust oneshot channel IDs (resolved via `send_plugin_response`).
102/// Using a newtype prevents accidentally mixing up these two callback mechanisms.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
104#[ts(export)]
105pub struct JsCallbackId(pub u64);
106
107impl JsCallbackId {
108    /// Create a new JS callback ID
109    pub fn new(id: u64) -> Self {
110        Self(id)
111    }
112
113    /// Get the underlying u64 value
114    pub fn as_u64(self) -> u64 {
115        self.0
116    }
117}
118
119impl From<u64> for JsCallbackId {
120    fn from(id: u64) -> Self {
121        Self(id)
122    }
123}
124
125impl From<JsCallbackId> for u64 {
126    fn from(id: JsCallbackId) -> u64 {
127        id.0
128    }
129}
130
131impl std::fmt::Display for JsCallbackId {
132    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133        write!(f, "{}", self.0)
134    }
135}
136
137/// Result of creating a virtual buffer
138#[derive(Debug, Clone, Serialize, Deserialize, TS)]
139#[serde(rename_all = "camelCase")]
140#[ts(export, rename_all = "camelCase")]
141pub struct VirtualBufferResult {
142    /// The created buffer ID
143    #[ts(type = "number")]
144    pub buffer_id: u64,
145    /// The split ID (if created in a new split)
146    #[ts(type = "number | null")]
147    pub split_id: Option<u64>,
148}
149
150/// Response from the editor for async plugin operations
151#[derive(Debug, Clone, Serialize, Deserialize, TS)]
152#[ts(export)]
153pub enum PluginResponse {
154    /// Response to CreateVirtualBufferInSplit with the created buffer ID and split ID
155    VirtualBufferCreated {
156        request_id: u64,
157        buffer_id: BufferId,
158        split_id: Option<SplitId>,
159    },
160    /// Response to a plugin-initiated LSP request
161    LspRequest {
162        request_id: u64,
163        #[ts(type = "any")]
164        result: Result<JsonValue, String>,
165    },
166    /// Response to RequestHighlights
167    HighlightsComputed {
168        request_id: u64,
169        spans: Vec<TsHighlightSpan>,
170    },
171    /// Response to GetBufferText with the text content
172    BufferText {
173        request_id: u64,
174        text: Result<String, String>,
175    },
176    /// Response to GetLineStartPosition with the byte offset
177    LineStartPosition {
178        request_id: u64,
179        /// None if line is out of range, Some(offset) for valid line
180        position: Option<usize>,
181    },
182    /// Response to CreateCompositeBuffer with the created buffer ID
183    CompositeBufferCreated {
184        request_id: u64,
185        buffer_id: BufferId,
186    },
187}
188
189/// Messages sent from async plugin tasks to the synchronous main loop
190#[derive(Debug, Clone, Serialize, Deserialize, TS)]
191#[ts(export)]
192pub enum PluginAsyncMessage {
193    /// Plugin process completed with output
194    ProcessOutput {
195        /// Unique ID for this process
196        process_id: u64,
197        /// Standard output
198        stdout: String,
199        /// Standard error
200        stderr: String,
201        /// Exit code
202        exit_code: i32,
203    },
204    /// Plugin delay/timer completed
205    DelayComplete {
206        /// Callback ID to resolve
207        callback_id: u64,
208    },
209    /// Background process stdout data
210    ProcessStdout { process_id: u64, data: String },
211    /// Background process stderr data
212    ProcessStderr { process_id: u64, data: String },
213    /// Background process exited
214    ProcessExit {
215        process_id: u64,
216        callback_id: u64,
217        exit_code: i32,
218    },
219    /// Response for a plugin-initiated LSP request
220    LspResponse {
221        language: String,
222        request_id: u64,
223        #[ts(type = "any")]
224        result: Result<JsonValue, String>,
225    },
226    /// Generic plugin response (e.g., GetBufferText result)
227    PluginResponse(crate::api::PluginResponse),
228}
229
230/// Information about a cursor in the editor
231#[derive(Debug, Clone, Serialize, Deserialize, TS)]
232#[ts(export)]
233pub struct CursorInfo {
234    /// Byte position of the cursor
235    pub position: usize,
236    /// Selection range (if any)
237    #[cfg_attr(
238        feature = "plugins",
239        ts(type = "{ start: number; end: number } | null")
240    )]
241    pub selection: Option<Range<usize>>,
242}
243
244/// Specification for an action to execute, with optional repeat count
245#[derive(Debug, Clone, Serialize, Deserialize, TS)]
246#[serde(deny_unknown_fields)]
247#[ts(export)]
248pub struct ActionSpec {
249    /// Action name (e.g., "move_word_right", "delete_line")
250    pub action: String,
251    /// Number of times to repeat the action (default 1)
252    #[serde(default = "default_action_count")]
253    pub count: u32,
254}
255
256fn default_action_count() -> u32 {
257    1
258}
259
260/// Information about a buffer
261#[derive(Debug, Clone, Serialize, Deserialize, TS)]
262#[ts(export)]
263pub struct BufferInfo {
264    /// Buffer ID
265    #[ts(type = "number")]
266    pub id: BufferId,
267    /// File path (if any)
268    #[serde(serialize_with = "serialize_path")]
269    #[ts(type = "string")]
270    pub path: Option<PathBuf>,
271    /// Whether the buffer has been modified
272    pub modified: bool,
273    /// Length of buffer in bytes
274    pub length: usize,
275}
276
277fn serialize_path<S: serde::Serializer>(path: &Option<PathBuf>, s: S) -> Result<S::Ok, S::Error> {
278    s.serialize_str(
279        &path
280            .as_ref()
281            .map(|p| p.to_string_lossy().to_string())
282            .unwrap_or_default(),
283    )
284}
285
286/// Serialize ranges as [start, end] tuples for JS compatibility
287fn serialize_ranges_as_tuples<S>(ranges: &[Range<usize>], serializer: S) -> Result<S::Ok, S::Error>
288where
289    S: serde::Serializer,
290{
291    use serde::ser::SerializeSeq;
292    let mut seq = serializer.serialize_seq(Some(ranges.len()))?;
293    for range in ranges {
294        seq.serialize_element(&(range.start, range.end))?;
295    }
296    seq.end()
297}
298
299/// Serialize optional ranges as [start, end] tuples for JS compatibility
300fn serialize_opt_ranges_as_tuples<S>(
301    ranges: &Option<Vec<Range<usize>>>,
302    serializer: S,
303) -> Result<S::Ok, S::Error>
304where
305    S: serde::Serializer,
306{
307    match ranges {
308        Some(ranges) => {
309            use serde::ser::SerializeSeq;
310            let mut seq = serializer.serialize_seq(Some(ranges.len()))?;
311            for range in ranges {
312                seq.serialize_element(&(range.start, range.end))?;
313            }
314            seq.end()
315        }
316        None => serializer.serialize_none(),
317    }
318}
319
320/// Diff between current buffer content and last saved snapshot
321#[derive(Debug, Clone, Serialize, Deserialize, TS)]
322#[ts(export)]
323pub struct BufferSavedDiff {
324    pub equal: bool,
325    #[serde(serialize_with = "serialize_ranges_as_tuples")]
326    #[ts(type = "Array<[number, number]>")]
327    pub byte_ranges: Vec<Range<usize>>,
328    #[serde(serialize_with = "serialize_opt_ranges_as_tuples")]
329    #[ts(type = "Array<[number, number]> | null")]
330    pub line_ranges: Option<Vec<Range<usize>>>,
331}
332
333/// Information about the viewport
334#[derive(Debug, Clone, Serialize, Deserialize, TS)]
335#[serde(rename_all = "camelCase")]
336#[ts(export, rename_all = "camelCase")]
337pub struct ViewportInfo {
338    /// Byte position of the first visible line
339    pub top_byte: usize,
340    /// Left column offset (horizontal scroll)
341    pub left_column: usize,
342    /// Viewport width
343    pub width: u16,
344    /// Viewport height
345    pub height: u16,
346}
347
348/// Layout hints supplied by plugins (e.g., Compose mode)
349#[derive(Debug, Clone, Serialize, Deserialize, TS)]
350#[serde(rename_all = "camelCase")]
351#[ts(export, rename_all = "camelCase")]
352pub struct LayoutHints {
353    /// Optional compose width for centering/wrapping
354    pub compose_width: Option<u16>,
355    /// Optional column guides for aligned tables
356    pub column_guides: Option<Vec<u16>>,
357}
358
359// ============================================================================
360// Overlay Types with Theme Support
361// ============================================================================
362
363/// Color specification that can be either RGB values or a theme key.
364///
365/// Theme keys reference colors from the current theme, e.g.:
366/// - "ui.status_bar_bg" - UI status bar background
367/// - "editor.selection_bg" - Editor selection background
368/// - "syntax.keyword" - Syntax highlighting for keywords
369/// - "diagnostic.error" - Error diagnostic color
370///
371/// When a theme key is used, the color is resolved at render time,
372/// so overlays automatically update when the theme changes.
373#[derive(Debug, Clone, Serialize, Deserialize, TS)]
374#[serde(untagged)]
375#[ts(export)]
376pub enum OverlayColorSpec {
377    /// RGB color as [r, g, b] array
378    #[ts(type = "[number, number, number]")]
379    Rgb(u8, u8, u8),
380    /// Theme key reference (e.g., "ui.status_bar_bg")
381    ThemeKey(String),
382}
383
384impl OverlayColorSpec {
385    /// Create an RGB color spec
386    pub fn rgb(r: u8, g: u8, b: u8) -> Self {
387        Self::Rgb(r, g, b)
388    }
389
390    /// Create a theme key color spec
391    pub fn theme_key(key: impl Into<String>) -> Self {
392        Self::ThemeKey(key.into())
393    }
394
395    /// Convert to RGB if this is an RGB spec, None if it's a theme key
396    pub fn as_rgb(&self) -> Option<(u8, u8, u8)> {
397        match self {
398            Self::Rgb(r, g, b) => Some((*r, *g, *b)),
399            Self::ThemeKey(_) => None,
400        }
401    }
402
403    /// Get the theme key if this is a theme key spec
404    pub fn as_theme_key(&self) -> Option<&str> {
405        match self {
406            Self::ThemeKey(key) => Some(key),
407            Self::Rgb(_, _, _) => None,
408        }
409    }
410}
411
412/// Options for adding an overlay with theme support.
413///
414/// This struct provides a type-safe way to specify overlay styling
415/// with optional theme key references for colors.
416#[derive(Debug, Clone, Serialize, Deserialize, TS)]
417#[serde(deny_unknown_fields, rename_all = "camelCase")]
418#[ts(export, rename_all = "camelCase")]
419pub struct OverlayOptions {
420    /// Foreground color - RGB array or theme key string
421    #[serde(default, skip_serializing_if = "Option::is_none")]
422    pub fg: Option<OverlayColorSpec>,
423
424    /// Background color - RGB array or theme key string
425    #[serde(default, skip_serializing_if = "Option::is_none")]
426    pub bg: Option<OverlayColorSpec>,
427
428    /// Whether to render with underline
429    #[serde(default)]
430    pub underline: bool,
431
432    /// Whether to render in bold
433    #[serde(default)]
434    pub bold: bool,
435
436    /// Whether to render in italic
437    #[serde(default)]
438    pub italic: bool,
439
440    /// Whether to extend background color to end of line
441    #[serde(default)]
442    pub extend_to_line_end: bool,
443}
444
445impl Default for OverlayOptions {
446    fn default() -> Self {
447        Self {
448            fg: None,
449            bg: None,
450            underline: false,
451            bold: false,
452            italic: false,
453            extend_to_line_end: false,
454        }
455    }
456}
457
458// ============================================================================
459// Composite Buffer Configuration (for multi-buffer single-tab views)
460// ============================================================================
461
462/// Layout configuration for composite buffers
463#[derive(Debug, Clone, Serialize, Deserialize, TS)]
464#[serde(deny_unknown_fields)]
465#[ts(export, rename = "TsCompositeLayoutConfig")]
466pub struct CompositeLayoutConfig {
467    /// Layout type: "side-by-side", "stacked", or "unified"
468    #[serde(rename = "type")]
469    #[ts(rename = "type")]
470    pub layout_type: String,
471    /// Width ratios for side-by-side (e.g., [0.5, 0.5])
472    #[serde(default)]
473    pub ratios: Option<Vec<f32>>,
474    /// Show separator between panes
475    #[serde(default = "default_true", rename = "showSeparator")]
476    #[ts(rename = "showSeparator")]
477    pub show_separator: bool,
478    /// Spacing for stacked layout
479    #[serde(default)]
480    pub spacing: Option<u16>,
481}
482
483fn default_true() -> bool {
484    true
485}
486
487/// Source pane configuration for composite buffers
488#[derive(Debug, Clone, Serialize, Deserialize, TS)]
489#[serde(deny_unknown_fields)]
490#[ts(export, rename = "TsCompositeSourceConfig")]
491pub struct CompositeSourceConfig {
492    /// Buffer ID of the source buffer (required)
493    #[serde(rename = "bufferId")]
494    #[ts(rename = "bufferId")]
495    pub buffer_id: usize,
496    /// Label for this pane (e.g., "OLD", "NEW")
497    pub label: String,
498    /// Whether this pane is editable
499    #[serde(default)]
500    pub editable: bool,
501    /// Style configuration
502    #[serde(default)]
503    pub style: Option<CompositePaneStyle>,
504}
505
506/// Style configuration for a composite pane
507#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
508#[serde(deny_unknown_fields)]
509#[ts(export, rename = "TsCompositePaneStyle")]
510pub struct CompositePaneStyle {
511    /// Background color for added lines (RGB)
512    /// Using [u8; 3] instead of (u8, u8, u8) for better rquickjs_serde compatibility
513    #[serde(default, rename = "addBg")]
514    #[ts(rename = "addBg", type = "[number, number, number] | null")]
515    pub add_bg: Option<[u8; 3]>,
516    /// Background color for removed lines (RGB)
517    #[serde(default, rename = "removeBg")]
518    #[ts(rename = "removeBg", type = "[number, number, number] | null")]
519    pub remove_bg: Option<[u8; 3]>,
520    /// Background color for modified lines (RGB)
521    #[serde(default, rename = "modifyBg")]
522    #[ts(rename = "modifyBg", type = "[number, number, number] | null")]
523    pub modify_bg: Option<[u8; 3]>,
524    /// Gutter style: "line-numbers", "diff-markers", "both", or "none"
525    #[serde(default, rename = "gutterStyle")]
526    #[ts(rename = "gutterStyle")]
527    pub gutter_style: Option<String>,
528}
529
530/// Diff hunk for composite buffer alignment
531#[derive(Debug, Clone, Serialize, Deserialize, TS)]
532#[serde(deny_unknown_fields)]
533#[ts(export, rename = "TsCompositeHunk")]
534pub struct CompositeHunk {
535    /// Starting line in old buffer (0-indexed)
536    #[serde(rename = "oldStart")]
537    #[ts(rename = "oldStart")]
538    pub old_start: usize,
539    /// Number of lines in old buffer
540    #[serde(rename = "oldCount")]
541    #[ts(rename = "oldCount")]
542    pub old_count: usize,
543    /// Starting line in new buffer (0-indexed)
544    #[serde(rename = "newStart")]
545    #[ts(rename = "newStart")]
546    pub new_start: usize,
547    /// Number of lines in new buffer
548    #[serde(rename = "newCount")]
549    #[ts(rename = "newCount")]
550    pub new_count: usize,
551}
552
553/// Options for creating a composite buffer (used by plugin API)
554#[derive(Debug, Clone, Serialize, Deserialize, TS)]
555#[serde(deny_unknown_fields)]
556#[ts(export, rename = "TsCreateCompositeBufferOptions")]
557pub struct CreateCompositeBufferOptions {
558    /// Buffer name (displayed in tabs/title)
559    #[serde(default)]
560    pub name: String,
561    /// Mode for keybindings
562    #[serde(default)]
563    pub mode: String,
564    /// Layout configuration
565    pub layout: CompositeLayoutConfig,
566    /// Source pane configurations
567    pub sources: Vec<CompositeSourceConfig>,
568    /// Diff hunks for alignment (optional)
569    #[serde(default)]
570    pub hunks: Option<Vec<CompositeHunk>>,
571}
572
573/// Wire-format view token kind (serialized for plugin transforms)
574#[derive(Debug, Clone, Serialize, Deserialize, TS)]
575#[ts(export)]
576pub enum ViewTokenWireKind {
577    Text(String),
578    Newline,
579    Space,
580    /// Visual line break inserted by wrapping (not from source)
581    /// Always has source_offset: None
582    Break,
583    /// A single binary byte that should be rendered as <XX>
584    /// Used in binary file mode to ensure cursor positioning works correctly
585    /// (all 4 display chars of <XX> map to the same source byte)
586    BinaryByte(u8),
587}
588
589/// Styling for view tokens (used for injected annotations)
590///
591/// This allows plugins to specify styling for tokens that don't have a source
592/// mapping (sourceOffset: None), such as annotation headers in git blame.
593/// For tokens with sourceOffset: Some(_), syntax highlighting is applied instead.
594#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
595#[serde(deny_unknown_fields)]
596#[ts(export)]
597pub struct ViewTokenStyle {
598    /// Foreground color as RGB tuple
599    #[serde(default)]
600    #[ts(type = "[number, number, number] | null")]
601    pub fg: Option<(u8, u8, u8)>,
602    /// Background color as RGB tuple
603    #[serde(default)]
604    #[ts(type = "[number, number, number] | null")]
605    pub bg: Option<(u8, u8, u8)>,
606    /// Whether to render in bold
607    #[serde(default)]
608    pub bold: bool,
609    /// Whether to render in italic
610    #[serde(default)]
611    pub italic: bool,
612}
613
614/// Wire-format view token with optional source mapping and styling
615#[derive(Debug, Clone, Serialize, Deserialize, TS)]
616#[serde(deny_unknown_fields)]
617#[ts(export)]
618pub struct ViewTokenWire {
619    /// Source byte offset in the buffer. None for injected content (annotations).
620    #[ts(type = "number | null")]
621    pub source_offset: Option<usize>,
622    /// The token content
623    pub kind: ViewTokenWireKind,
624    /// Optional styling for injected content (only used when source_offset is None)
625    #[serde(default)]
626    #[ts(optional)]
627    pub style: Option<ViewTokenStyle>,
628}
629
630/// Transformed view stream payload (plugin-provided)
631#[derive(Debug, Clone, Serialize, Deserialize, TS)]
632#[ts(export)]
633pub struct ViewTransformPayload {
634    /// Byte range this transform applies to (viewport)
635    pub range: Range<usize>,
636    /// Tokens in wire format
637    pub tokens: Vec<ViewTokenWire>,
638    /// Layout hints
639    pub layout_hints: Option<LayoutHints>,
640}
641
642/// Snapshot of editor state for plugin queries
643/// This is updated by the editor on each loop iteration
644#[derive(Debug, Clone, Serialize, Deserialize, TS)]
645#[ts(export)]
646pub struct EditorStateSnapshot {
647    /// Currently active buffer ID
648    pub active_buffer_id: BufferId,
649    /// Currently active split ID
650    pub active_split_id: usize,
651    /// Information about all open buffers
652    pub buffers: HashMap<BufferId, BufferInfo>,
653    /// Diff vs last saved snapshot for each buffer (line counts may be unknown)
654    pub buffer_saved_diffs: HashMap<BufferId, BufferSavedDiff>,
655    /// Primary cursor position for the active buffer
656    pub primary_cursor: Option<CursorInfo>,
657    /// All cursor positions for the active buffer
658    pub all_cursors: Vec<CursorInfo>,
659    /// Viewport information for the active buffer
660    pub viewport: Option<ViewportInfo>,
661    /// Cursor positions per buffer (for buffers other than active)
662    pub buffer_cursor_positions: HashMap<BufferId, usize>,
663    /// Text properties per buffer (for virtual buffers with properties)
664    pub buffer_text_properties: HashMap<BufferId, Vec<TextProperty>>,
665    /// Selected text from the primary cursor (if any selection exists)
666    /// This is populated on each update to avoid needing full buffer access
667    pub selected_text: Option<String>,
668    /// Internal clipboard content (for plugins that need clipboard access)
669    pub clipboard: String,
670    /// Editor's working directory (for file operations and spawning processes)
671    pub working_dir: PathBuf,
672    /// LSP diagnostics per file URI
673    /// Maps file URI string to Vec of diagnostics for that file
674    #[ts(type = "any")]
675    pub diagnostics: HashMap<String, Vec<lsp_types::Diagnostic>>,
676    /// Runtime config as serde_json::Value (merged user config + defaults)
677    /// This is the runtime config, not just the user's config file
678    #[ts(type = "any")]
679    pub config: serde_json::Value,
680    /// User config as serde_json::Value (only what's in the user's config file)
681    /// Fields not present here are using default values
682    #[ts(type = "any")]
683    pub user_config: serde_json::Value,
684    /// Global editor mode for modal editing (e.g., "vi-normal", "vi-insert")
685    /// When set, this mode's keybindings take precedence over normal key handling
686    pub editor_mode: Option<String>,
687}
688
689impl EditorStateSnapshot {
690    pub fn new() -> Self {
691        Self {
692            active_buffer_id: BufferId(0),
693            active_split_id: 0,
694            buffers: HashMap::new(),
695            buffer_saved_diffs: HashMap::new(),
696            primary_cursor: None,
697            all_cursors: Vec::new(),
698            viewport: None,
699            buffer_cursor_positions: HashMap::new(),
700            buffer_text_properties: HashMap::new(),
701            selected_text: None,
702            clipboard: String::new(),
703            working_dir: std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
704            diagnostics: HashMap::new(),
705            config: serde_json::Value::Null,
706            user_config: serde_json::Value::Null,
707            editor_mode: None,
708        }
709    }
710}
711
712impl Default for EditorStateSnapshot {
713    fn default() -> Self {
714        Self::new()
715    }
716}
717
718/// Position for inserting menu items or menus
719#[derive(Debug, Clone, Serialize, Deserialize, TS)]
720#[ts(export)]
721pub enum MenuPosition {
722    /// Add at the beginning
723    Top,
724    /// Add at the end
725    Bottom,
726    /// Add before a specific label
727    Before(String),
728    /// Add after a specific label
729    After(String),
730}
731
732/// Plugin command - allows plugins to send commands to the editor
733#[derive(Debug, Clone, Serialize, Deserialize, TS)]
734#[ts(export)]
735pub enum PluginCommand {
736    /// Insert text at a position in a buffer
737    InsertText {
738        buffer_id: BufferId,
739        position: usize,
740        text: String,
741    },
742
743    /// Delete a range of text from a buffer
744    DeleteRange {
745        buffer_id: BufferId,
746        range: Range<usize>,
747    },
748
749    /// Add an overlay to a buffer, returns handle via response channel
750    ///
751    /// Colors can be specified as RGB tuples or theme keys. When theme keys
752    /// are provided, they take precedence and are resolved at render time.
753    AddOverlay {
754        buffer_id: BufferId,
755        namespace: Option<OverlayNamespace>,
756        range: Range<usize>,
757        /// Overlay styling options (colors, modifiers, etc.)
758        options: OverlayOptions,
759    },
760
761    /// Remove an overlay by its opaque handle
762    RemoveOverlay {
763        buffer_id: BufferId,
764        handle: OverlayHandle,
765    },
766
767    /// Set status message
768    SetStatus { message: String },
769
770    /// Apply a theme by name
771    ApplyTheme { theme_name: String },
772
773    /// Reload configuration from file
774    /// After a plugin saves config changes, it should call this to reload the config
775    ReloadConfig,
776
777    /// Register a custom command
778    RegisterCommand { command: Command },
779
780    /// Unregister a command by name
781    UnregisterCommand { name: String },
782
783    /// Open a file in the editor (in background, without switching focus)
784    OpenFileInBackground { path: PathBuf },
785
786    /// Insert text at the current cursor position in the active buffer
787    InsertAtCursor { text: String },
788
789    /// Spawn an async process
790    SpawnProcess {
791        command: String,
792        args: Vec<String>,
793        cwd: Option<String>,
794        callback_id: JsCallbackId,
795    },
796
797    /// Delay/sleep for a duration (async, resolves callback when done)
798    Delay {
799        callback_id: JsCallbackId,
800        duration_ms: u64,
801    },
802
803    /// Spawn a long-running background process
804    /// Unlike SpawnProcess, this returns immediately with a process handle
805    /// and provides streaming output via hooks
806    SpawnBackgroundProcess {
807        /// Unique ID for this process (generated by plugin runtime)
808        process_id: u64,
809        /// Command to execute
810        command: String,
811        /// Arguments to pass
812        args: Vec<String>,
813        /// Working directory (optional)
814        cwd: Option<String>,
815        /// Callback ID to call when process exits
816        callback_id: JsCallbackId,
817    },
818
819    /// Kill a background process by ID
820    KillBackgroundProcess { process_id: u64 },
821
822    /// Wait for a process to complete and get its result
823    /// Used with processes started via SpawnProcess
824    SpawnProcessWait {
825        /// Process ID to wait for
826        process_id: u64,
827        /// Callback ID for async response
828        callback_id: JsCallbackId,
829    },
830
831    /// Set layout hints for a buffer/viewport
832    SetLayoutHints {
833        buffer_id: BufferId,
834        split_id: Option<SplitId>,
835        range: Range<usize>,
836        hints: LayoutHints,
837    },
838
839    /// Enable/disable line numbers for a buffer
840    SetLineNumbers { buffer_id: BufferId, enabled: bool },
841
842    /// Submit a transformed view stream for a viewport
843    SubmitViewTransform {
844        buffer_id: BufferId,
845        split_id: Option<SplitId>,
846        payload: ViewTransformPayload,
847    },
848
849    /// Clear view transform for a buffer/split (returns to normal rendering)
850    ClearViewTransform {
851        buffer_id: BufferId,
852        split_id: Option<SplitId>,
853    },
854
855    /// Remove all overlays from a buffer
856    ClearAllOverlays { buffer_id: BufferId },
857
858    /// Remove all overlays in a namespace
859    ClearNamespace {
860        buffer_id: BufferId,
861        namespace: OverlayNamespace,
862    },
863
864    /// Remove all overlays that overlap with a byte range
865    /// Used for targeted invalidation when content in a range changes
866    ClearOverlaysInRange {
867        buffer_id: BufferId,
868        start: usize,
869        end: usize,
870    },
871
872    /// Add virtual text (inline text that doesn't exist in the buffer)
873    /// Used for color swatches, type hints, parameter hints, etc.
874    AddVirtualText {
875        buffer_id: BufferId,
876        virtual_text_id: String,
877        position: usize,
878        text: String,
879        color: (u8, u8, u8),
880        use_bg: bool, // true = use color as background, false = use as foreground
881        before: bool, // true = before char, false = after char
882    },
883
884    /// Remove a virtual text by ID
885    RemoveVirtualText {
886        buffer_id: BufferId,
887        virtual_text_id: String,
888    },
889
890    /// Remove virtual texts whose ID starts with the given prefix
891    RemoveVirtualTextsByPrefix { buffer_id: BufferId, prefix: String },
892
893    /// Clear all virtual texts from a buffer
894    ClearVirtualTexts { buffer_id: BufferId },
895
896    /// Add a virtual LINE (full line above/below a position)
897    /// Used for git blame headers, code coverage, inline documentation, etc.
898    /// These lines do NOT show line numbers in the gutter.
899    AddVirtualLine {
900        buffer_id: BufferId,
901        /// Byte position to anchor the line to
902        position: usize,
903        /// Full line content to display
904        text: String,
905        /// Foreground color (RGB)
906        fg_color: (u8, u8, u8),
907        /// Background color (RGB), None = transparent
908        bg_color: Option<(u8, u8, u8)>,
909        /// true = above the line containing position, false = below
910        above: bool,
911        /// Namespace for bulk removal (e.g., "git-blame")
912        namespace: String,
913        /// Priority for ordering multiple lines at same position (higher = later)
914        priority: i32,
915    },
916
917    /// Clear all virtual texts in a namespace
918    /// This is the primary way to remove a plugin's virtual lines before updating them.
919    ClearVirtualTextNamespace {
920        buffer_id: BufferId,
921        namespace: String,
922    },
923
924    /// Refresh lines for a buffer (clear seen_lines cache to re-trigger lines_changed hook)
925    RefreshLines { buffer_id: BufferId },
926
927    /// Set a line indicator in the gutter's indicator column
928    /// Used for git gutter, breakpoints, bookmarks, etc.
929    SetLineIndicator {
930        buffer_id: BufferId,
931        /// Line number (0-indexed)
932        line: usize,
933        /// Namespace for grouping (e.g., "git-gutter", "breakpoints")
934        namespace: String,
935        /// Symbol to display (e.g., "│", "●", "★")
936        symbol: String,
937        /// Color as RGB tuple
938        color: (u8, u8, u8),
939        /// Priority for display when multiple indicators exist (higher wins)
940        priority: i32,
941    },
942
943    /// Clear all line indicators for a specific namespace
944    ClearLineIndicators {
945        buffer_id: BufferId,
946        /// Namespace to clear (e.g., "git-gutter")
947        namespace: String,
948    },
949
950    /// Set file explorer decorations for a namespace
951    SetFileExplorerDecorations {
952        /// Namespace for grouping (e.g., "git-status")
953        namespace: String,
954        /// Decorations to apply
955        decorations: Vec<FileExplorerDecoration>,
956    },
957
958    /// Clear file explorer decorations for a namespace
959    ClearFileExplorerDecorations {
960        /// Namespace to clear (e.g., "git-status")
961        namespace: String,
962    },
963
964    /// Open a file at a specific line and column
965    /// Line and column are 1-indexed to match git grep output
966    OpenFileAtLocation {
967        path: PathBuf,
968        line: Option<usize>,   // 1-indexed, None = go to start
969        column: Option<usize>, // 1-indexed, None = go to line start
970    },
971
972    /// Open a file in a specific split at a given line and column
973    /// Line and column are 1-indexed to match git grep output
974    OpenFileInSplit {
975        split_id: usize,
976        path: PathBuf,
977        line: Option<usize>,   // 1-indexed, None = go to start
978        column: Option<usize>, // 1-indexed, None = go to line start
979    },
980
981    /// Start a prompt (minibuffer) with a custom type identifier
982    /// This allows plugins to create interactive prompts
983    StartPrompt {
984        label: String,
985        prompt_type: String, // e.g., "git-grep", "git-find-file"
986    },
987
988    /// Start a prompt with pre-filled initial value
989    StartPromptWithInitial {
990        label: String,
991        prompt_type: String,
992        initial_value: String,
993    },
994
995    /// Start an async prompt that returns result via callback
996    /// The callback_id is used to resolve the promise when the prompt is confirmed or cancelled
997    StartPromptAsync {
998        label: String,
999        initial_value: String,
1000        callback_id: JsCallbackId,
1001    },
1002
1003    /// Update the suggestions list for the current prompt
1004    /// Uses the editor's Suggestion type
1005    SetPromptSuggestions { suggestions: Vec<Suggestion> },
1006
1007    /// Add a menu item to an existing menu
1008    /// Add a menu item to an existing menu
1009    AddMenuItem {
1010        menu_label: String,
1011        item: MenuItem,
1012        position: MenuPosition,
1013    },
1014
1015    /// Add a new top-level menu
1016    AddMenu { menu: Menu, position: MenuPosition },
1017
1018    /// Remove a menu item from a menu
1019    RemoveMenuItem {
1020        menu_label: String,
1021        item_label: String,
1022    },
1023
1024    /// Remove a top-level menu
1025    RemoveMenu { menu_label: String },
1026
1027    /// Create a new virtual buffer (not backed by a file)
1028    CreateVirtualBuffer {
1029        /// Display name (e.g., "*Diagnostics*")
1030        name: String,
1031        /// Mode name for buffer-local keybindings (e.g., "diagnostics-list")
1032        mode: String,
1033        /// Whether the buffer is read-only
1034        read_only: bool,
1035    },
1036
1037    /// Create a virtual buffer and set its content in one operation
1038    /// This is preferred over CreateVirtualBuffer + SetVirtualBufferContent
1039    /// because it doesn't require tracking the buffer ID
1040    CreateVirtualBufferWithContent {
1041        /// Display name (e.g., "*Diagnostics*")
1042        name: String,
1043        /// Mode name for buffer-local keybindings (e.g., "diagnostics-list")
1044        mode: String,
1045        /// Whether the buffer is read-only
1046        read_only: bool,
1047        /// Entries with text and embedded properties
1048        entries: Vec<TextPropertyEntry>,
1049        /// Whether to show line numbers in the gutter
1050        show_line_numbers: bool,
1051        /// Whether to show cursors in the buffer
1052        show_cursors: bool,
1053        /// Whether editing is disabled (blocks editing commands)
1054        editing_disabled: bool,
1055        /// Whether this buffer should be hidden from tabs (for composite source buffers)
1056        hidden_from_tabs: bool,
1057        /// Optional request ID for async response
1058        request_id: Option<u64>,
1059    },
1060
1061    /// Create a virtual buffer in a horizontal split
1062    /// Opens the buffer in a new pane below the current one
1063    CreateVirtualBufferInSplit {
1064        /// Display name (e.g., "*Diagnostics*")
1065        name: String,
1066        /// Mode name for buffer-local keybindings (e.g., "diagnostics-list")
1067        mode: String,
1068        /// Whether the buffer is read-only
1069        read_only: bool,
1070        /// Entries with text and embedded properties
1071        entries: Vec<TextPropertyEntry>,
1072        /// Split ratio (0.0 to 1.0, where 0.5 = equal split)
1073        ratio: f32,
1074        /// Split direction ("horizontal" or "vertical"), default horizontal
1075        direction: Option<String>,
1076        /// Optional panel ID for idempotent operations (if panel exists, update content)
1077        panel_id: Option<String>,
1078        /// Whether to show line numbers in the buffer (default true)
1079        show_line_numbers: bool,
1080        /// Whether to show cursors in the buffer (default true)
1081        show_cursors: bool,
1082        /// Whether editing is disabled for this buffer (default false)
1083        editing_disabled: bool,
1084        /// Whether line wrapping is enabled for this split (None = use global setting)
1085        line_wrap: Option<bool>,
1086        /// Optional request ID for async response (if set, editor will send back buffer ID)
1087        request_id: Option<u64>,
1088    },
1089
1090    /// Set the content of a virtual buffer with text properties
1091    SetVirtualBufferContent {
1092        buffer_id: BufferId,
1093        /// Entries with text and embedded properties
1094        entries: Vec<TextPropertyEntry>,
1095    },
1096
1097    /// Get text properties at the cursor position in a buffer
1098    GetTextPropertiesAtCursor { buffer_id: BufferId },
1099
1100    /// Define a buffer mode with keybindings
1101    DefineMode {
1102        name: String,
1103        parent: Option<String>,
1104        bindings: Vec<(String, String)>, // (key_string, command_name)
1105        read_only: bool,
1106    },
1107
1108    /// Switch the current split to display a buffer
1109    ShowBuffer { buffer_id: BufferId },
1110
1111    /// Create a virtual buffer in an existing split (replaces current buffer in that split)
1112    CreateVirtualBufferInExistingSplit {
1113        /// Display name (e.g., "*Commit Details*")
1114        name: String,
1115        /// Mode name for buffer-local keybindings
1116        mode: String,
1117        /// Whether the buffer is read-only
1118        read_only: bool,
1119        /// Entries with text and embedded properties
1120        entries: Vec<TextPropertyEntry>,
1121        /// Target split ID where the buffer should be displayed
1122        split_id: SplitId,
1123        /// Whether to show line numbers in the buffer (default true)
1124        show_line_numbers: bool,
1125        /// Whether to show cursors in the buffer (default true)
1126        show_cursors: bool,
1127        /// Whether editing is disabled for this buffer (default false)
1128        editing_disabled: bool,
1129        /// Whether line wrapping is enabled for this split (None = use global setting)
1130        line_wrap: Option<bool>,
1131        /// Optional request ID for async response
1132        request_id: Option<u64>,
1133    },
1134
1135    /// Close a buffer and remove it from all splits
1136    CloseBuffer { buffer_id: BufferId },
1137
1138    /// Create a composite buffer that displays multiple source buffers
1139    /// Used for side-by-side diff, unified diff, and 3-way merge views
1140    CreateCompositeBuffer {
1141        /// Display name (shown in tab bar)
1142        name: String,
1143        /// Mode name for keybindings (e.g., "diff-view")
1144        mode: String,
1145        /// Layout configuration
1146        layout: CompositeLayoutConfig,
1147        /// Source pane configurations
1148        sources: Vec<CompositeSourceConfig>,
1149        /// Diff hunks for line alignment (optional)
1150        hunks: Option<Vec<CompositeHunk>>,
1151        /// Request ID for async response
1152        request_id: Option<u64>,
1153    },
1154
1155    /// Update alignment for a composite buffer (e.g., after source edit)
1156    UpdateCompositeAlignment {
1157        buffer_id: BufferId,
1158        hunks: Vec<CompositeHunk>,
1159    },
1160
1161    /// Close a composite buffer
1162    CloseCompositeBuffer { buffer_id: BufferId },
1163
1164    /// Focus a specific split
1165    FocusSplit { split_id: SplitId },
1166
1167    /// Set the buffer displayed in a specific split
1168    SetSplitBuffer {
1169        split_id: SplitId,
1170        buffer_id: BufferId,
1171    },
1172
1173    /// Set the scroll position of a specific split
1174    SetSplitScroll { split_id: SplitId, top_byte: usize },
1175
1176    /// Request syntax highlights for a buffer range
1177    RequestHighlights {
1178        buffer_id: BufferId,
1179        range: Range<usize>,
1180        request_id: u64,
1181    },
1182
1183    /// Close a split (if not the last one)
1184    CloseSplit { split_id: SplitId },
1185
1186    /// Set the ratio of a split container
1187    SetSplitRatio {
1188        split_id: SplitId,
1189        /// Ratio between 0.0 and 1.0 (0.5 = equal split)
1190        ratio: f32,
1191    },
1192
1193    /// Distribute splits evenly - make all given splits equal size
1194    DistributeSplitsEvenly {
1195        /// Split IDs to distribute evenly
1196        split_ids: Vec<SplitId>,
1197    },
1198
1199    /// Set cursor position in a buffer (also scrolls viewport to show cursor)
1200    SetBufferCursor {
1201        buffer_id: BufferId,
1202        /// Byte offset position for the cursor
1203        position: usize,
1204    },
1205
1206    /// Send an arbitrary LSP request and return the raw JSON response
1207    SendLspRequest {
1208        language: String,
1209        method: String,
1210        #[ts(type = "any")]
1211        params: Option<JsonValue>,
1212        request_id: u64,
1213    },
1214
1215    /// Set the internal clipboard content
1216    SetClipboard { text: String },
1217
1218    /// Delete the current selection in the active buffer
1219    /// This deletes all selected text across all cursors
1220    DeleteSelection,
1221
1222    /// Set or unset a custom context
1223    /// Custom contexts are plugin-defined states that can be used to control command visibility
1224    /// For example, "config-editor" context could make config editor commands available
1225    SetContext {
1226        /// Context name (e.g., "config-editor")
1227        name: String,
1228        /// Whether the context is active
1229        active: bool,
1230    },
1231
1232    /// Set the hunks for the Review Diff tool
1233    SetReviewDiffHunks { hunks: Vec<ReviewHunk> },
1234
1235    /// Execute an editor action by name (e.g., "move_word_right", "delete_line")
1236    /// Used by vi mode plugin to run motions and calculate cursor ranges
1237    ExecuteAction {
1238        /// Action name (e.g., "move_word_right", "move_line_end")
1239        action_name: String,
1240    },
1241
1242    /// Execute multiple actions in sequence, each with an optional repeat count
1243    /// Used by vi mode for count prefix (e.g., "3dw" = delete 3 words)
1244    /// All actions execute atomically with no plugin roundtrips between them
1245    ExecuteActions {
1246        /// List of actions to execute in sequence
1247        actions: Vec<ActionSpec>,
1248    },
1249
1250    /// Get text from a buffer range (for yank operations)
1251    GetBufferText {
1252        /// Buffer ID
1253        buffer_id: BufferId,
1254        /// Start byte offset
1255        start: usize,
1256        /// End byte offset
1257        end: usize,
1258        /// Request ID for async response
1259        request_id: u64,
1260    },
1261
1262    /// Get byte offset of the start of a line (async)
1263    /// Line is 0-indexed (0 = first line)
1264    GetLineStartPosition {
1265        /// Buffer ID (0 for active buffer)
1266        buffer_id: BufferId,
1267        /// Line number (0-indexed)
1268        line: u32,
1269        /// Request ID for async response
1270        request_id: u64,
1271    },
1272
1273    /// Set the global editor mode (for modal editing like vi mode)
1274    /// When set, the mode's keybindings take precedence over normal editing
1275    SetEditorMode {
1276        /// Mode name (e.g., "vi-normal", "vi-insert") or None to clear
1277        mode: Option<String>,
1278    },
1279
1280    /// Show an action popup with buttons for user interaction
1281    /// When the user selects an action, the ActionPopupResult hook is fired
1282    ShowActionPopup {
1283        /// Unique identifier for the popup (used in ActionPopupResult)
1284        popup_id: String,
1285        /// Title text for the popup
1286        title: String,
1287        /// Body message (supports basic formatting)
1288        message: String,
1289        /// Action buttons to display
1290        actions: Vec<ActionPopupAction>,
1291    },
1292
1293    /// Disable LSP for a specific language and persist to config
1294    DisableLspForLanguage {
1295        /// The language to disable LSP for (e.g., "python", "rust")
1296        language: String,
1297    },
1298
1299    /// Set the workspace root URI for a specific language's LSP server
1300    /// This allows plugins to specify project roots (e.g., directory containing .csproj)
1301    /// If the LSP is already running, it will be restarted with the new root
1302    SetLspRootUri {
1303        /// The language to set root URI for (e.g., "csharp", "rust")
1304        language: String,
1305        /// The root URI (file:// URL format)
1306        uri: String,
1307    },
1308
1309    /// Create a scroll sync group for anchor-based synchronized scrolling
1310    /// Used for side-by-side diff views where two panes need to scroll together
1311    /// The plugin provides the group ID (must be unique per plugin)
1312    CreateScrollSyncGroup {
1313        /// Plugin-assigned group ID
1314        group_id: u32,
1315        /// The left (primary) split - scroll position is tracked in this split's line space
1316        left_split: SplitId,
1317        /// The right (secondary) split - position is derived from anchors
1318        right_split: SplitId,
1319    },
1320
1321    /// Set sync anchors for a scroll sync group
1322    /// Anchors map corresponding line numbers between left and right buffers
1323    SetScrollSyncAnchors {
1324        /// The group ID returned by CreateScrollSyncGroup
1325        group_id: u32,
1326        /// List of (left_line, right_line) pairs marking corresponding positions
1327        anchors: Vec<(usize, usize)>,
1328    },
1329
1330    /// Remove a scroll sync group
1331    RemoveScrollSyncGroup {
1332        /// The group ID returned by CreateScrollSyncGroup
1333        group_id: u32,
1334    },
1335
1336    /// Save a buffer to a specific file path
1337    /// Used by :w filename command to save unnamed buffers or save-as
1338    SaveBufferToPath {
1339        /// Buffer ID to save
1340        buffer_id: BufferId,
1341        /// Path to save to
1342        path: PathBuf,
1343    },
1344
1345    /// Load a plugin from a file path
1346    /// The plugin will be initialized and start receiving events
1347    LoadPlugin {
1348        /// Path to the plugin file (.ts or .js)
1349        path: PathBuf,
1350        /// Callback ID for async response (success/failure)
1351        callback_id: JsCallbackId,
1352    },
1353
1354    /// Unload a plugin by name
1355    /// The plugin will stop receiving events and be removed from memory
1356    UnloadPlugin {
1357        /// Plugin name (as registered)
1358        name: String,
1359        /// Callback ID for async response (success/failure)
1360        callback_id: JsCallbackId,
1361    },
1362
1363    /// Reload a plugin by name (unload + load)
1364    /// Useful for development when plugin code changes
1365    ReloadPlugin {
1366        /// Plugin name (as registered)
1367        name: String,
1368        /// Callback ID for async response (success/failure)
1369        callback_id: JsCallbackId,
1370    },
1371
1372    /// List all loaded plugins
1373    /// Returns plugin info (name, path, enabled) for all loaded plugins
1374    ListPlugins {
1375        /// Callback ID for async response (JSON array of plugin info)
1376        callback_id: JsCallbackId,
1377    },
1378
1379    /// Reload the theme registry from disk
1380    /// Call this after installing a theme package or saving a new theme
1381    ReloadThemes,
1382
1383    /// Register a TextMate grammar file for a language
1384    /// The grammar will be added to pending_grammars until ReloadGrammars is called
1385    RegisterGrammar {
1386        /// Language identifier (e.g., "elixir", "zig")
1387        language: String,
1388        /// Path to the grammar file (.sublime-syntax or .tmLanguage)
1389        grammar_path: String,
1390        /// File extensions to associate with this grammar (e.g., ["ex", "exs"])
1391        extensions: Vec<String>,
1392    },
1393
1394    /// Register language configuration (comment prefix, indentation, formatter)
1395    /// This is applied immediately to the runtime config
1396    RegisterLanguageConfig {
1397        /// Language identifier (e.g., "elixir")
1398        language: String,
1399        /// Language configuration
1400        config: LanguagePackConfig,
1401    },
1402
1403    /// Register an LSP server for a language
1404    /// This is applied immediately to the LSP manager and runtime config
1405    RegisterLspServer {
1406        /// Language identifier (e.g., "elixir")
1407        language: String,
1408        /// LSP server configuration
1409        config: LspServerPackConfig,
1410    },
1411
1412    /// Reload the grammar registry to apply registered grammars
1413    /// Call this after registering one or more grammars to rebuild the syntax set
1414    ReloadGrammars,
1415}
1416
1417// =============================================================================
1418// Language Pack Configuration Types
1419// =============================================================================
1420
1421/// Language configuration for language packs
1422///
1423/// This is a simplified version of the full LanguageConfig, containing only
1424/// the fields that can be set via the plugin API.
1425#[derive(Debug, Clone, Serialize, Deserialize, Default, TS)]
1426#[serde(rename_all = "camelCase")]
1427#[ts(export)]
1428pub struct LanguagePackConfig {
1429    /// Comment prefix for line comments (e.g., "//" or "#")
1430    #[serde(default)]
1431    pub comment_prefix: Option<String>,
1432
1433    /// Block comment start marker (e.g., slash-star)
1434    #[serde(default)]
1435    pub block_comment_start: Option<String>,
1436
1437    /// Block comment end marker (e.g., star-slash)
1438    #[serde(default)]
1439    pub block_comment_end: Option<String>,
1440
1441    /// Whether to use tabs instead of spaces for indentation
1442    #[serde(default)]
1443    pub use_tabs: Option<bool>,
1444
1445    /// Tab size (number of spaces per tab level)
1446    #[serde(default)]
1447    pub tab_size: Option<usize>,
1448
1449    /// Whether auto-indent is enabled
1450    #[serde(default)]
1451    pub auto_indent: Option<bool>,
1452
1453    /// Whether to show whitespace tab indicators (→) for this language
1454    /// Defaults to true. Set to false for languages like Go/Hare that use tabs for indentation.
1455    #[serde(default)]
1456    pub show_whitespace_tabs: Option<bool>,
1457
1458    /// Formatter configuration
1459    #[serde(default)]
1460    pub formatter: Option<FormatterPackConfig>,
1461}
1462
1463/// Formatter configuration for language packs
1464#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1465#[serde(rename_all = "camelCase")]
1466#[ts(export)]
1467pub struct FormatterPackConfig {
1468    /// Command to run (e.g., "prettier", "rustfmt")
1469    pub command: String,
1470
1471    /// Arguments to pass to the formatter
1472    #[serde(default)]
1473    pub args: Vec<String>,
1474}
1475
1476/// LSP server configuration for language packs
1477#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1478#[serde(rename_all = "camelCase")]
1479#[ts(export)]
1480pub struct LspServerPackConfig {
1481    /// Command to start the LSP server
1482    pub command: String,
1483
1484    /// Arguments to pass to the command
1485    #[serde(default)]
1486    pub args: Vec<String>,
1487
1488    /// Whether to auto-start the server when a matching file is opened
1489    #[serde(default)]
1490    pub auto_start: Option<bool>,
1491
1492    /// LSP initialization options
1493    #[serde(default)]
1494    #[ts(type = "Record<string, unknown> | null")]
1495    pub initialization_options: Option<JsonValue>,
1496}
1497
1498/// Hunk status for Review Diff
1499#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
1500#[ts(export)]
1501pub enum HunkStatus {
1502    Pending,
1503    Staged,
1504    Discarded,
1505}
1506
1507/// A high-level hunk directive for the Review Diff tool
1508#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1509#[ts(export)]
1510pub struct ReviewHunk {
1511    pub id: String,
1512    pub file: String,
1513    pub context_header: String,
1514    pub status: HunkStatus,
1515    /// 0-indexed line range in the base (HEAD) version
1516    pub base_range: Option<(usize, usize)>,
1517    /// 0-indexed line range in the modified (Working) version
1518    pub modified_range: Option<(usize, usize)>,
1519}
1520
1521/// Action button for action popups
1522#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1523#[serde(deny_unknown_fields)]
1524#[ts(export, rename = "TsActionPopupAction")]
1525pub struct ActionPopupAction {
1526    /// Unique action identifier (returned in ActionPopupResult)
1527    pub id: String,
1528    /// Display text for the button (can include command hints)
1529    pub label: String,
1530}
1531
1532/// Options for showActionPopup
1533#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1534#[serde(deny_unknown_fields)]
1535#[ts(export)]
1536pub struct ActionPopupOptions {
1537    /// Unique identifier for the popup (used in ActionPopupResult)
1538    pub id: String,
1539    /// Title text for the popup
1540    pub title: String,
1541    /// Body message (supports basic formatting)
1542    pub message: String,
1543    /// Action buttons to display
1544    pub actions: Vec<ActionPopupAction>,
1545}
1546
1547/// Syntax highlight span for a buffer range
1548#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1549#[ts(export)]
1550pub struct TsHighlightSpan {
1551    pub start: u32,
1552    pub end: u32,
1553    #[ts(type = "[number, number, number]")]
1554    pub color: (u8, u8, u8),
1555    pub bold: bool,
1556    pub italic: bool,
1557}
1558
1559/// Result from spawning a process with spawnProcess
1560#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1561#[ts(export)]
1562pub struct SpawnResult {
1563    /// Complete stdout as string
1564    pub stdout: String,
1565    /// Complete stderr as string
1566    pub stderr: String,
1567    /// Process exit code (0 usually means success, -1 if killed)
1568    pub exit_code: i32,
1569}
1570
1571/// Result from spawning a background process
1572#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1573#[ts(export)]
1574pub struct BackgroundProcessResult {
1575    /// Unique process ID for later reference
1576    #[ts(type = "number")]
1577    pub process_id: u64,
1578    /// Process exit code (0 usually means success, -1 if killed)
1579    /// Only present when the process has exited
1580    pub exit_code: i32,
1581}
1582
1583/// Entry for virtual buffer content with optional text properties (JS API version)
1584#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1585#[serde(deny_unknown_fields)]
1586#[ts(export, rename = "TextPropertyEntry")]
1587pub struct JsTextPropertyEntry {
1588    /// Text content for this entry
1589    pub text: String,
1590    /// Optional properties attached to this text (e.g., file path, line number)
1591    #[serde(default)]
1592    #[ts(optional, type = "Record<string, unknown>")]
1593    pub properties: Option<HashMap<String, JsonValue>>,
1594}
1595
1596/// Directory entry returned by readDir
1597#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1598#[ts(export)]
1599pub struct DirEntry {
1600    /// File/directory name
1601    pub name: String,
1602    /// True if this is a file
1603    pub is_file: bool,
1604    /// True if this is a directory
1605    pub is_dir: bool,
1606}
1607
1608/// Position in a document (line and character)
1609#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1610#[ts(export)]
1611pub struct JsPosition {
1612    /// Zero-indexed line number
1613    pub line: u32,
1614    /// Zero-indexed character offset
1615    pub character: u32,
1616}
1617
1618/// Range in a document (start and end positions)
1619#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1620#[ts(export)]
1621pub struct JsRange {
1622    /// Start position
1623    pub start: JsPosition,
1624    /// End position
1625    pub end: JsPosition,
1626}
1627
1628/// Diagnostic from LSP
1629#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1630#[ts(export)]
1631pub struct JsDiagnostic {
1632    /// Document URI
1633    pub uri: String,
1634    /// Diagnostic message
1635    pub message: String,
1636    /// Severity: 1=Error, 2=Warning, 3=Info, 4=Hint, null=unknown
1637    pub severity: Option<u8>,
1638    /// Range in the document
1639    pub range: JsRange,
1640    /// Source of the diagnostic (e.g., "typescript", "eslint")
1641    #[ts(optional)]
1642    pub source: Option<String>,
1643}
1644
1645/// Options for createVirtualBuffer
1646#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1647#[serde(deny_unknown_fields)]
1648#[ts(export)]
1649pub struct CreateVirtualBufferOptions {
1650    /// Buffer name (displayed in tabs/title)
1651    pub name: String,
1652    /// Mode for keybindings (e.g., "git-log", "search-results")
1653    #[serde(default)]
1654    #[ts(optional)]
1655    pub mode: Option<String>,
1656    /// Whether buffer is read-only (default: false)
1657    #[serde(default, rename = "readOnly")]
1658    #[ts(optional, rename = "readOnly")]
1659    pub read_only: Option<bool>,
1660    /// Show line numbers in gutter (default: false)
1661    #[serde(default, rename = "showLineNumbers")]
1662    #[ts(optional, rename = "showLineNumbers")]
1663    pub show_line_numbers: Option<bool>,
1664    /// Show cursor (default: true)
1665    #[serde(default, rename = "showCursors")]
1666    #[ts(optional, rename = "showCursors")]
1667    pub show_cursors: Option<bool>,
1668    /// Disable text editing (default: false)
1669    #[serde(default, rename = "editingDisabled")]
1670    #[ts(optional, rename = "editingDisabled")]
1671    pub editing_disabled: Option<bool>,
1672    /// Hide from tab bar (default: false)
1673    #[serde(default, rename = "hiddenFromTabs")]
1674    #[ts(optional, rename = "hiddenFromTabs")]
1675    pub hidden_from_tabs: Option<bool>,
1676    /// Initial content entries with optional properties
1677    #[serde(default)]
1678    #[ts(optional)]
1679    pub entries: Option<Vec<JsTextPropertyEntry>>,
1680}
1681
1682/// Options for createVirtualBufferInSplit
1683#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1684#[serde(deny_unknown_fields)]
1685#[ts(export)]
1686pub struct CreateVirtualBufferInSplitOptions {
1687    /// Buffer name (displayed in tabs/title)
1688    pub name: String,
1689    /// Mode for keybindings (e.g., "git-log", "search-results")
1690    #[serde(default)]
1691    #[ts(optional)]
1692    pub mode: Option<String>,
1693    /// Whether buffer is read-only (default: false)
1694    #[serde(default, rename = "readOnly")]
1695    #[ts(optional, rename = "readOnly")]
1696    pub read_only: Option<bool>,
1697    /// Split ratio 0.0-1.0 (default: 0.5)
1698    #[serde(default)]
1699    #[ts(optional)]
1700    pub ratio: Option<f32>,
1701    /// Split direction: "horizontal" or "vertical"
1702    #[serde(default)]
1703    #[ts(optional)]
1704    pub direction: Option<String>,
1705    /// Panel ID to split from
1706    #[serde(default, rename = "panelId")]
1707    #[ts(optional, rename = "panelId")]
1708    pub panel_id: Option<String>,
1709    /// Show line numbers in gutter (default: true)
1710    #[serde(default, rename = "showLineNumbers")]
1711    #[ts(optional, rename = "showLineNumbers")]
1712    pub show_line_numbers: Option<bool>,
1713    /// Show cursor (default: true)
1714    #[serde(default, rename = "showCursors")]
1715    #[ts(optional, rename = "showCursors")]
1716    pub show_cursors: Option<bool>,
1717    /// Disable text editing (default: false)
1718    #[serde(default, rename = "editingDisabled")]
1719    #[ts(optional, rename = "editingDisabled")]
1720    pub editing_disabled: Option<bool>,
1721    /// Enable line wrapping
1722    #[serde(default, rename = "lineWrap")]
1723    #[ts(optional, rename = "lineWrap")]
1724    pub line_wrap: Option<bool>,
1725    /// Initial content entries with optional properties
1726    #[serde(default)]
1727    #[ts(optional)]
1728    pub entries: Option<Vec<JsTextPropertyEntry>>,
1729}
1730
1731/// Options for createVirtualBufferInExistingSplit
1732#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1733#[serde(deny_unknown_fields)]
1734#[ts(export)]
1735pub struct CreateVirtualBufferInExistingSplitOptions {
1736    /// Buffer name (displayed in tabs/title)
1737    pub name: String,
1738    /// Target split ID (required)
1739    #[serde(rename = "splitId")]
1740    #[ts(rename = "splitId")]
1741    pub split_id: usize,
1742    /// Mode for keybindings (e.g., "git-log", "search-results")
1743    #[serde(default)]
1744    #[ts(optional)]
1745    pub mode: Option<String>,
1746    /// Whether buffer is read-only (default: false)
1747    #[serde(default, rename = "readOnly")]
1748    #[ts(optional, rename = "readOnly")]
1749    pub read_only: Option<bool>,
1750    /// Show line numbers in gutter (default: true)
1751    #[serde(default, rename = "showLineNumbers")]
1752    #[ts(optional, rename = "showLineNumbers")]
1753    pub show_line_numbers: Option<bool>,
1754    /// Show cursor (default: true)
1755    #[serde(default, rename = "showCursors")]
1756    #[ts(optional, rename = "showCursors")]
1757    pub show_cursors: Option<bool>,
1758    /// Disable text editing (default: false)
1759    #[serde(default, rename = "editingDisabled")]
1760    #[ts(optional, rename = "editingDisabled")]
1761    pub editing_disabled: Option<bool>,
1762    /// Enable line wrapping
1763    #[serde(default, rename = "lineWrap")]
1764    #[ts(optional, rename = "lineWrap")]
1765    pub line_wrap: Option<bool>,
1766    /// Initial content entries with optional properties
1767    #[serde(default)]
1768    #[ts(optional)]
1769    pub entries: Option<Vec<JsTextPropertyEntry>>,
1770}
1771
1772/// Result of getTextPropertiesAtCursor - array of property objects
1773///
1774/// Each element contains the properties from a text property span that overlaps
1775/// with the cursor position. Properties are dynamic key-value pairs set by plugins.
1776#[derive(Debug, Clone, Serialize, TS)]
1777#[ts(export, type = "Array<Record<string, unknown>>")]
1778pub struct TextPropertiesAtCursor(pub Vec<HashMap<String, JsonValue>>);
1779
1780// Implement FromJs for option types using rquickjs_serde
1781#[cfg(feature = "plugins")]
1782mod fromjs_impls {
1783    use super::*;
1784    use rquickjs::{Ctx, FromJs, Value};
1785
1786    impl<'js> FromJs<'js> for JsTextPropertyEntry {
1787        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1788            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1789                from: "object",
1790                to: "JsTextPropertyEntry",
1791                message: Some(e.to_string()),
1792            })
1793        }
1794    }
1795
1796    impl<'js> FromJs<'js> for CreateVirtualBufferOptions {
1797        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1798            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1799                from: "object",
1800                to: "CreateVirtualBufferOptions",
1801                message: Some(e.to_string()),
1802            })
1803        }
1804    }
1805
1806    impl<'js> FromJs<'js> for CreateVirtualBufferInSplitOptions {
1807        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1808            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1809                from: "object",
1810                to: "CreateVirtualBufferInSplitOptions",
1811                message: Some(e.to_string()),
1812            })
1813        }
1814    }
1815
1816    impl<'js> FromJs<'js> for CreateVirtualBufferInExistingSplitOptions {
1817        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1818            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1819                from: "object",
1820                to: "CreateVirtualBufferInExistingSplitOptions",
1821                message: Some(e.to_string()),
1822            })
1823        }
1824    }
1825
1826    impl<'js> rquickjs::IntoJs<'js> for TextPropertiesAtCursor {
1827        fn into_js(self, ctx: &Ctx<'js>) -> rquickjs::Result<Value<'js>> {
1828            rquickjs_serde::to_value(ctx.clone(), &self.0)
1829                .map_err(|e| rquickjs::Error::new_from_js_message("serialize", "", &e.to_string()))
1830        }
1831    }
1832
1833    // === Additional input types for type-safe plugin API ===
1834
1835    impl<'js> FromJs<'js> for ActionSpec {
1836        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1837            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1838                from: "object",
1839                to: "ActionSpec",
1840                message: Some(e.to_string()),
1841            })
1842        }
1843    }
1844
1845    impl<'js> FromJs<'js> for ActionPopupAction {
1846        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1847            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1848                from: "object",
1849                to: "ActionPopupAction",
1850                message: Some(e.to_string()),
1851            })
1852        }
1853    }
1854
1855    impl<'js> FromJs<'js> for ActionPopupOptions {
1856        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1857            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1858                from: "object",
1859                to: "ActionPopupOptions",
1860                message: Some(e.to_string()),
1861            })
1862        }
1863    }
1864
1865    impl<'js> FromJs<'js> for ViewTokenWire {
1866        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1867            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1868                from: "object",
1869                to: "ViewTokenWire",
1870                message: Some(e.to_string()),
1871            })
1872        }
1873    }
1874
1875    impl<'js> FromJs<'js> for ViewTokenStyle {
1876        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1877            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1878                from: "object",
1879                to: "ViewTokenStyle",
1880                message: Some(e.to_string()),
1881            })
1882        }
1883    }
1884
1885    impl<'js> FromJs<'js> for LayoutHints {
1886        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1887            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1888                from: "object",
1889                to: "LayoutHints",
1890                message: Some(e.to_string()),
1891            })
1892        }
1893    }
1894
1895    impl<'js> FromJs<'js> for CreateCompositeBufferOptions {
1896        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1897            // Use two-step deserialization for complex nested structures
1898            let json: serde_json::Value =
1899                rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1900                    from: "object",
1901                    to: "CreateCompositeBufferOptions (json)",
1902                    message: Some(e.to_string()),
1903                })?;
1904            serde_json::from_value(json).map_err(|e| rquickjs::Error::FromJs {
1905                from: "json",
1906                to: "CreateCompositeBufferOptions",
1907                message: Some(e.to_string()),
1908            })
1909        }
1910    }
1911
1912    impl<'js> FromJs<'js> for CompositeHunk {
1913        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1914            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1915                from: "object",
1916                to: "CompositeHunk",
1917                message: Some(e.to_string()),
1918            })
1919        }
1920    }
1921
1922    impl<'js> FromJs<'js> for LanguagePackConfig {
1923        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1924            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1925                from: "object",
1926                to: "LanguagePackConfig",
1927                message: Some(e.to_string()),
1928            })
1929        }
1930    }
1931
1932    impl<'js> FromJs<'js> for LspServerPackConfig {
1933        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1934            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1935                from: "object",
1936                to: "LspServerPackConfig",
1937                message: Some(e.to_string()),
1938            })
1939        }
1940    }
1941}
1942
1943/// Plugin API context - provides safe access to editor functionality
1944pub struct PluginApi {
1945    /// Hook registry (shared with editor)
1946    hooks: Arc<RwLock<HookRegistry>>,
1947
1948    /// Command registry (shared with editor)
1949    commands: Arc<RwLock<CommandRegistry>>,
1950
1951    /// Command queue for sending commands to editor
1952    command_sender: std::sync::mpsc::Sender<PluginCommand>,
1953
1954    /// Snapshot of editor state (read-only for plugins)
1955    state_snapshot: Arc<RwLock<EditorStateSnapshot>>,
1956}
1957
1958impl PluginApi {
1959    /// Create a new plugin API context
1960    pub fn new(
1961        hooks: Arc<RwLock<HookRegistry>>,
1962        commands: Arc<RwLock<CommandRegistry>>,
1963        command_sender: std::sync::mpsc::Sender<PluginCommand>,
1964        state_snapshot: Arc<RwLock<EditorStateSnapshot>>,
1965    ) -> Self {
1966        Self {
1967            hooks,
1968            commands,
1969            command_sender,
1970            state_snapshot,
1971        }
1972    }
1973
1974    /// Register a hook callback
1975    pub fn register_hook(&self, hook_name: &str, callback: HookCallback) {
1976        let mut hooks = self.hooks.write().unwrap();
1977        hooks.add_hook(hook_name, callback);
1978    }
1979
1980    /// Remove all hooks for a specific name
1981    pub fn unregister_hooks(&self, hook_name: &str) {
1982        let mut hooks = self.hooks.write().unwrap();
1983        hooks.remove_hooks(hook_name);
1984    }
1985
1986    /// Register a command
1987    pub fn register_command(&self, command: Command) {
1988        let commands = self.commands.read().unwrap();
1989        commands.register(command);
1990    }
1991
1992    /// Unregister a command by name
1993    pub fn unregister_command(&self, name: &str) {
1994        let commands = self.commands.read().unwrap();
1995        commands.unregister(name);
1996    }
1997
1998    /// Send a command to the editor (async/non-blocking)
1999    pub fn send_command(&self, command: PluginCommand) -> Result<(), String> {
2000        self.command_sender
2001            .send(command)
2002            .map_err(|e| format!("Failed to send command: {}", e))
2003    }
2004
2005    /// Insert text at a position in a buffer
2006    pub fn insert_text(
2007        &self,
2008        buffer_id: BufferId,
2009        position: usize,
2010        text: String,
2011    ) -> Result<(), String> {
2012        self.send_command(PluginCommand::InsertText {
2013            buffer_id,
2014            position,
2015            text,
2016        })
2017    }
2018
2019    /// Delete a range of text from a buffer
2020    pub fn delete_range(&self, buffer_id: BufferId, range: Range<usize>) -> Result<(), String> {
2021        self.send_command(PluginCommand::DeleteRange { buffer_id, range })
2022    }
2023
2024    /// Add an overlay (decoration) to a buffer
2025    /// Add an overlay to a buffer with styling options
2026    ///
2027    /// Returns an opaque handle that can be used to remove the overlay later.
2028    ///
2029    /// Colors can be specified as RGB arrays or theme key strings.
2030    /// Theme keys are resolved at render time, so overlays update with theme changes.
2031    pub fn add_overlay(
2032        &self,
2033        buffer_id: BufferId,
2034        namespace: Option<String>,
2035        range: Range<usize>,
2036        options: OverlayOptions,
2037    ) -> Result<(), String> {
2038        self.send_command(PluginCommand::AddOverlay {
2039            buffer_id,
2040            namespace: namespace.map(OverlayNamespace::from_string),
2041            range,
2042            options,
2043        })
2044    }
2045
2046    /// Remove an overlay from a buffer by its handle
2047    pub fn remove_overlay(&self, buffer_id: BufferId, handle: String) -> Result<(), String> {
2048        self.send_command(PluginCommand::RemoveOverlay {
2049            buffer_id,
2050            handle: OverlayHandle::from_string(handle),
2051        })
2052    }
2053
2054    /// Clear all overlays in a namespace from a buffer
2055    pub fn clear_namespace(&self, buffer_id: BufferId, namespace: String) -> Result<(), String> {
2056        self.send_command(PluginCommand::ClearNamespace {
2057            buffer_id,
2058            namespace: OverlayNamespace::from_string(namespace),
2059        })
2060    }
2061
2062    /// Clear all overlays that overlap with a byte range
2063    /// Used for targeted invalidation when content changes
2064    pub fn clear_overlays_in_range(
2065        &self,
2066        buffer_id: BufferId,
2067        start: usize,
2068        end: usize,
2069    ) -> Result<(), String> {
2070        self.send_command(PluginCommand::ClearOverlaysInRange {
2071            buffer_id,
2072            start,
2073            end,
2074        })
2075    }
2076
2077    /// Set the status message
2078    pub fn set_status(&self, message: String) -> Result<(), String> {
2079        self.send_command(PluginCommand::SetStatus { message })
2080    }
2081
2082    /// Open a file at a specific line and column (1-indexed)
2083    /// This is useful for jumping to locations from git grep, LSP definitions, etc.
2084    pub fn open_file_at_location(
2085        &self,
2086        path: PathBuf,
2087        line: Option<usize>,
2088        column: Option<usize>,
2089    ) -> Result<(), String> {
2090        self.send_command(PluginCommand::OpenFileAtLocation { path, line, column })
2091    }
2092
2093    /// Open a file in a specific split at a line and column
2094    ///
2095    /// Similar to open_file_at_location but targets a specific split pane.
2096    /// The split_id is the ID of the split pane to open the file in.
2097    pub fn open_file_in_split(
2098        &self,
2099        split_id: usize,
2100        path: PathBuf,
2101        line: Option<usize>,
2102        column: Option<usize>,
2103    ) -> Result<(), String> {
2104        self.send_command(PluginCommand::OpenFileInSplit {
2105            split_id,
2106            path,
2107            line,
2108            column,
2109        })
2110    }
2111
2112    /// Start a prompt (minibuffer) with a custom type identifier
2113    /// The prompt_type is used to filter hooks in plugin code
2114    pub fn start_prompt(&self, label: String, prompt_type: String) -> Result<(), String> {
2115        self.send_command(PluginCommand::StartPrompt { label, prompt_type })
2116    }
2117
2118    /// Set the suggestions for the current prompt
2119    /// This updates the prompt's autocomplete/selection list
2120    pub fn set_prompt_suggestions(&self, suggestions: Vec<Suggestion>) -> Result<(), String> {
2121        self.send_command(PluginCommand::SetPromptSuggestions { suggestions })
2122    }
2123
2124    /// Add a menu item to an existing menu
2125    pub fn add_menu_item(
2126        &self,
2127        menu_label: String,
2128        item: MenuItem,
2129        position: MenuPosition,
2130    ) -> Result<(), String> {
2131        self.send_command(PluginCommand::AddMenuItem {
2132            menu_label,
2133            item,
2134            position,
2135        })
2136    }
2137
2138    /// Add a new top-level menu
2139    pub fn add_menu(&self, menu: Menu, position: MenuPosition) -> Result<(), String> {
2140        self.send_command(PluginCommand::AddMenu { menu, position })
2141    }
2142
2143    /// Remove a menu item from a menu
2144    pub fn remove_menu_item(&self, menu_label: String, item_label: String) -> Result<(), String> {
2145        self.send_command(PluginCommand::RemoveMenuItem {
2146            menu_label,
2147            item_label,
2148        })
2149    }
2150
2151    /// Remove a top-level menu
2152    pub fn remove_menu(&self, menu_label: String) -> Result<(), String> {
2153        self.send_command(PluginCommand::RemoveMenu { menu_label })
2154    }
2155
2156    // === Virtual Buffer Methods ===
2157
2158    /// Create a new virtual buffer (not backed by a file)
2159    ///
2160    /// Virtual buffers are used for special displays like diagnostic lists,
2161    /// search results, etc. They have their own mode for keybindings.
2162    pub fn create_virtual_buffer(
2163        &self,
2164        name: String,
2165        mode: String,
2166        read_only: bool,
2167    ) -> Result<(), String> {
2168        self.send_command(PluginCommand::CreateVirtualBuffer {
2169            name,
2170            mode,
2171            read_only,
2172        })
2173    }
2174
2175    /// Create a virtual buffer and set its content in one operation
2176    ///
2177    /// This is the preferred way to create virtual buffers since it doesn't
2178    /// require tracking the buffer ID. The buffer is created and populated
2179    /// atomically.
2180    pub fn create_virtual_buffer_with_content(
2181        &self,
2182        name: String,
2183        mode: String,
2184        read_only: bool,
2185        entries: Vec<TextPropertyEntry>,
2186    ) -> Result<(), String> {
2187        self.send_command(PluginCommand::CreateVirtualBufferWithContent {
2188            name,
2189            mode,
2190            read_only,
2191            entries,
2192            show_line_numbers: true,
2193            show_cursors: true,
2194            editing_disabled: false,
2195            hidden_from_tabs: false,
2196            request_id: None,
2197        })
2198    }
2199
2200    /// Set the content of a virtual buffer with text properties
2201    ///
2202    /// Each entry contains text and metadata properties (e.g., source location).
2203    pub fn set_virtual_buffer_content(
2204        &self,
2205        buffer_id: BufferId,
2206        entries: Vec<TextPropertyEntry>,
2207    ) -> Result<(), String> {
2208        self.send_command(PluginCommand::SetVirtualBufferContent { buffer_id, entries })
2209    }
2210
2211    /// Get text properties at cursor position in a buffer
2212    ///
2213    /// This triggers a command that will make properties available to plugins.
2214    pub fn get_text_properties_at_cursor(&self, buffer_id: BufferId) -> Result<(), String> {
2215        self.send_command(PluginCommand::GetTextPropertiesAtCursor { buffer_id })
2216    }
2217
2218    /// Define a buffer mode with keybindings
2219    ///
2220    /// Modes can inherit from parent modes (e.g., "diagnostics-list" inherits from "special").
2221    /// Bindings are specified as (key_string, command_name) pairs.
2222    pub fn define_mode(
2223        &self,
2224        name: String,
2225        parent: Option<String>,
2226        bindings: Vec<(String, String)>,
2227        read_only: bool,
2228    ) -> Result<(), String> {
2229        self.send_command(PluginCommand::DefineMode {
2230            name,
2231            parent,
2232            bindings,
2233            read_only,
2234        })
2235    }
2236
2237    /// Switch the current split to display a buffer
2238    pub fn show_buffer(&self, buffer_id: BufferId) -> Result<(), String> {
2239        self.send_command(PluginCommand::ShowBuffer { buffer_id })
2240    }
2241
2242    /// Set the scroll position of a specific split
2243    pub fn set_split_scroll(&self, split_id: usize, top_byte: usize) -> Result<(), String> {
2244        self.send_command(PluginCommand::SetSplitScroll {
2245            split_id: SplitId(split_id),
2246            top_byte,
2247        })
2248    }
2249
2250    /// Request syntax highlights for a buffer range
2251    pub fn get_highlights(
2252        &self,
2253        buffer_id: BufferId,
2254        range: Range<usize>,
2255        request_id: u64,
2256    ) -> Result<(), String> {
2257        self.send_command(PluginCommand::RequestHighlights {
2258            buffer_id,
2259            range,
2260            request_id,
2261        })
2262    }
2263
2264    // === Query Methods ===
2265
2266    /// Get the currently active buffer ID
2267    pub fn get_active_buffer_id(&self) -> BufferId {
2268        let snapshot = self.state_snapshot.read().unwrap();
2269        snapshot.active_buffer_id
2270    }
2271
2272    /// Get the currently active split ID
2273    pub fn get_active_split_id(&self) -> usize {
2274        let snapshot = self.state_snapshot.read().unwrap();
2275        snapshot.active_split_id
2276    }
2277
2278    /// Get information about a specific buffer
2279    pub fn get_buffer_info(&self, buffer_id: BufferId) -> Option<BufferInfo> {
2280        let snapshot = self.state_snapshot.read().unwrap();
2281        snapshot.buffers.get(&buffer_id).cloned()
2282    }
2283
2284    /// Get all buffer IDs
2285    pub fn list_buffers(&self) -> Vec<BufferInfo> {
2286        let snapshot = self.state_snapshot.read().unwrap();
2287        snapshot.buffers.values().cloned().collect()
2288    }
2289
2290    /// Get primary cursor information for the active buffer
2291    pub fn get_primary_cursor(&self) -> Option<CursorInfo> {
2292        let snapshot = self.state_snapshot.read().unwrap();
2293        snapshot.primary_cursor.clone()
2294    }
2295
2296    /// Get all cursor information for the active buffer
2297    pub fn get_all_cursors(&self) -> Vec<CursorInfo> {
2298        let snapshot = self.state_snapshot.read().unwrap();
2299        snapshot.all_cursors.clone()
2300    }
2301
2302    /// Get viewport information for the active buffer
2303    pub fn get_viewport(&self) -> Option<ViewportInfo> {
2304        let snapshot = self.state_snapshot.read().unwrap();
2305        snapshot.viewport.clone()
2306    }
2307
2308    /// Get access to the state snapshot Arc (for internal use)
2309    pub fn state_snapshot_handle(&self) -> Arc<RwLock<EditorStateSnapshot>> {
2310        Arc::clone(&self.state_snapshot)
2311    }
2312}
2313
2314impl Clone for PluginApi {
2315    fn clone(&self) -> Self {
2316        Self {
2317            hooks: Arc::clone(&self.hooks),
2318            commands: Arc::clone(&self.commands),
2319            command_sender: self.command_sender.clone(),
2320            state_snapshot: Arc::clone(&self.state_snapshot),
2321        }
2322    }
2323}
2324
2325#[cfg(test)]
2326mod tests {
2327    use super::*;
2328
2329    #[test]
2330    fn test_plugin_api_creation() {
2331        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2332        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2333        let (tx, _rx) = std::sync::mpsc::channel();
2334        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2335
2336        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2337
2338        // Should not panic
2339        let _clone = api.clone();
2340    }
2341
2342    #[test]
2343    fn test_register_hook() {
2344        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2345        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2346        let (tx, _rx) = std::sync::mpsc::channel();
2347        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2348
2349        let api = PluginApi::new(hooks.clone(), commands, tx, state_snapshot);
2350
2351        api.register_hook("test-hook", Box::new(|_| true));
2352
2353        let hook_registry = hooks.read().unwrap();
2354        assert_eq!(hook_registry.hook_count("test-hook"), 1);
2355    }
2356
2357    #[test]
2358    fn test_send_command() {
2359        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2360        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2361        let (tx, rx) = std::sync::mpsc::channel();
2362        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2363
2364        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2365
2366        let result = api.insert_text(BufferId(1), 0, "test".to_string());
2367        assert!(result.is_ok());
2368
2369        // Verify command was sent
2370        let received = rx.try_recv();
2371        assert!(received.is_ok());
2372
2373        match received.unwrap() {
2374            PluginCommand::InsertText {
2375                buffer_id,
2376                position,
2377                text,
2378            } => {
2379                assert_eq!(buffer_id.0, 1);
2380                assert_eq!(position, 0);
2381                assert_eq!(text, "test");
2382            }
2383            _ => panic!("Wrong command type"),
2384        }
2385    }
2386
2387    #[test]
2388    fn test_add_overlay_command() {
2389        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2390        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2391        let (tx, rx) = std::sync::mpsc::channel();
2392        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2393
2394        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2395
2396        let result = api.add_overlay(
2397            BufferId(1),
2398            Some("test-overlay".to_string()),
2399            0..10,
2400            OverlayOptions {
2401                fg: Some(OverlayColorSpec::ThemeKey("ui.status_bar_fg".to_string())),
2402                bg: None,
2403                underline: true,
2404                bold: false,
2405                italic: false,
2406                extend_to_line_end: false,
2407            },
2408        );
2409        assert!(result.is_ok());
2410
2411        let received = rx.try_recv().unwrap();
2412        match received {
2413            PluginCommand::AddOverlay {
2414                buffer_id,
2415                namespace,
2416                range,
2417                options,
2418            } => {
2419                assert_eq!(buffer_id.0, 1);
2420                assert_eq!(namespace.as_ref().map(|n| n.as_str()), Some("test-overlay"));
2421                assert_eq!(range, 0..10);
2422                assert!(matches!(
2423                    options.fg,
2424                    Some(OverlayColorSpec::ThemeKey(ref k)) if k == "ui.status_bar_fg"
2425                ));
2426                assert!(options.bg.is_none());
2427                assert!(options.underline);
2428                assert!(!options.bold);
2429                assert!(!options.italic);
2430                assert!(!options.extend_to_line_end);
2431            }
2432            _ => panic!("Wrong command type"),
2433        }
2434    }
2435
2436    #[test]
2437    fn test_set_status_command() {
2438        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2439        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2440        let (tx, rx) = std::sync::mpsc::channel();
2441        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2442
2443        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2444
2445        let result = api.set_status("Test status".to_string());
2446        assert!(result.is_ok());
2447
2448        let received = rx.try_recv().unwrap();
2449        match received {
2450            PluginCommand::SetStatus { message } => {
2451                assert_eq!(message, "Test status");
2452            }
2453            _ => panic!("Wrong command type"),
2454        }
2455    }
2456
2457    #[test]
2458    fn test_get_active_buffer_id() {
2459        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2460        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2461        let (tx, _rx) = std::sync::mpsc::channel();
2462        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2463
2464        // Set active buffer to 5
2465        {
2466            let mut snapshot = state_snapshot.write().unwrap();
2467            snapshot.active_buffer_id = BufferId(5);
2468        }
2469
2470        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2471
2472        let active_id = api.get_active_buffer_id();
2473        assert_eq!(active_id.0, 5);
2474    }
2475
2476    #[test]
2477    fn test_get_buffer_info() {
2478        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2479        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2480        let (tx, _rx) = std::sync::mpsc::channel();
2481        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2482
2483        // Add buffer info
2484        {
2485            let mut snapshot = state_snapshot.write().unwrap();
2486            let buffer_info = BufferInfo {
2487                id: BufferId(1),
2488                path: Some(std::path::PathBuf::from("/test/file.txt")),
2489                modified: true,
2490                length: 100,
2491            };
2492            snapshot.buffers.insert(BufferId(1), buffer_info);
2493        }
2494
2495        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2496
2497        let info = api.get_buffer_info(BufferId(1));
2498        assert!(info.is_some());
2499        let info = info.unwrap();
2500        assert_eq!(info.id.0, 1);
2501        assert_eq!(
2502            info.path.as_ref().unwrap().to_str().unwrap(),
2503            "/test/file.txt"
2504        );
2505        assert!(info.modified);
2506        assert_eq!(info.length, 100);
2507
2508        // Non-existent buffer
2509        let no_info = api.get_buffer_info(BufferId(999));
2510        assert!(no_info.is_none());
2511    }
2512
2513    #[test]
2514    fn test_list_buffers() {
2515        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2516        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2517        let (tx, _rx) = std::sync::mpsc::channel();
2518        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2519
2520        // Add multiple buffers
2521        {
2522            let mut snapshot = state_snapshot.write().unwrap();
2523            snapshot.buffers.insert(
2524                BufferId(1),
2525                BufferInfo {
2526                    id: BufferId(1),
2527                    path: Some(std::path::PathBuf::from("/file1.txt")),
2528                    modified: false,
2529                    length: 50,
2530                },
2531            );
2532            snapshot.buffers.insert(
2533                BufferId(2),
2534                BufferInfo {
2535                    id: BufferId(2),
2536                    path: Some(std::path::PathBuf::from("/file2.txt")),
2537                    modified: true,
2538                    length: 100,
2539                },
2540            );
2541            snapshot.buffers.insert(
2542                BufferId(3),
2543                BufferInfo {
2544                    id: BufferId(3),
2545                    path: None,
2546                    modified: false,
2547                    length: 0,
2548                },
2549            );
2550        }
2551
2552        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2553
2554        let buffers = api.list_buffers();
2555        assert_eq!(buffers.len(), 3);
2556
2557        // Verify all buffers are present
2558        assert!(buffers.iter().any(|b| b.id.0 == 1));
2559        assert!(buffers.iter().any(|b| b.id.0 == 2));
2560        assert!(buffers.iter().any(|b| b.id.0 == 3));
2561    }
2562
2563    #[test]
2564    fn test_get_primary_cursor() {
2565        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2566        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2567        let (tx, _rx) = std::sync::mpsc::channel();
2568        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2569
2570        // Add cursor info
2571        {
2572            let mut snapshot = state_snapshot.write().unwrap();
2573            snapshot.primary_cursor = Some(CursorInfo {
2574                position: 42,
2575                selection: Some(10..42),
2576            });
2577        }
2578
2579        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2580
2581        let cursor = api.get_primary_cursor();
2582        assert!(cursor.is_some());
2583        let cursor = cursor.unwrap();
2584        assert_eq!(cursor.position, 42);
2585        assert_eq!(cursor.selection, Some(10..42));
2586    }
2587
2588    #[test]
2589    fn test_get_all_cursors() {
2590        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2591        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2592        let (tx, _rx) = std::sync::mpsc::channel();
2593        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2594
2595        // Add multiple cursors
2596        {
2597            let mut snapshot = state_snapshot.write().unwrap();
2598            snapshot.all_cursors = vec![
2599                CursorInfo {
2600                    position: 10,
2601                    selection: None,
2602                },
2603                CursorInfo {
2604                    position: 20,
2605                    selection: Some(15..20),
2606                },
2607                CursorInfo {
2608                    position: 30,
2609                    selection: Some(25..30),
2610                },
2611            ];
2612        }
2613
2614        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2615
2616        let cursors = api.get_all_cursors();
2617        assert_eq!(cursors.len(), 3);
2618        assert_eq!(cursors[0].position, 10);
2619        assert_eq!(cursors[0].selection, None);
2620        assert_eq!(cursors[1].position, 20);
2621        assert_eq!(cursors[1].selection, Some(15..20));
2622        assert_eq!(cursors[2].position, 30);
2623        assert_eq!(cursors[2].selection, Some(25..30));
2624    }
2625
2626    #[test]
2627    fn test_get_viewport() {
2628        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2629        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2630        let (tx, _rx) = std::sync::mpsc::channel();
2631        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2632
2633        // Add viewport info
2634        {
2635            let mut snapshot = state_snapshot.write().unwrap();
2636            snapshot.viewport = Some(ViewportInfo {
2637                top_byte: 100,
2638                left_column: 5,
2639                width: 80,
2640                height: 24,
2641            });
2642        }
2643
2644        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2645
2646        let viewport = api.get_viewport();
2647        assert!(viewport.is_some());
2648        let viewport = viewport.unwrap();
2649        assert_eq!(viewport.top_byte, 100);
2650        assert_eq!(viewport.left_column, 5);
2651        assert_eq!(viewport.width, 80);
2652        assert_eq!(viewport.height, 24);
2653    }
2654
2655    #[test]
2656    fn test_composite_buffer_options_rejects_unknown_fields() {
2657        // Valid JSON with correct field names
2658        let valid_json = r#"{
2659            "name": "test",
2660            "mode": "diff",
2661            "layout": {"type": "side-by-side", "ratios": [0.5, 0.5], "showSeparator": true},
2662            "sources": [{"bufferId": 1, "label": "old"}]
2663        }"#;
2664        let result: Result<CreateCompositeBufferOptions, _> = serde_json::from_str(valid_json);
2665        assert!(
2666            result.is_ok(),
2667            "Valid JSON should parse: {:?}",
2668            result.err()
2669        );
2670
2671        // Invalid JSON with unknown field (buffer_id instead of bufferId)
2672        let invalid_json = r#"{
2673            "name": "test",
2674            "mode": "diff",
2675            "layout": {"type": "side-by-side", "ratios": [0.5, 0.5], "showSeparator": true},
2676            "sources": [{"buffer_id": 1, "label": "old"}]
2677        }"#;
2678        let result: Result<CreateCompositeBufferOptions, _> = serde_json::from_str(invalid_json);
2679        assert!(
2680            result.is_err(),
2681            "JSON with unknown field should fail to parse"
2682        );
2683        let err = result.unwrap_err().to_string();
2684        assert!(
2685            err.contains("unknown field") || err.contains("buffer_id"),
2686            "Error should mention unknown field: {}",
2687            err
2688        );
2689    }
2690
2691    #[test]
2692    fn test_composite_hunk_rejects_unknown_fields() {
2693        // Valid JSON with correct field names
2694        let valid_json = r#"{"oldStart": 0, "oldCount": 5, "newStart": 0, "newCount": 7}"#;
2695        let result: Result<CompositeHunk, _> = serde_json::from_str(valid_json);
2696        assert!(
2697            result.is_ok(),
2698            "Valid JSON should parse: {:?}",
2699            result.err()
2700        );
2701
2702        // Invalid JSON with unknown field (old_start instead of oldStart)
2703        let invalid_json = r#"{"old_start": 0, "oldCount": 5, "newStart": 0, "newCount": 7}"#;
2704        let result: Result<CompositeHunk, _> = serde_json::from_str(invalid_json);
2705        assert!(
2706            result.is_err(),
2707            "JSON with unknown field should fail to parse"
2708        );
2709        let err = result.unwrap_err().to_string();
2710        assert!(
2711            err.contains("unknown field") || err.contains("old_start"),
2712            "Error should mention unknown field: {}",
2713            err
2714        );
2715    }
2716}