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    /// Producers with asynchronous document versions must discard stale responses before creating
37    /// this edit; core applies it as an authoritative replacement for the current document state.
38    /// If `preserve_collapsed` is true, regions that match an existing collapsed derived region,
39    /// including conservative matches across small line-number drift, remain collapsed.
40    ReplaceFoldingRegions {
41        /// The complete set of folding regions.
42        regions: Vec<FoldRegion>,
43        /// Whether to preserve the collapsed/expanded state for regions that still exist.
44        preserve_collapsed: bool,
45    },
46    /// Clear all derived folding regions (leaves user-created folds intact).
47    ClearFoldingRegions,
48    /// Replace the current diagnostic list (character offsets).
49    ReplaceDiagnostics {
50        /// Full diagnostic list for the document.
51        diagnostics: Vec<Diagnostic>,
52    },
53    /// Clear all diagnostics.
54    ClearDiagnostics,
55    /// Replace a decoration layer wholesale.
56    ReplaceDecorations {
57        /// Decoration layer being replaced.
58        layer: DecorationLayerId,
59        /// Full decoration list for the layer (character offsets).
60        decorations: Vec<Decoration>,
61    },
62    /// Clear a decoration layer.
63    ClearDecorations {
64        /// Decoration layer being cleared.
65        layer: DecorationLayerId,
66    },
67    /// Replace the document outline / symbol tree wholesale.
68    ReplaceDocumentSymbols {
69        /// The full document outline.
70        symbols: DocumentOutline,
71    },
72    /// Clear the current document symbol set.
73    ClearDocumentSymbols,
74}
75
76/// A generic processor that produces [`ProcessingEdit`]s for an editor document.
77pub trait DocumentProcessor {
78    /// The error type returned by [`DocumentProcessor::process`].
79    type Error;
80
81    /// Compute derived state updates to apply to the editor.
82    ///
83    /// Implementations should avoid mutating `state`; instead, return edits that the caller can
84    /// apply (e.g. via [`EditorStateManager::apply_processing_edits`](crate::EditorStateManager::apply_processing_edits)).
85    fn process(&mut self, state: &EditorStateManager) -> Result<Vec<ProcessingEdit>, Self::Error>;
86}