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};
15
16/// A change to derived editor state (highlighting, folding, etc.).
17#[derive(Debug, Clone)]
18pub enum ProcessingEdit {
19    /// Replace an entire style layer with the given intervals (char offsets).
20    ReplaceStyleLayer {
21        /// The style layer being replaced.
22        layer: StyleLayerId,
23        /// The full set of style intervals for the layer (char offsets, half-open).
24        intervals: Vec<Interval>,
25    },
26    /// Clear a style layer.
27    ClearStyleLayer {
28        /// The style layer being cleared.
29        layer: StyleLayerId,
30    },
31    /// Replace folding regions.
32    ///
33    /// If `preserve_collapsed` is true, regions that match an existing collapsed region
34    /// (`start_line`, `end_line`) will remain collapsed after replacement.
35    ReplaceFoldingRegions {
36        /// The complete set of folding regions.
37        regions: Vec<FoldRegion>,
38        /// Whether to preserve the collapsed/expanded state for regions that still exist.
39        preserve_collapsed: bool,
40    },
41    /// Clear all folding regions.
42    ClearFoldingRegions,
43    /// Replace the current diagnostic list (character offsets).
44    ReplaceDiagnostics {
45        /// Full diagnostic list for the document.
46        diagnostics: Vec<Diagnostic>,
47    },
48    /// Clear all diagnostics.
49    ClearDiagnostics,
50    /// Replace a decoration layer wholesale.
51    ReplaceDecorations {
52        /// Decoration layer being replaced.
53        layer: DecorationLayerId,
54        /// Full decoration list for the layer (character offsets).
55        decorations: Vec<Decoration>,
56    },
57    /// Clear a decoration layer.
58    ClearDecorations {
59        /// Decoration layer being cleared.
60        layer: DecorationLayerId,
61    },
62}
63
64/// A generic processor that produces [`ProcessingEdit`]s for an editor document.
65pub trait DocumentProcessor {
66    /// The error type returned by [`DocumentProcessor::process`].
67    type Error;
68
69    /// Compute derived state updates to apply to the editor.
70    ///
71    /// Implementations should avoid mutating `state`; instead, return edits that the caller can
72    /// apply (e.g. via [`EditorStateManager::apply_processing_edits`](crate::EditorStateManager::apply_processing_edits)).
73    fn process(&mut self, state: &EditorStateManager) -> Result<Vec<ProcessingEdit>, Self::Error>;
74}