aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/picker.rs
blob: 001526c4423de64fee8943fae66db8207f86f5e8 (plain) (blame)
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
use crate::{
    alt,
    compositor::{self, Component, Compositor, Context, Event, EventResult},
    ctrl,
    job::Callback,
    key, shift,
    ui::{
        self,
        document::{render_document, LineDecoration, LinePos, TextRenderer},
        fuzzy_match::FuzzyQuery,
        EditorView,
    },
};
use futures_util::{future::BoxFuture, FutureExt};
use tui::{
    buffer::Buffer as Surface,
    layout::Constraint,
    text::{Span, Spans},
    widgets::{Block, BorderType, Borders, Cell, Table},
};

use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use tui::widgets::Widget;

use std::cmp::{self, Ordering};
use std::{collections::HashMap, io::Read, path::PathBuf};

use crate::ui::{Prompt, PromptEvent};
use helix_core::{
    movement::Direction, text_annotations::TextAnnotations,
    unicode::segmentation::UnicodeSegmentation, Position, Syntax,
};
use helix_view::{
    editor::Action,
    graphics::{CursorKind, Margin, Modifier, Rect},
    theme::Style,
    view::ViewPosition,
    Document, DocumentId, Editor,
};

use super::{menu::Item, overlay::Overlay};

pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72;
/// Biggest file size to preview in bytes
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024;

#[derive(PartialEq, Eq, Hash)]
pub enum PathOrId {
    Id(DocumentId),
    Path(PathBuf),
}

impl PathOrId {
    fn get_canonicalized(self) -> std::io::Result<Self> {
        use PathOrId::*;
        Ok(match self {
            Path(path) => Path(helix_core::path::get_canonicalized_path(&path)?),
            Id(id) => Id(id),
        })
    }
}

impl From<PathBuf> for PathOrId {
    fn from(v: PathBuf) -> Self {
        Self::Path(v)
    }
}

impl From<DocumentId> for PathOrId {
    fn from(v: DocumentId) -> Self {
        Self::Id(v)
    }
}

type FileCallback<T> = Box<dyn Fn(&Editor, &T) -> Option<FileLocation>>;

/// File path and range of lines (used to align and highlight lines)
pub type FileLocation = (PathOrId, Option<(usize, usize)>);

pub enum CachedPreview {
    Document(Box<Document>),
    Binary,
    LargeFile,
    NotFound,
}

// We don't store this enum in the cache so as to avoid lifetime constraints
// from borrowing a document already opened in the editor.
pub enum Preview<'picker, 'editor> {
    Cached(&'picker CachedPreview),
    EditorDocument(&'editor Document),
}

impl Preview<'_, '_> {
    fn document(&self) -> Option<&Document> {
        match self {
            Preview::EditorDocument(doc) => Some(doc),
            Preview::Cached(CachedPreview::Document(doc)) => Some(doc),
            _ => None,
        }
    }

    /// Alternate text to show for the preview.
    fn placeholder(&self) -> &str {
        match *self {
            Self::EditorDocument(_) => "<File preview>",
            Self::Cached(preview) => match preview {
                CachedPreview::Document(_) => "<File preview>",
                CachedPreview::Binary => "<Binary file>",
                CachedPreview::LargeFile => "<File too large to preview>",
                CachedPreview::NotFound => "<File not found>",
            },
        }
    }
}

pub struct FilePicker<T: Item> {
    options: Vec<T>,
    editor_data: T::Data,
    // filter: String,
    matcher: Box<Matcher>,
    matches: Vec<PickerMatch>,

    /// Current height of the completions box
    completion_height: u16,

    cursor: usize,
    // pattern: String,
    prompt: Prompt,
    previous_pattern: (String, FuzzyQuery),
    /// Whether to show the preview panel (default true)
    show_preview: bool,
    /// Constraints for tabular formatting
    widths: Vec<Constraint>,

    callback_fn: PickerCallback<T>,

    picker: Picker<T>,
    pub truncate_start: bool,
    /// Caches paths to documents
    preview_cache: HashMap<PathBuf, CachedPreview>,
    read_buffer: Vec<u8>,
    /// Given an item in the picker, return the file path and line number to display.
    file_fn: Option<FileCallback<T>>,
}

impl<T: Item + 'static> FilePicker<T> {
    pub fn new(
        options: Vec<T>,
        editor_data: T::Data,
        callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
    ) -> Self {
        let prompt = Prompt::new(
            "".into(),
            None,
            ui::completers::none,
            |_editor: &mut Context, _pattern: &str, _event: PromptEvent| {},
        );

        let mut picker = Self {
            options,
            editor_data,
            matcher: Box::default(),
            matches: Vec::new(),
            cursor: 0,
            prompt,
            previous_pattern: (String::new(), FuzzyQuery::default()),
            truncate_start: true,
            show_preview: true,
            callback_fn: Box::new(callback_fn),
            completion_height: 0,
            widths: Vec::new(),
            preview_cache: HashMap::new(),
            read_buffer: Vec::with_capacity(1024),
            file_fn: None,

            picker: unimplemented!(),
        };

        picker.calculate_column_widths();

        // scoring on empty input
        // TODO: just reuse score()
        picker
            .matches
            .extend(picker.options.iter().enumerate().map(|(index, option)| {
                let text = option.filter_text(&picker.editor_data);
                PickerMatch {
                    index,
                    score: 0,
                    len: text.chars().count(),
                }
            }));

        picker
    }

    pub fn truncate_start(mut self, truncate_start: bool) -> Self {
        self.truncate_start = truncate_start;
        self.picker.truncate_start = truncate_start;
        self
    }

    pub fn with_preview(
        mut self,
        preview_fn: impl Fn(&Editor, &T) -> Option<FileLocation> + 'static,
    ) -> Self {
        self.file_fn = Some(Box::new(preview_fn));
        self
    }

    pub fn set_options(&mut self, new_options: Vec<T>) {
        self.options = new_options;
        self.cursor = 0;
        self.force_score();
        self.calculate_column_widths();
    }

    /// Calculate the width constraints using the maximum widths of each column
    /// for the current options.
    fn calculate_column_widths(&mut self) {
        let n = self
            .options
            .first()
            .map(|option| option.format(&self.editor_data).cells.len())
            .unwrap_or_default();
        let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| {
            let row = option.format(&self.editor_data);
            // maintain max for each column
            for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) {
                let width = cell.content.width();
                if width > *acc {
                    *acc = width;
                }
            }
            acc
        });
        self.widths = max_lens
            .into_iter()
            .map(|len| Constraint::Length(len as u16))
            .collect();
    }

    pub fn score(&mut self) {
        let pattern = self.prompt.line();

        if pattern == &self.previous_pattern.0 {
            return;
        }

        let (query, is_refined) = self
            .previous_pattern
            .1
            .refine(pattern, &self.previous_pattern.0);

        if pattern.is_empty() {
            // Fast path for no pattern.
            self.matches.clear();
            self.matches
                .extend(self.options.iter().enumerate().map(|(index, option)| {
                    let text = option.filter_text(&self.editor_data);
                    PickerMatch {
                        index,
                        score: 0,
                        len: text.chars().count(),
                    }
                }));
        } else if is_refined {
            // optimization: if the pattern is a more specific version of the previous one
            // then we can score the filtered set.
            self.matches.retain_mut(|pmatch| {
                let option = &self.options[pmatch.index];
                let text = option.sort_text(&self.editor_data);

                match query.fuzzy_match(&text, &self.matcher) {
                    Some(s) => {
                        // Update the score
                        pmatch.score = s;
                        true
                    }
                    None => false,
                }
            });

            self.matches.sort_unstable();
        } else {
            self.force_score();
        }

        // reset cursor position
        self.cursor = 0;
        let pattern = self.prompt.line();
        self.previous_pattern.0.clone_from(pattern);
        self.previous_pattern.1 = query;
    }

    pub fn force_score(&mut self) {
        let pattern = self.prompt.line();

        let query = FuzzyQuery::new(pattern);
        self.matches.clear();
        self.matches.extend(
            self.options
                .iter()
                .enumerate()
                .filter_map(|(index, option)| {
                    let text = option.filter_text(&self.editor_data);

                    query
                        .fuzzy_match(&text, &self.matcher)
                        .map(|score| PickerMatch {
                            index,
                            score,
                            len: text.chars().count(),
                        })
                }),
        );

        self.matches.sort_unstable();
    }

    /// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)
    pub fn move_by(&mut self, amount: usize, direction: Direction) {
        let len = self.matches.len();

        if len == 0 {
            // No results, can't move.
            return;
        }

        match direction {
            Direction::Forward => {
                self.cursor = self.cursor.saturating_add(amount) % len;
            }
            Direction::Backward => {
                self.cursor = self.cursor.saturating_add(len).saturating_sub(amount) % len;
            }
        }
    }

    /// Move the cursor down by exactly one page. After the last page comes the first page.
    pub fn page_up(&mut self) {
        self.move_by(self.completion_height as usize, Direction::Backward);
    }

    /// Move the cursor up by exactly one page. After the first page comes the last page.
    pub fn page_down(&mut self) {
        self.move_by(self.completion_height as usize, Direction::Forward);
    }

    /// Move the cursor to the first entry
    pub fn to_start(&mut self) {
        self.cursor = 0;
    }

    /// Move the cursor to the last entry
    pub fn to_end(&mut self) {
        self.cursor = self.matches.len().saturating_sub(1);
    }

    pub fn selection(&self) -> Option<&T> {
        self.matches
            .get(self.cursor)
            .map(|pmatch| &self.options[pmatch.index])
    }

    pub fn toggle_preview(&mut self) {
        self.show_preview = !self.show_preview;
    }

    fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
        if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) {
            // TODO: recalculate only if pattern changed
            self.score();
        }
        EventResult::Consumed(None)
    }

    fn current_file(&self, editor: &Editor) -> Option<FileLocation> {
        self.picker
            .selection()
            .and_then(|current| (self.file_fn.as_ref()?)(editor, current))
            .and_then(|(path_or_id, line)| path_or_id.get_canonicalized().ok().zip(Some(line)))
    }

    /// Get (cached) preview for a given path. If a document corresponding
    /// to the path is already open in the editor, it is used instead.
    fn get_preview<'picker, 'editor>(
        &'picker mut self,
        path_or_id: PathOrId,
        editor: &'editor Editor,
    ) -> Preview<'picker, 'editor> {
        match path_or_id {
            PathOrId::Path(path) => {
                let path = &path;
                if let Some(doc) = editor.document_by_path(path) {
                    return Preview::EditorDocument(doc);
                }

                if self.preview_cache.contains_key(path) {
                    return Preview::Cached(&self.preview_cache[path]);
                }

                let data = std::fs::File::open(path).and_then(|file| {
                    let metadata = file.metadata()?;
                    // Read up to 1kb to detect the content type
                    let n = file.take(1024).read_to_end(&mut self.read_buffer)?;
                    let content_type = content_inspector::inspect(&self.read_buffer[..n]);
                    self.read_buffer.clear();
                    Ok((metadata, content_type))
                });
                let preview = data
                    .map(
                        |(metadata, content_type)| match (metadata.len(), content_type) {
                            (_, content_inspector::ContentType::BINARY) => CachedPreview::Binary,
                            (size, _) if size > MAX_FILE_SIZE_FOR_PREVIEW => {
                                CachedPreview::LargeFile
                            }
                            _ => {
                                // TODO: enable syntax highlighting; blocked by async rendering
                                Document::open(path, None, None, editor.config.clone())
                                    .map(|doc| CachedPreview::Document(Box::new(doc)))
                                    .unwrap_or(CachedPreview::NotFound)
                            }
                        },
                    )
                    .unwrap_or(CachedPreview::NotFound);
                self.preview_cache.insert(path.to_owned(), preview);
                Preview::Cached(&self.preview_cache[path])
            }
            PathOrId::Id(id) => {
                let doc = editor.documents.get(&id).unwrap();
                Preview::EditorDocument(doc)
            }
        }
    }

    fn handle_idle_timeout(&mut self, cx: &mut Context) -> EventResult {
        let Some((current_file, _)) = self.current_file(cx.editor) else {
            return EventResult::Consumed(None)
        };

        // Try to find a document in the cache
        let doc = match &current_file {
            PathOrId::Id(doc_id) => doc_mut!(cx.editor, doc_id),
            PathOrId::Path(path) => match self.preview_cache.get_mut(path) {
                Some(CachedPreview::Document(ref mut doc)) => doc,
                _ => return EventResult::Consumed(None),
            },
        };

        let mut callback: Option<compositor::Callback> = None;

        // Then attempt to highlight it if it has no language set
        if doc.language_config().is_none() {
            if let Some(language_config) = doc.detect_language_config(&cx.editor.syn_loader) {
                doc.language = Some(language_config.clone());
                let text = doc.text().clone();
                let loader = cx.editor.syn_loader.clone();
                let job = tokio::task::spawn_blocking(move || {
                    let syntax = language_config
                        .highlight_config(&loader.scopes())
                        .and_then(|highlight_config| Syntax::new(&text, highlight_config, loader));
                    let callback = move |editor: &mut Editor, compositor: &mut Compositor| {
                        let Some(syntax) = syntax else {
                            log::info!("highlighting picker item failed");
                            return
                        };
                        let Some(Overlay { content: picker, .. }) = compositor.find::<Overlay<Self>>() else {
                            log::info!("picker closed before syntax highlighting finished");
                            return
                        };
                        // Try to find a document in the cache
                        let doc = match current_file {
                            PathOrId::Id(doc_id) => doc_mut!(editor, &doc_id),
                            PathOrId::Path(path) => match picker.preview_cache.get_mut(&path) {
                                Some(CachedPreview::Document(ref mut doc)) => doc,
                                _ => return,
                            },
                        };
                        doc.syntax = Some(syntax);
                    };
                    Callback::EditorCompositor(Box::new(callback))
                });
                let tmp: compositor::Callback = Box::new(move |_, ctx| {
                    ctx.jobs
                        .callback(job.map(|res| res.map_err(anyhow::Error::from)))
                });
                callback = Some(Box::new(tmp))
            }
        }

        // QUESTION: do we want to compute inlay hints in pickers too ? Probably not for now
        // but it could be interesting in the future

        EventResult::Consumed(callback)
    }

    fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        let text_style = cx.editor.theme.get("ui.text");
        let selected = cx.editor.theme.get("ui.text.focus");
        let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD);

        // -- Render the frame:
        // clear area
        let background = cx.editor.theme.get("ui.background");
        surface.clear_with(area, background);

        // don't like this but the lifetime sucks
        let block = Block::default().borders(Borders::ALL);

        // calculate the inner area inside the box
        let inner = block.inner(area);

        block.render(area, surface);

        // -- Render the input bar:

        let area = inner.clip_left(1).with_height(1);

        let count = format!("{}/{}", self.matches.len(), self.options.len());
        surface.set_stringn(
            (area.x + area.width).saturating_sub(count.len() as u16 + 1),
            area.y,
            &count,
            (count.len()).min(area.width as usize),
            text_style,
        );

        self.prompt.render(area, surface, cx);

        // -- Separator
        let sep_style = cx.editor.theme.get("ui.background.separator");
        let borders = BorderType::line_symbols(BorderType::Plain);
        for x in inner.left()..inner.right() {
            if let Some(cell) = surface.get_mut(x, inner.y + 1) {
                cell.set_symbol(borders.horizontal).set_style(sep_style);
            }
        }

        // -- Render the contents:
        // subtract area of prompt from top
        let inner = inner.clip_top(2);

        let rows = inner.height;
        let offset = self.cursor - (self.cursor % std::cmp::max(1, rows as usize));
        let cursor = self.cursor.saturating_sub(offset);

        let options = self
            .matches
            .iter()
            .skip(offset)
            .take(rows as usize)
            .map(|pmatch| &self.options[pmatch.index])
            .map(|option| option.format(&self.editor_data))
            .map(|mut row| {
                const TEMP_CELL_SEP: &str = " ";

                let line = row.cell_text().fold(String::new(), |mut s, frag| {
                    s.push_str(&frag);
                    s.push_str(TEMP_CELL_SEP);
                    s
                });

                // Items are filtered by using the text returned by menu::Item::filter_text
                // but we do highlighting here using the text in Row and therefore there
                // might be inconsistencies. This is the best we can do since only the
                // text in Row is displayed to the end user.
                let (_score, highlights) = FuzzyQuery::new(self.prompt.line())
                    .fuzzy_indices(&line, &self.matcher)
                    .unwrap_or_default();

                let highlight_byte_ranges: Vec<_> = line
                    .char_indices()
                    .enumerate()
                    .filter_map(|(char_idx, (byte_offset, ch))| {
                        highlights
                            .contains(&char_idx)
                            .then(|| byte_offset..byte_offset + ch.len_utf8())
                    })
                    .collect();

                // The starting byte index of the current (iterating) cell
                let mut cell_start_byte_offset = 0;
                for cell in row.cells.iter_mut() {
                    let spans = match cell.content.lines.get(0) {
                        Some(s) => s,
                        None => {
                            cell_start_byte_offset += TEMP_CELL_SEP.len();
                            continue;
                        }
                    };

                    let mut cell_len = 0;

                    let graphemes_with_style: Vec<_> = spans
                        .0
                        .iter()
                        .flat_map(|span| {
                            span.content
                                .grapheme_indices(true)
                                .zip(std::iter::repeat(span.style))
                        })
                        .map(|((grapheme_byte_offset, grapheme), style)| {
                            cell_len += grapheme.len();
                            let start = cell_start_byte_offset;

                            let grapheme_byte_range =
                                grapheme_byte_offset..grapheme_byte_offset + grapheme.len();

                            if highlight_byte_ranges.iter().any(|hl_rng| {
                                hl_rng.start >= start + grapheme_byte_range.start
                                    && hl_rng.end <= start + grapheme_byte_range.end
                            }) {
                                (grapheme, style.patch(highlight_style))
                            } else {
                                (grapheme, style)
                            }
                        })
                        .collect();

                    let mut span_list: Vec<(String, Style)> = Vec::new();
                    for (grapheme, style) in graphemes_with_style {
                        if span_list.last().map(|(_, sty)| sty) == Some(&style) {
                            let (string, _) = span_list.last_mut().unwrap();
                            string.push_str(grapheme);
                        } else {
                            span_list.push((String::from(grapheme), style))
                        }
                    }

                    let spans: Vec<Span> = span_list
                        .into_iter()
                        .map(|(string, style)| Span::styled(string, style))
                        .collect();
                    let spans: Spans = spans.into();
                    *cell = Cell::from(spans);

                    cell_start_byte_offset += cell_len + TEMP_CELL_SEP.len();
                }

                row
            });

        let table = Table::new(options)
            .style(text_style)
            .highlight_style(selected)
            .highlight_symbol(" > ")
            .column_spacing(1)
            .widths(&self.widths);

        use tui::widgets::TableState;

        table.render_table(
            inner,
            surface,
            &mut TableState {
                offset: 0,
                selected: Some(cursor),
            },
            self.truncate_start,
        );
    }

    fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        // -- Render the frame:
        // clear area
        let background = cx.editor.theme.get("ui.background");
        let text = cx.editor.theme.get("ui.text");
        surface.clear_with(area, background);

        // don't like this but the lifetime sucks
        let block = Block::default().borders(Borders::ALL);

        // calculate the inner area inside the box
        let inner = block.inner(area);
        // 1 column gap on either side
        let margin = Margin::horizontal(1);
        let inner = inner.inner(&margin);
        block.render(area, surface);

        if let Some((path, range)) = self.current_file(cx.editor) {
            let preview = self.get_preview(path, cx.editor);
            let doc = match preview.document() {
                Some(doc) => doc,
                None => {
                    let alt_text = preview.placeholder();
                    let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2;
                    let y = inner.y + inner.height / 2;
                    surface.set_stringn(x, y, alt_text, inner.width as usize, text);
                    return;
                }
            };

            // align to middle
            let first_line = range
                .map(|(start, end)| {
                    let height = end.saturating_sub(start) + 1;
                    let middle = start + (height.saturating_sub(1) / 2);
                    middle.saturating_sub(inner.height as usize / 2).min(start)
                })
                .unwrap_or(0);

            let offset = ViewPosition {
                anchor: doc.text().line_to_char(first_line),
                horizontal_offset: 0,
                vertical_offset: 0,
            };

            let mut highlights = EditorView::doc_syntax_highlights(
                doc,
                offset.anchor,
                area.height,
                &cx.editor.theme,
            );
            for spans in EditorView::doc_diagnostics_highlights(doc, &cx.editor.theme) {
                if spans.is_empty() {
                    continue;
                }
                highlights = Box::new(helix_core::syntax::merge(highlights, spans));
            }
            let mut decorations: Vec<Box<dyn LineDecoration>> = Vec::new();

            if let Some((start, end)) = range {
                let style = cx
                    .editor
                    .theme
                    .try_get("ui.highlight")
                    .unwrap_or_else(|| cx.editor.theme.get("ui.selection"));
                let draw_highlight = move |renderer: &mut TextRenderer, pos: LinePos| {
                    if (start..=end).contains(&pos.doc_line) {
                        let area = Rect::new(
                            renderer.viewport.x,
                            renderer.viewport.y + pos.visual_line,
                            renderer.viewport.width,
                            1,
                        );
                        renderer.surface.set_style(area, style)
                    }
                };
                decorations.push(Box::new(draw_highlight))
            }

            render_document(
                surface,
                inner,
                doc,
                offset,
                // TODO: compute text annotations asynchronously here (like inlay hints)
                &TextAnnotations::default(),
                highlights,
                &cx.editor.theme,
                &mut decorations,
                &mut [],
            );
        }
    }

    fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
        if let Event::IdleTimeout = event {
            return self.handle_idle_timeout(ctx);
        }
        // TODO: keybinds for scrolling preview

        let key_event = match event {
            Event::Key(event) => *event,
            Event::Paste(..) => return self.prompt_handle_event(event, ctx),
            Event::Resize(..) => return EventResult::Consumed(None),
            _ => return EventResult::Ignored(None),
        };

        let close_fn =
            EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _ctx| {
                // remove the layer
                compositor.last_picker = compositor.pop();
            })));

        // So that idle timeout retriggers
        ctx.editor.reset_idle_timer();

        match key_event {
            shift!(Tab) | key!(Up) | ctrl!('p') => {
                self.move_by(1, Direction::Backward);
            }
            key!(Tab) | key!(Down) | ctrl!('n') => {
                self.move_by(1, Direction::Forward);
            }
            key!(PageDown) | ctrl!('d') => {
                self.page_down();
            }
            key!(PageUp) | ctrl!('u') => {
                self.page_up();
            }
            key!(Home) => {
                self.to_start();
            }
            key!(End) => {
                self.to_end();
            }
            key!(Esc) | ctrl!('c') => {
                return close_fn;
            }
            alt!(Enter) => {
                if let Some(option) = self.selection() {
                    (self.callback_fn)(ctx, option, Action::Load);
                }
            }
            key!(Enter) => {
                if let Some(option) = self.selection() {
                    (self.callback_fn)(ctx, option, Action::Replace);
                }
                return close_fn;
            }
            ctrl!('s') => {
                if let Some(option) = self.selection() {
                    (self.callback_fn)(ctx, option, Action::HorizontalSplit);
                }
                return close_fn;
            }
            ctrl!('v') => {
                if let Some(option) = self.selection() {
                    (self.callback_fn)(ctx, option, Action::VerticalSplit);
                }
                return close_fn;
            }
            ctrl!('t') => {
                self.toggle_preview();
            }
            _ => {
                self.prompt_handle_event(event, ctx);
            }
        }

        EventResult::Consumed(None)
    }

    fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
        let block = Block::default().borders(Borders::ALL);
        // calculate the inner area inside the box
        let inner = block.inner(area);

        // prompt area
        let area = inner.clip_left(1).with_height(1);

        self.prompt.cursor(area, editor)
    }

    fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> {
        self.completion_height = height.saturating_sub(4);
        Some((width, height))
    }

    fn id(&self) -> Option<&'static str> {
        Some("file-picker")
    }
}

impl<T: Item + 'static> Component for FilePicker<T> {
    fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        // +---------+ +---------+
        // |prompt   | |preview  |
        // +---------+ |         |
        // |picker   | |         |
        // |         | |         |
        // +---------+ +---------+

        let render_preview = self.picker.show_preview && area.width > MIN_AREA_WIDTH_FOR_PREVIEW;

        let picker_width = if render_preview {
            area.width / 2
        } else {
            area.width
        };

        let picker_area = area.with_width(picker_width);
        self.render_picker(picker_area, surface, cx);

        if render_preview {
            let preview_area = area.clip_left(picker_width);
            self.render_preview(preview_area, surface, cx);
        }
    }
}

#[derive(PartialEq, Eq, Debug)]
struct PickerMatch {
    score: i64,
    index: usize,
    len: usize,
}

impl PickerMatch {
    fn key(&self) -> impl Ord {
        (cmp::Reverse(self.score), self.len, self.index)
    }
}

impl PartialOrd for PickerMatch {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for PickerMatch {
    fn cmp(&self, other: &Self) -> Ordering {
        self.key().cmp(&other.key())
    }
}

type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;

pub struct Picker<T: Item> {
    options: Vec<T>,
    editor_data: T::Data,
    // filter: String,
    matcher: Box<Matcher>,
    matches: Vec<PickerMatch>,

    /// Current height of the completions box
    completion_height: u16,

    cursor: usize,
    // pattern: String,
    prompt: Prompt,
    previous_pattern: (String, FuzzyQuery),
    /// Whether to truncate the start (default true)
    pub truncate_start: bool,
    /// Whether to show the preview panel (default true)
    show_preview: bool,
    /// Constraints for tabular formatting
    widths: Vec<Constraint>,

    callback_fn: PickerCallback<T>,
}

impl<T: Item> Picker<T> {
    pub fn new(
        options: Vec<T>,
        editor_data: T::Data,
        callback_fn: impl Fn(&mut Context, &T, Action) + 'static,
    ) -> Self {
        unimplemented!()
    }

    pub fn set_options(&mut self, new_options: Vec<T>) {
        unimplemented!()
    }

    pub fn score(&mut self) {
        unimplemented!()
    }

    pub fn force_score(&mut self) {
        unimplemented!()
    }

    /// Move the cursor by a number of lines, either down (`Forward`) or up (`Backward`)
    pub fn move_by(&mut self, amount: usize, direction: Direction) {
        unimplemented!()
    }

    /// Move the cursor down by exactly one page. After the last page comes the first page.
    pub fn page_up(&mut self) {
        unimplemented!()
    }

    /// Move the cursor up by exactly one page. After the first page comes the last page.
    pub fn page_down(&mut self) {
        unimplemented!()
    }

    /// Move the cursor to the first entry
    pub fn to_start(&mut self) {
        unimplemented!()
    }

    /// Move the cursor to the last entry
    pub fn to_end(&mut self) {
        unimplemented!()
    }

    pub fn selection(&self) -> Option<&T> {
        unimplemented!()
    }

    pub fn toggle_preview(&mut self) {
        unimplemented!()
    }

    fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
        unimplemented!()
    }
}

// process:
// - read all the files into a list, maxed out at a large value
// - on input change:
//  - score all the names in relation to input

impl<T: Item + 'static> Component for Picker<T> {
    fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
        unimplemented!()
    }

    fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
        unimplemented!()
    }

    fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        unimplemented!()
    }

    fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) {
        unimplemented!()
    }
}

/// Returns a new list of options to replace the contents of the picker
/// when called with the current picker query,
pub type DynQueryCallback<T> =
    Box<dyn Fn(String, &mut Editor) -> BoxFuture<'static, anyhow::Result<Vec<T>>>>;

/// A picker that updates its contents via a callback whenever the
/// query string changes. Useful for live grep, workspace symbols, etc.
pub struct DynamicPicker<T: ui::menu::Item + Send> {
    file_picker: FilePicker<T>,
    query_callback: DynQueryCallback<T>,
    query: String,
}

impl<T: ui::menu::Item + Send> DynamicPicker<T> {
    pub const ID: &'static str = "dynamic-picker";

    pub fn new(file_picker: FilePicker<T>, query_callback: DynQueryCallback<T>) -> Self {
        Self {
            file_picker,
            query_callback,
            query: String::new(),
        }
    }
}

impl<T: Item + Send + 'static> Component for DynamicPicker<T> {
    fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        self.file_picker.render(area, surface, cx);
    }

    fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult {
        let event_result = self.file_picker.handle_event(event, cx);
        let current_query = self.file_picker.picker.prompt.line();

        if !matches!(event, Event::IdleTimeout) || self.query == *current_query {
            return event_result;
        }

        self.query.clone_from(current_query);

        let new_options = (self.query_callback)(current_query.to_owned(), cx.editor);

        cx.jobs.callback(async move {
            let new_options = new_options.await?;
            let callback = Callback::EditorCompositor(Box::new(move |editor, compositor| {
                // Wrapping of pickers in overlay is done outside the picker code,
                // so this is fragile and will break if wrapped in some other widget.
                let picker = match compositor.find_id::<Overlay<DynamicPicker<T>>>(Self::ID) {
                    Some(overlay) => &mut overlay.content.file_picker.picker,
                    None => return,
                };
                picker.set_options(new_options);
                editor.reset_idle_timer();
            }));
            anyhow::Ok(callback)
        });
        EventResult::Consumed(None)
    }

    fn cursor(&self, area: Rect, ctx: &Editor) -> (Option<Position>, CursorKind) {
        self.file_picker.cursor(area, ctx)
    }

    fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
        self.file_picker.required_size(viewport)
    }

    fn id(&self) -> Option<&'static str> {
        Some(Self::ID)
    }
}