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 (.tmLanguage.json or .sublime-syntax)
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    /// Formatter configuration
1454    #[serde(default)]
1455    pub formatter: Option<FormatterPackConfig>,
1456}
1457
1458/// Formatter configuration for language packs
1459#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1460#[serde(rename_all = "camelCase")]
1461#[ts(export)]
1462pub struct FormatterPackConfig {
1463    /// Command to run (e.g., "prettier", "rustfmt")
1464    pub command: String,
1465
1466    /// Arguments to pass to the formatter
1467    #[serde(default)]
1468    pub args: Vec<String>,
1469}
1470
1471/// LSP server configuration for language packs
1472#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1473#[serde(rename_all = "camelCase")]
1474#[ts(export)]
1475pub struct LspServerPackConfig {
1476    /// Command to start the LSP server
1477    pub command: String,
1478
1479    /// Arguments to pass to the command
1480    #[serde(default)]
1481    pub args: Vec<String>,
1482
1483    /// Whether to auto-start the server when a matching file is opened
1484    #[serde(default)]
1485    pub auto_start: Option<bool>,
1486
1487    /// LSP initialization options
1488    #[serde(default)]
1489    #[ts(type = "Record<string, unknown> | null")]
1490    pub initialization_options: Option<JsonValue>,
1491}
1492
1493/// Hunk status for Review Diff
1494#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, TS)]
1495#[ts(export)]
1496pub enum HunkStatus {
1497    Pending,
1498    Staged,
1499    Discarded,
1500}
1501
1502/// A high-level hunk directive for the Review Diff tool
1503#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1504#[ts(export)]
1505pub struct ReviewHunk {
1506    pub id: String,
1507    pub file: String,
1508    pub context_header: String,
1509    pub status: HunkStatus,
1510    /// 0-indexed line range in the base (HEAD) version
1511    pub base_range: Option<(usize, usize)>,
1512    /// 0-indexed line range in the modified (Working) version
1513    pub modified_range: Option<(usize, usize)>,
1514}
1515
1516/// Action button for action popups
1517#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1518#[serde(deny_unknown_fields)]
1519#[ts(export, rename = "TsActionPopupAction")]
1520pub struct ActionPopupAction {
1521    /// Unique action identifier (returned in ActionPopupResult)
1522    pub id: String,
1523    /// Display text for the button (can include command hints)
1524    pub label: String,
1525}
1526
1527/// Options for showActionPopup
1528#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1529#[serde(deny_unknown_fields)]
1530#[ts(export)]
1531pub struct ActionPopupOptions {
1532    /// Unique identifier for the popup (used in ActionPopupResult)
1533    pub id: String,
1534    /// Title text for the popup
1535    pub title: String,
1536    /// Body message (supports basic formatting)
1537    pub message: String,
1538    /// Action buttons to display
1539    pub actions: Vec<ActionPopupAction>,
1540}
1541
1542/// Syntax highlight span for a buffer range
1543#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1544#[ts(export)]
1545pub struct TsHighlightSpan {
1546    pub start: u32,
1547    pub end: u32,
1548    #[ts(type = "[number, number, number]")]
1549    pub color: (u8, u8, u8),
1550    pub bold: bool,
1551    pub italic: bool,
1552}
1553
1554/// Result from spawning a process with spawnProcess
1555#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1556#[ts(export)]
1557pub struct SpawnResult {
1558    /// Complete stdout as string
1559    pub stdout: String,
1560    /// Complete stderr as string
1561    pub stderr: String,
1562    /// Process exit code (0 usually means success, -1 if killed)
1563    pub exit_code: i32,
1564}
1565
1566/// Result from spawning a background process
1567#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1568#[ts(export)]
1569pub struct BackgroundProcessResult {
1570    /// Unique process ID for later reference
1571    #[ts(type = "number")]
1572    pub process_id: u64,
1573    /// Process exit code (0 usually means success, -1 if killed)
1574    /// Only present when the process has exited
1575    pub exit_code: i32,
1576}
1577
1578/// Entry for virtual buffer content with optional text properties (JS API version)
1579#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1580#[serde(deny_unknown_fields)]
1581#[ts(export, rename = "TextPropertyEntry")]
1582pub struct JsTextPropertyEntry {
1583    /// Text content for this entry
1584    pub text: String,
1585    /// Optional properties attached to this text (e.g., file path, line number)
1586    #[serde(default)]
1587    #[ts(optional, type = "Record<string, unknown>")]
1588    pub properties: Option<HashMap<String, JsonValue>>,
1589}
1590
1591/// Directory entry returned by readDir
1592#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1593#[ts(export)]
1594pub struct DirEntry {
1595    /// File/directory name
1596    pub name: String,
1597    /// True if this is a file
1598    pub is_file: bool,
1599    /// True if this is a directory
1600    pub is_dir: bool,
1601}
1602
1603/// Position in a document (line and character)
1604#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1605#[ts(export)]
1606pub struct JsPosition {
1607    /// Zero-indexed line number
1608    pub line: u32,
1609    /// Zero-indexed character offset
1610    pub character: u32,
1611}
1612
1613/// Range in a document (start and end positions)
1614#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1615#[ts(export)]
1616pub struct JsRange {
1617    /// Start position
1618    pub start: JsPosition,
1619    /// End position
1620    pub end: JsPosition,
1621}
1622
1623/// Diagnostic from LSP
1624#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1625#[ts(export)]
1626pub struct JsDiagnostic {
1627    /// Document URI
1628    pub uri: String,
1629    /// Diagnostic message
1630    pub message: String,
1631    /// Severity: 1=Error, 2=Warning, 3=Info, 4=Hint, null=unknown
1632    pub severity: Option<u8>,
1633    /// Range in the document
1634    pub range: JsRange,
1635    /// Source of the diagnostic (e.g., "typescript", "eslint")
1636    #[ts(optional)]
1637    pub source: Option<String>,
1638}
1639
1640/// Options for createVirtualBuffer
1641#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1642#[serde(deny_unknown_fields)]
1643#[ts(export)]
1644pub struct CreateVirtualBufferOptions {
1645    /// Buffer name (displayed in tabs/title)
1646    pub name: String,
1647    /// Mode for keybindings (e.g., "git-log", "search-results")
1648    #[serde(default)]
1649    #[ts(optional)]
1650    pub mode: Option<String>,
1651    /// Whether buffer is read-only (default: false)
1652    #[serde(default, rename = "readOnly")]
1653    #[ts(optional, rename = "readOnly")]
1654    pub read_only: Option<bool>,
1655    /// Show line numbers in gutter (default: false)
1656    #[serde(default, rename = "showLineNumbers")]
1657    #[ts(optional, rename = "showLineNumbers")]
1658    pub show_line_numbers: Option<bool>,
1659    /// Show cursor (default: true)
1660    #[serde(default, rename = "showCursors")]
1661    #[ts(optional, rename = "showCursors")]
1662    pub show_cursors: Option<bool>,
1663    /// Disable text editing (default: false)
1664    #[serde(default, rename = "editingDisabled")]
1665    #[ts(optional, rename = "editingDisabled")]
1666    pub editing_disabled: Option<bool>,
1667    /// Hide from tab bar (default: false)
1668    #[serde(default, rename = "hiddenFromTabs")]
1669    #[ts(optional, rename = "hiddenFromTabs")]
1670    pub hidden_from_tabs: Option<bool>,
1671    /// Initial content entries with optional properties
1672    #[serde(default)]
1673    #[ts(optional)]
1674    pub entries: Option<Vec<JsTextPropertyEntry>>,
1675}
1676
1677/// Options for createVirtualBufferInSplit
1678#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1679#[serde(deny_unknown_fields)]
1680#[ts(export)]
1681pub struct CreateVirtualBufferInSplitOptions {
1682    /// Buffer name (displayed in tabs/title)
1683    pub name: String,
1684    /// Mode for keybindings (e.g., "git-log", "search-results")
1685    #[serde(default)]
1686    #[ts(optional)]
1687    pub mode: Option<String>,
1688    /// Whether buffer is read-only (default: false)
1689    #[serde(default, rename = "readOnly")]
1690    #[ts(optional, rename = "readOnly")]
1691    pub read_only: Option<bool>,
1692    /// Split ratio 0.0-1.0 (default: 0.5)
1693    #[serde(default)]
1694    #[ts(optional)]
1695    pub ratio: Option<f32>,
1696    /// Split direction: "horizontal" or "vertical"
1697    #[serde(default)]
1698    #[ts(optional)]
1699    pub direction: Option<String>,
1700    /// Panel ID to split from
1701    #[serde(default, rename = "panelId")]
1702    #[ts(optional, rename = "panelId")]
1703    pub panel_id: Option<String>,
1704    /// Show line numbers in gutter (default: true)
1705    #[serde(default, rename = "showLineNumbers")]
1706    #[ts(optional, rename = "showLineNumbers")]
1707    pub show_line_numbers: Option<bool>,
1708    /// Show cursor (default: true)
1709    #[serde(default, rename = "showCursors")]
1710    #[ts(optional, rename = "showCursors")]
1711    pub show_cursors: Option<bool>,
1712    /// Disable text editing (default: false)
1713    #[serde(default, rename = "editingDisabled")]
1714    #[ts(optional, rename = "editingDisabled")]
1715    pub editing_disabled: Option<bool>,
1716    /// Enable line wrapping
1717    #[serde(default, rename = "lineWrap")]
1718    #[ts(optional, rename = "lineWrap")]
1719    pub line_wrap: Option<bool>,
1720    /// Initial content entries with optional properties
1721    #[serde(default)]
1722    #[ts(optional)]
1723    pub entries: Option<Vec<JsTextPropertyEntry>>,
1724}
1725
1726/// Options for createVirtualBufferInExistingSplit
1727#[derive(Debug, Clone, Serialize, Deserialize, TS)]
1728#[serde(deny_unknown_fields)]
1729#[ts(export)]
1730pub struct CreateVirtualBufferInExistingSplitOptions {
1731    /// Buffer name (displayed in tabs/title)
1732    pub name: String,
1733    /// Target split ID (required)
1734    #[serde(rename = "splitId")]
1735    #[ts(rename = "splitId")]
1736    pub split_id: usize,
1737    /// Mode for keybindings (e.g., "git-log", "search-results")
1738    #[serde(default)]
1739    #[ts(optional)]
1740    pub mode: Option<String>,
1741    /// Whether buffer is read-only (default: false)
1742    #[serde(default, rename = "readOnly")]
1743    #[ts(optional, rename = "readOnly")]
1744    pub read_only: Option<bool>,
1745    /// Show line numbers in gutter (default: true)
1746    #[serde(default, rename = "showLineNumbers")]
1747    #[ts(optional, rename = "showLineNumbers")]
1748    pub show_line_numbers: Option<bool>,
1749    /// Show cursor (default: true)
1750    #[serde(default, rename = "showCursors")]
1751    #[ts(optional, rename = "showCursors")]
1752    pub show_cursors: Option<bool>,
1753    /// Disable text editing (default: false)
1754    #[serde(default, rename = "editingDisabled")]
1755    #[ts(optional, rename = "editingDisabled")]
1756    pub editing_disabled: Option<bool>,
1757    /// Enable line wrapping
1758    #[serde(default, rename = "lineWrap")]
1759    #[ts(optional, rename = "lineWrap")]
1760    pub line_wrap: Option<bool>,
1761    /// Initial content entries with optional properties
1762    #[serde(default)]
1763    #[ts(optional)]
1764    pub entries: Option<Vec<JsTextPropertyEntry>>,
1765}
1766
1767/// Result of getTextPropertiesAtCursor - array of property objects
1768///
1769/// Each element contains the properties from a text property span that overlaps
1770/// with the cursor position. Properties are dynamic key-value pairs set by plugins.
1771#[derive(Debug, Clone, Serialize, TS)]
1772#[ts(export, type = "Array<Record<string, unknown>>")]
1773pub struct TextPropertiesAtCursor(pub Vec<HashMap<String, JsonValue>>);
1774
1775// Implement FromJs for option types using rquickjs_serde
1776#[cfg(feature = "plugins")]
1777mod fromjs_impls {
1778    use super::*;
1779    use rquickjs::{Ctx, FromJs, Value};
1780
1781    impl<'js> FromJs<'js> for JsTextPropertyEntry {
1782        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1783            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1784                from: "object",
1785                to: "JsTextPropertyEntry",
1786                message: Some(e.to_string()),
1787            })
1788        }
1789    }
1790
1791    impl<'js> FromJs<'js> for CreateVirtualBufferOptions {
1792        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1793            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1794                from: "object",
1795                to: "CreateVirtualBufferOptions",
1796                message: Some(e.to_string()),
1797            })
1798        }
1799    }
1800
1801    impl<'js> FromJs<'js> for CreateVirtualBufferInSplitOptions {
1802        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1803            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1804                from: "object",
1805                to: "CreateVirtualBufferInSplitOptions",
1806                message: Some(e.to_string()),
1807            })
1808        }
1809    }
1810
1811    impl<'js> FromJs<'js> for CreateVirtualBufferInExistingSplitOptions {
1812        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1813            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1814                from: "object",
1815                to: "CreateVirtualBufferInExistingSplitOptions",
1816                message: Some(e.to_string()),
1817            })
1818        }
1819    }
1820
1821    impl<'js> rquickjs::IntoJs<'js> for TextPropertiesAtCursor {
1822        fn into_js(self, ctx: &Ctx<'js>) -> rquickjs::Result<Value<'js>> {
1823            rquickjs_serde::to_value(ctx.clone(), &self.0)
1824                .map_err(|e| rquickjs::Error::new_from_js_message("serialize", "", &e.to_string()))
1825        }
1826    }
1827
1828    // === Additional input types for type-safe plugin API ===
1829
1830    impl<'js> FromJs<'js> for ActionSpec {
1831        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1832            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1833                from: "object",
1834                to: "ActionSpec",
1835                message: Some(e.to_string()),
1836            })
1837        }
1838    }
1839
1840    impl<'js> FromJs<'js> for ActionPopupAction {
1841        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1842            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1843                from: "object",
1844                to: "ActionPopupAction",
1845                message: Some(e.to_string()),
1846            })
1847        }
1848    }
1849
1850    impl<'js> FromJs<'js> for ActionPopupOptions {
1851        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1852            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1853                from: "object",
1854                to: "ActionPopupOptions",
1855                message: Some(e.to_string()),
1856            })
1857        }
1858    }
1859
1860    impl<'js> FromJs<'js> for ViewTokenWire {
1861        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1862            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1863                from: "object",
1864                to: "ViewTokenWire",
1865                message: Some(e.to_string()),
1866            })
1867        }
1868    }
1869
1870    impl<'js> FromJs<'js> for ViewTokenStyle {
1871        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1872            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1873                from: "object",
1874                to: "ViewTokenStyle",
1875                message: Some(e.to_string()),
1876            })
1877        }
1878    }
1879
1880    impl<'js> FromJs<'js> for LayoutHints {
1881        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1882            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1883                from: "object",
1884                to: "LayoutHints",
1885                message: Some(e.to_string()),
1886            })
1887        }
1888    }
1889
1890    impl<'js> FromJs<'js> for CreateCompositeBufferOptions {
1891        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1892            // Use two-step deserialization for complex nested structures
1893            let json: serde_json::Value =
1894                rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1895                    from: "object",
1896                    to: "CreateCompositeBufferOptions (json)",
1897                    message: Some(e.to_string()),
1898                })?;
1899            serde_json::from_value(json).map_err(|e| rquickjs::Error::FromJs {
1900                from: "json",
1901                to: "CreateCompositeBufferOptions",
1902                message: Some(e.to_string()),
1903            })
1904        }
1905    }
1906
1907    impl<'js> FromJs<'js> for CompositeHunk {
1908        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1909            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1910                from: "object",
1911                to: "CompositeHunk",
1912                message: Some(e.to_string()),
1913            })
1914        }
1915    }
1916
1917    impl<'js> FromJs<'js> for LanguagePackConfig {
1918        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1919            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1920                from: "object",
1921                to: "LanguagePackConfig",
1922                message: Some(e.to_string()),
1923            })
1924        }
1925    }
1926
1927    impl<'js> FromJs<'js> for LspServerPackConfig {
1928        fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> rquickjs::Result<Self> {
1929            rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
1930                from: "object",
1931                to: "LspServerPackConfig",
1932                message: Some(e.to_string()),
1933            })
1934        }
1935    }
1936}
1937
1938/// Plugin API context - provides safe access to editor functionality
1939pub struct PluginApi {
1940    /// Hook registry (shared with editor)
1941    hooks: Arc<RwLock<HookRegistry>>,
1942
1943    /// Command registry (shared with editor)
1944    commands: Arc<RwLock<CommandRegistry>>,
1945
1946    /// Command queue for sending commands to editor
1947    command_sender: std::sync::mpsc::Sender<PluginCommand>,
1948
1949    /// Snapshot of editor state (read-only for plugins)
1950    state_snapshot: Arc<RwLock<EditorStateSnapshot>>,
1951}
1952
1953impl PluginApi {
1954    /// Create a new plugin API context
1955    pub fn new(
1956        hooks: Arc<RwLock<HookRegistry>>,
1957        commands: Arc<RwLock<CommandRegistry>>,
1958        command_sender: std::sync::mpsc::Sender<PluginCommand>,
1959        state_snapshot: Arc<RwLock<EditorStateSnapshot>>,
1960    ) -> Self {
1961        Self {
1962            hooks,
1963            commands,
1964            command_sender,
1965            state_snapshot,
1966        }
1967    }
1968
1969    /// Register a hook callback
1970    pub fn register_hook(&self, hook_name: &str, callback: HookCallback) {
1971        let mut hooks = self.hooks.write().unwrap();
1972        hooks.add_hook(hook_name, callback);
1973    }
1974
1975    /// Remove all hooks for a specific name
1976    pub fn unregister_hooks(&self, hook_name: &str) {
1977        let mut hooks = self.hooks.write().unwrap();
1978        hooks.remove_hooks(hook_name);
1979    }
1980
1981    /// Register a command
1982    pub fn register_command(&self, command: Command) {
1983        let commands = self.commands.read().unwrap();
1984        commands.register(command);
1985    }
1986
1987    /// Unregister a command by name
1988    pub fn unregister_command(&self, name: &str) {
1989        let commands = self.commands.read().unwrap();
1990        commands.unregister(name);
1991    }
1992
1993    /// Send a command to the editor (async/non-blocking)
1994    pub fn send_command(&self, command: PluginCommand) -> Result<(), String> {
1995        self.command_sender
1996            .send(command)
1997            .map_err(|e| format!("Failed to send command: {}", e))
1998    }
1999
2000    /// Insert text at a position in a buffer
2001    pub fn insert_text(
2002        &self,
2003        buffer_id: BufferId,
2004        position: usize,
2005        text: String,
2006    ) -> Result<(), String> {
2007        self.send_command(PluginCommand::InsertText {
2008            buffer_id,
2009            position,
2010            text,
2011        })
2012    }
2013
2014    /// Delete a range of text from a buffer
2015    pub fn delete_range(&self, buffer_id: BufferId, range: Range<usize>) -> Result<(), String> {
2016        self.send_command(PluginCommand::DeleteRange { buffer_id, range })
2017    }
2018
2019    /// Add an overlay (decoration) to a buffer
2020    /// Add an overlay to a buffer with styling options
2021    ///
2022    /// Returns an opaque handle that can be used to remove the overlay later.
2023    ///
2024    /// Colors can be specified as RGB arrays or theme key strings.
2025    /// Theme keys are resolved at render time, so overlays update with theme changes.
2026    pub fn add_overlay(
2027        &self,
2028        buffer_id: BufferId,
2029        namespace: Option<String>,
2030        range: Range<usize>,
2031        options: OverlayOptions,
2032    ) -> Result<(), String> {
2033        self.send_command(PluginCommand::AddOverlay {
2034            buffer_id,
2035            namespace: namespace.map(OverlayNamespace::from_string),
2036            range,
2037            options,
2038        })
2039    }
2040
2041    /// Remove an overlay from a buffer by its handle
2042    pub fn remove_overlay(&self, buffer_id: BufferId, handle: String) -> Result<(), String> {
2043        self.send_command(PluginCommand::RemoveOverlay {
2044            buffer_id,
2045            handle: OverlayHandle::from_string(handle),
2046        })
2047    }
2048
2049    /// Clear all overlays in a namespace from a buffer
2050    pub fn clear_namespace(&self, buffer_id: BufferId, namespace: String) -> Result<(), String> {
2051        self.send_command(PluginCommand::ClearNamespace {
2052            buffer_id,
2053            namespace: OverlayNamespace::from_string(namespace),
2054        })
2055    }
2056
2057    /// Clear all overlays that overlap with a byte range
2058    /// Used for targeted invalidation when content changes
2059    pub fn clear_overlays_in_range(
2060        &self,
2061        buffer_id: BufferId,
2062        start: usize,
2063        end: usize,
2064    ) -> Result<(), String> {
2065        self.send_command(PluginCommand::ClearOverlaysInRange {
2066            buffer_id,
2067            start,
2068            end,
2069        })
2070    }
2071
2072    /// Set the status message
2073    pub fn set_status(&self, message: String) -> Result<(), String> {
2074        self.send_command(PluginCommand::SetStatus { message })
2075    }
2076
2077    /// Open a file at a specific line and column (1-indexed)
2078    /// This is useful for jumping to locations from git grep, LSP definitions, etc.
2079    pub fn open_file_at_location(
2080        &self,
2081        path: PathBuf,
2082        line: Option<usize>,
2083        column: Option<usize>,
2084    ) -> Result<(), String> {
2085        self.send_command(PluginCommand::OpenFileAtLocation { path, line, column })
2086    }
2087
2088    /// Open a file in a specific split at a line and column
2089    ///
2090    /// Similar to open_file_at_location but targets a specific split pane.
2091    /// The split_id is the ID of the split pane to open the file in.
2092    pub fn open_file_in_split(
2093        &self,
2094        split_id: usize,
2095        path: PathBuf,
2096        line: Option<usize>,
2097        column: Option<usize>,
2098    ) -> Result<(), String> {
2099        self.send_command(PluginCommand::OpenFileInSplit {
2100            split_id,
2101            path,
2102            line,
2103            column,
2104        })
2105    }
2106
2107    /// Start a prompt (minibuffer) with a custom type identifier
2108    /// The prompt_type is used to filter hooks in plugin code
2109    pub fn start_prompt(&self, label: String, prompt_type: String) -> Result<(), String> {
2110        self.send_command(PluginCommand::StartPrompt { label, prompt_type })
2111    }
2112
2113    /// Set the suggestions for the current prompt
2114    /// This updates the prompt's autocomplete/selection list
2115    pub fn set_prompt_suggestions(&self, suggestions: Vec<Suggestion>) -> Result<(), String> {
2116        self.send_command(PluginCommand::SetPromptSuggestions { suggestions })
2117    }
2118
2119    /// Add a menu item to an existing menu
2120    pub fn add_menu_item(
2121        &self,
2122        menu_label: String,
2123        item: MenuItem,
2124        position: MenuPosition,
2125    ) -> Result<(), String> {
2126        self.send_command(PluginCommand::AddMenuItem {
2127            menu_label,
2128            item,
2129            position,
2130        })
2131    }
2132
2133    /// Add a new top-level menu
2134    pub fn add_menu(&self, menu: Menu, position: MenuPosition) -> Result<(), String> {
2135        self.send_command(PluginCommand::AddMenu { menu, position })
2136    }
2137
2138    /// Remove a menu item from a menu
2139    pub fn remove_menu_item(&self, menu_label: String, item_label: String) -> Result<(), String> {
2140        self.send_command(PluginCommand::RemoveMenuItem {
2141            menu_label,
2142            item_label,
2143        })
2144    }
2145
2146    /// Remove a top-level menu
2147    pub fn remove_menu(&self, menu_label: String) -> Result<(), String> {
2148        self.send_command(PluginCommand::RemoveMenu { menu_label })
2149    }
2150
2151    // === Virtual Buffer Methods ===
2152
2153    /// Create a new virtual buffer (not backed by a file)
2154    ///
2155    /// Virtual buffers are used for special displays like diagnostic lists,
2156    /// search results, etc. They have their own mode for keybindings.
2157    pub fn create_virtual_buffer(
2158        &self,
2159        name: String,
2160        mode: String,
2161        read_only: bool,
2162    ) -> Result<(), String> {
2163        self.send_command(PluginCommand::CreateVirtualBuffer {
2164            name,
2165            mode,
2166            read_only,
2167        })
2168    }
2169
2170    /// Create a virtual buffer and set its content in one operation
2171    ///
2172    /// This is the preferred way to create virtual buffers since it doesn't
2173    /// require tracking the buffer ID. The buffer is created and populated
2174    /// atomically.
2175    pub fn create_virtual_buffer_with_content(
2176        &self,
2177        name: String,
2178        mode: String,
2179        read_only: bool,
2180        entries: Vec<TextPropertyEntry>,
2181    ) -> Result<(), String> {
2182        self.send_command(PluginCommand::CreateVirtualBufferWithContent {
2183            name,
2184            mode,
2185            read_only,
2186            entries,
2187            show_line_numbers: true,
2188            show_cursors: true,
2189            editing_disabled: false,
2190            hidden_from_tabs: false,
2191            request_id: None,
2192        })
2193    }
2194
2195    /// Set the content of a virtual buffer with text properties
2196    ///
2197    /// Each entry contains text and metadata properties (e.g., source location).
2198    pub fn set_virtual_buffer_content(
2199        &self,
2200        buffer_id: BufferId,
2201        entries: Vec<TextPropertyEntry>,
2202    ) -> Result<(), String> {
2203        self.send_command(PluginCommand::SetVirtualBufferContent { buffer_id, entries })
2204    }
2205
2206    /// Get text properties at cursor position in a buffer
2207    ///
2208    /// This triggers a command that will make properties available to plugins.
2209    pub fn get_text_properties_at_cursor(&self, buffer_id: BufferId) -> Result<(), String> {
2210        self.send_command(PluginCommand::GetTextPropertiesAtCursor { buffer_id })
2211    }
2212
2213    /// Define a buffer mode with keybindings
2214    ///
2215    /// Modes can inherit from parent modes (e.g., "diagnostics-list" inherits from "special").
2216    /// Bindings are specified as (key_string, command_name) pairs.
2217    pub fn define_mode(
2218        &self,
2219        name: String,
2220        parent: Option<String>,
2221        bindings: Vec<(String, String)>,
2222        read_only: bool,
2223    ) -> Result<(), String> {
2224        self.send_command(PluginCommand::DefineMode {
2225            name,
2226            parent,
2227            bindings,
2228            read_only,
2229        })
2230    }
2231
2232    /// Switch the current split to display a buffer
2233    pub fn show_buffer(&self, buffer_id: BufferId) -> Result<(), String> {
2234        self.send_command(PluginCommand::ShowBuffer { buffer_id })
2235    }
2236
2237    /// Set the scroll position of a specific split
2238    pub fn set_split_scroll(&self, split_id: usize, top_byte: usize) -> Result<(), String> {
2239        self.send_command(PluginCommand::SetSplitScroll {
2240            split_id: SplitId(split_id),
2241            top_byte,
2242        })
2243    }
2244
2245    /// Request syntax highlights for a buffer range
2246    pub fn get_highlights(
2247        &self,
2248        buffer_id: BufferId,
2249        range: Range<usize>,
2250        request_id: u64,
2251    ) -> Result<(), String> {
2252        self.send_command(PluginCommand::RequestHighlights {
2253            buffer_id,
2254            range,
2255            request_id,
2256        })
2257    }
2258
2259    // === Query Methods ===
2260
2261    /// Get the currently active buffer ID
2262    pub fn get_active_buffer_id(&self) -> BufferId {
2263        let snapshot = self.state_snapshot.read().unwrap();
2264        snapshot.active_buffer_id
2265    }
2266
2267    /// Get the currently active split ID
2268    pub fn get_active_split_id(&self) -> usize {
2269        let snapshot = self.state_snapshot.read().unwrap();
2270        snapshot.active_split_id
2271    }
2272
2273    /// Get information about a specific buffer
2274    pub fn get_buffer_info(&self, buffer_id: BufferId) -> Option<BufferInfo> {
2275        let snapshot = self.state_snapshot.read().unwrap();
2276        snapshot.buffers.get(&buffer_id).cloned()
2277    }
2278
2279    /// Get all buffer IDs
2280    pub fn list_buffers(&self) -> Vec<BufferInfo> {
2281        let snapshot = self.state_snapshot.read().unwrap();
2282        snapshot.buffers.values().cloned().collect()
2283    }
2284
2285    /// Get primary cursor information for the active buffer
2286    pub fn get_primary_cursor(&self) -> Option<CursorInfo> {
2287        let snapshot = self.state_snapshot.read().unwrap();
2288        snapshot.primary_cursor.clone()
2289    }
2290
2291    /// Get all cursor information for the active buffer
2292    pub fn get_all_cursors(&self) -> Vec<CursorInfo> {
2293        let snapshot = self.state_snapshot.read().unwrap();
2294        snapshot.all_cursors.clone()
2295    }
2296
2297    /// Get viewport information for the active buffer
2298    pub fn get_viewport(&self) -> Option<ViewportInfo> {
2299        let snapshot = self.state_snapshot.read().unwrap();
2300        snapshot.viewport.clone()
2301    }
2302
2303    /// Get access to the state snapshot Arc (for internal use)
2304    pub fn state_snapshot_handle(&self) -> Arc<RwLock<EditorStateSnapshot>> {
2305        Arc::clone(&self.state_snapshot)
2306    }
2307}
2308
2309impl Clone for PluginApi {
2310    fn clone(&self) -> Self {
2311        Self {
2312            hooks: Arc::clone(&self.hooks),
2313            commands: Arc::clone(&self.commands),
2314            command_sender: self.command_sender.clone(),
2315            state_snapshot: Arc::clone(&self.state_snapshot),
2316        }
2317    }
2318}
2319
2320#[cfg(test)]
2321mod tests {
2322    use super::*;
2323
2324    #[test]
2325    fn test_plugin_api_creation() {
2326        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2327        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2328        let (tx, _rx) = std::sync::mpsc::channel();
2329        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2330
2331        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2332
2333        // Should not panic
2334        let _clone = api.clone();
2335    }
2336
2337    #[test]
2338    fn test_register_hook() {
2339        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2340        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2341        let (tx, _rx) = std::sync::mpsc::channel();
2342        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2343
2344        let api = PluginApi::new(hooks.clone(), commands, tx, state_snapshot);
2345
2346        api.register_hook("test-hook", Box::new(|_| true));
2347
2348        let hook_registry = hooks.read().unwrap();
2349        assert_eq!(hook_registry.hook_count("test-hook"), 1);
2350    }
2351
2352    #[test]
2353    fn test_send_command() {
2354        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2355        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2356        let (tx, rx) = std::sync::mpsc::channel();
2357        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2358
2359        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2360
2361        let result = api.insert_text(BufferId(1), 0, "test".to_string());
2362        assert!(result.is_ok());
2363
2364        // Verify command was sent
2365        let received = rx.try_recv();
2366        assert!(received.is_ok());
2367
2368        match received.unwrap() {
2369            PluginCommand::InsertText {
2370                buffer_id,
2371                position,
2372                text,
2373            } => {
2374                assert_eq!(buffer_id.0, 1);
2375                assert_eq!(position, 0);
2376                assert_eq!(text, "test");
2377            }
2378            _ => panic!("Wrong command type"),
2379        }
2380    }
2381
2382    #[test]
2383    fn test_add_overlay_command() {
2384        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2385        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2386        let (tx, rx) = std::sync::mpsc::channel();
2387        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2388
2389        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2390
2391        let result = api.add_overlay(
2392            BufferId(1),
2393            Some("test-overlay".to_string()),
2394            0..10,
2395            OverlayOptions {
2396                fg: Some(OverlayColorSpec::ThemeKey("ui.status_bar_fg".to_string())),
2397                bg: None,
2398                underline: true,
2399                bold: false,
2400                italic: false,
2401                extend_to_line_end: false,
2402            },
2403        );
2404        assert!(result.is_ok());
2405
2406        let received = rx.try_recv().unwrap();
2407        match received {
2408            PluginCommand::AddOverlay {
2409                buffer_id,
2410                namespace,
2411                range,
2412                options,
2413            } => {
2414                assert_eq!(buffer_id.0, 1);
2415                assert_eq!(namespace.as_ref().map(|n| n.as_str()), Some("test-overlay"));
2416                assert_eq!(range, 0..10);
2417                assert!(matches!(
2418                    options.fg,
2419                    Some(OverlayColorSpec::ThemeKey(ref k)) if k == "ui.status_bar_fg"
2420                ));
2421                assert!(options.bg.is_none());
2422                assert!(options.underline);
2423                assert!(!options.bold);
2424                assert!(!options.italic);
2425                assert!(!options.extend_to_line_end);
2426            }
2427            _ => panic!("Wrong command type"),
2428        }
2429    }
2430
2431    #[test]
2432    fn test_set_status_command() {
2433        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2434        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2435        let (tx, rx) = std::sync::mpsc::channel();
2436        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2437
2438        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2439
2440        let result = api.set_status("Test status".to_string());
2441        assert!(result.is_ok());
2442
2443        let received = rx.try_recv().unwrap();
2444        match received {
2445            PluginCommand::SetStatus { message } => {
2446                assert_eq!(message, "Test status");
2447            }
2448            _ => panic!("Wrong command type"),
2449        }
2450    }
2451
2452    #[test]
2453    fn test_get_active_buffer_id() {
2454        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2455        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2456        let (tx, _rx) = std::sync::mpsc::channel();
2457        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2458
2459        // Set active buffer to 5
2460        {
2461            let mut snapshot = state_snapshot.write().unwrap();
2462            snapshot.active_buffer_id = BufferId(5);
2463        }
2464
2465        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2466
2467        let active_id = api.get_active_buffer_id();
2468        assert_eq!(active_id.0, 5);
2469    }
2470
2471    #[test]
2472    fn test_get_buffer_info() {
2473        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2474        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2475        let (tx, _rx) = std::sync::mpsc::channel();
2476        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2477
2478        // Add buffer info
2479        {
2480            let mut snapshot = state_snapshot.write().unwrap();
2481            let buffer_info = BufferInfo {
2482                id: BufferId(1),
2483                path: Some(std::path::PathBuf::from("/test/file.txt")),
2484                modified: true,
2485                length: 100,
2486            };
2487            snapshot.buffers.insert(BufferId(1), buffer_info);
2488        }
2489
2490        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2491
2492        let info = api.get_buffer_info(BufferId(1));
2493        assert!(info.is_some());
2494        let info = info.unwrap();
2495        assert_eq!(info.id.0, 1);
2496        assert_eq!(
2497            info.path.as_ref().unwrap().to_str().unwrap(),
2498            "/test/file.txt"
2499        );
2500        assert!(info.modified);
2501        assert_eq!(info.length, 100);
2502
2503        // Non-existent buffer
2504        let no_info = api.get_buffer_info(BufferId(999));
2505        assert!(no_info.is_none());
2506    }
2507
2508    #[test]
2509    fn test_list_buffers() {
2510        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2511        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2512        let (tx, _rx) = std::sync::mpsc::channel();
2513        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2514
2515        // Add multiple buffers
2516        {
2517            let mut snapshot = state_snapshot.write().unwrap();
2518            snapshot.buffers.insert(
2519                BufferId(1),
2520                BufferInfo {
2521                    id: BufferId(1),
2522                    path: Some(std::path::PathBuf::from("/file1.txt")),
2523                    modified: false,
2524                    length: 50,
2525                },
2526            );
2527            snapshot.buffers.insert(
2528                BufferId(2),
2529                BufferInfo {
2530                    id: BufferId(2),
2531                    path: Some(std::path::PathBuf::from("/file2.txt")),
2532                    modified: true,
2533                    length: 100,
2534                },
2535            );
2536            snapshot.buffers.insert(
2537                BufferId(3),
2538                BufferInfo {
2539                    id: BufferId(3),
2540                    path: None,
2541                    modified: false,
2542                    length: 0,
2543                },
2544            );
2545        }
2546
2547        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2548
2549        let buffers = api.list_buffers();
2550        assert_eq!(buffers.len(), 3);
2551
2552        // Verify all buffers are present
2553        assert!(buffers.iter().any(|b| b.id.0 == 1));
2554        assert!(buffers.iter().any(|b| b.id.0 == 2));
2555        assert!(buffers.iter().any(|b| b.id.0 == 3));
2556    }
2557
2558    #[test]
2559    fn test_get_primary_cursor() {
2560        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2561        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2562        let (tx, _rx) = std::sync::mpsc::channel();
2563        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2564
2565        // Add cursor info
2566        {
2567            let mut snapshot = state_snapshot.write().unwrap();
2568            snapshot.primary_cursor = Some(CursorInfo {
2569                position: 42,
2570                selection: Some(10..42),
2571            });
2572        }
2573
2574        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2575
2576        let cursor = api.get_primary_cursor();
2577        assert!(cursor.is_some());
2578        let cursor = cursor.unwrap();
2579        assert_eq!(cursor.position, 42);
2580        assert_eq!(cursor.selection, Some(10..42));
2581    }
2582
2583    #[test]
2584    fn test_get_all_cursors() {
2585        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2586        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2587        let (tx, _rx) = std::sync::mpsc::channel();
2588        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2589
2590        // Add multiple cursors
2591        {
2592            let mut snapshot = state_snapshot.write().unwrap();
2593            snapshot.all_cursors = vec![
2594                CursorInfo {
2595                    position: 10,
2596                    selection: None,
2597                },
2598                CursorInfo {
2599                    position: 20,
2600                    selection: Some(15..20),
2601                },
2602                CursorInfo {
2603                    position: 30,
2604                    selection: Some(25..30),
2605                },
2606            ];
2607        }
2608
2609        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2610
2611        let cursors = api.get_all_cursors();
2612        assert_eq!(cursors.len(), 3);
2613        assert_eq!(cursors[0].position, 10);
2614        assert_eq!(cursors[0].selection, None);
2615        assert_eq!(cursors[1].position, 20);
2616        assert_eq!(cursors[1].selection, Some(15..20));
2617        assert_eq!(cursors[2].position, 30);
2618        assert_eq!(cursors[2].selection, Some(25..30));
2619    }
2620
2621    #[test]
2622    fn test_get_viewport() {
2623        let hooks = Arc::new(RwLock::new(HookRegistry::new()));
2624        let commands = Arc::new(RwLock::new(CommandRegistry::new()));
2625        let (tx, _rx) = std::sync::mpsc::channel();
2626        let state_snapshot = Arc::new(RwLock::new(EditorStateSnapshot::new()));
2627
2628        // Add viewport info
2629        {
2630            let mut snapshot = state_snapshot.write().unwrap();
2631            snapshot.viewport = Some(ViewportInfo {
2632                top_byte: 100,
2633                left_column: 5,
2634                width: 80,
2635                height: 24,
2636            });
2637        }
2638
2639        let api = PluginApi::new(hooks, commands, tx, state_snapshot);
2640
2641        let viewport = api.get_viewport();
2642        assert!(viewport.is_some());
2643        let viewport = viewport.unwrap();
2644        assert_eq!(viewport.top_byte, 100);
2645        assert_eq!(viewport.left_column, 5);
2646        assert_eq!(viewport.width, 80);
2647        assert_eq!(viewport.height, 24);
2648    }
2649
2650    #[test]
2651    fn test_composite_buffer_options_rejects_unknown_fields() {
2652        // Valid JSON with correct field names
2653        let valid_json = r#"{
2654            "name": "test",
2655            "mode": "diff",
2656            "layout": {"type": "side-by-side", "ratios": [0.5, 0.5], "showSeparator": true},
2657            "sources": [{"bufferId": 1, "label": "old"}]
2658        }"#;
2659        let result: Result<CreateCompositeBufferOptions, _> = serde_json::from_str(valid_json);
2660        assert!(
2661            result.is_ok(),
2662            "Valid JSON should parse: {:?}",
2663            result.err()
2664        );
2665
2666        // Invalid JSON with unknown field (buffer_id instead of bufferId)
2667        let invalid_json = r#"{
2668            "name": "test",
2669            "mode": "diff",
2670            "layout": {"type": "side-by-side", "ratios": [0.5, 0.5], "showSeparator": true},
2671            "sources": [{"buffer_id": 1, "label": "old"}]
2672        }"#;
2673        let result: Result<CreateCompositeBufferOptions, _> = serde_json::from_str(invalid_json);
2674        assert!(
2675            result.is_err(),
2676            "JSON with unknown field should fail to parse"
2677        );
2678        let err = result.unwrap_err().to_string();
2679        assert!(
2680            err.contains("unknown field") || err.contains("buffer_id"),
2681            "Error should mention unknown field: {}",
2682            err
2683        );
2684    }
2685
2686    #[test]
2687    fn test_composite_hunk_rejects_unknown_fields() {
2688        // Valid JSON with correct field names
2689        let valid_json = r#"{"oldStart": 0, "oldCount": 5, "newStart": 0, "newCount": 7}"#;
2690        let result: Result<CompositeHunk, _> = serde_json::from_str(valid_json);
2691        assert!(
2692            result.is_ok(),
2693            "Valid JSON should parse: {:?}",
2694            result.err()
2695        );
2696
2697        // Invalid JSON with unknown field (old_start instead of oldStart)
2698        let invalid_json = r#"{"old_start": 0, "oldCount": 5, "newStart": 0, "newCount": 7}"#;
2699        let result: Result<CompositeHunk, _> = serde_json::from_str(invalid_json);
2700        assert!(
2701            result.is_err(),
2702            "JSON with unknown field should fail to parse"
2703        );
2704        let err = result.unwrap_err().to_string();
2705        assert!(
2706            err.contains("unknown field") || err.contains("old_start"),
2707            "Error should mention unknown field: {}",
2708            err
2709        );
2710    }
2711}