gpui-base 0.6.5

Behavior, interaction, and infrastructure foundations for GPUI applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Document annotations for atomic inline objects. Coordinates are always source bytes.
use std::ops::Range;

use gpui::{Context, EntityInputHandler as _, SharedString, Window};
use unicode_segmentation::UnicodeSegmentation as _;

use super::{InputBaseState, InputModeKind, undo_manager::EditIntent};

/// An application-defined reference rendered as one inline editing unit. The
/// ID names the referenced resource; the same ID may occur more than once.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct InlineToken {
    id: SharedString,
    text: SharedString,
    label: SharedString,
}

impl InlineToken {
    pub fn new(id: impl Into<SharedString>, text: impl Into<SharedString>) -> Self {
        let text = text.into();
        Self {
            id: id.into(),
            label: text.clone(),
            text,
        }
    }
    pub fn with_label(mut self, label: impl Into<SharedString>) -> Self {
        self.label = label.into();
        self
    }
    pub fn id(&self) -> &SharedString {
        &self.id
    }
    pub fn text(&self) -> &SharedString {
        &self.text
    }
    pub fn label(&self) -> &SharedString {
        &self.label
    }

    fn validate(&self) -> Result<(), InlineTokenError> {
        if self.id.trim().is_empty()
            || [&self.text, &self.label].iter().any(|s| {
                s.is_empty()
                    || s.chars()
                        .any(|c| c.is_control() || matches!(c, '\u{2028}' | '\u{2029}'))
            })
        {
            return Err(InlineTokenError::InvalidToken);
        }
        Ok(())
    }
}

/// A token and its current half-open UTF-8 byte range.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InlineTokenSpan {
    range: Range<usize>,
    token: InlineToken,
}
impl InlineTokenSpan {
    pub fn range(&self) -> Range<usize> {
        self.range.clone()
    }
    pub fn token(&self) -> &InlineToken {
        &self.token
    }
    fn shifted(&self, delta: isize) -> Self {
        Self {
            range: self
                .range
                .start
                .checked_add_signed(delta)
                .expect("valid token start")
                ..self
                    .range
                    .end
                    .checked_add_signed(delta)
                    .expect("valid token end"),
            token: self.token.clone(),
        }
    }
}

/// An owned, coherent text-and-token snapshot: what `content()` returns and
/// what `set_value` accepts. Plain text converts into content without tokens,
/// and every token is validated against the text as it is attached, so a
/// content value is always consistent.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InputContent {
    text: SharedString,
    tokens: Vec<InlineTokenSpan>,
}
impl InputContent {
    pub fn new(text: impl Into<SharedString>) -> Self {
        Self {
            text: text.into(),
            tokens: vec![],
        }
    }
    /// Attach a token to a half-open UTF-8 byte range of the text. The range
    /// must be nonempty, sit on grapheme boundaries, contain exactly the
    /// token's text and not overlap another token.
    pub fn with_token(
        mut self,
        range: Range<usize>,
        token: InlineToken,
    ) -> Result<Self, InlineTokenError> {
        token.validate()?;
        validate_range(&self.text, &range)?;
        if range.is_empty() {
            return Err(InlineTokenError::InvalidRange);
        }
        if &self.text[range.clone()] != token.text.as_ref() {
            return Err(InlineTokenError::TextMismatch);
        }
        let ix = self
            .tokens
            .partition_point(|span| span.range.end <= range.start);
        if self
            .tokens
            .get(ix)
            .is_some_and(|span| span.range.start < range.end)
        {
            return Err(InlineTokenError::OverlappingTokens);
        }
        self.tokens.insert(ix, InlineTokenSpan { range, token });
        Ok(self)
    }
    pub fn text(&self) -> &SharedString {
        &self.text
    }
    pub fn tokens(&self) -> &[InlineTokenSpan] {
        &self.tokens
    }
}

macro_rules! content_from_text {
    ($($text:ty),* $(,)?) => {
        $(impl From<$text> for InputContent {
            fn from(text: $text) -> Self {
                Self::new(text)
            }
        })*
    };
}
// Every text type `set_value` accepted before it took content.
content_from_text!(
    &str,
    &mut str,
    &String,
    String,
    char,
    Box<str>,
    std::sync::Arc<str>,
    &std::sync::Arc<str>,
    std::borrow::Cow<'_, str>,
    &SharedString,
    SharedString,
);

/// A rejected token operation never partially changes the document.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum InlineTokenError {
    InvalidRange,
    InvalidBoundary,
    InvalidToken,
    OverlappingTokens,
    TextMismatch,
    UnsupportedMode,
    ValidationRejected,
    CompositionActive,
}

impl std::error::Error for InlineTokenError {}
impl std::fmt::Display for InlineTokenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::InvalidRange => "token range is outside the document or empty",
            Self::InvalidBoundary => "token range splits a Unicode grapheme",
            Self::InvalidToken => {
                "token requires a nonempty ID, text and label without control characters"
            }
            Self::OverlappingTokens => "token ranges overlap",
            Self::TextMismatch => "token text does not match its range or input normalization",
            Self::UnsupportedMode => "tokens are not supported by this input mode",
            Self::ValidationRejected => "input validation rejected the content",
            Self::CompositionActive => "finish the active IME composition before editing tokens",
        })
    }
}

fn validate_range(text: &str, range: &Range<usize>) -> Result<(), InlineTokenError> {
    if range.start > range.end || range.end > text.len() {
        return Err(InlineTokenError::InvalidRange);
    }
    let boundary =
        |offset| offset == text.len() || text.grapheme_indices(true).any(|(ix, _)| ix == offset);
    if !boundary(range.start) || !boundary(range.end) {
        return Err(InlineTokenError::InvalidBoundary);
    }
    Ok(())
}

#[derive(Default)]
pub(super) struct InlineTokenStore {
    spans: Vec<InlineTokenSpan>,
}

/// Only affected records are retained in history. Ranges are relative to the edit start.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct TokenDelta {
    removed: Vec<InlineTokenSpan>,
    inserted: Vec<InlineTokenSpan>,
}

impl InlineTokenStore {
    fn replace(
        &mut self,
        range: &Range<usize>,
        new_len: usize,
        inserted: &[InlineTokenSpan],
    ) -> Option<Box<TokenDelta>> {
        let mut removed = Vec::new();
        let shift = new_len as isize - range.len() as isize;
        self.spans.retain_mut(|span| {
            if span.range.start < range.end && range.start < span.range.end {
                removed.push(span.shifted(-(range.start as isize)));
                false
            } else {
                if span.range.start >= range.end {
                    *span = span.shifted(shift);
                }
                true
            }
        });
        self.spans
            .extend(inserted.iter().map(|s| s.shifted(range.start as isize)));
        if !inserted.is_empty() {
            self.spans.sort_by_key(|s| s.range.start);
        }
        (!removed.is_empty() || !inserted.is_empty()).then(|| {
            Box::new(TokenDelta {
                removed,
                inserted: inserted.to_vec(),
            })
        })
    }
}

impl<M: InputModeKind> InputBaseState<M> {
    pub(super) fn token_spans(&self) -> &[InlineTokenSpan] {
        self.inline_tokens
            .as_ref()
            .map_or(&[], |store| &store.spans)
    }
    pub(super) fn token_boundary(&self, offset: usize, bias: sum_tree::Bias) -> usize {
        let spans = self.token_spans();
        let ix = spans.partition_point(|s| s.range.end <= offset);
        if let Some(span) = spans.get(ix).filter(|s| s.range.start < offset) {
            if bias == sum_tree::Bias::Left {
                span.range.start
            } else {
                span.range.end
            }
        } else {
            offset
        }
    }
    pub(super) fn normalize_token_range(&self, range: Range<usize>) -> Range<usize> {
        if self.replaying_history {
            return range;
        }
        if range.is_empty() {
            let offset = self.token_boundary(range.start, sum_tree::Bias::Right);
            offset..offset
        } else {
            self.token_boundary(range.start, sum_tree::Bias::Left)
                ..self.token_boundary(range.end, sum_tree::Bias::Right)
        }
    }
    pub(super) fn edit_tokens(
        &mut self,
        range: &Range<usize>,
        new_len: usize,
    ) -> Option<Box<TokenDelta>> {
        if self.replaying_history {
            return None;
        }
        let inserted = self.pending_token.take().map(|token| InlineTokenSpan {
            range: 0..new_len,
            token,
        });
        if inserted.is_some() && self.inline_tokens.is_none() {
            self.inline_tokens = Some(Box::default());
        }
        self.inline_tokens
            .as_mut()?
            .replace(range, new_len, inserted.as_slice())
    }
    pub(super) fn replay_tokens(
        &mut self,
        range: &Range<usize>,
        new_len: usize,
        delta: Option<&TokenDelta>,
        undo: bool,
    ) {
        let inserted = delta
            .map(|d| {
                if undo {
                    &d.removed[..]
                } else {
                    &d.inserted[..]
                }
            })
            .unwrap_or_default();
        if !inserted.is_empty() && self.inline_tokens.is_none() {
            self.inline_tokens = Some(Box::default());
        }
        if let Some(store) = self.inline_tokens.as_mut() {
            store.replace(range, new_len, inserted);
        }
    }
    fn check_token_mode(&self) -> Result<(), InlineTokenError> {
        if self.ime_marked_range.is_some() {
            return Err(InlineTokenError::CompositionActive);
        }
        if M::CODE_EDITOR || self.masked || self.token_is_secret() || !self.mask_pattern.is_none() {
            return Err(InlineTokenError::UnsupportedMode);
        }
        Ok(())
    }
    fn replace_token(
        &mut self,
        range: Range<usize>,
        token: InlineToken,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Result<(), InlineTokenError> {
        self.check_token_mode()?;
        token.validate()?;
        let text = self.text.to_string();
        validate_range(&text, &range)?;
        let range = self.normalize_token_range(range);
        let mut next = text;
        next.replace_range(range.clone(), &token.text);
        validate_range(&next, &(range.start..range.start + token.text.len()))?;
        if self.normalize_input(&next) != next {
            return Err(InlineTokenError::TextMismatch);
        }
        if !self.is_valid_input(&next, cx) {
            return Err(InlineTokenError::ValidationRejected);
        }
        if self
            .token_spans()
            .iter()
            .any(|s| s.range == range && s.token == token)
        {
            return Ok(());
        }
        let new_text = token.text.clone();
        self.pending_token = Some(token);
        self.undo_manager.break_transaction_coalescing();
        self.undo_manager.set_pending_intent(EditIntent::Atomic);
        let range_utf16 = self.range_to_utf16(&range);
        self.validated_token_edit = true;
        self.with_edits_allowed(|state| {
            state.replace_text_in_range(Some(range_utf16), &new_text, window, cx)
        });
        self.validated_token_edit = false;
        self.pending_token = None;
        Ok(())
    }
    /// Adopt the tokens of content whose text was just installed by
    /// `set_value`. Tokens are dropped when this mode cannot show them or when
    /// normalization changed the text, since their ranges would no longer
    /// describe it.
    pub(super) fn install_tokens(&mut self, content: InputContent) {
        let supported = !M::CODE_EDITOR && self.mask_pattern.is_none();
        let text_kept = self.text == content.text.as_ref();
        self.inline_tokens = (supported && text_kept && !content.tokens.is_empty()).then(|| {
            Box::new(InlineTokenStore {
                spans: content.tokens,
            })
        });
    }
}

macro_rules! token_api {
    ($mode:ty) => {
        impl InputBaseState<$mode> {
            /// Replace the selection with an atomic token; no delimiter is added.
            pub fn replace_with_token(
                &mut self,
                token: InlineToken,
                window: &mut Window,
                cx: &mut Context<Self>,
            ) -> Result<(), InlineTokenError> {
                self.replace_token(self.selected_range(), token, window, cx)
            }
            /// Replace a UTF-8 byte range, expanding overlaps to whole tokens.
            pub fn replace_range_with_token(
                &mut self,
                range: Range<usize>,
                token: InlineToken,
                window: &mut Window,
                cx: &mut Context<Self>,
            ) -> Result<(), InlineTokenError> {
                self.replace_token(range, token, window, cx)
            }
            pub fn tokens(&self) -> &[InlineTokenSpan] {
                self.token_spans()
            }
            /// The text with its tokens, as `set_value` accepts it.
            pub fn content(&self) -> InputContent {
                InputContent {
                    text: self.value(),
                    tokens: self.token_spans().to_vec(),
                }
            }
        }
    };
}
token_api!(super::InputMode);
token_api!(super::TextareaMode);