Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T03:53:27.243Z 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

code_label.rs

187 lines · 4.9 KB · rust
1use crate::highlight_map::HighlightId;
2use std::ops::Range;
3
4#[derive(Debug, Eq, PartialEq, Copy, Clone)]
5pub enum SymbolKind {
6    File,
7    Module,
8    Namespace,
9    Package,
10    Class,
11    Method,
12    Property,
13    Field,
14    Constructor,
15    Enum,
16    Interface,
17    Function,
18    Variable,
19    Constant,
20    String,
21    Number,
22    Boolean,
23    Array,
24    Object,
25    Key,
26    Null,
27    EnumMember,
28    Struct,
29    Event,
30    Operator,
31    TypeParameter,
32}
33
34impl SymbolKind {
35    pub fn from_proto(i32: i32) -> Self {
36        match i32 {
37            1 => SymbolKind::File,
38            2 => SymbolKind::Module,
39            3 => SymbolKind::Namespace,
40            4 => SymbolKind::Package,
41            5 => SymbolKind::Class,
42            6 => SymbolKind::Method,
43            7 => SymbolKind::Property,
44            8 => SymbolKind::Field,
45            9 => SymbolKind::Constructor,
46            10 => SymbolKind::Enum,
47            11 => SymbolKind::Interface,
48            12 => SymbolKind::Function,
49            13 => SymbolKind::Variable,
50            14 => SymbolKind::Constant,
51            15 => SymbolKind::String,
52            16 => SymbolKind::Number,
53            17 => SymbolKind::Boolean,
54            18 => SymbolKind::Array,
55            19 => SymbolKind::Object,
56            20 => SymbolKind::Key,
57            21 => SymbolKind::Null,
58            22 => SymbolKind::EnumMember,
59            23 => SymbolKind::Struct,
60            24 => SymbolKind::Event,
61            25 => SymbolKind::Operator,
62            26 => SymbolKind::TypeParameter,
63            _ => SymbolKind::Null,
64        }
65    }
66}
67
68#[derive(Debug, Clone)]
69pub struct Symbol {
70    pub name: String,
71    pub kind: SymbolKind,
72    pub container_name: Option<String>,
73}
74
75#[derive(Clone, Debug, Default, PartialEq, Eq)]
76pub struct CodeLabel {
77    /// The text to display.
78    pub text: String,
79    /// Syntax highlighting runs.
80    pub runs: Vec<(Range<usize>, HighlightId)>,
81    /// The portion of the text that should be used in fuzzy filtering.
82    pub filter_range: Range<usize>,
83}
84
85#[derive(Clone, Debug, Default, PartialEq, Eq)]
86pub struct CodeLabelBuilder {
87    /// The text to display.
88    text: String,
89    /// Syntax highlighting runs.
90    runs: Vec<(Range<usize>, HighlightId)>,
91    /// The portion of the text that should be used in fuzzy filtering.
92    filter_range: Range<usize>,
93}
94
95impl CodeLabel {
96    pub fn plain(text: String, filter_text: Option<&str>) -> Self {
97        Self::filtered(text.clone(), text.len(), filter_text, Vec::new())
98    }
99
100    pub fn filtered(
101        text: String,
102        label_len: usize,
103        filter_text: Option<&str>,
104        runs: Vec<(Range<usize>, HighlightId)>,
105    ) -> Self {
106        assert!(label_len <= text.len());
107        let filter_range = filter_text
108            .and_then(|filter| text.find(filter).map(|index| index..index + filter.len()))
109            .unwrap_or(0..label_len);
110        Self::new(text, filter_range, runs)
111    }
112
113    pub fn new(
114        text: String,
115        filter_range: Range<usize>,
116        runs: Vec<(Range<usize>, HighlightId)>,
117    ) -> Self {
118        assert!(
119            text.get(filter_range.clone()).is_some(),
120            "invalid filter range"
121        );
122        runs.iter().for_each(|(range, _)| {
123            assert!(
124                text.get(range.clone()).is_some(),
125                "invalid run range with inputs. Requested range {range:?} in text '{text}'",
126            );
127        });
128        Self {
129            runs,
130            filter_range,
131            text,
132        }
133    }
134
135    pub fn text(&self) -> &str {
136        self.text.as_str()
137    }
138
139    pub fn filter_text(&self) -> &str {
140        &self.text[self.filter_range.clone()]
141    }
142}
143
144impl From<String> for CodeLabel {
145    fn from(value: String) -> Self {
146        Self::plain(value, None)
147    }
148}
149
150impl From<&str> for CodeLabel {
151    fn from(value: &str) -> Self {
152        Self::plain(value.to_string(), None)
153    }
154}
155
156impl CodeLabelBuilder {
157    pub fn respan_filter_range(&mut self, filter_text: Option<&str>) {
158        self.filter_range = filter_text
159            .and_then(|filter| {
160                self.text
161                    .find(filter)
162                    .map(|index| index..index + filter.len())
163            })
164            .unwrap_or(0..self.text.len());
165    }
166
167    pub fn push_str(&mut self, text: &str, highlight: Option<HighlightId>) {
168        let start_index = self.text.len();
169        self.text.push_str(text);
170        if let Some(highlight) = highlight {
171            let end_index = self.text.len();
172            self.runs.push((start_index..end_index, highlight));
173        }
174    }
175
176    pub fn build(mut self) -> CodeLabel {
177        if self.filter_range.end == 0 {
178            self.respan_filter_range(None);
179        }
180        CodeLabel {
181            text: self.text,
182            runs: self.runs,
183            filter_range: self.filter_range,
184        }
185    }
186}
187
Served at tenant.openagents/omega Member data and write actions are omitted.