| 1 |
use crate::compositor::{Component, Compositor, Context, Event, EventResult}; |
| 2 |
use crate::{alt, ctrl, key, shift, ui}; |
| 3 |
use arc_swap::ArcSwap; |
| 4 |
use helix_core::syntax; |
| 5 |
use helix_view::document::Mode; |
| 6 |
use helix_view::input::KeyEvent; |
| 7 |
use helix_view::keyboard::KeyCode; |
| 8 |
use std::sync::Arc; |
| 9 |
use std::{borrow::Cow, ops::RangeFrom}; |
| 10 |
use tui::buffer::Buffer as Surface; |
| 11 |
use tui::text::Span; |
| 12 |
use tui::widgets::{Block, Widget}; |
| 13 |
|
| 14 |
use helix_core::{ |
| 15 |
unicode::segmentation::{GraphemeCursor, UnicodeSegmentation}, |
| 16 |
unicode::width::UnicodeWidthStr, |
| 17 |
Position, |
| 18 |
}; |
| 19 |
use helix_view::{ |
| 20 |
graphics::{CursorKind, Margin, Rect}, |
| 21 |
Editor, |
| 22 |
}; |
| 23 |
|
| 24 |
type PromptCharHandler = Box<dyn Fn(&mut Prompt, char, &Context)>; |
| 25 |
|
| 26 |
pub type Completion = (RangeFrom<usize>, Span<'static>); |
| 27 |
type CompletionFn = Box<dyn FnMut(&Editor, &str) -> Vec<Completion>>; |
| 28 |
type CallbackFn = Box<dyn FnMut(&mut Context, &str, PromptEvent)>; |
| 29 |
pub type DocFn = Box<dyn Fn(&str) -> Option<Cow<str>>>; |
| 30 |
|
| 31 |
pub struct Prompt { |
| 32 |
prompt: Cow<'static, str>, |
| 33 |
line: String, |
| 34 |
cursor: usize, |
| 35 |
|
| 36 |
line_area: Rect, |
| 37 |
anchor: usize, |
| 38 |
truncate_start: bool, |
| 39 |
truncate_end: bool, |
| 40 |
|
| 41 |
completion: Vec<Completion>, |
| 42 |
selection: Option<usize>, |
| 43 |
history_register: Option<char>, |
| 44 |
history_pos: Option<usize>, |
| 45 |
completion_fn: CompletionFn, |
| 46 |
callback_fn: CallbackFn, |
| 47 |
pub doc_fn: DocFn, |
| 48 |
next_char_handler: Option<PromptCharHandler>, |
| 49 |
language: Option<(&'static str, Arc<ArcSwap<syntax::Loader>>)>, |
| 50 |
} |
| 51 |
|
| 52 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 53 |
pub enum PromptEvent { |
| 54 |
|
| 55 |
Update, |
| 56 |
|
| 57 |
Validate, |
| 58 |
|
| 59 |
Abort, |
| 60 |
} |
| 61 |
|
| 62 |
pub enum CompletionDirection { |
| 63 |
Forward, |
| 64 |
Backward, |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Debug, Clone, Copy)] |
| 68 |
pub enum Movement { |
| 69 |
BackwardChar(usize), |
| 70 |
BackwardWord(usize), |
| 71 |
ForwardChar(usize), |
| 72 |
ForwardWord(usize), |
| 73 |
StartOfLine, |
| 74 |
EndOfLine, |
| 75 |
None, |
| 76 |
} |
| 77 |
|
| 78 |
fn is_word_sep(c: char) -> bool { |
| 79 |
c == std::path::MAIN_SEPARATOR || c.is_whitespace() |
| 80 |
} |
| 81 |
|
| 82 |
impl Prompt { |
| 83 |
pub fn new( |
| 84 |
prompt: Cow<'static, str>, |
| 85 |
history_register: Option<char>, |
| 86 |
completion_fn: impl FnMut(&Editor, &str) -> Vec<Completion> + 'static, |
| 87 |
callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static, |
| 88 |
) -> Self { |
| 89 |
Self { |
| 90 |
prompt, |
| 91 |
line: String::new(), |
| 92 |
cursor: 0, |
| 93 |
line_area: Rect::default(), |
| 94 |
anchor: 0, |
| 95 |
truncate_start: false, |
| 96 |
truncate_end: false, |
| 97 |
completion: Vec::new(), |
| 98 |
selection: None, |
| 99 |
history_register, |
| 100 |
history_pos: None, |
| 101 |
completion_fn: Box::new(completion_fn), |
| 102 |
callback_fn: Box::new(callback_fn), |
| 103 |
doc_fn: Box::new(|_| None), |
| 104 |
next_char_handler: None, |
| 105 |
language: None, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
#[inline] |
| 111 |
pub(crate) fn position(&self) -> usize { |
| 112 |
self.cursor |
| 113 |
} |
| 114 |
|
| 115 |
pub fn with_line(mut self, line: String, editor: &Editor) -> Self { |
| 116 |
self.set_line(line, editor); |
| 117 |
self |
| 118 |
} |
| 119 |
|
| 120 |
pub fn set_line(&mut self, line: String, editor: &Editor) { |
| 121 |
let cursor = line.len(); |
| 122 |
self.line = line; |
| 123 |
self.cursor = cursor; |
| 124 |
self.recalculate_completion(editor); |
| 125 |
} |
| 126 |
|
| 127 |
pub fn with_language( |
| 128 |
mut self, |
| 129 |
language: &'static str, |
| 130 |
loader: Arc<ArcSwap<syntax::Loader>>, |
| 131 |
) -> Self { |
| 132 |
self.language = Some((language, loader)); |
| 133 |
self |
| 134 |
} |
| 135 |
|
| 136 |
pub fn line(&self) -> &String { |
| 137 |
&self.line |
| 138 |
} |
| 139 |
|
| 140 |
pub fn with_history_register(&mut self, history_register: Option<char>) -> &mut Self { |
| 141 |
self.history_register = history_register; |
| 142 |
self |
| 143 |
} |
| 144 |
|
| 145 |
pub(crate) fn history_register(&self) -> Option<char> { |
| 146 |
self.history_register |
| 147 |
} |
| 148 |
|
| 149 |
pub(crate) fn first_history_completion<'a>( |
| 150 |
&'a self, |
| 151 |
editor: &'a Editor, |
| 152 |
) -> Option<Cow<'a, str>> { |
| 153 |
self.history_register |
| 154 |
.and_then(|reg| editor.registers.first(reg, editor)) |
| 155 |
} |
| 156 |
|
| 157 |
pub fn recalculate_completion(&mut self, editor: &Editor) { |
| 158 |
self.exit_selection(); |
| 159 |
self.completion = (self.completion_fn)(editor, &self.line); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
fn eval_movement(&self, movement: Movement) -> usize { |
| 165 |
match movement { |
| 166 |
Movement::BackwardChar(rep) => { |
| 167 |
let mut position = self.cursor; |
| 168 |
for _ in 0..rep { |
| 169 |
let mut cursor = GraphemeCursor::new(position, self.line.len(), false); |
| 170 |
if let Ok(Some(pos)) = cursor.prev_boundary(&self.line, 0) { |
| 171 |
position = pos; |
| 172 |
} else { |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
position |
| 177 |
} |
| 178 |
Movement::BackwardWord(rep) => { |
| 179 |
let char_indices: Vec<(usize, char)> = self.line.char_indices().collect(); |
| 180 |
if char_indices.is_empty() { |
| 181 |
return self.cursor; |
| 182 |
} |
| 183 |
let mut char_position = char_indices |
| 184 |
.iter() |
| 185 |
.position(|(idx, _)| *idx == self.cursor) |
| 186 |
.unwrap_or(char_indices.len() - 1); |
| 187 |
|
| 188 |
for _ in 0..rep { |
| 189 |
if char_position == 0 { |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
let mut found = None; |
| 194 |
for prev in (0..char_position - 1).rev() { |
| 195 |
if is_word_sep(char_indices[prev].1) { |
| 196 |
found = Some(prev + 1); |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
char_position = found.unwrap_or(0); |
| 202 |
} |
| 203 |
char_indices[char_position].0 |
| 204 |
} |
| 205 |
Movement::ForwardWord(rep) => { |
| 206 |
let char_indices: Vec<(usize, char)> = self.line.char_indices().collect(); |
| 207 |
if char_indices.is_empty() { |
| 208 |
return self.cursor; |
| 209 |
} |
| 210 |
let mut char_position = char_indices |
| 211 |
.iter() |
| 212 |
.position(|(idx, _)| *idx == self.cursor) |
| 213 |
.unwrap_or(char_indices.len()); |
| 214 |
|
| 215 |
for _ in 0..rep { |
| 216 |
|
| 217 |
while char_position < char_indices.len() |
| 218 |
&& !is_word_sep(char_indices[char_position].1) |
| 219 |
{ |
| 220 |
char_position += 1; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
while char_position < char_indices.len() |
| 225 |
&& is_word_sep(char_indices[char_position].1) |
| 226 |
{ |
| 227 |
char_position += 1; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
} |
| 232 |
char_indices |
| 233 |
.get(char_position) |
| 234 |
.map(|(i, _)| *i) |
| 235 |
.unwrap_or_else(|| self.line.len()) |
| 236 |
} |
| 237 |
Movement::ForwardChar(rep) => { |
| 238 |
let mut position = self.cursor; |
| 239 |
for _ in 0..rep { |
| 240 |
let mut cursor = GraphemeCursor::new(position, self.line.len(), false); |
| 241 |
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) { |
| 242 |
position = pos; |
| 243 |
} else { |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
position |
| 248 |
} |
| 249 |
Movement::StartOfLine => 0, |
| 250 |
Movement::EndOfLine => self.line.len(), |
| 251 |
Movement::None => self.cursor, |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
pub fn insert_char(&mut self, c: char, cx: &Context) { |
| 256 |
if let Some(handler) = &self.next_char_handler.take() { |
| 257 |
handler(self, c, cx); |
| 258 |
|
| 259 |
self.next_char_handler = None; |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
self.line.insert(self.cursor, c); |
| 264 |
let mut cursor = GraphemeCursor::new(self.cursor, self.line.len(), false); |
| 265 |
if let Ok(Some(pos)) = cursor.next_boundary(&self.line, 0) { |
| 266 |
self.cursor = pos; |
| 267 |
} |
| 268 |
self.recalculate_completion(cx.editor); |
| 269 |
} |
| 270 |
|
| 271 |
pub fn insert_str(&mut self, s: &str, editor: &Editor) { |
| 272 |
self.line.insert_str(self.cursor, s); |
| 273 |
self.cursor += s.len(); |
| 274 |
self.recalculate_completion(editor); |
| 275 |
} |
| 276 |
|
| 277 |
pub fn move_cursor(&mut self, movement: Movement) { |
| 278 |
let pos = self.eval_movement(movement); |
| 279 |
self.cursor = pos |
| 280 |
} |
| 281 |
|
| 282 |
pub fn move_start(&mut self) { |
| 283 |
self.cursor = 0; |
| 284 |
} |
| 285 |
|
| 286 |
pub fn move_end(&mut self) { |
| 287 |
self.cursor = self.line.len(); |
| 288 |
} |
| 289 |
|
| 290 |
pub fn delete_char_backwards(&mut self, editor: &Editor) { |
| 291 |
let pos = self.eval_movement(Movement::BackwardChar(1)); |
| 292 |
self.line.replace_range(pos..self.cursor, ""); |
| 293 |
self.cursor = pos; |
| 294 |
|
| 295 |
self.recalculate_completion(editor); |
| 296 |
} |
| 297 |
|
| 298 |
pub fn delete_char_forwards(&mut self, editor: &Editor) { |
| 299 |
let pos = self.eval_movement(Movement::ForwardChar(1)); |
| 300 |
self.line.replace_range(self.cursor..pos, ""); |
| 301 |
|
| 302 |
self.recalculate_completion(editor); |
| 303 |
} |
| 304 |
|
| 305 |
pub fn delete_word_backwards(&mut self, editor: &Editor) { |
| 306 |
let pos = self.eval_movement(Movement::BackwardWord(1)); |
| 307 |
self.line.replace_range(pos..self.cursor, ""); |
| 308 |
self.cursor = pos; |
| 309 |
|
| 310 |
self.recalculate_completion(editor); |
| 311 |
} |
| 312 |
|
| 313 |
pub fn delete_word_forwards(&mut self, editor: &Editor) { |
| 314 |
let pos = self.eval_movement(Movement::ForwardWord(1)); |
| 315 |
self.line.replace_range(self.cursor..pos, ""); |
| 316 |
|
| 317 |
self.recalculate_completion(editor); |
| 318 |
} |
| 319 |
|
| 320 |
pub fn kill_to_start_of_line(&mut self, editor: &Editor) { |
| 321 |
let pos = self.eval_movement(Movement::StartOfLine); |
| 322 |
self.line.replace_range(pos..self.cursor, ""); |
| 323 |
self.cursor = pos; |
| 324 |
|
| 325 |
self.recalculate_completion(editor); |
| 326 |
} |
| 327 |
|
| 328 |
pub fn kill_to_end_of_line(&mut self, editor: &Editor) { |
| 329 |
let pos = self.eval_movement(Movement::EndOfLine); |
| 330 |
self.line.replace_range(self.cursor..pos, ""); |
| 331 |
|
| 332 |
self.recalculate_completion(editor); |
| 333 |
} |
| 334 |
|
| 335 |
pub fn clear(&mut self, editor: &Editor) { |
| 336 |
self.line.clear(); |
| 337 |
self.cursor = 0; |
| 338 |
self.recalculate_completion(editor); |
| 339 |
} |
| 340 |
|
| 341 |
pub fn change_history( |
| 342 |
&mut self, |
| 343 |
cx: &mut Context, |
| 344 |
register: char, |
| 345 |
direction: CompletionDirection, |
| 346 |
) { |
| 347 |
(self.callback_fn)(cx, &self.line, PromptEvent::Abort); |
| 348 |
let mut values = match cx.editor.registers.read(register, cx.editor) { |
| 349 |
Some(values) if values.len() > 0 => values.rev(), |
| 350 |
_ => return, |
| 351 |
}; |
| 352 |
|
| 353 |
let end = values.len().saturating_sub(1); |
| 354 |
|
| 355 |
let index = match direction { |
| 356 |
CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1), |
| 357 |
CompletionDirection::Backward => self |
| 358 |
.history_pos |
| 359 |
.unwrap_or_else(|| values.len()) |
| 360 |
.saturating_sub(1), |
| 361 |
} |
| 362 |
.min(end); |
| 363 |
|
| 364 |
self.line = values.nth(index).unwrap().to_string(); |
| 365 |
|
| 366 |
drop(values); |
| 367 |
|
| 368 |
self.history_pos = Some(index); |
| 369 |
|
| 370 |
self.move_end(); |
| 371 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 372 |
self.recalculate_completion(cx.editor); |
| 373 |
} |
| 374 |
|
| 375 |
pub fn change_completion_selection(&mut self, direction: CompletionDirection) { |
| 376 |
if self.completion.is_empty() { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
let index = match direction { |
| 381 |
CompletionDirection::Forward => self.selection.map_or(0, |i| i + 1), |
| 382 |
CompletionDirection::Backward => { |
| 383 |
self.selection.unwrap_or(0) + self.completion.len() - 1 |
| 384 |
} |
| 385 |
} % self.completion.len(); |
| 386 |
|
| 387 |
self.selection = Some(index); |
| 388 |
|
| 389 |
let (range, item) = &self.completion[index]; |
| 390 |
|
| 391 |
self.line.replace_range(range.clone(), &item.content); |
| 392 |
|
| 393 |
self.move_end(); |
| 394 |
} |
| 395 |
|
| 396 |
pub fn exit_selection(&mut self) { |
| 397 |
self.selection = None; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
const BASE_WIDTH: u16 = 30; |
| 402 |
|
| 403 |
impl Prompt { |
| 404 |
pub fn render_prompt(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 405 |
let theme = &cx.editor.theme; |
| 406 |
let prompt_color = theme.get("ui.text"); |
| 407 |
let completion_color = theme.get("ui.menu"); |
| 408 |
let selected_color = theme.get("ui.menu.selected"); |
| 409 |
let suggestion_color = theme.get("ui.text.inactive"); |
| 410 |
let background = theme.get("ui.background"); |
| 411 |
|
| 412 |
|
| 413 |
let max_len = self |
| 414 |
.completion |
| 415 |
.iter() |
| 416 |
.map(|(_, completion)| completion.content.len() as u16) |
| 417 |
.max() |
| 418 |
.unwrap_or(BASE_WIDTH) |
| 419 |
.max(BASE_WIDTH); |
| 420 |
|
| 421 |
let cols = std::cmp::max(1, area.width / max_len); |
| 422 |
let col_width = (area.width.saturating_sub(cols)) / cols; |
| 423 |
|
| 424 |
let height = (self.completion.len() as u16) |
| 425 |
.div_ceil(cols) |
| 426 |
.min(10) |
| 427 |
.min(area.height.saturating_sub(1)); |
| 428 |
|
| 429 |
let completion_area = Rect::new( |
| 430 |
area.x, |
| 431 |
(area.height - height).saturating_sub(1), |
| 432 |
area.width, |
| 433 |
height, |
| 434 |
); |
| 435 |
|
| 436 |
if completion_area.height > 0 && !self.completion.is_empty() { |
| 437 |
let area = completion_area; |
| 438 |
let background = theme.get("ui.menu"); |
| 439 |
|
| 440 |
let items = height as usize * cols as usize; |
| 441 |
|
| 442 |
let offset = self |
| 443 |
.selection |
| 444 |
.map(|selection| selection / items * items) |
| 445 |
.unwrap_or_default(); |
| 446 |
|
| 447 |
surface.clear_with(area, background); |
| 448 |
|
| 449 |
let mut row = 0; |
| 450 |
let mut col = 0; |
| 451 |
|
| 452 |
for (i, (_range, completion)) in |
| 453 |
self.completion.iter().enumerate().skip(offset).take(items) |
| 454 |
{ |
| 455 |
let is_selected = Some(i) == self.selection; |
| 456 |
|
| 457 |
let completion_item_style = if is_selected { |
| 458 |
selected_color |
| 459 |
} else { |
| 460 |
completion_color.patch(completion.style) |
| 461 |
}; |
| 462 |
|
| 463 |
surface.set_stringn( |
| 464 |
area.x + col * (1 + col_width), |
| 465 |
area.y + row, |
| 466 |
&completion.content, |
| 467 |
col_width.saturating_sub(1) as usize, |
| 468 |
completion_item_style, |
| 469 |
); |
| 470 |
|
| 471 |
row += 1; |
| 472 |
if row > area.height - 1 { |
| 473 |
row = 0; |
| 474 |
col += 1; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if let Some(doc) = (self.doc_fn)(&self.line) { |
| 480 |
let mut text = ui::Text::new(doc.to_string()); |
| 481 |
|
| 482 |
let max_width = BASE_WIDTH * 3; |
| 483 |
let horizontal_padding = 2; |
| 484 |
let vertical_padding = 1; |
| 485 |
let text_width = max_width - horizontal_padding * 2; |
| 486 |
|
| 487 |
let viewport = area; |
| 488 |
|
| 489 |
let (_width, height) = ui::text::required_size(&text.contents, text_width); |
| 490 |
|
| 491 |
let area = viewport.intersection(Rect::new( |
| 492 |
completion_area.x, |
| 493 |
completion_area |
| 494 |
.y |
| 495 |
.saturating_sub(height + vertical_padding * 2), |
| 496 |
max_width, |
| 497 |
height + vertical_padding * 2, |
| 498 |
)); |
| 499 |
|
| 500 |
let background = theme.get("ui.help"); |
| 501 |
surface.clear_with(area, background); |
| 502 |
|
| 503 |
let block = Block::bordered() |
| 504 |
|
| 505 |
.border_style(background); |
| 506 |
|
| 507 |
let inner = block.inner(area).inner(Margin::horizontal(1)); |
| 508 |
|
| 509 |
block.render(area, surface); |
| 510 |
text.render(inner, surface, cx); |
| 511 |
} |
| 512 |
|
| 513 |
let line = area.height - 1; |
| 514 |
surface.clear_with(area.clip_top(line), background); |
| 515 |
|
| 516 |
surface.set_string(area.x, area.y + line, &self.prompt, prompt_color); |
| 517 |
|
| 518 |
self.line_area = area |
| 519 |
.clip_left(self.prompt.len() as u16) |
| 520 |
.clip_top(line) |
| 521 |
.clip_right(2); |
| 522 |
|
| 523 |
if self.line.is_empty() { |
| 524 |
self.anchor = 0; |
| 525 |
|
| 526 |
if let Some(suggestion) = self.first_history_completion(cx.editor) { |
| 527 |
surface.set_string( |
| 528 |
self.line_area.x, |
| 529 |
self.line_area.y, |
| 530 |
&suggestion, |
| 531 |
suggestion_color, |
| 532 |
); |
| 533 |
} |
| 534 |
} else if let Some((language, loader)) = self.language.as_ref() { |
| 535 |
let mut text: ui::text::Text = crate::ui::markdown::highlighted_code_block( |
| 536 |
&self.line, |
| 537 |
language, |
| 538 |
Some(&cx.editor.theme), |
| 539 |
&loader.load(), |
| 540 |
None, |
| 541 |
) |
| 542 |
.into(); |
| 543 |
text.render(self.line_area, surface, cx); |
| 544 |
} else { |
| 545 |
let line_width = self.line_area.width as usize; |
| 546 |
|
| 547 |
if self.line.width() < line_width { |
| 548 |
self.anchor = 0; |
| 549 |
} else if self.cursor <= self.anchor { |
| 550 |
|
| 551 |
self.anchor = self.line[..self.cursor] |
| 552 |
.grapheme_indices(true) |
| 553 |
.next_back() |
| 554 |
.map(|(i, _)| i) |
| 555 |
.unwrap_or_default(); |
| 556 |
} else if self.line[self.anchor..self.cursor].width() > line_width { |
| 557 |
|
| 558 |
let mut width = 0; |
| 559 |
self.anchor = self.line[..self.cursor] |
| 560 |
.grapheme_indices(true) |
| 561 |
.rev() |
| 562 |
.find_map(|(idx, g)| { |
| 563 |
width += g.width(); |
| 564 |
if width > line_width { |
| 565 |
Some(idx + g.len()) |
| 566 |
} else { |
| 567 |
None |
| 568 |
} |
| 569 |
}) |
| 570 |
.unwrap(); |
| 571 |
} |
| 572 |
|
| 573 |
self.truncate_start = self.anchor > 0; |
| 574 |
self.truncate_end = self.line[self.anchor..].width() > line_width; |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
if self.truncate_end && self.line[self.anchor..self.cursor].width() >= line_width { |
| 579 |
|
| 580 |
self.anchor += self.line[self.anchor..] |
| 581 |
.grapheme_indices(true) |
| 582 |
.find_map(|(idx, g)| { |
| 583 |
if g.width() > 0 { |
| 584 |
Some(idx + g.len()) |
| 585 |
} else { |
| 586 |
None |
| 587 |
} |
| 588 |
}) |
| 589 |
.unwrap(); |
| 590 |
} |
| 591 |
|
| 592 |
surface.set_string_anchored( |
| 593 |
self.line_area.x, |
| 594 |
self.line_area.y, |
| 595 |
self.truncate_start, |
| 596 |
self.truncate_end, |
| 597 |
&self.line.as_str()[self.anchor..], |
| 598 |
line_width, |
| 599 |
|_| prompt_color, |
| 600 |
); |
| 601 |
} |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
impl Component for Prompt { |
| 606 |
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { |
| 607 |
let event = match event { |
| 608 |
Event::Paste(data) => { |
| 609 |
self.insert_str(data, cx.editor); |
| 610 |
self.recalculate_completion(cx.editor); |
| 611 |
return EventResult::Consumed(None); |
| 612 |
} |
| 613 |
Event::Key(event) => *event, |
| 614 |
Event::Resize(..) => return EventResult::Consumed(None), |
| 615 |
|
| 616 |
|
| 617 |
Event::Mouse(_) => return EventResult::Consumed(None), |
| 618 |
_ => return EventResult::Ignored(None), |
| 619 |
}; |
| 620 |
|
| 621 |
let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| { |
| 622 |
|
| 623 |
compositor.pop(); |
| 624 |
}))); |
| 625 |
|
| 626 |
match event { |
| 627 |
ctrl!('c') | key!(Esc) => { |
| 628 |
(self.callback_fn)(cx, &self.line, PromptEvent::Abort); |
| 629 |
return close_fn; |
| 630 |
} |
| 631 |
alt!('b') | ctrl!(Left) => self.move_cursor(Movement::BackwardWord(1)), |
| 632 |
alt!('f') | ctrl!(Right) => self.move_cursor(Movement::ForwardWord(1)), |
| 633 |
ctrl!('b') | key!(Left) => self.move_cursor(Movement::BackwardChar(1)), |
| 634 |
ctrl!('f') | key!(Right) => self.move_cursor(Movement::ForwardChar(1)), |
| 635 |
ctrl!('e') | key!(End) => self.move_end(), |
| 636 |
ctrl!('a') | key!(Home) => self.move_start(), |
| 637 |
ctrl!('w') | alt!(Backspace) | ctrl!(Backspace) => { |
| 638 |
self.delete_word_backwards(cx.editor); |
| 639 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 640 |
} |
| 641 |
alt!('d') | alt!(Delete) | ctrl!(Delete) => { |
| 642 |
self.delete_word_forwards(cx.editor); |
| 643 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 644 |
} |
| 645 |
ctrl!('k') => { |
| 646 |
self.kill_to_end_of_line(cx.editor); |
| 647 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 648 |
} |
| 649 |
ctrl!('u') => { |
| 650 |
self.kill_to_start_of_line(cx.editor); |
| 651 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 652 |
} |
| 653 |
ctrl!('h') | key!(Backspace) | shift!(Backspace) => { |
| 654 |
self.delete_char_backwards(cx.editor); |
| 655 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 656 |
} |
| 657 |
ctrl!('d') | key!(Delete) => { |
| 658 |
self.delete_char_forwards(cx.editor); |
| 659 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 660 |
} |
| 661 |
ctrl!('s') => { |
| 662 |
let (view, doc) = current!(cx.editor); |
| 663 |
let text = doc.text().slice(..); |
| 664 |
|
| 665 |
use helix_core::textobject; |
| 666 |
let range = textobject::textobject_word( |
| 667 |
text, |
| 668 |
doc.selection(view.id).primary(), |
| 669 |
textobject::TextObject::Inside, |
| 670 |
1, |
| 671 |
false, |
| 672 |
); |
| 673 |
let line = text.slice(range.from()..range.to()).to_string(); |
| 674 |
if !line.is_empty() { |
| 675 |
self.insert_str(line.as_str(), cx.editor); |
| 676 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 677 |
} |
| 678 |
} |
| 679 |
key!(Enter) => { |
| 680 |
if self.selection.is_some() && self.line.ends_with(std::path::MAIN_SEPARATOR) { |
| 681 |
self.recalculate_completion(cx.editor); |
| 682 |
} else { |
| 683 |
let last_item = self |
| 684 |
.first_history_completion(cx.editor) |
| 685 |
.map(|entry| entry.to_string()) |
| 686 |
.unwrap_or_else(|| String::from("")); |
| 687 |
|
| 688 |
|
| 689 |
let input = if self.line.is_empty() { |
| 690 |
&last_item |
| 691 |
} else { |
| 692 |
if last_item != self.line { |
| 693 |
|
| 694 |
if let Some(register) = self.history_register { |
| 695 |
if let Err(err) = |
| 696 |
cx.editor.registers.push(register, self.line.clone()) |
| 697 |
{ |
| 698 |
cx.editor.set_error(err.to_string()); |
| 699 |
} |
| 700 |
}; |
| 701 |
} |
| 702 |
|
| 703 |
&self.line |
| 704 |
}; |
| 705 |
|
| 706 |
(self.callback_fn)(cx, input, PromptEvent::Validate); |
| 707 |
|
| 708 |
return close_fn; |
| 709 |
} |
| 710 |
} |
| 711 |
ctrl!('p') | key!(Up) => { |
| 712 |
if let Some(register) = self.history_register { |
| 713 |
self.change_history(cx, register, CompletionDirection::Backward); |
| 714 |
} |
| 715 |
} |
| 716 |
ctrl!('n') | key!(Down) => { |
| 717 |
if let Some(register) = self.history_register { |
| 718 |
self.change_history(cx, register, CompletionDirection::Forward); |
| 719 |
} |
| 720 |
} |
| 721 |
key!(Tab) => { |
| 722 |
self.change_completion_selection(CompletionDirection::Forward); |
| 723 |
|
| 724 |
if self.completion.len() == 1 && self.line.ends_with(std::path::MAIN_SEPARATOR) { |
| 725 |
self.recalculate_completion(cx.editor); |
| 726 |
} |
| 727 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update) |
| 728 |
} |
| 729 |
shift!(Tab) => { |
| 730 |
self.change_completion_selection(CompletionDirection::Backward); |
| 731 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update) |
| 732 |
} |
| 733 |
ctrl!('q') => self.exit_selection(), |
| 734 |
ctrl!('r') => { |
| 735 |
self.completion = cx |
| 736 |
.editor |
| 737 |
.registers |
| 738 |
.iter_preview() |
| 739 |
.map(|(ch, preview)| (0.., format!("{} {}", ch, &preview).into())) |
| 740 |
.collect(); |
| 741 |
self.next_char_handler = Some(Box::new(|prompt, c, context| { |
| 742 |
prompt.insert_str( |
| 743 |
&context |
| 744 |
.editor |
| 745 |
.registers |
| 746 |
.first(c, context.editor) |
| 747 |
.unwrap_or_default(), |
| 748 |
context.editor, |
| 749 |
); |
| 750 |
})); |
| 751 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 752 |
return EventResult::Consumed(None); |
| 753 |
} |
| 754 |
|
| 755 |
KeyEvent { |
| 756 |
code: KeyCode::Char(c), |
| 757 |
modifiers: _, |
| 758 |
} => { |
| 759 |
self.insert_char(c, cx); |
| 760 |
(self.callback_fn)(cx, &self.line, PromptEvent::Update); |
| 761 |
} |
| 762 |
_ => (), |
| 763 |
}; |
| 764 |
|
| 765 |
EventResult::Consumed(None) |
| 766 |
} |
| 767 |
|
| 768 |
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 769 |
self.render_prompt(area, surface, cx) |
| 770 |
} |
| 771 |
|
| 772 |
fn cursor(&self, area: Rect, editor: &Editor) -> (Option<Position>, CursorKind) { |
| 773 |
let area = area |
| 774 |
.clip_left(self.prompt.len() as u16) |
| 775 |
.clip_right(if self.prompt.is_empty() { 2 } else { 0 }); |
| 776 |
|
| 777 |
let mut col = area.left() as usize + self.line[self.anchor..self.cursor].width(); |
| 778 |
|
| 779 |
|
| 780 |
if self.truncate_end |
| 781 |
&& self.line[self.anchor..self.cursor].width() >= self.line_area.width as usize |
| 782 |
{ |
| 783 |
col -= 1; |
| 784 |
} |
| 785 |
|
| 786 |
if self.truncate_start && self.cursor == self.anchor { |
| 787 |
col += self.line[self.cursor..] |
| 788 |
.graphemes(true) |
| 789 |
.next() |
| 790 |
.map_or(0, |g| g.width()); |
| 791 |
} |
| 792 |
|
| 793 |
let line = area.height as usize - 1; |
| 794 |
|
| 795 |
( |
| 796 |
Some(Position::new(area.y as usize + line, col)), |
| 797 |
editor.config().cursor_shape.from_mode(Mode::Insert), |
| 798 |
) |
| 799 |
} |
| 800 |
} |