| 1 |
mod handlers; |
| 2 |
mod query; |
| 3 |
|
| 4 |
use crate::{ |
| 5 |
alt, |
| 6 |
compositor::{self, Component, Compositor, Context, Event, EventResult}, |
| 7 |
ctrl, key, shift, |
| 8 |
ui::{ |
| 9 |
self, |
| 10 |
document::{render_document, LinePos, TextRenderer}, |
| 11 |
picker::query::PickerQuery, |
| 12 |
text_decorations::DecorationManager, |
| 13 |
EditorView, |
| 14 |
}, |
| 15 |
}; |
| 16 |
use futures_util::future::BoxFuture; |
| 17 |
use helix_event::AsyncHook; |
| 18 |
use nucleo::pattern::{CaseMatching, Normalization}; |
| 19 |
use nucleo::{Config, Nucleo}; |
| 20 |
use thiserror::Error; |
| 21 |
use tokio::sync::mpsc::Sender; |
| 22 |
use tui::{ |
| 23 |
buffer::Buffer as Surface, |
| 24 |
layout::Constraint, |
| 25 |
text::{Span, Spans}, |
| 26 |
widgets::{Block, BorderType, Cell, Row, Table}, |
| 27 |
}; |
| 28 |
|
| 29 |
use tui::widgets::Widget; |
| 30 |
|
| 31 |
use std::{ |
| 32 |
borrow::Cow, |
| 33 |
collections::HashMap, |
| 34 |
io::Read, |
| 35 |
path::Path, |
| 36 |
sync::{ |
| 37 |
atomic::{self, AtomicUsize}, |
| 38 |
Arc, |
| 39 |
}, |
| 40 |
}; |
| 41 |
|
| 42 |
use crate::ui::{Prompt, PromptEvent}; |
| 43 |
use helix_core::{ |
| 44 |
char_idx_at_visual_offset, fuzzy::MATCHER, movement::Direction, |
| 45 |
text_annotations::TextAnnotations, unicode::segmentation::UnicodeSegmentation, Position, |
| 46 |
}; |
| 47 |
use helix_view::{ |
| 48 |
editor::Action, |
| 49 |
graphics::{CursorKind, Margin, Modifier, Rect}, |
| 50 |
theme::Style, |
| 51 |
view::ViewPosition, |
| 52 |
Document, DocumentId, Editor, |
| 53 |
}; |
| 54 |
|
| 55 |
use self::handlers::{DynamicQueryChange, DynamicQueryHandler, PreviewHighlightHandler}; |
| 56 |
|
| 57 |
pub const ID: &str = "picker"; |
| 58 |
|
| 59 |
pub const MIN_AREA_WIDTH_FOR_PREVIEW: u16 = 72; |
| 60 |
|
| 61 |
pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024; |
| 62 |
|
| 63 |
#[derive(PartialEq, Eq, Hash)] |
| 64 |
pub enum PathOrId<'a> { |
| 65 |
Id(DocumentId), |
| 66 |
Path(&'a Path), |
| 67 |
} |
| 68 |
|
| 69 |
impl<'a> From<&'a Path> for PathOrId<'a> { |
| 70 |
fn from(path: &'a Path) -> Self { |
| 71 |
Self::Path(path) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl From<DocumentId> for PathOrId<'_> { |
| 76 |
fn from(v: DocumentId) -> Self { |
| 77 |
Self::Id(v) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
type FileCallback<T> = Box<dyn for<'a> Fn(&'a Editor, &'a T) -> Option<FileLocation<'a>>>; |
| 82 |
|
| 83 |
|
| 84 |
pub type FileLocation<'a> = (PathOrId<'a>, Option<(usize, usize)>); |
| 85 |
|
| 86 |
pub enum CachedPreview { |
| 87 |
Document(Box<Document>), |
| 88 |
Directory(Vec<(String, bool)>), |
| 89 |
Binary, |
| 90 |
LargeFile, |
| 91 |
NotFound, |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub enum Preview<'picker, 'editor> { |
| 97 |
Cached(&'picker CachedPreview), |
| 98 |
EditorDocument(&'editor Document), |
| 99 |
} |
| 100 |
|
| 101 |
impl Preview<'_, '_> { |
| 102 |
fn document(&self) -> Option<&Document> { |
| 103 |
match self { |
| 104 |
Preview::EditorDocument(doc) => Some(doc), |
| 105 |
Preview::Cached(CachedPreview::Document(doc)) => Some(doc), |
| 106 |
_ => None, |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
fn dir_content(&self) -> Option<&Vec<(String, bool)>> { |
| 111 |
match self { |
| 112 |
Preview::Cached(CachedPreview::Directory(dir_content)) => Some(dir_content), |
| 113 |
_ => None, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
fn placeholder(&self) -> &str { |
| 119 |
match *self { |
| 120 |
Self::EditorDocument(_) => "<Invalid file location>", |
| 121 |
Self::Cached(preview) => match preview { |
| 122 |
CachedPreview::Document(_) => "<Invalid file location>", |
| 123 |
CachedPreview::Directory(_) => "<Invalid directory location>", |
| 124 |
CachedPreview::Binary => "<Binary file>", |
| 125 |
CachedPreview::LargeFile => "<File too large to preview>", |
| 126 |
CachedPreview::NotFound => "<File not found>", |
| 127 |
}, |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
fn inject_nucleo_item<T, D>( |
| 133 |
injector: &nucleo::Injector<T>, |
| 134 |
columns: &[Column<T, D>], |
| 135 |
item: T, |
| 136 |
editor_data: &D, |
| 137 |
) { |
| 138 |
injector.push(item, |item, dst| { |
| 139 |
for (column, text) in columns.iter().filter(|column| column.filter).zip(dst) { |
| 140 |
*text = column.format_text(item, editor_data).into() |
| 141 |
} |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
pub struct Injector<T, D> { |
| 146 |
dst: nucleo::Injector<T>, |
| 147 |
columns: Arc<[Column<T, D>]>, |
| 148 |
editor_data: Arc<D>, |
| 149 |
version: usize, |
| 150 |
picker_version: Arc<AtomicUsize>, |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
_redraw: helix_event::RequestRedrawOnDrop, |
| 157 |
} |
| 158 |
|
| 159 |
impl<I, D> Clone for Injector<I, D> { |
| 160 |
fn clone(&self) -> Self { |
| 161 |
Injector { |
| 162 |
dst: self.dst.clone(), |
| 163 |
columns: self.columns.clone(), |
| 164 |
editor_data: self.editor_data.clone(), |
| 165 |
version: self.version, |
| 166 |
picker_version: self.picker_version.clone(), |
| 167 |
_redraw: helix_event::RequestRedrawOnDrop, |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
#[derive(Error, Debug)] |
| 173 |
#[error("picker has been shut down")] |
| 174 |
pub struct InjectorShutdown; |
| 175 |
|
| 176 |
impl<T, D> Injector<T, D> { |
| 177 |
pub fn push(&self, item: T) -> Result<(), InjectorShutdown> { |
| 178 |
if self.version != self.picker_version.load(atomic::Ordering::Relaxed) { |
| 179 |
return Err(InjectorShutdown); |
| 180 |
} |
| 181 |
|
| 182 |
inject_nucleo_item(&self.dst, &self.columns, item, &self.editor_data); |
| 183 |
Ok(()) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
type ColumnFormatFn<T, D> = for<'a> fn(&'a T, &'a D) -> Cell<'a>; |
| 188 |
|
| 189 |
pub struct Column<T, D> { |
| 190 |
name: Arc<str>, |
| 191 |
format: ColumnFormatFn<T, D>, |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
filter: bool, |
| 196 |
hidden: bool, |
| 197 |
} |
| 198 |
|
| 199 |
impl<T, D> Column<T, D> { |
| 200 |
pub fn new(name: impl Into<Arc<str>>, format: ColumnFormatFn<T, D>) -> Self { |
| 201 |
Self { |
| 202 |
name: name.into(), |
| 203 |
format, |
| 204 |
filter: true, |
| 205 |
hidden: false, |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
pub fn hidden(name: impl Into<Arc<str>>) -> Self { |
| 211 |
let format = |_: &T, _: &D| unreachable!(); |
| 212 |
|
| 213 |
Self { |
| 214 |
name: name.into(), |
| 215 |
format, |
| 216 |
filter: false, |
| 217 |
hidden: true, |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
pub fn without_filtering(mut self) -> Self { |
| 222 |
self.filter = false; |
| 223 |
self |
| 224 |
} |
| 225 |
|
| 226 |
fn format<'a>(&self, item: &'a T, data: &'a D) -> Cell<'a> { |
| 227 |
(self.format)(item, data) |
| 228 |
} |
| 229 |
|
| 230 |
fn format_text<'a>(&self, item: &'a T, data: &'a D) -> Cow<'a, str> { |
| 231 |
let text: String = self.format(item, data).content.into(); |
| 232 |
text.into() |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
type DynQueryCallback<T, D> = |
| 239 |
fn(&str, &mut Editor, Arc<D>, &Injector<T, D>) -> BoxFuture<'static, anyhow::Result<()>>; |
| 240 |
|
| 241 |
pub struct Picker<T: 'static + Send + Sync, D: 'static> { |
| 242 |
columns: Arc<[Column<T, D>]>, |
| 243 |
primary_column: usize, |
| 244 |
editor_data: Arc<D>, |
| 245 |
version: Arc<AtomicUsize>, |
| 246 |
matcher: Nucleo<T>, |
| 247 |
|
| 248 |
|
| 249 |
completion_height: u16, |
| 250 |
|
| 251 |
cursor: u32, |
| 252 |
prompt: Prompt, |
| 253 |
query: PickerQuery, |
| 254 |
|
| 255 |
|
| 256 |
show_preview: bool, |
| 257 |
|
| 258 |
widths: Vec<Constraint>, |
| 259 |
|
| 260 |
callback_fn: PickerCallback<T>, |
| 261 |
default_action: Action, |
| 262 |
|
| 263 |
pub truncate_start: bool, |
| 264 |
|
| 265 |
preview_cache: HashMap<Arc<Path>, CachedPreview>, |
| 266 |
read_buffer: Vec<u8>, |
| 267 |
|
| 268 |
file_fn: Option<FileCallback<T>>, |
| 269 |
|
| 270 |
preview_highlight_handler: Sender<Arc<Path>>, |
| 271 |
dynamic_query_handler: Option<Sender<DynamicQueryChange>>, |
| 272 |
} |
| 273 |
|
| 274 |
impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> { |
| 275 |
pub fn stream( |
| 276 |
columns: impl IntoIterator<Item = Column<T, D>>, |
| 277 |
editor_data: D, |
| 278 |
) -> (Nucleo<T>, Injector<T, D>) { |
| 279 |
let columns: Arc<[_]> = columns.into_iter().collect(); |
| 280 |
let matcher_columns = columns.iter().filter(|col| col.filter).count() as u32; |
| 281 |
assert!(matcher_columns > 0); |
| 282 |
let matcher = Nucleo::new( |
| 283 |
Config::DEFAULT, |
| 284 |
Arc::new(helix_event::request_redraw), |
| 285 |
None, |
| 286 |
matcher_columns, |
| 287 |
); |
| 288 |
let streamer = Injector { |
| 289 |
dst: matcher.injector(), |
| 290 |
columns, |
| 291 |
editor_data: Arc::new(editor_data), |
| 292 |
version: 0, |
| 293 |
picker_version: Arc::new(AtomicUsize::new(0)), |
| 294 |
_redraw: helix_event::RequestRedrawOnDrop, |
| 295 |
}; |
| 296 |
(matcher, streamer) |
| 297 |
} |
| 298 |
|
| 299 |
pub fn new<C, O, F>( |
| 300 |
columns: C, |
| 301 |
primary_column: usize, |
| 302 |
options: O, |
| 303 |
editor_data: D, |
| 304 |
callback_fn: F, |
| 305 |
) -> Self |
| 306 |
where |
| 307 |
C: IntoIterator<Item = Column<T, D>>, |
| 308 |
O: IntoIterator<Item = T>, |
| 309 |
F: Fn(&mut Context, &T, Action) + 'static, |
| 310 |
{ |
| 311 |
let columns: Arc<[_]> = columns.into_iter().collect(); |
| 312 |
let matcher_columns = columns |
| 313 |
.iter() |
| 314 |
.filter(|col: &&Column<T, D>| col.filter) |
| 315 |
.count() as u32; |
| 316 |
assert!(matcher_columns > 0); |
| 317 |
let matcher = Nucleo::new( |
| 318 |
Config::DEFAULT, |
| 319 |
Arc::new(helix_event::request_redraw), |
| 320 |
None, |
| 321 |
matcher_columns, |
| 322 |
); |
| 323 |
let injector = matcher.injector(); |
| 324 |
for item in options { |
| 325 |
inject_nucleo_item(&injector, &columns, item, &editor_data); |
| 326 |
} |
| 327 |
Self::with( |
| 328 |
matcher, |
| 329 |
columns, |
| 330 |
primary_column, |
| 331 |
Arc::new(editor_data), |
| 332 |
Arc::new(AtomicUsize::new(0)), |
| 333 |
callback_fn, |
| 334 |
) |
| 335 |
} |
| 336 |
|
| 337 |
pub fn with_stream( |
| 338 |
matcher: Nucleo<T>, |
| 339 |
primary_column: usize, |
| 340 |
injector: Injector<T, D>, |
| 341 |
callback_fn: impl Fn(&mut Context, &T, Action) + 'static, |
| 342 |
) -> Self { |
| 343 |
Self::with( |
| 344 |
matcher, |
| 345 |
injector.columns, |
| 346 |
primary_column, |
| 347 |
injector.editor_data, |
| 348 |
injector.picker_version, |
| 349 |
callback_fn, |
| 350 |
) |
| 351 |
} |
| 352 |
|
| 353 |
fn with( |
| 354 |
matcher: Nucleo<T>, |
| 355 |
columns: Arc<[Column<T, D>]>, |
| 356 |
default_column: usize, |
| 357 |
editor_data: Arc<D>, |
| 358 |
version: Arc<AtomicUsize>, |
| 359 |
callback_fn: impl Fn(&mut Context, &T, Action) + 'static, |
| 360 |
) -> Self { |
| 361 |
assert!(!columns.is_empty()); |
| 362 |
|
| 363 |
let prompt = Prompt::new( |
| 364 |
"".into(), |
| 365 |
None, |
| 366 |
ui::completers::none, |
| 367 |
|_editor: &mut Context, _pattern: &str, _event: PromptEvent| {}, |
| 368 |
); |
| 369 |
|
| 370 |
let widths = columns |
| 371 |
.iter() |
| 372 |
.map(|column| Constraint::Length(column.name.chars().count() as u16)) |
| 373 |
.collect(); |
| 374 |
|
| 375 |
let query = PickerQuery::new(columns.iter().map(|col| &col.name).cloned(), default_column); |
| 376 |
|
| 377 |
Self { |
| 378 |
columns, |
| 379 |
primary_column: default_column, |
| 380 |
matcher, |
| 381 |
editor_data, |
| 382 |
version, |
| 383 |
cursor: 0, |
| 384 |
prompt, |
| 385 |
query, |
| 386 |
truncate_start: true, |
| 387 |
show_preview: true, |
| 388 |
callback_fn: Box::new(callback_fn), |
| 389 |
default_action: Action::Replace, |
| 390 |
completion_height: 0, |
| 391 |
widths, |
| 392 |
preview_cache: HashMap::new(), |
| 393 |
read_buffer: Vec::with_capacity(1024), |
| 394 |
file_fn: None, |
| 395 |
preview_highlight_handler: PreviewHighlightHandler::<T, D>::default().spawn(), |
| 396 |
dynamic_query_handler: None, |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
pub fn injector(&self) -> Injector<T, D> { |
| 401 |
Injector { |
| 402 |
dst: self.matcher.injector(), |
| 403 |
columns: self.columns.clone(), |
| 404 |
editor_data: self.editor_data.clone(), |
| 405 |
version: self.version.load(atomic::Ordering::Relaxed), |
| 406 |
picker_version: self.version.clone(), |
| 407 |
_redraw: helix_event::RequestRedrawOnDrop, |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
pub fn truncate_start(mut self, truncate_start: bool) -> Self { |
| 412 |
self.truncate_start = truncate_start; |
| 413 |
self |
| 414 |
} |
| 415 |
|
| 416 |
pub fn with_preview( |
| 417 |
mut self, |
| 418 |
preview_fn: impl for<'a> Fn(&'a Editor, &'a T) -> Option<FileLocation<'a>> + 'static, |
| 419 |
) -> Self { |
| 420 |
self.file_fn = Some(Box::new(preview_fn)); |
| 421 |
|
| 422 |
|
| 423 |
self.matcher.update_config(Config::DEFAULT.match_paths()); |
| 424 |
self |
| 425 |
} |
| 426 |
|
| 427 |
pub fn with_history_register(mut self, history_register: Option<char>) -> Self { |
| 428 |
self.prompt.with_history_register(history_register); |
| 429 |
self |
| 430 |
} |
| 431 |
|
| 432 |
pub fn with_initial_cursor(mut self, cursor: u32) -> Self { |
| 433 |
self.cursor = cursor; |
| 434 |
self |
| 435 |
} |
| 436 |
|
| 437 |
pub fn with_dynamic_query( |
| 438 |
mut self, |
| 439 |
callback: DynQueryCallback<T, D>, |
| 440 |
debounce_ms: Option<u64>, |
| 441 |
) -> Self { |
| 442 |
let handler = DynamicQueryHandler::new(callback, debounce_ms).spawn(); |
| 443 |
let event = DynamicQueryChange { |
| 444 |
query: self.primary_query(), |
| 445 |
|
| 446 |
is_paste: true, |
| 447 |
}; |
| 448 |
helix_event::send_blocking(&handler, event); |
| 449 |
self.dynamic_query_handler = Some(handler); |
| 450 |
self |
| 451 |
} |
| 452 |
|
| 453 |
pub fn with_default_action(mut self, action: Action) -> Self { |
| 454 |
self.default_action = action; |
| 455 |
self |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
pub fn move_by(&mut self, amount: u32, direction: Direction) { |
| 460 |
let len = self.matcher.snapshot().matched_item_count(); |
| 461 |
|
| 462 |
if len == 0 { |
| 463 |
|
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
match direction { |
| 468 |
Direction::Forward => { |
| 469 |
self.cursor = self.cursor.saturating_add(amount) % len; |
| 470 |
} |
| 471 |
Direction::Backward => { |
| 472 |
self.cursor = self.cursor.saturating_add(len).saturating_sub(amount) % len; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
pub fn page_up(&mut self) { |
| 479 |
self.move_by(self.completion_height as u32, Direction::Backward); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
pub fn page_down(&mut self) { |
| 484 |
self.move_by(self.completion_height as u32, Direction::Forward); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
pub fn to_start(&mut self) { |
| 489 |
self.cursor = 0; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
pub fn to_end(&mut self) { |
| 494 |
self.cursor = self |
| 495 |
.matcher |
| 496 |
.snapshot() |
| 497 |
.matched_item_count() |
| 498 |
.saturating_sub(1); |
| 499 |
} |
| 500 |
|
| 501 |
pub fn selection(&self) -> Option<&T> { |
| 502 |
self.matcher |
| 503 |
.snapshot() |
| 504 |
.get_matched_item(self.cursor) |
| 505 |
.map(|item| item.data) |
| 506 |
} |
| 507 |
|
| 508 |
fn primary_query(&self) -> Arc<str> { |
| 509 |
self.query |
| 510 |
.get(&self.columns[self.primary_column].name) |
| 511 |
.cloned() |
| 512 |
.unwrap_or_else(|| "".into()) |
| 513 |
} |
| 514 |
|
| 515 |
fn header_height(&self) -> u16 { |
| 516 |
if self.columns.len() > 1 { |
| 517 |
1 |
| 518 |
} else { |
| 519 |
0 |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
pub fn toggle_preview(&mut self) { |
| 524 |
self.show_preview = !self.show_preview; |
| 525 |
} |
| 526 |
|
| 527 |
fn prompt_handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { |
| 528 |
if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) { |
| 529 |
self.handle_prompt_change(matches!(event, Event::Paste(_))); |
| 530 |
} |
| 531 |
EventResult::Consumed(None) |
| 532 |
} |
| 533 |
|
| 534 |
fn handle_prompt_change(&mut self, is_paste: bool) { |
| 535 |
|
| 536 |
let line = self.prompt.line(); |
| 537 |
let old_query = self.query.parse(line); |
| 538 |
if self.query == old_query { |
| 539 |
return; |
| 540 |
} |
| 541 |
|
| 542 |
self.cursor = 0; |
| 543 |
|
| 544 |
for (i, column) in self |
| 545 |
.columns |
| 546 |
.iter() |
| 547 |
.filter(|column| column.filter) |
| 548 |
.enumerate() |
| 549 |
{ |
| 550 |
let pattern = self |
| 551 |
.query |
| 552 |
.get(&column.name) |
| 553 |
.map(|f| &**f) |
| 554 |
.unwrap_or_default(); |
| 555 |
let old_pattern = old_query |
| 556 |
.get(&column.name) |
| 557 |
.map(|f| &**f) |
| 558 |
.unwrap_or_default(); |
| 559 |
|
| 560 |
if pattern == old_pattern { |
| 561 |
continue; |
| 562 |
} |
| 563 |
let is_append = pattern.starts_with(old_pattern); |
| 564 |
self.matcher.pattern.reparse( |
| 565 |
i, |
| 566 |
pattern, |
| 567 |
CaseMatching::Smart, |
| 568 |
Normalization::Smart, |
| 569 |
is_append, |
| 570 |
); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
if let Some(handler) = &self.dynamic_query_handler { |
| 575 |
let event = DynamicQueryChange { |
| 576 |
query: self.primary_query(), |
| 577 |
is_paste, |
| 578 |
}; |
| 579 |
helix_event::send_blocking(handler, event); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
fn get_preview<'picker, 'editor>( |
| 586 |
&'picker mut self, |
| 587 |
editor: &'editor Editor, |
| 588 |
) -> Option<(Preview<'picker, 'editor>, Option<(usize, usize)>)> { |
| 589 |
let current = self.selection()?; |
| 590 |
let (path_or_id, range) = (self.file_fn.as_ref()?)(editor, current)?; |
| 591 |
|
| 592 |
match path_or_id { |
| 593 |
PathOrId::Path(path) => { |
| 594 |
if let Some(doc) = editor.document_by_path(path) { |
| 595 |
return Some((Preview::EditorDocument(doc), range)); |
| 596 |
} |
| 597 |
|
| 598 |
if self.preview_cache.contains_key(path) { |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
let (path, preview) = self.preview_cache.get_key_value(path).unwrap(); |
| 603 |
if matches!(preview, CachedPreview::Document(doc) if doc.syntax().is_none()) { |
| 604 |
helix_event::send_blocking(&self.preview_highlight_handler, path.clone()); |
| 605 |
} |
| 606 |
return Some((Preview::Cached(preview), range)); |
| 607 |
} |
| 608 |
|
| 609 |
let path: Arc<Path> = path.into(); |
| 610 |
let preview = std::fs::metadata(&path) |
| 611 |
.and_then(|metadata| { |
| 612 |
if metadata.is_dir() { |
| 613 |
let files = super::directory_content(&path, editor)?; |
| 614 |
let file_names: Vec<_> = files |
| 615 |
.iter() |
| 616 |
.filter_map(|(file_path, is_dir)| { |
| 617 |
let name = file_path |
| 618 |
.strip_prefix(&path) |
| 619 |
.map(|p| Some(p.as_os_str())) |
| 620 |
.unwrap_or_else(|_| file_path.file_name())? |
| 621 |
.to_string_lossy(); |
| 622 |
if *is_dir { |
| 623 |
Some((format!("{}/", name), true)) |
| 624 |
} else { |
| 625 |
Some((name.into_owned(), false)) |
| 626 |
} |
| 627 |
}) |
| 628 |
.collect(); |
| 629 |
Ok(CachedPreview::Directory(file_names)) |
| 630 |
} else if metadata.is_file() { |
| 631 |
if metadata.len() > MAX_FILE_SIZE_FOR_PREVIEW { |
| 632 |
return Ok(CachedPreview::LargeFile); |
| 633 |
} |
| 634 |
let is_binary = std::fs::File::open(&path).and_then(|file| { |
| 635 |
|
| 636 |
let n = file.take(1024).read_to_end(&mut self.read_buffer)?; |
| 637 |
let is_binary = crate::is_binary(&self.read_buffer[..n]); |
| 638 |
self.read_buffer.clear(); |
| 639 |
Ok(is_binary) |
| 640 |
})?; |
| 641 |
if is_binary { |
| 642 |
return Ok(CachedPreview::Binary); |
| 643 |
} |
| 644 |
let mut doc = Document::open( |
| 645 |
&path, |
| 646 |
None, |
| 647 |
false, |
| 648 |
editor.config.clone(), |
| 649 |
editor.syn_loader.clone(), |
| 650 |
) |
| 651 |
.or(Err(std::io::Error::new( |
| 652 |
std::io::ErrorKind::NotFound, |
| 653 |
"Cannot open document", |
| 654 |
)))?; |
| 655 |
let loader = editor.syn_loader.load(); |
| 656 |
if let Some(language_config) = doc.detect_language_config(&loader) { |
| 657 |
doc.language = Some(language_config); |
| 658 |
|
| 659 |
helix_event::send_blocking( |
| 660 |
&self.preview_highlight_handler, |
| 661 |
path.clone(), |
| 662 |
); |
| 663 |
} |
| 664 |
Ok(CachedPreview::Document(Box::new(doc))) |
| 665 |
} else { |
| 666 |
Err(std::io::Error::new( |
| 667 |
std::io::ErrorKind::NotFound, |
| 668 |
"Neither a dir, nor a file", |
| 669 |
)) |
| 670 |
} |
| 671 |
}) |
| 672 |
.unwrap_or(CachedPreview::NotFound); |
| 673 |
self.preview_cache.insert(path.clone(), preview); |
| 674 |
Some((Preview::Cached(&self.preview_cache[&path]), range)) |
| 675 |
} |
| 676 |
PathOrId::Id(id) => { |
| 677 |
let doc = editor.documents.get(&id).unwrap(); |
| 678 |
Some((Preview::EditorDocument(doc), range)) |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
fn render_picker(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 684 |
let status = self.matcher.tick(10); |
| 685 |
let snapshot = self.matcher.snapshot(); |
| 686 |
if status.changed { |
| 687 |
self.cursor = self |
| 688 |
.cursor |
| 689 |
.min(snapshot.matched_item_count().saturating_sub(1)) |
| 690 |
} |
| 691 |
|
| 692 |
let text_style = cx.editor.theme.get("ui.text"); |
| 693 |
let selected = cx.editor.theme.get("ui.text.focus"); |
| 694 |
let highlight_style = cx.editor.theme.get("special").add_modifier(Modifier::BOLD); |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
let background = cx.editor.theme.get("ui.background"); |
| 699 |
surface.clear_with(area, background); |
| 700 |
|
| 701 |
const BLOCK: Block<'_> = Block::bordered(); |
| 702 |
|
| 703 |
|
| 704 |
let inner = BLOCK.inner(area); |
| 705 |
|
| 706 |
BLOCK.render(area, surface); |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
let count = format!( |
| 711 |
"{}{}/{}", |
| 712 |
if status.running || self.matcher.active_injectors() > 0 { |
| 713 |
"(running) " |
| 714 |
} else { |
| 715 |
"" |
| 716 |
}, |
| 717 |
snapshot.matched_item_count(), |
| 718 |
snapshot.item_count(), |
| 719 |
); |
| 720 |
|
| 721 |
let area = inner.clip_left(1).with_height(1); |
| 722 |
let line_area = area.clip_right(count.len() as u16 + 1); |
| 723 |
|
| 724 |
|
| 725 |
self.prompt.render(line_area, surface, cx); |
| 726 |
|
| 727 |
surface.set_stringn( |
| 728 |
(area.x + area.width).saturating_sub(count.len() as u16 + 1), |
| 729 |
area.y, |
| 730 |
&count, |
| 731 |
(count.len()).min(area.width as usize), |
| 732 |
text_style, |
| 733 |
); |
| 734 |
|
| 735 |
|
| 736 |
let sep_style = cx.editor.theme.get("ui.background.separator"); |
| 737 |
let borders = BorderType::line_symbols(BorderType::Plain); |
| 738 |
for x in inner.left()..inner.right() { |
| 739 |
if let Some(cell) = surface.get_mut(x, inner.y + 1) { |
| 740 |
cell.set_symbol(borders.horizontal).set_style(sep_style); |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
let inner = inner.clip_top(2); |
| 747 |
let rows = inner.height.saturating_sub(self.header_height()) as u32; |
| 748 |
let offset = self.cursor - (self.cursor % std::cmp::max(1, rows)); |
| 749 |
let cursor = self.cursor.saturating_sub(offset); |
| 750 |
let end = offset |
| 751 |
.saturating_add(rows) |
| 752 |
.min(snapshot.matched_item_count()); |
| 753 |
let mut indices = Vec::new(); |
| 754 |
let mut matcher = MATCHER.lock(); |
| 755 |
matcher.config = Config::DEFAULT; |
| 756 |
if self.file_fn.is_some() { |
| 757 |
matcher.config.set_match_paths() |
| 758 |
} |
| 759 |
|
| 760 |
let options = snapshot.matched_items(offset..end).map(|item| { |
| 761 |
let mut widths = self.widths.iter_mut(); |
| 762 |
let mut matcher_index = 0; |
| 763 |
|
| 764 |
Row::new(self.columns.iter().map(|column| { |
| 765 |
if column.hidden { |
| 766 |
return Cell::default(); |
| 767 |
} |
| 768 |
|
| 769 |
let Some(Constraint::Length(max_width)) = widths.next() else { |
| 770 |
unreachable!(); |
| 771 |
}; |
| 772 |
let mut cell = column.format(item.data, &self.editor_data); |
| 773 |
let width = if column.filter { |
| 774 |
snapshot.pattern().column_pattern(matcher_index).indices( |
| 775 |
item.matcher_columns[matcher_index].slice(..), |
| 776 |
&mut matcher, |
| 777 |
&mut indices, |
| 778 |
); |
| 779 |
indices.sort_unstable(); |
| 780 |
indices.dedup(); |
| 781 |
let mut indices = indices.drain(..); |
| 782 |
let mut next_highlight_idx = indices.next().unwrap_or(u32::MAX); |
| 783 |
let mut span_list = Vec::new(); |
| 784 |
let mut current_span = String::new(); |
| 785 |
let mut current_style = Style::default(); |
| 786 |
let mut grapheme_idx = 0u32; |
| 787 |
let mut width = 0; |
| 788 |
|
| 789 |
let spans: &[Span] = |
| 790 |
cell.content.lines.first().map_or(&[], |it| it.0.as_slice()); |
| 791 |
for span in spans { |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
for grapheme in span.content.graphemes(true) { |
| 798 |
let style = if grapheme_idx == next_highlight_idx { |
| 799 |
next_highlight_idx = indices.next().unwrap_or(u32::MAX); |
| 800 |
span.style.patch(highlight_style) |
| 801 |
} else { |
| 802 |
span.style |
| 803 |
}; |
| 804 |
if style != current_style { |
| 805 |
if !current_span.is_empty() { |
| 806 |
span_list.push(Span::styled(current_span, current_style)) |
| 807 |
} |
| 808 |
current_span = String::new(); |
| 809 |
current_style = style; |
| 810 |
} |
| 811 |
current_span.push_str(grapheme); |
| 812 |
grapheme_idx += 1; |
| 813 |
} |
| 814 |
width += span.width(); |
| 815 |
} |
| 816 |
|
| 817 |
span_list.push(Span::styled(current_span, current_style)); |
| 818 |
cell = Cell::from(Spans::from(span_list)); |
| 819 |
matcher_index += 1; |
| 820 |
width |
| 821 |
} else { |
| 822 |
cell.content |
| 823 |
.lines |
| 824 |
.first() |
| 825 |
.map(|line| line.width()) |
| 826 |
.unwrap_or_default() |
| 827 |
}; |
| 828 |
|
| 829 |
if width as u16 > *max_width { |
| 830 |
*max_width = width as u16; |
| 831 |
} |
| 832 |
|
| 833 |
cell |
| 834 |
})) |
| 835 |
}); |
| 836 |
|
| 837 |
let mut table = Table::new(options) |
| 838 |
.style(text_style) |
| 839 |
.highlight_style(selected) |
| 840 |
.highlight_symbol(" > ") |
| 841 |
.column_spacing(1) |
| 842 |
.widths(&self.widths); |
| 843 |
|
| 844 |
|
| 845 |
if self.columns.len() > 1 { |
| 846 |
let active_column = self.query.active_column(self.prompt.position()); |
| 847 |
let header_style = cx.editor.theme.get("ui.picker.header"); |
| 848 |
let header_column_style = cx.editor.theme.get("ui.picker.header.column"); |
| 849 |
|
| 850 |
table = table.header( |
| 851 |
Row::new(self.columns.iter().map(|column| { |
| 852 |
if column.hidden { |
| 853 |
Cell::default() |
| 854 |
} else { |
| 855 |
let style = |
| 856 |
if active_column.is_some_and(|name| Arc::ptr_eq(name, &column.name)) { |
| 857 |
cx.editor.theme.get("ui.picker.header.column.active") |
| 858 |
} else { |
| 859 |
header_column_style |
| 860 |
}; |
| 861 |
|
| 862 |
Cell::from(Span::styled(Cow::from(&*column.name), style)) |
| 863 |
} |
| 864 |
})) |
| 865 |
.style(header_style), |
| 866 |
); |
| 867 |
} |
| 868 |
|
| 869 |
use tui::widgets::TableState; |
| 870 |
|
| 871 |
table.render_table( |
| 872 |
inner, |
| 873 |
surface, |
| 874 |
&mut TableState { |
| 875 |
offset: 0, |
| 876 |
selected: Some(cursor as usize), |
| 877 |
}, |
| 878 |
self.truncate_start, |
| 879 |
); |
| 880 |
} |
| 881 |
|
| 882 |
fn render_preview(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 883 |
|
| 884 |
|
| 885 |
let background = cx.editor.theme.get("ui.background"); |
| 886 |
let text = cx.editor.theme.get("ui.text"); |
| 887 |
let directory = cx.editor.theme.get("ui.text.directory"); |
| 888 |
surface.clear_with(area, background); |
| 889 |
|
| 890 |
const BLOCK: Block<'_> = Block::bordered(); |
| 891 |
|
| 892 |
|
| 893 |
let inner = BLOCK.inner(area); |
| 894 |
|
| 895 |
let margin = Margin::horizontal(1); |
| 896 |
let inner = inner.inner(margin); |
| 897 |
BLOCK.render(area, surface); |
| 898 |
|
| 899 |
if let Some((preview, range)) = self.get_preview(cx.editor) { |
| 900 |
let doc = match preview.document() { |
| 901 |
Some(doc) |
| 902 |
if range.is_none_or(|(start, end)| { |
| 903 |
start <= end && end <= doc.text().len_lines() |
| 904 |
}) => |
| 905 |
{ |
| 906 |
doc |
| 907 |
} |
| 908 |
_ => { |
| 909 |
if let Some(dir_content) = preview.dir_content() { |
| 910 |
for (i, (path, is_dir)) in |
| 911 |
dir_content.iter().take(inner.height as usize).enumerate() |
| 912 |
{ |
| 913 |
let style = if *is_dir { directory } else { text }; |
| 914 |
surface.set_stringn( |
| 915 |
inner.x, |
| 916 |
inner.y + i as u16, |
| 917 |
path, |
| 918 |
inner.width as usize, |
| 919 |
style, |
| 920 |
); |
| 921 |
} |
| 922 |
return; |
| 923 |
} |
| 924 |
|
| 925 |
let alt_text = preview.placeholder(); |
| 926 |
let x = inner.x + inner.width.saturating_sub(alt_text.len() as u16) / 2; |
| 927 |
let y = inner.y + inner.height / 2; |
| 928 |
surface.set_stringn(x, y, alt_text, inner.width as usize, text); |
| 929 |
return; |
| 930 |
} |
| 931 |
}; |
| 932 |
|
| 933 |
let mut offset = ViewPosition::default(); |
| 934 |
if let Some((start_line, end_line)) = range { |
| 935 |
let height = end_line - start_line; |
| 936 |
let text = doc.text().slice(..); |
| 937 |
let start = text.line_to_char(start_line); |
| 938 |
let middle = text.line_to_char(start_line + height / 2); |
| 939 |
if height < inner.height as usize { |
| 940 |
let text_fmt = doc.text_format(inner.width, None); |
| 941 |
let annotations = TextAnnotations::default(); |
| 942 |
(offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset( |
| 943 |
text, |
| 944 |
middle, |
| 945 |
|
| 946 |
-(inner.height as isize / 2), |
| 947 |
0, |
| 948 |
&text_fmt, |
| 949 |
&annotations, |
| 950 |
); |
| 951 |
if start < offset.anchor { |
| 952 |
offset.anchor = start; |
| 953 |
offset.vertical_offset = 0; |
| 954 |
} |
| 955 |
} else { |
| 956 |
offset.anchor = start; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
let loader = cx.editor.syn_loader.load(); |
| 961 |
let config = cx.editor.config(); |
| 962 |
|
| 963 |
let syntax_highlighter = |
| 964 |
EditorView::doc_syntax_highlighter(doc, offset.anchor, area.height, &loader); |
| 965 |
let mut overlay_highlights = Vec::new(); |
| 966 |
if doc |
| 967 |
.language_config() |
| 968 |
.and_then(|config| config.rainbow_brackets) |
| 969 |
.unwrap_or(config.rainbow_brackets) |
| 970 |
{ |
| 971 |
if let Some(overlay) = EditorView::doc_rainbow_highlights( |
| 972 |
doc, |
| 973 |
offset.anchor, |
| 974 |
area.height, |
| 975 |
&cx.editor.theme, |
| 976 |
&loader, |
| 977 |
) { |
| 978 |
overlay_highlights.push(overlay); |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
EditorView::doc_diagnostics_highlights_into( |
| 983 |
doc, |
| 984 |
&cx.editor.theme, |
| 985 |
&mut overlay_highlights, |
| 986 |
); |
| 987 |
|
| 988 |
let mut decorations = DecorationManager::default(); |
| 989 |
|
| 990 |
if let Some((start, end)) = range { |
| 991 |
let style = cx |
| 992 |
.editor |
| 993 |
.theme |
| 994 |
.try_get("ui.highlight") |
| 995 |
.unwrap_or_else(|| cx.editor.theme.get("ui.selection")); |
| 996 |
let draw_highlight = move |renderer: &mut TextRenderer, pos: LinePos| { |
| 997 |
if (start..=end).contains(&pos.doc_line) { |
| 998 |
let area = Rect::new( |
| 999 |
renderer.viewport.x, |
| 1000 |
pos.visual_line, |
| 1001 |
renderer.viewport.width, |
| 1002 |
1, |
| 1003 |
); |
| 1004 |
renderer.set_style(area, style) |
| 1005 |
} |
| 1006 |
}; |
| 1007 |
decorations.add_decoration(draw_highlight); |
| 1008 |
} |
| 1009 |
|
| 1010 |
render_document( |
| 1011 |
surface, |
| 1012 |
inner, |
| 1013 |
doc, |
| 1014 |
offset, |
| 1015 |
|
| 1016 |
&TextAnnotations::default(), |
| 1017 |
syntax_highlighter, |
| 1018 |
overlay_highlights, |
| 1019 |
&cx.editor.theme, |
| 1020 |
decorations, |
| 1021 |
); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I, D> { |
| 1027 |
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
let render_preview = |
| 1036 |
self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; |
| 1037 |
|
| 1038 |
let picker_width = if render_preview { |
| 1039 |
area.width / 2 |
| 1040 |
} else { |
| 1041 |
area.width |
| 1042 |
}; |
| 1043 |
|
| 1044 |
let picker_area = area.with_width(picker_width); |
| 1045 |
self.render_picker(picker_area, surface, cx); |
| 1046 |
|
| 1047 |
if render_preview { |
| 1048 |
let preview_area = area.clip_left(picker_width); |
| 1049 |
self.render_preview(preview_area, surface, cx); |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult { |
| 1054 |
|
| 1055 |
|
| 1056 |
let key_event = match event { |
| 1057 |
Event::Key(event) => *event, |
| 1058 |
Event::Paste(..) => return self.prompt_handle_event(event, ctx), |
| 1059 |
Event::Resize(..) => return EventResult::Consumed(None), |
| 1060 |
|
| 1061 |
|
| 1062 |
Event::Mouse(_) => return EventResult::Consumed(None), |
| 1063 |
_ => return EventResult::Ignored(None), |
| 1064 |
}; |
| 1065 |
|
| 1066 |
let close_fn = |picker: &mut Self| { |
| 1067 |
|
| 1068 |
|
| 1069 |
let callback: compositor::Callback = |
| 1070 |
if picker.matcher.snapshot().item_count() > 1_000_000 { |
| 1071 |
Box::new(|compositor: &mut Compositor, _ctx| { |
| 1072 |
|
| 1073 |
compositor.pop(); |
| 1074 |
}) |
| 1075 |
} else { |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
picker.version.fetch_add(1, atomic::Ordering::Relaxed); |
| 1081 |
Box::new(|compositor: &mut Compositor, _ctx| { |
| 1082 |
|
| 1083 |
compositor.last_picker = compositor.pop(); |
| 1084 |
}) |
| 1085 |
}; |
| 1086 |
EventResult::Consumed(Some(callback)) |
| 1087 |
}; |
| 1088 |
|
| 1089 |
match key_event { |
| 1090 |
shift!(Tab) | key!(Up) | ctrl!('p') => { |
| 1091 |
self.move_by(1, Direction::Backward); |
| 1092 |
} |
| 1093 |
key!(Tab) | key!(Down) | ctrl!('n') => { |
| 1094 |
self.move_by(1, Direction::Forward); |
| 1095 |
} |
| 1096 |
key!(PageDown) | ctrl!('d') => { |
| 1097 |
self.page_down(); |
| 1098 |
} |
| 1099 |
key!(PageUp) | ctrl!('u') => { |
| 1100 |
self.page_up(); |
| 1101 |
} |
| 1102 |
key!(Home) => { |
| 1103 |
self.to_start(); |
| 1104 |
} |
| 1105 |
key!(End) => { |
| 1106 |
self.to_end(); |
| 1107 |
} |
| 1108 |
key!(Esc) | ctrl!('c') => return close_fn(self), |
| 1109 |
alt!(Enter) => { |
| 1110 |
if let Some(option) = self.selection() { |
| 1111 |
(self.callback_fn)(ctx, option, self.default_action); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
key!(Enter) => { |
| 1115 |
|
| 1116 |
|
| 1117 |
if let Some(completion) = self |
| 1118 |
.prompt |
| 1119 |
.first_history_completion(ctx.editor) |
| 1120 |
.filter(|_| self.prompt.line().is_empty()) |
| 1121 |
{ |
| 1122 |
|
| 1123 |
|
| 1124 |
let completion = if completion.contains('%') { |
| 1125 |
completion.replace('%', "\\%") |
| 1126 |
} else { |
| 1127 |
completion.into_owned() |
| 1128 |
}; |
| 1129 |
self.prompt.set_line(completion, ctx.editor); |
| 1130 |
|
| 1131 |
|
| 1132 |
self.handle_prompt_change(true); |
| 1133 |
} else { |
| 1134 |
if let Some(option) = self.selection() { |
| 1135 |
(self.callback_fn)(ctx, option, self.default_action); |
| 1136 |
} |
| 1137 |
if let Some(history_register) = self.prompt.history_register() { |
| 1138 |
if let Err(err) = ctx |
| 1139 |
.editor |
| 1140 |
.registers |
| 1141 |
.push(history_register, self.primary_query().to_string()) |
| 1142 |
{ |
| 1143 |
ctx.editor.set_error(err.to_string()); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
return close_fn(self); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
ctrl!('s') => { |
| 1150 |
if let Some(option) = self.selection() { |
| 1151 |
(self.callback_fn)(ctx, option, Action::HorizontalSplit); |
| 1152 |
} |
| 1153 |
return close_fn(self); |
| 1154 |
} |
| 1155 |
ctrl!('v') => { |
| 1156 |
if let Some(option) = self.selection() { |
| 1157 |
(self.callback_fn)(ctx, option, Action::VerticalSplit); |
| 1158 |
} |
| 1159 |
return close_fn(self); |
| 1160 |
} |
| 1161 |
ctrl!('t') => { |
| 1162 |
self.toggle_preview(); |
| 1163 |
} |
| 1164 |
_ => { |
| 1165 |
self.prompt_handle_event(event, ctx); |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
EventResult::Consumed(None) |
| 1170 |
} |
| 1171 |
|
| 1172 |
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { |
| 1173 |
let block = Block::bordered(); |
| 1174 |
|
| 1175 |
let inner = block.inner(area); |
| 1176 |
|
| 1177 |
|
| 1178 |
let render_preview = |
| 1179 |
self.show_preview && self.file_fn.is_some() && area.width > MIN_AREA_WIDTH_FOR_PREVIEW; |
| 1180 |
|
| 1181 |
let picker_width = if render_preview { |
| 1182 |
area.width / 2 |
| 1183 |
} else { |
| 1184 |
area.width |
| 1185 |
}; |
| 1186 |
let area = inner.clip_left(1).with_height(1).with_width(picker_width); |
| 1187 |
|
| 1188 |
self.prompt.cursor(area, editor) |
| 1189 |
} |
| 1190 |
|
| 1191 |
fn required_size(&mut self, (width, height): (u16, u16)) -> Option<(u16, u16)> { |
| 1192 |
self.completion_height = height.saturating_sub(4 + self.header_height()); |
| 1193 |
Some((width, height)) |
| 1194 |
} |
| 1195 |
|
| 1196 |
fn id(&self) -> Option<&'static str> { |
| 1197 |
Some(ID) |
| 1198 |
} |
| 1199 |
} |
| 1200 |
impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> { |
| 1201 |
fn drop(&mut self) { |
| 1202 |
|
| 1203 |
self.version.fetch_add(1, atomic::Ordering::Relaxed); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>; |