duat-base 0.10.0

Basic components common in Duat, included by default on duat
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Duat's completion widget
//!
//! This widget is heavily inspired by Kakoune's completions in terms
//! of functionality. By default, on duatmode, completions will show
//! up automatically whenever the user is in `insert` mode, and given
//! the tab settings of that mode, the user should be able to just
//! press `<Tab>` to complete everything, making for a very smooth
//! experience for those who don't really use tabs.
//!
//! This widget is very extensible, as you can add your own
//! [`CompletionsProvider`], which are selected by priority. This lets
//! you, for example, have the default completions be provided by an
//! LSP, with fallbacks for path and words completions.
use std::{
    any::{Any, TypeId},
    collections::HashMap,
    sync::{Arc, LazyLock, Mutex},
};

use duat_core::{
    Ns,
    cmd::{
        CfgOrManifest, ColorSchemeArg, Existing, OtherBuffer, Parameter, ReloadOptions,
        ValidFilePath,
    },
    context::{self, Handle},
    data::Pass,
    hook::{self, FocusChanged, KeySent, OnMouseEvent, WidgetOpened},
    mode::MouseEventKind,
    text::{Point, Spawn, Strs, Text, TextMut, txt},
    ui::{Area, DynSpawnSpecs, Orientation, Side, Widget},
};
use duat_term::Frame;

pub use self::{commands::CommandsCompletions, lists::ExhaustiveCompletionsList};
use crate::widgets::{
    Info,
    completions::{paths::PathCompletions, words::WordCompletions},
};

mod commands;
mod lists;
mod paths;
mod words;

static OPENED_PARAM_COMPLETION: Mutex<Option<Vec<TypeId>>> = Mutex::new(None);
static NS: LazyLock<Ns> = Ns::new_lazy();
static COMPLETIONS: LazyLock<Mutex<HashMap<TypeId, (usize, ParamCompletions)>>> =
    LazyLock::new(Mutex::default);

/// Initial setup for completions
///
/// ONLY MEANT TO BE USED BY THE DUAT EXECUTABLE
#[doc(hidden)]
pub fn setup_completions() {
    words::track_words();
    Completions::set_for_parameter::<ValidFilePath>(75, |_, builder| {
        builder.with_provider(paths::PathCompletions::new(true))
    });

    Completions::set_for_parameter::<Handle>(50, |pa, builder| {
        let mut list: Vec<String> = context::windows()
            .buffers(pa)
            .into_iter()
            .map(|buf| buf.read(pa).name())
            .collect();

        list.sort_unstable();

        builder.with_provider(list)
    });

    Completions::set_for_parameter::<OtherBuffer>(50, |pa, builder| {
        let current = context::current_buffer(pa).read(pa).name();
        let mut list: Vec<String> = context::windows()
            .buffers(pa)
            .into_iter()
            .map(|buf| buf.read(pa).name())
            .filter(|name| *name != current)
            .collect();

        list.sort_unstable();

        builder.with_provider(list)
    });

    Completions::set_for_parameter::<Existing>(25, |_, builder| {
        builder.with_provider(["--existing"])
    });

    Completions::set_for_parameter::<CfgOrManifest>(30, |_, builder| {
        builder.with_provider(["--cfg", "--cfg-manifest"])
    });

    Completions::set_for_parameter::<ReloadOptions>(50, |_, builder| {
        builder.with_provider(ExhaustiveCompletionsList {
            list: vec!["--clean", "--update"],
            only_one: false,
        })
    });

    Completions::set_for_parameter::<ColorSchemeArg>(50, |_, builder| {
        builder.with_provider(duat_core::form::colorscheme_list())
    });

    hook::add::<WidgetOpened<Completions>>(move |pa, completions| {
        Completions::set_frame(pa, completions);

        let completions = completions.clone();
        let ns = Ns::new();

        hook::add::<KeySent>(move |pa, _| {
            if completions.is_closed() {
                hook::remove(ns);
                return;
            }

            Completions::update_text_and_position(pa, &completions, 0);
            let completions_master = completions.master(pa).unwrap();
            completions.write(pa).last_caret = completions_master.selections(pa).main().caret();
            if !completions.is_closed() {
                Completions::set_frame(pa, &completions);
            }
        })
        .grouped(ns);
    });

    hook::add::<FocusChanged>(move |pa, (prev, _)| prev.text_mut(pa).remove_tags(*NS, ..));

    hook::add::<OnMouseEvent<Completions>>(|pa, event| match event.kind {
        MouseEventKind::ScrollDown => _ = Completions::scroll(pa, 1),
        MouseEventKind::ScrollUp => _ = Completions::scroll(pa, -1),
        _ => {}
    });
}

/// A builder for [`Completions`], a [`Widget`] to show word
/// completions
///
/// The `Completions` widget is supposed to give the ability to
/// automatically fill in words, usually by pressing the `Tab`
/// character in order to scroll through a list of options.
///
/// The [`Completions`] will show words that match the word behind the
/// main [`Selection`]'s [caret], and they will automatically follow
/// the [`Selection`] if it moves to other words.
///
/// Initially, even if there is no word before the [caret],
/// completions will be shown, unless you set [`show_without_prefix`]
/// to `false`. However, as the list moves around, completions will
/// only show up if there is a word behind the [caret], as to not be
/// bothersome.
///
/// [`Selection`]: duat_core::mode::Selection
/// [caret]: duat_core::mode::Selection::caret
/// [`show_without_prefix`]: Self::show_without_prefix
pub struct CompletionsBuilder {
    providers: Option<ProvidersFn>,
    /// Show the [`Widget`] even if there is no word behind the cursor
    /// This is set to `true` by default when first opening the
    /// `Widget`, but is disabled if the cursor moves around, which
    /// creates a smooth typing experience.
    pub show_without_prefix: bool,
}

impl CompletionsBuilder {
    /// Opens the [`Completions`] [`Widget`]
    ///
    /// This `Widget` works in an autonomous way, that is, it will
    /// follow the main [`Selection`] around, always positioned at the
    /// beginning of the current word.
    ///
    /// If you wish to close the `Widget`, you can call
    /// [`Completions::close`].
    ///
    /// [`Selection`]: duat_core::mode::Selection
    pub fn open(self, pa: &mut Pass) {
        let handle = context::current_widget(pa).clone();
        // Do both removals, because the previous Completions need to be
        // removed immediately.
        handle.text_mut(pa).remove_tags(*NS, ..);
        if let Some(completions) = context::handle_of::<Completions>(pa) {
            _ = completions.close(pa);
        }

        let Some(main) = handle.selections(pa).get_main() else {
            context::warn!("Tried spawning [a]Completions[] on a Widget with no [a]Selection[]s");
            return;
        };

        let Some((providers, start_byte, entries)) =
            self.providers.map(|call| call(handle.text(pa), 20))
        else {
            return;
        };

        let (text, sidebar) = entries.unwrap_or_default();

        let completions = Completions {
            providers,
            text,
            sidebar,
            max_height: 20,
            start_byte,
            show_without_prefix: self.show_without_prefix,
            last_caret: main.caret(),
            info_handle: None,
        };

        let mut text = handle.text_mut(pa);
        text.insert_tag(*NS, start_byte, Spawn::new(completions, SPAWN_SPECS));
    }

    /// Adds a new [`CompletionsProvider`] to be prioritized over
    /// earlier ones
    pub fn with_provider(mut self, provider: impl CompletionsProvider) -> Self {
        let prev = self.providers.take();

        self.providers = Some(Box::new(move |text, height| {
            let (inner, start_byte, entries) = InnerProvider::new(provider, text, height);

            let Some((mut providers, reserve_start_byte, reserve_entries)) =
                prev.map(|call| call(text, height))
            else {
                return (vec![Box::new(inner)], start_byte, entries);
            };

            providers.insert(0, Box::new(inner));

            let start_byte = entries
                .as_ref()
                .and(Some(start_byte))
                .unwrap_or(reserve_start_byte);

            (providers, start_byte, entries.or(reserve_entries))
        }));

        self
    }
}

/// A list of completions, used to quickly fill in matches
pub struct Completions {
    providers: Vec<Box<dyn ErasedInnerProvider>>,
    text: Text,
    sidebar: Text,
    max_height: usize,
    start_byte: usize,
    show_without_prefix: bool,
    last_caret: Point,
    info_handle: Option<Handle<Info>>,
}

impl Completions {
    /// Returns a new `CompletionsBuilder`
    ///
    /// This `CompletionsBuilder` will not have any
    /// [`CompletionsProvider`]s by default, so it won't spawn a
    /// `Completions` list.
    ///
    /// You can add `CompletionsProvider`s by calling
    /// [`CompletionsBuilder::with_provider`].
    pub fn builder() -> CompletionsBuilder {
        CompletionsBuilder {
            providers: None,
            show_without_prefix: true,
        }
    }

    /// Set the [`CompletionsBuilder`] function for a [`Parameter`]
    /// type
    pub fn set_for_parameter<P: Parameter>(
        priority: usize,
        func: impl FnMut(&Pass, CompletionsBuilder) -> CompletionsBuilder + Send + Sync + 'static,
    ) {
        COMPLETIONS
            .lock()
            .unwrap()
            .insert(TypeId::of::<P>(), (priority, Box::new(Mutex::new(func))));
    }

    /// Spawn the `Completions` list
    pub fn open_default(pa: &mut Pass) {
        Self::builder()
            .with_provider(WordCompletions)
            .with_provider(PathCompletions::new(false))
            .open(pa);
    }

    /// Open the `Completions` for a given [`Parameter`]'s [`TypeId`]
    ///
    /// This completions must've been previously added via
    /// [`Completions::set_for_parameter`].
    ///
    /// Returns [`None`] if none of the `TypeId`s had completions set
    /// for them.
    pub fn open_for(pa: &mut Pass, param_type_ids: &[TypeId]) {
        let completions = COMPLETIONS.lock().unwrap();
        let mut opened_param_completion = OPENED_PARAM_COMPLETION.lock().unwrap();

        let mut builder = Completions::builder();
        builder.show_without_prefix = true;

        let mut param_fns: Vec<_> = param_type_ids
            .iter()
            .filter_map(|ty| Some(*ty).zip(completions.get(ty)))
            .collect();

        if param_fns.is_empty() {
            Completions::close(pa);
            return;
        }

        param_fns.sort_by_key(|(_, (priority, _))| priority);

        if param_fns
            .iter()
            .map(|(ty, _)| ty)
            .eq(opened_param_completion.iter().flatten())
        {
            return;
        }

        *opened_param_completion = Some(param_fns.iter().map(|(ty, _)| *ty).collect());

        for (_, (_, param_fn)) in param_fns {
            builder = (param_fn.lock().unwrap())(pa, builder);
        }

        builder.open(pa);
    }

    /// Closes the `Completions` list
    pub fn close(pa: &mut Pass) {
        let handle = context::current_widget(pa).clone();
        handle.text_mut(pa).remove_tags(*NS, ..);
    }

    /// Goes to the next entry on the list.
    pub fn scroll(pa: &mut Pass, scroll: i32) -> Option<(String, String)> {
        if scroll == 0 {
            context::warn!("Scrolling [a]Completions[] by 0");
            return None;
        }

        let handle = context::handle_of::<Completions>(pa)?;

        let main_repl = Completions::update_text_and_position(pa, &handle, scroll);
        handle.write(pa).show_without_prefix = true;
        main_repl
    }

    /// Wether there is an open `Completions` [`Widget`]
    pub fn is_open(pa: &Pass) -> bool {
        context::handle_of::<Completions>(pa).is_some()
    }

    #[track_caller]
    fn update_text_and_position(
        pa: &mut Pass,
        completions: &Handle<Self>,
        scroll: i32,
    ) -> Option<(String, String)> {
        let master_handle = completions.master(pa).unwrap();
        let (master, area, comp) = pa.write_many((
            master_handle.widget(),
            completions.area(),
            completions.widget(),
        ));

        let mat = {
            let mut lists: Vec<_> = comp
                .providers
                .iter_mut()
                .map(|inner| {
                    let texts_and_match = inner.texts_and_match(
                        master.text(),
                        scroll,
                        Some(area),
                        comp.max_height,
                        comp.show_without_prefix,
                    );
                    (texts_and_match, inner.start_fn())
                })
                .collect();
            lists.sort_by_key(|((start, _), _)| *start);
            lists
                .into_iter()
                .find_map(|((start, list), start_fn)| list.map(|list| ((start, list), start_fn)))
        };

        // Believe it or not, this is necessary to prevent Drop semantincs
        // from invalidating the following code.
        let (other, start_fn) = mat.unzip();

        let main_replacement = if let Some((start_byte, ((text, sides), replacement))) = other {
            comp.text = text;
            comp.sidebar = sides;

            let mut main_replacement = None;

            let mut new_start_byte = start_byte;
            if let Some((replacement, info_text)) = replacement {
                // Also necessary, believe it or not.
                let start_fn = start_fn.unwrap();

                let mut starts = master
                    .text()
                    .selections()
                    .iter()
                    .map(|(sel, _)| start_fn(master.text(), sel.caret()))
                    .collect::<Vec<_>>()
                    .into_iter();

                drop(start_fn);
                let mut shift = 0;

                master_handle.edit_all(pa, |mut c| {
                    let start = (starts.next().unwrap() as i32 + shift) as usize;
                    shift += replacement.len() as i32 - (c.caret().byte() as i32 - start as i32);

                    c.move_to(start..c.caret().byte());

                    if c.is_main() {
                        main_replacement = Some((c.selection().to_string(), replacement.clone()));
                    }

                    c.replace(&replacement);
                    c.unset_anchor();
                    if !replacement.is_empty() {
                        c.move_hor(1);
                    }

                    if c.is_main() {
                        new_start_byte = start;
                    }
                });

                if let Some((info_text, orientation)) = info_text {
                    let info_handle = if let Some(info) = completions.read(pa).info_handle.clone() {
                        Info::set_text(pa, &info, |text| *text = info_text);
                        Some(info)
                    } else {
                        let specs = DynSpawnSpecs {
                            orientation,
                            width: None,
                            height: None,
                            ..Default::default()
                        };

                        let info_handle = completions.spawn_widget(pa, Info::new(info_text), specs);
                        completions.write(pa).info_handle = info_handle.clone();
                        info_handle
                    };

                    if let Some(info_handle) = info_handle.as_ref()
                        && let Some(area) = info_handle.area().write_as::<duat_term::Area>(pa)
                    {
                        let mut frame = Frame {
                            above: true,
                            below: true,
                            left: true,
                            right: true,
                            ..Default::default()
                        };
                        frame.set_text(Side::Above, move |_| {
                            txt!("[terminal.frame.Info]┤{replacement}[terminal.frame.Info]├")
                        });
                        area.set_frame(frame);
                    }
                } else if let Some(prev) = completions.write(pa).info_handle.take() {
                    let _ = prev.close(pa);
                }
            } else {
                drop(start_fn);
            }

            let comp = completions.write(pa);

            // In this case, move the Completions to a new location
            if start_byte != comp.start_byte {
                let new_comp = Self {
                    providers: std::mem::take(&mut comp.providers),
                    text: std::mem::take(&mut comp.text),
                    sidebar: std::mem::take(&mut comp.sidebar),
                    max_height: comp.max_height,
                    start_byte: new_start_byte,
                    show_without_prefix: false,
                    last_caret: comp.last_caret,
                    info_handle: comp.info_handle.take(),
                };

                let mut text = master_handle.text_mut(pa);
                text.insert_tag(*NS, start_byte, Spawn::new(new_comp, SPAWN_SPECS));
                _ = completions.close(pa);
                return main_replacement;
            } else {
                comp.start_byte = new_start_byte;
            }

            main_replacement
        } else {
            drop(start_fn);
            comp.text = Text::default();
            comp.sidebar = Text::default();
            None
        };

        Completions::set_frame(pa, completions);

        main_replacement
    }

    #[track_caller]
    fn set_frame(pa: &mut Pass, completions: &Handle<Self>) {
        let sidebar = completions.read(pa).sidebar.clone();
        if let Some(area) = completions.area().write_as::<duat_term::Area>(pa) {
            let mut frame = Frame {
                left: true,
                right: true,
                ..Frame::default()
            };
            frame.set_text(Side::Left, {
                let sidebar = sidebar.clone();
                move |_| sidebar.clone()
            });
            frame.set_text(Side::Right, move |_| sidebar.clone());
            area.set_frame(frame);
        }

        let (comp, area) = completions.write_with_area(pa);
        area.set_width(
            area.size_of_text(comp.print_opts(), &comp.text)
                .unwrap()
                .x
                .max(40.0),
        )
        .unwrap();

        let height = comp.text.end_point().line() as f32;
        let height = if comp.text.is_empty() { 0.0 } else { height };
        area.set_height(height).unwrap();
    }
}

impl Widget for Completions {
    fn text(&self) -> &Text {
        &self.text
    }

    fn text_mut(&mut self) -> TextMut<'_> {
        self.text.as_mut()
    }
}

impl Drop for Completions {
    fn drop(&mut self) {
        *OPENED_PARAM_COMPLETION.lock().unwrap() = None;
    }
}

/// A provider for word completions
pub trait CompletionsProvider: Send + Sized + 'static {
    /// Additional information about a given entry in the completion
    /// list
    ///
    /// This information is supposed to be displayed alongside the
    /// entry itself, usually on the right side.
    type Info: Send;

    /// The default formatting for entries from this provider
    ///
    /// Each [`Text`] must only be one line long (Nothing bad happens
    /// if they are multiple lines long, but don't expect the
    /// [`Completions`] to show things correctly).
    fn default_fmt(entry: &str, info: &Self::Info) -> Text;

    /// Get all completions at a given [`Point`] in the [`Text`]
    ///
    /// This function should return a list of all possible matches,
    /// given the prefix and surrounding context. It should be sorted
    /// in an appropriate manner (e.g. by word proximity or
    /// frequency).
    ///
    /// The `caret` is the position where the main cursor's [caret]
    /// lies, And the `prefix` and `suffix` are .
    ///
    /// If the returned [`Vec`] is empty, then the next provider will
    /// be selected to return a list of matches.
    ///
    /// This function is only called if the prefix changes, which
    /// would happen if the user types something, deletes something,
    /// or moves the cursor around.
    ///
    /// [caret]: duat_core::mode::Selection::caret
    fn matches(&mut self, text: &Text, caret: Point, prefix: &str) -> Vec<(Arc<str>, Self::Info)>;

    /// Get the starting byte for this completions
    ///
    /// This function should look at the bytes before `from` (and
    /// sometimes after), in order to figure out where the completion
    /// starts.
    ///
    /// For example, if you're completing words, you should logially
    /// look for the start of the word that intersects with the
    /// `from`th byte:
    ///
    /// ```text
    ///         v------ starting byte index.
    /// This is bein|g typed.
    ///             ^-- main cursor caret.
    /// ```
    ///
    /// If you're typing arguments in a command, you'd return the byte
    /// index of the current argument:
    ///
    /// ```text
    ///       v--------------------------- starting byte index.
    /// :edit 'This is a quoted argum|ent
    ///                              ^---< main cursor caret.
    /// ```
    ///
    /// This function is used in order to determine which providers
    /// should be prioritized, giving higher priority to the ones that
    /// have longer matches.
    fn get_start(&self, text: &Text, caret: Point) -> Option<usize>;

    /// Additional information about an entry, which can be shown when
    /// it is selected.
    #[allow(unused_variables)]
    fn default_info_on(&self, entry: &str, info: &Self::Info) -> Option<(Text, Orientation)> {
        None
    }
}

trait ErasedInnerProvider: Any + Send {
    #[allow(clippy::type_complexity)]
    fn texts_and_match(
        &mut self,
        text: &Text,
        scroll: i32,
        area: Option<&mut Area>,
        max_height: usize,
        show_without_prefix: bool,
    ) -> (
        usize,
        Option<((Text, Text), Option<(String, Option<(Text, Orientation)>)>)>,
    );

    #[allow(clippy::type_complexity)]
    fn start_fn(&self) -> Box<dyn Fn(&Text, Point) -> usize + '_>;
}

#[allow(clippy::type_complexity)]
struct InnerProvider<P: CompletionsProvider> {
    provider: P,
    fmt: Box<dyn FnMut(&str, &P::Info) -> Text + Send>,

    orig_prefix: String,
    current: Option<(Arc<str>, usize)>,

    matches: Vec<(Arc<str>, P::Info)>,
}

impl<P: CompletionsProvider> InnerProvider<P> {
    #[allow(clippy::type_complexity)]
    fn new(mut provider: P, text: &Text, height: usize) -> (Self, usize, Option<(Text, Text)>) {
        let Some(main_caret) = text.get_main_sel().map(|sel| sel.caret()) else {
            panic!("Tried to spawn completions on a Text with no main selection");
        };

        let start = provider
            .get_start(text, main_caret)
            .unwrap_or(main_caret.byte());

        let orig_prefix = text[start..main_caret.byte()].to_string();
        let matches = provider.matches(text, main_caret, &orig_prefix);

        let mut inner = Self {
            provider,
            orig_prefix,
            current: None,
            matches,
            fmt: Box::new(P::default_fmt),
        };

        let (start, text) = inner.texts_and_match(text, 0, None, height, true);
        (inner, start, text.unzip().0)
    }
}

impl<P: CompletionsProvider> ErasedInnerProvider for InnerProvider<P> {
    #[track_caller]
    fn texts_and_match(
        &mut self,
        text: &Text,
        scroll: i32,
        area: Option<&mut Area>,
        max_height: usize,
        show_without_prefix: bool,
    ) -> (
        usize,
        Option<((Text, Text), Option<(String, Option<(Text, Orientation)>)>)>,
    ) {
        let Some(caret) = text.get_main_sel().map(|sel| sel.caret()) else {
            panic!("Tried to update completions on a Text with no main selection");
        };

        let start = self.provider.get_start(text, caret).unwrap_or(caret.byte());

        let Some(prefix) = text.get(start..caret.byte()).map(Strs::to_string) else {
            panic!("Failed to get prefix from {:?}", start..caret.byte());
        };

        // This should only be true if edits other than the one applied by
        // Completions take place.
        let target_changed = self.current.as_ref().is_some_and(|(c, _)| **c != prefix)
            || (self.current.is_none() && self.orig_prefix != prefix);

        if target_changed {
            self.matches = self.provider.matches(text, caret, &prefix);
            self.current = None;
            self.orig_prefix = prefix;
        }

        if self.matches.is_empty() || (start == caret.byte() && !show_without_prefix) {
            self.current = None;
            return (start, None);
        }

        let height = if let Some(area) = area {
            area.set_height(self.matches.len().min(max_height) as f32)
                .unwrap();
            area.height() as usize
        } else {
            max_height
        };

        let mut ret_info = None;
        if scroll != 0 {
            // No try blocks on stable Rust 🤮.
            self.current = (|| -> Option<(Arc<str>, usize)> {
                if let Some((prev, dist)) = &self.current {
                    let dist = dist.saturating_add_signed(scroll as isize).min(height - 1);
                    let prev_i = self.matches.iter().position(|(w, _)| w == prev)?;
                    let (word, info) = self
                        .matches
                        .get(prev_i.checked_add_signed(scroll as isize)?)?;

                    ret_info = Some(info);
                    Some((word.clone(), dist))
                } else if scroll > 0 {
                    let scroll = scroll.unsigned_abs() as usize - 1;
                    let dist = (scroll).min(height - 1);
                    let (word, info) = self.matches.get(scroll)?;

                    ret_info = Some(info);
                    Some((word.clone(), dist))
                } else {
                    let scroll = scroll.unsigned_abs() as usize;
                    let dist = height.saturating_sub(scroll);
                    let (word, info) = self.matches.get(self.matches.len().checked_sub(scroll)?)?;

                    ret_info = Some(info);
                    Some((word.clone(), dist))
                }
            })();
        }

        let mut entries_builder = Text::builder();
        let mut sidebar_builder = Text::builder();

        if let Some((word, dist)) = &mut self.current
            && let Some(word_i) = self.matches.iter().position(|(w, _)| w == word)
        {
            *dist = (*dist).min(height - 1);

            let top_i = word_i.saturating_sub(*dist);
            for (i, (entry, info)) in self.matches.iter().enumerate().skip(top_i).take(height) {
                if i == word_i {
                    entries_builder
                        .push(txt!("[selected.Completions]{}\n", (self.fmt)(entry, info)));
                    sidebar_builder.push(txt!("[selected.Completions] \n"));
                } else {
                    entries_builder.push(txt!("{}\n", (self.fmt)(entry, info)));
                    sidebar_builder.push(txt!("[default.Completions] \n"));
                }
            }
        } else {
            for (entry, info) in self.matches.iter().take(height) {
                entries_builder.push(txt!("{}\n", (self.fmt)(entry, info)));
                sidebar_builder.push(txt!("[default.Completions] \n"));
            }
        }

        let replacement = if scroll != 0 {
            self.current
                .clone()
                .map(|(word, _)| {
                    let text = self.provider.default_info_on(&word, ret_info.unwrap());
                    (word.to_string(), text)
                })
                .or_else(|| Some((self.orig_prefix.clone(), None)))
        } else {
            None
        };

        let entries_text = entries_builder.build_no_double_nl();
        let sidebar_text = sidebar_builder.build_no_double_nl();
        (start, Some(((entries_text, sidebar_text), replacement)))
    }

    fn start_fn(&self) -> Box<dyn Fn(&Text, Point) -> usize + '_> {
        Box::new(|text, caret| self.provider.get_start(text, caret).unwrap_or(caret.byte()))
    }
}

/// A simple [`String`] comparison function, which prioritizes matched
/// clusters as well as earlier matches
///
/// The priorization is done in the following order:
///
/// - Matching clusters.
/// - Earlier matches in `cmp`.
/// - Size of `cmp`
///
/// If any `char` in the `target` fails to match (in the correct
/// order), then this function returns [`None`].
///
/// If this function returns [`Some(0)`], then it is an exact match.
fn string_cmp(target: &str, entry: &str) -> Option<usize> {
    let mut diff = 0;
    let mut eq_i = 0;
    let mut cmp_chars = entry.chars().enumerate();

    for char in target.chars() {
        let (i, _) = cmp_chars.find(|&(_, other)| other == char)?;
        diff += i - eq_i;
        eq_i = i + 1;
    }

    Some(diff)
}

const SPAWN_SPECS: DynSpawnSpecs = DynSpawnSpecs {
    orientation: Orientation::VerLeftBelow,
    height: Some(20.0),
    width: Some(50.0),
    hidden: true,
    inside: false,
};

type ProvidersFn = Box<
    dyn FnOnce(
        &Text,
        usize,
    ) -> (
        Vec<Box<dyn ErasedInnerProvider>>,
        usize,
        Option<(Text, Text)>,
    ),
>;
type ParamCompletions =
    Box<Mutex<dyn FnMut(&Pass, CompletionsBuilder) -> CompletionsBuilder + Send + Sync>>;