Skip to main content

editor_core/
processing.rs

1//! Generic document processing interfaces.
2//!
3//! This module defines a shared "edit" format for derived editor state, such as:
4//! - syntax / semantic highlighting (style layers)
5//! - folding regions
6//!
7//! External crates (`editor-core-*`) can produce [`ProcessingEdit`] values and apply them to an
8//! [`EditorStateManager`] via
9//! [`EditorStateManager::apply_processing_edits`](crate::EditorStateManager::apply_processing_edits).
10
11use crate::EditorStateManager;
12use crate::decorations::{Decoration, DecorationLayerId};
13use crate::diagnostics::Diagnostic;
14use crate::intervals::{FoldRegion, Interval, StyleLayerId};
15use crate::symbols::DocumentOutline;
16
17/// A change to derived editor state (highlighting, folding, etc.).
18#[derive(Debug, Clone)]
19pub enum ProcessingEdit {
20    /// Replace an entire style layer with the given intervals (char offsets).
21    ReplaceStyleLayer {
22        /// The style layer being replaced.
23        layer: StyleLayerId,
24        /// The full set of style intervals for the layer (char offsets, half-open).
25        intervals: Vec<Interval>,
26    },
27    /// Clear a style layer.
28    ClearStyleLayer {
29        /// The style layer being cleared.
30        layer: StyleLayerId,
31    },
32    /// Replace folding regions.
33    ///
34    /// This affects the **derived** fold set (from external providers), leaving user-created folds intact.
35    ///
36    /// If `preserve_collapsed` is true, regions that match an existing collapsed region
37    /// (`start_line`, `end_line`) will remain collapsed after replacement.
38    ReplaceFoldingRegions {
39        /// The complete set of folding regions.
40        regions: Vec<FoldRegion>,
41        /// Whether to preserve the collapsed/expanded state for regions that still exist.
42        preserve_collapsed: bool,
43    },
44    /// Clear all derived folding regions (leaves user-created folds intact).
45    ClearFoldingRegions,
46    /// Replace the current diagnostic list (character offsets).
47    ReplaceDiagnostics {
48        /// Full diagnostic list for the document.
49        diagnostics: Vec<Diagnostic>,
50    },
51    /// Clear all diagnostics.
52    ClearDiagnostics,
53    /// Replace a decoration layer wholesale.
54    ReplaceDecorations {
55        /// Decoration layer being replaced.
56        layer: DecorationLayerId,
57        /// Full decoration list for the layer (character offsets).
58        decorations: Vec<Decoration>,
59    },
60    /// Clear a decoration layer.
61    ClearDecorations {
62        /// Decoration layer being cleared.
63        layer: DecorationLayerId,
64    },
65    /// Replace the document outline / symbol tree wholesale.
66    ReplaceDocumentSymbols {
67        /// The full document outline.
68        symbols: DocumentOutline,
69    },
70    /// Clear the current document symbol set.
71    ClearDocumentSymbols,
72}
73
74/// A generic processor that produces [`ProcessingEdit`]s for an editor document.
75pub trait DocumentProcessor {
76    /// The error type returned by [`DocumentProcessor::process`].
77    type Error;
78
79    /// Compute derived state updates to apply to the editor.
80    ///
81    /// Implementations should avoid mutating `state`; instead, return edits that the caller can
82    /// apply (e.g. via [`EditorStateManager::apply_processing_edits`](crate::EditorStateManager::apply_processing_edits)).
83    fn process(&mut self, state: &EditorStateManager) -> Result<Vec<ProcessingEdit>, Self::Error>;
84}