Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T03:33:50.210Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

preview.rs

140 lines · 4.5 KB · rust
1use std::ops::Range;
2use std::path::PathBuf;
3use std::sync::Arc;
4
5use gpui::{AnyElement, App, Entity, IntoElement, Window};
6use language::{Anchor, Buffer, HighlightedText};
7use project::Symbol;
8
9/// The editor-agnostic interface a [`Picker`](crate::Picker) uses to drive its
10/// preview.
11pub trait PreviewBackend: 'static {
12    fn update(&self, update: Update, window: &mut Window, cx: &mut App);
13    fn render(&self, layout: Layout, cx: &mut App) -> AnyElement;
14    /// Called after a resize to let the preview do resizing logic like scrolling.
15    fn adjust_to_new_size(&self, window: &mut Window, cx: &mut App);
16    /// Empty the preview and show a placeholder message.
17    fn clear(&self, cx: &mut App);
18}
19
20/// The preview window of a [`Picker`](crate::Picker).
21pub struct Preview {
22    content: Arc<dyn PreviewBackend>,
23    pub(crate) layout: Layout,
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
27pub enum Layout {
28    #[default]
29    Hidden,
30    Below,
31    Right,
32}
33
34impl Preview {
35    pub fn new(content: Arc<dyn PreviewBackend>) -> Self {
36        Preview {
37            content,
38            layout: Layout::default(),
39        }
40    }
41
42    pub fn update(&mut self, update: Update, window: &mut Window, cx: &mut App) {
43        self.content.update(update, window, cx);
44    }
45
46    pub fn render(&self, cx: &mut App) -> impl IntoElement {
47        self.content.render(self.layout, cx)
48    }
49
50    pub fn adjust_to_new_size(&self, window: &mut Window, cx: &mut App) {
51        self.content.adjust_to_new_size(window, cx);
52    }
53
54    pub(crate) fn clear(&self, cx: &mut App) {
55        self.content.clear(cx);
56    }
57}
58
59/// Identifies what a preview should show.
60pub enum PreviewSource {
61    /// The buffer is identified by its absolute path; the preview opens it.
62    ///
63    /// Used by pickers (like the file finder) that only know the path of the
64    /// match.
65    Path(PathBuf),
66    /// The buffer is provided directly.
67    ///
68    /// Used by pickers (like the text picker) that already hold the matched
69    /// buffer.
70    Buffer(Entity<Buffer>),
71    /// The buffer is identified by a project symbol; the preview opens it and
72    /// highlights the symbol's range.
73    ///
74    /// Used by pickers (like the project symbols picker) that only know the
75    /// matched symbol. The highlight is derived from the symbol once its buffer
76    /// loads, so callers don't supply a [`MatchLocation`].
77    Symbol(Symbol),
78    /// No buffer to show; display this message centered in the preview instead.
79    ///
80    /// Used by pickers that have a selection without a previewable buffer (like
81    /// the file finder's "create new file" entry). Built as a [`HighlightedText`]
82    /// so callers can emphasize parts of the message (e.g. a file path).
83    Message(HighlightedText),
84}
85
86pub struct MatchLocation {
87    /// The location of the match (for highlighting)
88    pub anchor_range: Range<Anchor>,
89    /// The location of the match as an offset (for scrolling)
90    pub range: Range<usize>,
91}
92
93/// An update for the [`Preview`] window of a [`Picker`](crate::Picker).
94pub struct Update {
95    /// Where to source the buffer to preview.
96    pub source: PreviewSource,
97    /// The location to highlight and scroll to, if any.
98    pub match_location: Option<MatchLocation>,
99}
100
101impl Update {
102    /// Preview the buffer at `abs_path` without highlighting anything.
103    pub fn from_path(abs_path: PathBuf) -> Self {
104        Self {
105            source: PreviewSource::Path(abs_path),
106            match_location: None,
107        }
108    }
109
110    /// Show `message` centered in the preview instead of a buffer.
111    ///
112    /// The message is a [`HighlightedText`], so parts of it (e.g. a file path)
113    /// can be emphasized.
114    pub fn message(message: HighlightedText) -> Self {
115        Self {
116            source: PreviewSource::Message(message),
117            match_location: None,
118        }
119    }
120
121    /// Preview `buffer`, highlighting and scrolling to `highlight`.
122    pub fn from_buffer(buffer: Entity<Buffer>, highlight: MatchLocation) -> Self {
123        Self {
124            source: PreviewSource::Buffer(buffer),
125            match_location: Some(highlight),
126        }
127    }
128
129    /// Preview the buffer for `symbol`, highlighting and scrolling to its range.
130    ///
131    /// The buffer is opened and the highlight derived by the preview once the
132    /// buffer loads.
133    pub fn from_symbol(symbol: Symbol) -> Self {
134        Self {
135            source: PreviewSource::Symbol(symbol),
136            match_location: None,
137        }
138    }
139}
140
Served at tenant.openagents/omega Member data and write actions are omitted.