| 1 |
use std::fmt::Write; |
| 2 |
use std::io::BufReader; |
| 3 |
use std::ops::{self, Deref}; |
| 4 |
|
| 5 |
use crate::job::Job; |
| 6 |
|
| 7 |
use super::*; |
| 8 |
|
| 9 |
use helix_core::command_line::{Args, Flag, Signature, Token, TokenKind}; |
| 10 |
use helix_core::fuzzy::fuzzy_match; |
| 11 |
use helix_core::indent::MAX_INDENT; |
| 12 |
use helix_core::line_ending; |
| 13 |
use helix_stdx::path::home_dir; |
| 14 |
use helix_view::document::{read_to_string, DEFAULT_LANGUAGE_NAME}; |
| 15 |
use helix_view::editor::{CloseError, ConfigEvent}; |
| 16 |
use helix_view::expansion; |
| 17 |
use serde_json::Value; |
| 18 |
use ui::completers::{self, Completer}; |
| 19 |
|
| 20 |
#[derive(Clone)] |
| 21 |
pub struct TypableCommand { |
| 22 |
pub name: &'static str, |
| 23 |
pub aliases: &'static [&'static str], |
| 24 |
pub doc: &'static str, |
| 25 |
|
| 26 |
pub fun: fn(&mut compositor::Context, Args, PromptEvent) -> anyhow::Result<()>, |
| 27 |
|
| 28 |
pub completer: CommandCompleter, |
| 29 |
pub signature: Signature, |
| 30 |
} |
| 31 |
|
| 32 |
#[derive(Clone)] |
| 33 |
pub struct CommandCompleter { |
| 34 |
|
| 35 |
positional_args: &'static [Completer], |
| 36 |
|
| 37 |
|
| 38 |
var_args: Completer, |
| 39 |
} |
| 40 |
|
| 41 |
impl CommandCompleter { |
| 42 |
const fn none() -> Self { |
| 43 |
Self { |
| 44 |
positional_args: &[], |
| 45 |
var_args: completers::none, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
const fn positional(completers: &'static [Completer]) -> Self { |
| 50 |
Self { |
| 51 |
positional_args: completers, |
| 52 |
var_args: completers::none, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
const fn all(completer: Completer) -> Self { |
| 57 |
Self { |
| 58 |
positional_args: &[], |
| 59 |
var_args: completer, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn for_argument_number(&self, n: usize) -> &Completer { |
| 64 |
match self.positional_args.get(n) { |
| 65 |
Some(completer) => completer, |
| 66 |
_ => &self.var_args, |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
fn exit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 72 |
if event != PromptEvent::Validate { |
| 73 |
return Ok(()); |
| 74 |
} |
| 75 |
|
| 76 |
if doc!(cx.editor).is_modified() { |
| 77 |
write_impl( |
| 78 |
cx, |
| 79 |
args.first(), |
| 80 |
WriteOptions { |
| 81 |
force: false, |
| 82 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 83 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 84 |
}, |
| 85 |
)?; |
| 86 |
} |
| 87 |
cx.block_try_flush_writes()?; |
| 88 |
quit(cx, Args::default(), event) |
| 89 |
} |
| 90 |
|
| 91 |
fn force_exit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 92 |
if event != PromptEvent::Validate { |
| 93 |
return Ok(()); |
| 94 |
} |
| 95 |
|
| 96 |
if doc!(cx.editor).is_modified() { |
| 97 |
write_impl( |
| 98 |
cx, |
| 99 |
args.first(), |
| 100 |
WriteOptions { |
| 101 |
force: true, |
| 102 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 103 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 104 |
}, |
| 105 |
)?; |
| 106 |
} |
| 107 |
cx.block_try_flush_writes()?; |
| 108 |
quit(cx, Args::default(), event) |
| 109 |
} |
| 110 |
|
| 111 |
fn quit(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 112 |
log::debug!("quitting..."); |
| 113 |
|
| 114 |
if event != PromptEvent::Validate { |
| 115 |
return Ok(()); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
if cx.editor.tree.views().count() == 1 { |
| 120 |
buffers_remaining_impl(cx.editor)? |
| 121 |
} |
| 122 |
|
| 123 |
cx.block_try_flush_writes()?; |
| 124 |
cx.editor.close(view!(cx.editor).id); |
| 125 |
|
| 126 |
Ok(()) |
| 127 |
} |
| 128 |
|
| 129 |
fn force_quit(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 130 |
if event != PromptEvent::Validate { |
| 131 |
return Ok(()); |
| 132 |
} |
| 133 |
|
| 134 |
cx.block_try_flush_writes()?; |
| 135 |
cx.editor.close(view!(cx.editor).id); |
| 136 |
|
| 137 |
Ok(()) |
| 138 |
} |
| 139 |
|
| 140 |
fn open(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 141 |
if event != PromptEvent::Validate { |
| 142 |
return Ok(()); |
| 143 |
} |
| 144 |
|
| 145 |
open_impl(cx, args, Action::Replace) |
| 146 |
} |
| 147 |
|
| 148 |
fn open_impl(cx: &mut compositor::Context, args: Args, action: Action) -> anyhow::Result<()> { |
| 149 |
for arg in args { |
| 150 |
let (path, pos) = crate::args::parse_file(&arg); |
| 151 |
let path = helix_stdx::path::expand_tilde(path); |
| 152 |
|
| 153 |
|
| 154 |
if let Ok(true) = std::fs::canonicalize(&path).map(|p| p.is_dir()) { |
| 155 |
let callback = async move { |
| 156 |
let call: job::Callback = job::Callback::EditorCompositor(Box::new( |
| 157 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 158 |
let picker = |
| 159 |
ui::file_picker(editor, path.into_owned()).with_default_action(action); |
| 160 |
compositor.push(Box::new(overlaid(picker))); |
| 161 |
}, |
| 162 |
)); |
| 163 |
Ok(call) |
| 164 |
}; |
| 165 |
cx.jobs.callback(callback); |
| 166 |
} else { |
| 167 |
|
| 168 |
let _ = cx.editor.open(&path, action)?; |
| 169 |
let (view, doc) = current!(cx.editor); |
| 170 |
let pos = Selection::point(pos_at_coords(doc.text().slice(..), pos, true)); |
| 171 |
doc.set_selection(view.id, pos); |
| 172 |
|
| 173 |
align_view(doc, view, Align::Center); |
| 174 |
} |
| 175 |
} |
| 176 |
Ok(()) |
| 177 |
} |
| 178 |
|
| 179 |
fn buffer_close_by_ids_impl( |
| 180 |
cx: &mut compositor::Context, |
| 181 |
doc_ids: &[DocumentId], |
| 182 |
force: bool, |
| 183 |
) -> anyhow::Result<()> { |
| 184 |
cx.block_try_flush_writes()?; |
| 185 |
|
| 186 |
let (modified_ids, modified_names): (Vec<_>, Vec<_>) = doc_ids |
| 187 |
.iter() |
| 188 |
.filter_map(|&doc_id| { |
| 189 |
if let Err(CloseError::BufferModified(name)) = cx.editor.close_document(doc_id, force) { |
| 190 |
Some((doc_id, name)) |
| 191 |
} else { |
| 192 |
None |
| 193 |
} |
| 194 |
}) |
| 195 |
.unzip(); |
| 196 |
|
| 197 |
if let Some(first) = modified_ids.first() { |
| 198 |
let current = doc!(cx.editor); |
| 199 |
|
| 200 |
|
| 201 |
if !modified_ids.contains(¤t.id()) { |
| 202 |
cx.editor.switch(*first, Action::Replace); |
| 203 |
} |
| 204 |
bail!( |
| 205 |
"{} unsaved buffer{} remaining: {:?}", |
| 206 |
modified_names.len(), |
| 207 |
if modified_names.len() == 1 { "" } else { "s" }, |
| 208 |
modified_names, |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
Ok(()) |
| 213 |
} |
| 214 |
|
| 215 |
fn buffer_gather_paths_impl(editor: &mut Editor, args: Args) -> Vec<DocumentId> { |
| 216 |
|
| 217 |
if args.is_empty() { |
| 218 |
let doc_id = view!(editor).doc; |
| 219 |
return vec![doc_id]; |
| 220 |
} |
| 221 |
|
| 222 |
let mut nonexistent_buffers = vec![]; |
| 223 |
let mut document_ids = vec![]; |
| 224 |
for arg in args { |
| 225 |
let doc_id = editor.documents().find_map(|doc| { |
| 226 |
let arg_path = Some(Path::new(arg.as_ref())); |
| 227 |
if doc.path() == arg_path || doc.relative_path() == arg_path { |
| 228 |
Some(doc.id()) |
| 229 |
} else { |
| 230 |
None |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
match doc_id { |
| 235 |
Some(doc_id) => document_ids.push(doc_id), |
| 236 |
None => nonexistent_buffers.push(format!("'{}'", arg)), |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if !nonexistent_buffers.is_empty() { |
| 241 |
editor.set_error(format!( |
| 242 |
"cannot close non-existent buffers: {}", |
| 243 |
nonexistent_buffers.join(", ") |
| 244 |
)); |
| 245 |
} |
| 246 |
|
| 247 |
document_ids |
| 248 |
} |
| 249 |
|
| 250 |
fn buffer_close( |
| 251 |
cx: &mut compositor::Context, |
| 252 |
args: Args, |
| 253 |
event: PromptEvent, |
| 254 |
) -> anyhow::Result<()> { |
| 255 |
if event != PromptEvent::Validate { |
| 256 |
return Ok(()); |
| 257 |
} |
| 258 |
|
| 259 |
let document_ids = buffer_gather_paths_impl(cx.editor, args); |
| 260 |
buffer_close_by_ids_impl(cx, &document_ids, false) |
| 261 |
} |
| 262 |
|
| 263 |
fn force_buffer_close( |
| 264 |
cx: &mut compositor::Context, |
| 265 |
args: Args, |
| 266 |
event: PromptEvent, |
| 267 |
) -> anyhow::Result<()> { |
| 268 |
if event != PromptEvent::Validate { |
| 269 |
return Ok(()); |
| 270 |
} |
| 271 |
|
| 272 |
let document_ids = buffer_gather_paths_impl(cx.editor, args); |
| 273 |
buffer_close_by_ids_impl(cx, &document_ids, true) |
| 274 |
} |
| 275 |
|
| 276 |
fn buffer_gather_others_impl(editor: &mut Editor, skip_visible: bool) -> Vec<DocumentId> { |
| 277 |
if skip_visible { |
| 278 |
let visible_document_ids = editor |
| 279 |
.tree |
| 280 |
.views() |
| 281 |
.map(|view| &view.0.doc) |
| 282 |
.collect::<HashSet<_>>(); |
| 283 |
editor |
| 284 |
.documents() |
| 285 |
.map(|doc| doc.id()) |
| 286 |
.filter(|doc_id| !visible_document_ids.contains(doc_id)) |
| 287 |
.collect() |
| 288 |
} else { |
| 289 |
let current_document = &doc!(editor).id(); |
| 290 |
editor |
| 291 |
.documents() |
| 292 |
.map(|doc| doc.id()) |
| 293 |
.filter(|doc_id| doc_id != current_document) |
| 294 |
.collect() |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
fn buffer_close_others( |
| 299 |
cx: &mut compositor::Context, |
| 300 |
args: Args, |
| 301 |
event: PromptEvent, |
| 302 |
) -> anyhow::Result<()> { |
| 303 |
if event != PromptEvent::Validate { |
| 304 |
return Ok(()); |
| 305 |
} |
| 306 |
|
| 307 |
let document_ids = buffer_gather_others_impl(cx.editor, args.has_flag("skip-visible")); |
| 308 |
buffer_close_by_ids_impl(cx, &document_ids, false) |
| 309 |
} |
| 310 |
|
| 311 |
fn force_buffer_close_others( |
| 312 |
cx: &mut compositor::Context, |
| 313 |
args: Args, |
| 314 |
event: PromptEvent, |
| 315 |
) -> anyhow::Result<()> { |
| 316 |
if event != PromptEvent::Validate { |
| 317 |
return Ok(()); |
| 318 |
} |
| 319 |
|
| 320 |
let document_ids = buffer_gather_others_impl(cx.editor, args.has_flag("skip-visible")); |
| 321 |
buffer_close_by_ids_impl(cx, &document_ids, true) |
| 322 |
} |
| 323 |
|
| 324 |
fn buffer_gather_all_impl(editor: &mut Editor) -> Vec<DocumentId> { |
| 325 |
editor.documents().map(|doc| doc.id()).collect() |
| 326 |
} |
| 327 |
|
| 328 |
fn buffer_close_all( |
| 329 |
cx: &mut compositor::Context, |
| 330 |
_args: Args, |
| 331 |
event: PromptEvent, |
| 332 |
) -> anyhow::Result<()> { |
| 333 |
if event != PromptEvent::Validate { |
| 334 |
return Ok(()); |
| 335 |
} |
| 336 |
|
| 337 |
let document_ids = buffer_gather_all_impl(cx.editor); |
| 338 |
buffer_close_by_ids_impl(cx, &document_ids, false) |
| 339 |
} |
| 340 |
|
| 341 |
fn force_buffer_close_all( |
| 342 |
cx: &mut compositor::Context, |
| 343 |
_args: Args, |
| 344 |
event: PromptEvent, |
| 345 |
) -> anyhow::Result<()> { |
| 346 |
if event != PromptEvent::Validate { |
| 347 |
return Ok(()); |
| 348 |
} |
| 349 |
|
| 350 |
let document_ids = buffer_gather_all_impl(cx.editor); |
| 351 |
buffer_close_by_ids_impl(cx, &document_ids, true) |
| 352 |
} |
| 353 |
|
| 354 |
fn buffer_next( |
| 355 |
cx: &mut compositor::Context, |
| 356 |
_args: Args, |
| 357 |
event: PromptEvent, |
| 358 |
) -> anyhow::Result<()> { |
| 359 |
if event != PromptEvent::Validate { |
| 360 |
return Ok(()); |
| 361 |
} |
| 362 |
|
| 363 |
goto_buffer(cx.editor, Direction::Forward, 1); |
| 364 |
Ok(()) |
| 365 |
} |
| 366 |
|
| 367 |
fn buffer_previous( |
| 368 |
cx: &mut compositor::Context, |
| 369 |
_args: Args, |
| 370 |
event: PromptEvent, |
| 371 |
) -> anyhow::Result<()> { |
| 372 |
if event != PromptEvent::Validate { |
| 373 |
return Ok(()); |
| 374 |
} |
| 375 |
|
| 376 |
goto_buffer(cx.editor, Direction::Backward, 1); |
| 377 |
Ok(()) |
| 378 |
} |
| 379 |
|
| 380 |
fn write_impl( |
| 381 |
cx: &mut compositor::Context, |
| 382 |
path: Option<&str>, |
| 383 |
options: WriteOptions, |
| 384 |
) -> anyhow::Result<()> { |
| 385 |
let config = cx.editor.config(); |
| 386 |
let (view, doc) = current!(cx.editor); |
| 387 |
let doc_id = doc.id(); |
| 388 |
let view_id = view.id; |
| 389 |
|
| 390 |
if doc.trim_trailing_whitespace() { |
| 391 |
trim_trailing_whitespace(doc, view_id); |
| 392 |
} |
| 393 |
if config.trim_final_newlines { |
| 394 |
trim_final_newlines(doc, view_id); |
| 395 |
} |
| 396 |
if doc.insert_final_newline() { |
| 397 |
insert_final_newline(doc, view_id); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
doc.append_changes_to_history(view); |
| 402 |
|
| 403 |
let auto_format = config.auto_format && options.auto_format; |
| 404 |
let force = options.force; |
| 405 |
let path: Option<PathBuf> = path.map(Into::into); |
| 406 |
|
| 407 |
|
| 408 |
let run_code_actions = options.code_actions |
| 409 |
&& doc!(cx.editor, &doc_id) |
| 410 |
.language_config() |
| 411 |
.and_then(|c| c.code_actions_on_save.as_deref()) |
| 412 |
.is_some_and(|kinds| !kinds.is_empty()); |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
let tail = (auto_format || run_code_actions).then(|| { |
| 420 |
let path = path.clone(); |
| 421 |
let callback = Callback::Followup(Box::new(move |editor| { |
| 422 |
|
| 423 |
if !editor.documents.contains_key(&doc_id) { |
| 424 |
return None; |
| 425 |
} |
| 426 |
let doc = doc!(editor, &doc_id); |
| 427 |
let fmt_job = auto_format |
| 428 |
.then(|| doc.auto_format(editor)) |
| 429 |
.flatten() |
| 430 |
.map(|fmt| { |
| 431 |
let call = make_format_callback( |
| 432 |
doc_id, |
| 433 |
doc.version(), |
| 434 |
view_id, |
| 435 |
fmt, |
| 436 |
Some((path.clone(), force)), |
| 437 |
); |
| 438 |
Job::with_callback(call).wait_before_exiting() |
| 439 |
}); |
| 440 |
if fmt_job.is_none() { |
| 441 |
if let Err(err) = editor.save(doc_id, path, force) { |
| 442 |
editor.set_error(format!("Error saving: {}", err)); |
| 443 |
} |
| 444 |
} |
| 445 |
fmt_job |
| 446 |
})); |
| 447 |
Job::with_callback(async { Ok(callback) }).wait_before_exiting() |
| 448 |
}); |
| 449 |
|
| 450 |
let job = if run_code_actions { |
| 451 |
code_actions_on_save(cx, doc_id, tail) |
| 452 |
} else { |
| 453 |
tail |
| 454 |
}; |
| 455 |
|
| 456 |
if let Some(job) = job { |
| 457 |
cx.jobs.add(job); |
| 458 |
} else { |
| 459 |
cx.editor.save(doc_id, path, force)?; |
| 460 |
} |
| 461 |
|
| 462 |
Ok(()) |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
fn trim_trailing_whitespace(doc: &mut Document, view_id: ViewId) { |
| 467 |
let text = doc.text(); |
| 468 |
let mut pos = 0; |
| 469 |
let transaction = Transaction::delete( |
| 470 |
text, |
| 471 |
text.lines().filter_map(|line| { |
| 472 |
let line_end_len_chars = line_ending::get_line_ending(&line) |
| 473 |
.map(|le| le.len_chars()) |
| 474 |
.unwrap_or_default(); |
| 475 |
|
| 476 |
|
| 477 |
let first_trailing_whitespace = |
| 478 |
pos + line.last_non_whitespace_char().map_or(0, |idx| idx + 1); |
| 479 |
pos += line.len_chars(); |
| 480 |
|
| 481 |
|
| 482 |
let line_end = pos - line_end_len_chars; |
| 483 |
if first_trailing_whitespace != line_end { |
| 484 |
Some((first_trailing_whitespace, line_end)) |
| 485 |
} else { |
| 486 |
None |
| 487 |
} |
| 488 |
}), |
| 489 |
); |
| 490 |
doc.apply(&transaction, view_id); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
fn trim_final_newlines(doc: &mut Document, view_id: ViewId) { |
| 495 |
let rope = doc.text(); |
| 496 |
let mut text = rope.slice(..); |
| 497 |
let mut total_char_len = 0; |
| 498 |
let mut final_char_len = 0; |
| 499 |
while let Some(line_ending) = line_ending::get_line_ending(&text) { |
| 500 |
total_char_len += line_ending.len_chars(); |
| 501 |
final_char_len = line_ending.len_chars(); |
| 502 |
text = text.slice(..text.len_chars() - line_ending.len_chars()); |
| 503 |
} |
| 504 |
let chars_to_delete = total_char_len - final_char_len; |
| 505 |
if chars_to_delete != 0 { |
| 506 |
let transaction = Transaction::delete( |
| 507 |
rope, |
| 508 |
[(rope.len_chars() - chars_to_delete, rope.len_chars())].into_iter(), |
| 509 |
); |
| 510 |
doc.apply(&transaction, view_id); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
fn insert_final_newline(doc: &mut Document, view_id: ViewId) { |
| 516 |
let text = doc.text(); |
| 517 |
if text.len_chars() > 0 && line_ending::get_line_ending(&text.slice(..)).is_none() { |
| 518 |
let eof = Selection::point(text.len_chars()); |
| 519 |
let insert = Transaction::insert(text, &eof, doc.line_ending.as_str().into()); |
| 520 |
doc.apply(&insert, view_id); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
#[derive(Debug, Clone, Copy)] |
| 525 |
pub struct WriteOptions { |
| 526 |
pub force: bool, |
| 527 |
pub auto_format: bool, |
| 528 |
pub code_actions: bool, |
| 529 |
} |
| 530 |
|
| 531 |
fn write(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 532 |
if event != PromptEvent::Validate { |
| 533 |
return Ok(()); |
| 534 |
} |
| 535 |
|
| 536 |
write_impl( |
| 537 |
cx, |
| 538 |
args.first(), |
| 539 |
WriteOptions { |
| 540 |
force: false, |
| 541 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 542 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 543 |
}, |
| 544 |
) |
| 545 |
} |
| 546 |
|
| 547 |
fn force_write(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 548 |
if event != PromptEvent::Validate { |
| 549 |
return Ok(()); |
| 550 |
} |
| 551 |
|
| 552 |
write_impl( |
| 553 |
cx, |
| 554 |
args.first(), |
| 555 |
WriteOptions { |
| 556 |
force: true, |
| 557 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 558 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 559 |
}, |
| 560 |
) |
| 561 |
} |
| 562 |
|
| 563 |
fn write_buffer_close( |
| 564 |
cx: &mut compositor::Context, |
| 565 |
args: Args, |
| 566 |
event: PromptEvent, |
| 567 |
) -> anyhow::Result<()> { |
| 568 |
if event != PromptEvent::Validate { |
| 569 |
return Ok(()); |
| 570 |
} |
| 571 |
|
| 572 |
write_impl( |
| 573 |
cx, |
| 574 |
args.first(), |
| 575 |
WriteOptions { |
| 576 |
force: false, |
| 577 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 578 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 579 |
}, |
| 580 |
)?; |
| 581 |
|
| 582 |
let document_ids = buffer_gather_paths_impl(cx.editor, args); |
| 583 |
buffer_close_by_ids_impl(cx, &document_ids, false) |
| 584 |
} |
| 585 |
|
| 586 |
fn force_write_buffer_close( |
| 587 |
cx: &mut compositor::Context, |
| 588 |
args: Args, |
| 589 |
event: PromptEvent, |
| 590 |
) -> anyhow::Result<()> { |
| 591 |
if event != PromptEvent::Validate { |
| 592 |
return Ok(()); |
| 593 |
} |
| 594 |
|
| 595 |
write_impl( |
| 596 |
cx, |
| 597 |
args.first(), |
| 598 |
WriteOptions { |
| 599 |
force: true, |
| 600 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 601 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 602 |
}, |
| 603 |
)?; |
| 604 |
|
| 605 |
let document_ids = buffer_gather_paths_impl(cx.editor, args); |
| 606 |
buffer_close_by_ids_impl(cx, &document_ids, false) |
| 607 |
} |
| 608 |
|
| 609 |
fn new_file(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 610 |
if event != PromptEvent::Validate { |
| 611 |
return Ok(()); |
| 612 |
} |
| 613 |
|
| 614 |
cx.editor.new_file(Action::Replace); |
| 615 |
|
| 616 |
Ok(()) |
| 617 |
} |
| 618 |
|
| 619 |
fn format(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 620 |
if event != PromptEvent::Validate { |
| 621 |
return Ok(()); |
| 622 |
} |
| 623 |
|
| 624 |
let (view, doc) = current_ref!(cx.editor); |
| 625 |
let format = doc.format(cx.editor).context( |
| 626 |
"A formatter isn't available, and no language server provides formatting capabilities", |
| 627 |
)?; |
| 628 |
let callback = make_format_callback(doc.id(), doc.version(), view.id, format, None); |
| 629 |
cx.jobs.callback(callback); |
| 630 |
|
| 631 |
Ok(()) |
| 632 |
} |
| 633 |
|
| 634 |
fn set_indent_style( |
| 635 |
cx: &mut compositor::Context, |
| 636 |
args: Args, |
| 637 |
event: PromptEvent, |
| 638 |
) -> anyhow::Result<()> { |
| 639 |
if event != PromptEvent::Validate { |
| 640 |
return Ok(()); |
| 641 |
} |
| 642 |
|
| 643 |
use IndentStyle::*; |
| 644 |
|
| 645 |
|
| 646 |
if args.is_empty() { |
| 647 |
let style = doc!(cx.editor).indent_style; |
| 648 |
cx.editor.set_status(match style { |
| 649 |
Tabs => "tabs".to_owned(), |
| 650 |
Spaces(1) => "1 space".to_owned(), |
| 651 |
Spaces(n) => format!("{} spaces", n), |
| 652 |
}); |
| 653 |
return Ok(()); |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
let style = match args.first() { |
| 658 |
Some(arg) if "tabs".starts_with(&arg.to_lowercase()) => Some(Tabs), |
| 659 |
Some("0") => Some(Tabs), |
| 660 |
Some(arg) => arg |
| 661 |
.parse::<u8>() |
| 662 |
.ok() |
| 663 |
.filter(|n| (1..=MAX_INDENT).contains(n)) |
| 664 |
.map(Spaces), |
| 665 |
_ => None, |
| 666 |
}; |
| 667 |
|
| 668 |
let style = style.context("invalid indent style")?; |
| 669 |
let doc = doc_mut!(cx.editor); |
| 670 |
doc.indent_style = style; |
| 671 |
|
| 672 |
Ok(()) |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
fn set_line_ending( |
| 677 |
cx: &mut compositor::Context, |
| 678 |
args: Args, |
| 679 |
event: PromptEvent, |
| 680 |
) -> anyhow::Result<()> { |
| 681 |
if event != PromptEvent::Validate { |
| 682 |
return Ok(()); |
| 683 |
} |
| 684 |
|
| 685 |
use LineEnding::*; |
| 686 |
|
| 687 |
|
| 688 |
if args.is_empty() { |
| 689 |
let line_ending = doc!(cx.editor).line_ending; |
| 690 |
cx.editor.set_status(match line_ending { |
| 691 |
Crlf => "crlf", |
| 692 |
LF => "line feed", |
| 693 |
#[cfg(feature = "unicode-lines")] |
| 694 |
FF => "form feed", |
| 695 |
#[cfg(feature = "unicode-lines")] |
| 696 |
CR => "carriage return", |
| 697 |
#[cfg(feature = "unicode-lines")] |
| 698 |
Nel => "next line", |
| 699 |
|
| 700 |
|
| 701 |
#[cfg(feature = "unicode-lines")] |
| 702 |
VT | LS | PS => "error", |
| 703 |
}); |
| 704 |
|
| 705 |
return Ok(()); |
| 706 |
} |
| 707 |
|
| 708 |
let arg = args |
| 709 |
.first() |
| 710 |
.context("argument missing")? |
| 711 |
.to_ascii_lowercase(); |
| 712 |
|
| 713 |
|
| 714 |
let line_ending = match arg { |
| 715 |
arg if arg.starts_with("crlf") => Crlf, |
| 716 |
arg if arg.starts_with("lf") => LF, |
| 717 |
#[cfg(feature = "unicode-lines")] |
| 718 |
arg if arg.starts_with("cr") => CR, |
| 719 |
#[cfg(feature = "unicode-lines")] |
| 720 |
arg if arg.starts_with("ff") => FF, |
| 721 |
#[cfg(feature = "unicode-lines")] |
| 722 |
arg if arg.starts_with("nel") => Nel, |
| 723 |
_ => bail!("invalid line ending"), |
| 724 |
}; |
| 725 |
let (view, doc) = current!(cx.editor); |
| 726 |
doc.line_ending = line_ending; |
| 727 |
|
| 728 |
let mut pos = 0; |
| 729 |
let transaction = Transaction::change( |
| 730 |
doc.text(), |
| 731 |
doc.text().lines().filter_map(|line| { |
| 732 |
pos += line.len_chars(); |
| 733 |
match helix_core::line_ending::get_line_ending(&line) { |
| 734 |
Some(ending) if ending != line_ending => { |
| 735 |
let start = pos - ending.len_chars(); |
| 736 |
let end = pos; |
| 737 |
Some((start, end, Some(line_ending.as_str().into()))) |
| 738 |
} |
| 739 |
_ => None, |
| 740 |
} |
| 741 |
}), |
| 742 |
); |
| 743 |
doc.apply(&transaction, view.id); |
| 744 |
doc.append_changes_to_history(view); |
| 745 |
|
| 746 |
Ok(()) |
| 747 |
} |
| 748 |
fn earlier(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 749 |
if event != PromptEvent::Validate { |
| 750 |
return Ok(()); |
| 751 |
} |
| 752 |
|
| 753 |
let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?; |
| 754 |
|
| 755 |
let (view, doc) = current!(cx.editor); |
| 756 |
let success = doc.earlier(view, uk); |
| 757 |
if !success { |
| 758 |
cx.editor.set_status("Already at oldest change"); |
| 759 |
} |
| 760 |
|
| 761 |
Ok(()) |
| 762 |
} |
| 763 |
|
| 764 |
fn later(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 765 |
if event != PromptEvent::Validate { |
| 766 |
return Ok(()); |
| 767 |
} |
| 768 |
|
| 769 |
let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?; |
| 770 |
let (view, doc) = current!(cx.editor); |
| 771 |
let success = doc.later(view, uk); |
| 772 |
if !success { |
| 773 |
cx.editor.set_status("Already at newest change"); |
| 774 |
} |
| 775 |
|
| 776 |
Ok(()) |
| 777 |
} |
| 778 |
|
| 779 |
fn write_quit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 780 |
if event != PromptEvent::Validate { |
| 781 |
return Ok(()); |
| 782 |
} |
| 783 |
|
| 784 |
write_impl( |
| 785 |
cx, |
| 786 |
args.first(), |
| 787 |
WriteOptions { |
| 788 |
force: false, |
| 789 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 790 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 791 |
}, |
| 792 |
)?; |
| 793 |
cx.block_try_flush_writes()?; |
| 794 |
quit(cx, Args::default(), event) |
| 795 |
} |
| 796 |
|
| 797 |
fn force_write_quit( |
| 798 |
cx: &mut compositor::Context, |
| 799 |
args: Args, |
| 800 |
event: PromptEvent, |
| 801 |
) -> anyhow::Result<()> { |
| 802 |
if event != PromptEvent::Validate { |
| 803 |
return Ok(()); |
| 804 |
} |
| 805 |
|
| 806 |
write_impl( |
| 807 |
cx, |
| 808 |
args.first(), |
| 809 |
WriteOptions { |
| 810 |
force: true, |
| 811 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 812 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 813 |
}, |
| 814 |
)?; |
| 815 |
cx.block_try_flush_writes()?; |
| 816 |
force_quit(cx, Args::default(), event) |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
pub(super) fn buffers_remaining_impl(editor: &mut Editor) -> anyhow::Result<()> { |
| 823 |
let modified_ids: Vec<_> = editor |
| 824 |
.documents() |
| 825 |
.filter(|doc| doc.is_modified()) |
| 826 |
.map(|doc| doc.id()) |
| 827 |
.collect(); |
| 828 |
|
| 829 |
if let Some(first) = modified_ids.first() { |
| 830 |
let current = doc!(editor); |
| 831 |
|
| 832 |
|
| 833 |
if !modified_ids.contains(¤t.id()) { |
| 834 |
editor.switch(*first, Action::Replace); |
| 835 |
} |
| 836 |
|
| 837 |
let modified_names: Vec<_> = modified_ids |
| 838 |
.iter() |
| 839 |
.map(|doc_id| doc!(editor, doc_id).display_name()) |
| 840 |
.collect(); |
| 841 |
|
| 842 |
bail!( |
| 843 |
"{} unsaved buffer{} remaining: {:?}", |
| 844 |
modified_names.len(), |
| 845 |
if modified_names.len() == 1 { "" } else { "s" }, |
| 846 |
modified_names, |
| 847 |
); |
| 848 |
} |
| 849 |
Ok(()) |
| 850 |
} |
| 851 |
|
| 852 |
#[derive(Debug, Clone, Copy)] |
| 853 |
pub struct WriteAllOptions { |
| 854 |
pub force: bool, |
| 855 |
pub write_scratch: bool, |
| 856 |
pub auto_format: bool, |
| 857 |
pub code_actions: bool, |
| 858 |
} |
| 859 |
|
| 860 |
pub fn write_all_impl( |
| 861 |
cx: &mut compositor::Context, |
| 862 |
options: WriteAllOptions, |
| 863 |
) -> anyhow::Result<()> { |
| 864 |
let mut errors: Vec<&'static str> = Vec::new(); |
| 865 |
let config = cx.editor.config(); |
| 866 |
let saves: Vec<_> = cx |
| 867 |
.editor |
| 868 |
.documents |
| 869 |
.keys() |
| 870 |
.cloned() |
| 871 |
.collect::<Vec<_>>() |
| 872 |
.into_iter() |
| 873 |
.filter_map(|id| { |
| 874 |
let doc = doc!(cx.editor, &id); |
| 875 |
if !doc.is_modified() { |
| 876 |
return None; |
| 877 |
} |
| 878 |
if doc.path().is_none() { |
| 879 |
if options.write_scratch { |
| 880 |
errors.push("cannot write a buffer without a filename"); |
| 881 |
} |
| 882 |
return None; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
let target_view = cx.editor.get_synced_view_id(doc.id()); |
| 887 |
Some((id, target_view)) |
| 888 |
}) |
| 889 |
.collect(); |
| 890 |
|
| 891 |
for (doc_id, target_view) in saves { |
| 892 |
let doc = doc_mut!(cx.editor, &doc_id); |
| 893 |
let view = view_mut!(cx.editor, target_view); |
| 894 |
|
| 895 |
if doc.trim_trailing_whitespace() { |
| 896 |
trim_trailing_whitespace(doc, target_view); |
| 897 |
} |
| 898 |
if config.trim_final_newlines { |
| 899 |
trim_final_newlines(doc, target_view); |
| 900 |
} |
| 901 |
if doc.insert_final_newline() { |
| 902 |
insert_final_newline(doc, target_view); |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
doc.append_changes_to_history(view); |
| 907 |
|
| 908 |
let auto_format = config.auto_format && options.auto_format; |
| 909 |
let force = options.force; |
| 910 |
|
| 911 |
let run_code_actions = options.code_actions |
| 912 |
&& doc!(cx.editor, &doc_id) |
| 913 |
.language_config() |
| 914 |
.and_then(|c| c.code_actions_on_save.as_deref()) |
| 915 |
.is_some_and(|kinds| !kinds.is_empty()); |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
let tail = (auto_format || run_code_actions).then(|| { |
| 920 |
let callback: job::Callback = Callback::Followup(Box::new(move |editor| { |
| 921 |
|
| 922 |
if !editor.documents.contains_key(&doc_id) { |
| 923 |
return None; |
| 924 |
} |
| 925 |
let doc = doc!(editor, &doc_id); |
| 926 |
let fmt_job = auto_format |
| 927 |
.then(|| doc.auto_format(editor)) |
| 928 |
.flatten() |
| 929 |
.map(|fmt| { |
| 930 |
let call = make_format_callback( |
| 931 |
doc_id, |
| 932 |
doc.version(), |
| 933 |
target_view, |
| 934 |
fmt, |
| 935 |
Some((None, force)), |
| 936 |
); |
| 937 |
Job::with_callback(call).wait_before_exiting() |
| 938 |
}); |
| 939 |
if fmt_job.is_none() { |
| 940 |
if let Err(err) = editor.save::<PathBuf>(doc_id, None, force) { |
| 941 |
editor.set_error(format!("Error saving: {}", err)); |
| 942 |
} |
| 943 |
} |
| 944 |
fmt_job |
| 945 |
})); |
| 946 |
Job::with_callback(async { Ok(callback) }).wait_before_exiting() |
| 947 |
}); |
| 948 |
|
| 949 |
let job = if run_code_actions { |
| 950 |
code_actions_on_save(cx, doc_id, tail) |
| 951 |
} else { |
| 952 |
tail |
| 953 |
}; |
| 954 |
|
| 955 |
if let Some(job) = job { |
| 956 |
cx.jobs.add(job); |
| 957 |
} else { |
| 958 |
cx.editor.save::<PathBuf>(doc_id, None, force)?; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
if !errors.is_empty() && !options.force { |
| 963 |
bail!("{:?}", errors); |
| 964 |
} |
| 965 |
|
| 966 |
Ok(()) |
| 967 |
} |
| 968 |
|
| 969 |
fn write_all(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 970 |
if event != PromptEvent::Validate { |
| 971 |
return Ok(()); |
| 972 |
} |
| 973 |
|
| 974 |
write_all_impl( |
| 975 |
cx, |
| 976 |
WriteAllOptions { |
| 977 |
force: false, |
| 978 |
write_scratch: true, |
| 979 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 980 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 981 |
}, |
| 982 |
) |
| 983 |
} |
| 984 |
|
| 985 |
fn force_write_all( |
| 986 |
cx: &mut compositor::Context, |
| 987 |
args: Args, |
| 988 |
event: PromptEvent, |
| 989 |
) -> anyhow::Result<()> { |
| 990 |
if event != PromptEvent::Validate { |
| 991 |
return Ok(()); |
| 992 |
} |
| 993 |
|
| 994 |
write_all_impl( |
| 995 |
cx, |
| 996 |
WriteAllOptions { |
| 997 |
force: true, |
| 998 |
write_scratch: true, |
| 999 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 1000 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 1001 |
}, |
| 1002 |
) |
| 1003 |
} |
| 1004 |
|
| 1005 |
fn write_all_quit( |
| 1006 |
cx: &mut compositor::Context, |
| 1007 |
args: Args, |
| 1008 |
event: PromptEvent, |
| 1009 |
) -> anyhow::Result<()> { |
| 1010 |
if event != PromptEvent::Validate { |
| 1011 |
return Ok(()); |
| 1012 |
} |
| 1013 |
write_all_impl( |
| 1014 |
cx, |
| 1015 |
WriteAllOptions { |
| 1016 |
force: false, |
| 1017 |
write_scratch: true, |
| 1018 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 1019 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 1020 |
}, |
| 1021 |
)?; |
| 1022 |
quit_all_impl(cx, false) |
| 1023 |
} |
| 1024 |
|
| 1025 |
fn force_write_all_quit( |
| 1026 |
cx: &mut compositor::Context, |
| 1027 |
args: Args, |
| 1028 |
event: PromptEvent, |
| 1029 |
) -> anyhow::Result<()> { |
| 1030 |
if event != PromptEvent::Validate { |
| 1031 |
return Ok(()); |
| 1032 |
} |
| 1033 |
let _ = write_all_impl( |
| 1034 |
cx, |
| 1035 |
WriteAllOptions { |
| 1036 |
force: true, |
| 1037 |
write_scratch: true, |
| 1038 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 1039 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 1040 |
}, |
| 1041 |
); |
| 1042 |
quit_all_impl(cx, true) |
| 1043 |
} |
| 1044 |
|
| 1045 |
fn quit_all_impl(cx: &mut compositor::Context, force: bool) -> anyhow::Result<()> { |
| 1046 |
cx.block_try_flush_writes()?; |
| 1047 |
if !force { |
| 1048 |
buffers_remaining_impl(cx.editor)?; |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect(); |
| 1053 |
for view_id in views { |
| 1054 |
cx.editor.close(view_id); |
| 1055 |
} |
| 1056 |
|
| 1057 |
Ok(()) |
| 1058 |
} |
| 1059 |
|
| 1060 |
fn quit_all(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1061 |
if event != PromptEvent::Validate { |
| 1062 |
return Ok(()); |
| 1063 |
} |
| 1064 |
|
| 1065 |
quit_all_impl(cx, false) |
| 1066 |
} |
| 1067 |
|
| 1068 |
fn force_quit_all( |
| 1069 |
cx: &mut compositor::Context, |
| 1070 |
_args: Args, |
| 1071 |
event: PromptEvent, |
| 1072 |
) -> anyhow::Result<()> { |
| 1073 |
if event != PromptEvent::Validate { |
| 1074 |
return Ok(()); |
| 1075 |
} |
| 1076 |
|
| 1077 |
quit_all_impl(cx, true) |
| 1078 |
} |
| 1079 |
|
| 1080 |
fn cquit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1081 |
if event != PromptEvent::Validate { |
| 1082 |
return Ok(()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
let exit_code = args |
| 1086 |
.first() |
| 1087 |
.and_then(|code| code.parse::<i32>().ok()) |
| 1088 |
.unwrap_or(1); |
| 1089 |
|
| 1090 |
cx.editor.exit_code = exit_code; |
| 1091 |
quit_all_impl(cx, false) |
| 1092 |
} |
| 1093 |
|
| 1094 |
fn force_cquit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1095 |
if event != PromptEvent::Validate { |
| 1096 |
return Ok(()); |
| 1097 |
} |
| 1098 |
|
| 1099 |
let exit_code = args |
| 1100 |
.first() |
| 1101 |
.and_then(|code| code.parse::<i32>().ok()) |
| 1102 |
.unwrap_or(1); |
| 1103 |
cx.editor.exit_code = exit_code; |
| 1104 |
|
| 1105 |
quit_all_impl(cx, true) |
| 1106 |
} |
| 1107 |
|
| 1108 |
fn theme(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1109 |
let true_color = cx.editor.config.load().true_color || crate::true_color(); |
| 1110 |
match event { |
| 1111 |
PromptEvent::Abort => { |
| 1112 |
cx.editor.unset_theme_preview()?; |
| 1113 |
} |
| 1114 |
PromptEvent::Update => { |
| 1115 |
if args.is_empty() { |
| 1116 |
|
| 1117 |
cx.editor.unset_theme_preview()?; |
| 1118 |
} else if let Some(theme_name) = args.first() { |
| 1119 |
if let Ok(theme) = cx.editor.theme_loader.load(theme_name) { |
| 1120 |
if !(true_color || theme.is_16_color()) { |
| 1121 |
bail!("Unsupported theme: theme requires true color support"); |
| 1122 |
} |
| 1123 |
cx.editor.set_theme_preview(theme)?; |
| 1124 |
}; |
| 1125 |
}; |
| 1126 |
} |
| 1127 |
PromptEvent::Validate => { |
| 1128 |
if let Some(theme_name) = args.first() { |
| 1129 |
let theme = cx |
| 1130 |
.editor |
| 1131 |
.theme_loader |
| 1132 |
.load(theme_name) |
| 1133 |
.map_err(|err| anyhow::anyhow!("Could not load theme: {}", err))?; |
| 1134 |
if !(true_color || theme.is_16_color()) { |
| 1135 |
bail!("Unsupported theme: theme requires true color support"); |
| 1136 |
} |
| 1137 |
cx.editor.set_theme(theme)?; |
| 1138 |
} else { |
| 1139 |
let name = cx.editor.theme.name().to_string(); |
| 1140 |
|
| 1141 |
cx.editor.set_status(name); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
}; |
| 1145 |
|
| 1146 |
Ok(()) |
| 1147 |
} |
| 1148 |
|
| 1149 |
fn yank_main_selection_to_clipboard( |
| 1150 |
cx: &mut compositor::Context, |
| 1151 |
_args: Args, |
| 1152 |
event: PromptEvent, |
| 1153 |
) -> anyhow::Result<()> { |
| 1154 |
if event != PromptEvent::Validate { |
| 1155 |
return Ok(()); |
| 1156 |
} |
| 1157 |
|
| 1158 |
yank_main_selection_to_register(cx.editor, '+'); |
| 1159 |
Ok(()) |
| 1160 |
} |
| 1161 |
|
| 1162 |
fn yank_joined(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1163 |
if event != PromptEvent::Validate { |
| 1164 |
return Ok(()); |
| 1165 |
} |
| 1166 |
|
| 1167 |
let doc = doc!(cx.editor); |
| 1168 |
let default_sep = Cow::Borrowed(doc.line_ending.as_str()); |
| 1169 |
let separator = args.first().unwrap_or(&default_sep); |
| 1170 |
let register = cx |
| 1171 |
.editor |
| 1172 |
.selected_register |
| 1173 |
.unwrap_or(cx.editor.config().default_yank_register); |
| 1174 |
yank_joined_impl(cx.editor, separator, register); |
| 1175 |
Ok(()) |
| 1176 |
} |
| 1177 |
|
| 1178 |
fn yank_joined_to_clipboard( |
| 1179 |
cx: &mut compositor::Context, |
| 1180 |
args: Args, |
| 1181 |
event: PromptEvent, |
| 1182 |
) -> anyhow::Result<()> { |
| 1183 |
if event != PromptEvent::Validate { |
| 1184 |
return Ok(()); |
| 1185 |
} |
| 1186 |
|
| 1187 |
let doc = doc!(cx.editor); |
| 1188 |
let default_sep = Cow::Borrowed(doc.line_ending.as_str()); |
| 1189 |
let separator = args.first().unwrap_or(&default_sep); |
| 1190 |
yank_joined_impl(cx.editor, separator, '+'); |
| 1191 |
Ok(()) |
| 1192 |
} |
| 1193 |
|
| 1194 |
fn yank_main_selection_to_primary_clipboard( |
| 1195 |
cx: &mut compositor::Context, |
| 1196 |
_args: Args, |
| 1197 |
event: PromptEvent, |
| 1198 |
) -> anyhow::Result<()> { |
| 1199 |
if event != PromptEvent::Validate { |
| 1200 |
return Ok(()); |
| 1201 |
} |
| 1202 |
|
| 1203 |
yank_main_selection_to_register(cx.editor, '*'); |
| 1204 |
Ok(()) |
| 1205 |
} |
| 1206 |
|
| 1207 |
fn yank_joined_to_primary_clipboard( |
| 1208 |
cx: &mut compositor::Context, |
| 1209 |
args: Args, |
| 1210 |
event: PromptEvent, |
| 1211 |
) -> anyhow::Result<()> { |
| 1212 |
if event != PromptEvent::Validate { |
| 1213 |
return Ok(()); |
| 1214 |
} |
| 1215 |
|
| 1216 |
let doc = doc!(cx.editor); |
| 1217 |
let default_sep = Cow::Borrowed(doc.line_ending.as_str()); |
| 1218 |
let separator = args.first().unwrap_or(&default_sep); |
| 1219 |
yank_joined_impl(cx.editor, separator, '*'); |
| 1220 |
Ok(()) |
| 1221 |
} |
| 1222 |
|
| 1223 |
fn paste_clipboard_after( |
| 1224 |
cx: &mut compositor::Context, |
| 1225 |
_args: Args, |
| 1226 |
event: PromptEvent, |
| 1227 |
) -> anyhow::Result<()> { |
| 1228 |
if event != PromptEvent::Validate { |
| 1229 |
return Ok(()); |
| 1230 |
} |
| 1231 |
|
| 1232 |
paste(cx.editor, '+', Paste::After, 1); |
| 1233 |
Ok(()) |
| 1234 |
} |
| 1235 |
|
| 1236 |
fn paste_clipboard_before( |
| 1237 |
cx: &mut compositor::Context, |
| 1238 |
_args: Args, |
| 1239 |
event: PromptEvent, |
| 1240 |
) -> anyhow::Result<()> { |
| 1241 |
if event != PromptEvent::Validate { |
| 1242 |
return Ok(()); |
| 1243 |
} |
| 1244 |
|
| 1245 |
paste(cx.editor, '+', Paste::Before, 1); |
| 1246 |
Ok(()) |
| 1247 |
} |
| 1248 |
|
| 1249 |
fn paste_primary_clipboard_after( |
| 1250 |
cx: &mut compositor::Context, |
| 1251 |
_args: Args, |
| 1252 |
event: PromptEvent, |
| 1253 |
) -> anyhow::Result<()> { |
| 1254 |
if event != PromptEvent::Validate { |
| 1255 |
return Ok(()); |
| 1256 |
} |
| 1257 |
|
| 1258 |
paste(cx.editor, '*', Paste::After, 1); |
| 1259 |
Ok(()) |
| 1260 |
} |
| 1261 |
|
| 1262 |
fn paste_primary_clipboard_before( |
| 1263 |
cx: &mut compositor::Context, |
| 1264 |
_args: Args, |
| 1265 |
event: PromptEvent, |
| 1266 |
) -> anyhow::Result<()> { |
| 1267 |
if event != PromptEvent::Validate { |
| 1268 |
return Ok(()); |
| 1269 |
} |
| 1270 |
|
| 1271 |
paste(cx.editor, '*', Paste::Before, 1); |
| 1272 |
Ok(()) |
| 1273 |
} |
| 1274 |
|
| 1275 |
fn replace_selections_with_clipboard( |
| 1276 |
cx: &mut compositor::Context, |
| 1277 |
_args: Args, |
| 1278 |
event: PromptEvent, |
| 1279 |
) -> anyhow::Result<()> { |
| 1280 |
if event != PromptEvent::Validate { |
| 1281 |
return Ok(()); |
| 1282 |
} |
| 1283 |
|
| 1284 |
replace_selections_with_register(cx.editor, '+', 1); |
| 1285 |
Ok(()) |
| 1286 |
} |
| 1287 |
|
| 1288 |
fn replace_selections_with_primary_clipboard( |
| 1289 |
cx: &mut compositor::Context, |
| 1290 |
_args: Args, |
| 1291 |
event: PromptEvent, |
| 1292 |
) -> anyhow::Result<()> { |
| 1293 |
if event != PromptEvent::Validate { |
| 1294 |
return Ok(()); |
| 1295 |
} |
| 1296 |
|
| 1297 |
replace_selections_with_register(cx.editor, '*', 1); |
| 1298 |
Ok(()) |
| 1299 |
} |
| 1300 |
|
| 1301 |
fn show_clipboard_provider( |
| 1302 |
cx: &mut compositor::Context, |
| 1303 |
_args: Args, |
| 1304 |
event: PromptEvent, |
| 1305 |
) -> anyhow::Result<()> { |
| 1306 |
if event != PromptEvent::Validate { |
| 1307 |
return Ok(()); |
| 1308 |
} |
| 1309 |
|
| 1310 |
cx.editor |
| 1311 |
.set_status(cx.editor.registers.clipboard_provider_name()); |
| 1312 |
Ok(()) |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
#[inline] |
| 1317 |
fn parse_first_arg_as_dir(args: &Args, last_cwd: Option<PathBuf>) -> anyhow::Result<PathBuf> { |
| 1318 |
match args.first().map(AsRef::as_ref) { |
| 1319 |
Some("-") => last_cwd.ok_or_else(|| anyhow!("No previous working directory")), |
| 1320 |
Some(path) => Ok(helix_stdx::path::expand_tilde(Path::new(path)).into_owned()), |
| 1321 |
None => Ok(home_dir()?), |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
#[inline] |
| 1327 |
fn apply_directory_change(cx: &mut compositor::Context, dir: &Path) -> anyhow::Result<()> { |
| 1328 |
cx.editor.set_cwd(dir).map_err(|err| { |
| 1329 |
anyhow!( |
| 1330 |
"Could not change working directory to '{}': {err}", |
| 1331 |
dir.display() |
| 1332 |
) |
| 1333 |
})?; |
| 1334 |
|
| 1335 |
cx.editor.set_status(format!( |
| 1336 |
"Current working directory is now {}", |
| 1337 |
helix_stdx::env::current_working_dir().display() |
| 1338 |
)); |
| 1339 |
|
| 1340 |
Ok(()) |
| 1341 |
} |
| 1342 |
|
| 1343 |
fn change_current_directory( |
| 1344 |
cx: &mut compositor::Context, |
| 1345 |
args: Args, |
| 1346 |
event: PromptEvent, |
| 1347 |
) -> anyhow::Result<()> { |
| 1348 |
if event != PromptEvent::Validate { |
| 1349 |
return Ok(()); |
| 1350 |
} |
| 1351 |
|
| 1352 |
let dir = parse_first_arg_as_dir(&args, cx.editor.get_last_cwd().map(|p| p.to_path_buf()))?; |
| 1353 |
|
| 1354 |
apply_directory_change(cx, &dir) |
| 1355 |
} |
| 1356 |
|
| 1357 |
fn show_directory_stack( |
| 1358 |
cx: &mut compositor::Context, |
| 1359 |
_args: Args, |
| 1360 |
event: PromptEvent, |
| 1361 |
) -> anyhow::Result<()> { |
| 1362 |
if event != PromptEvent::Validate { |
| 1363 |
return Ok(()); |
| 1364 |
} |
| 1365 |
|
| 1366 |
let serialized_stack = cx |
| 1367 |
.editor |
| 1368 |
.dir_stack |
| 1369 |
.iter() |
| 1370 |
.map(|p| p.display().to_string()) |
| 1371 |
.collect::<Vec<_>>() |
| 1372 |
.join(" "); |
| 1373 |
|
| 1374 |
if !serialized_stack.is_empty() { |
| 1375 |
cx.editor.set_status(serialized_stack); |
| 1376 |
} else { |
| 1377 |
cx.editor.set_error("Stack is empty"); |
| 1378 |
} |
| 1379 |
|
| 1380 |
Ok(()) |
| 1381 |
} |
| 1382 |
|
| 1383 |
fn push_directory( |
| 1384 |
cx: &mut compositor::Context, |
| 1385 |
args: Args, |
| 1386 |
event: PromptEvent, |
| 1387 |
) -> anyhow::Result<()> { |
| 1388 |
if event != PromptEvent::Validate { |
| 1389 |
return Ok(()); |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
if cx.editor.dir_stack.len() == cx.editor.dir_stack.capacity() { |
| 1394 |
cx.editor.dir_stack.pop_back(); |
| 1395 |
} |
| 1396 |
|
| 1397 |
cx.editor |
| 1398 |
.dir_stack |
| 1399 |
.push_front(helix_stdx::env::current_working_dir()); |
| 1400 |
|
| 1401 |
change_current_directory(cx, args, event) |
| 1402 |
} |
| 1403 |
|
| 1404 |
fn pop_directory( |
| 1405 |
cx: &mut compositor::Context, |
| 1406 |
_args: Args, |
| 1407 |
event: PromptEvent, |
| 1408 |
) -> anyhow::Result<()> { |
| 1409 |
if event != PromptEvent::Validate { |
| 1410 |
return Ok(()); |
| 1411 |
} |
| 1412 |
|
| 1413 |
if let Some(dir) = cx.editor.dir_stack.pop_front() { |
| 1414 |
apply_directory_change(cx, &dir)?; |
| 1415 |
} else { |
| 1416 |
cx.editor.set_error("Stack is empty"); |
| 1417 |
} |
| 1418 |
|
| 1419 |
Ok(()) |
| 1420 |
} |
| 1421 |
|
| 1422 |
fn show_current_directory( |
| 1423 |
cx: &mut compositor::Context, |
| 1424 |
_args: Args, |
| 1425 |
event: PromptEvent, |
| 1426 |
) -> anyhow::Result<()> { |
| 1427 |
if event != PromptEvent::Validate { |
| 1428 |
return Ok(()); |
| 1429 |
} |
| 1430 |
|
| 1431 |
let cwd = helix_stdx::env::current_working_dir(); |
| 1432 |
let message = format!("Current working directory is {}", cwd.display()); |
| 1433 |
|
| 1434 |
if cwd.exists() { |
| 1435 |
cx.editor.set_status(message); |
| 1436 |
} else { |
| 1437 |
cx.editor.set_error(format!("{} (deleted)", message)); |
| 1438 |
} |
| 1439 |
Ok(()) |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
fn set_encoding( |
| 1444 |
cx: &mut compositor::Context, |
| 1445 |
args: Args, |
| 1446 |
event: PromptEvent, |
| 1447 |
) -> anyhow::Result<()> { |
| 1448 |
if event != PromptEvent::Validate { |
| 1449 |
return Ok(()); |
| 1450 |
} |
| 1451 |
|
| 1452 |
let doc = doc_mut!(cx.editor); |
| 1453 |
if let Some(label) = args.first() { |
| 1454 |
doc.set_encoding(label) |
| 1455 |
} else { |
| 1456 |
let encoding = doc.encoding().name().to_owned(); |
| 1457 |
cx.editor.set_status(encoding); |
| 1458 |
Ok(()) |
| 1459 |
} |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
fn get_character_info( |
| 1464 |
cx: &mut compositor::Context, |
| 1465 |
_args: Args, |
| 1466 |
event: PromptEvent, |
| 1467 |
) -> anyhow::Result<()> { |
| 1468 |
if event != PromptEvent::Validate { |
| 1469 |
return Ok(()); |
| 1470 |
} |
| 1471 |
|
| 1472 |
let (view, doc) = current_ref!(cx.editor); |
| 1473 |
let text = doc.text().slice(..); |
| 1474 |
|
| 1475 |
let grapheme_start = doc.selection(view.id).primary().cursor(text); |
| 1476 |
let grapheme_end = graphemes::next_grapheme_boundary(text, grapheme_start); |
| 1477 |
|
| 1478 |
if grapheme_start == grapheme_end { |
| 1479 |
return Ok(()); |
| 1480 |
} |
| 1481 |
|
| 1482 |
let grapheme = text.slice(grapheme_start..grapheme_end).to_string(); |
| 1483 |
let encoding = doc.encoding(); |
| 1484 |
|
| 1485 |
let printable = grapheme.chars().fold(String::new(), |mut s, c| { |
| 1486 |
match c { |
| 1487 |
'\0' => s.push_str("\\0"), |
| 1488 |
'\t' => s.push_str("\\t"), |
| 1489 |
'\n' => s.push_str("\\n"), |
| 1490 |
'\r' => s.push_str("\\r"), |
| 1491 |
_ => s.push(c), |
| 1492 |
} |
| 1493 |
|
| 1494 |
s |
| 1495 |
}); |
| 1496 |
|
| 1497 |
|
| 1498 |
let unicode = if encoding == encoding::UTF_8 { |
| 1499 |
let mut unicode = " (".to_owned(); |
| 1500 |
|
| 1501 |
for (i, char) in grapheme.chars().enumerate() { |
| 1502 |
if i != 0 { |
| 1503 |
unicode.push(' '); |
| 1504 |
} |
| 1505 |
|
| 1506 |
unicode.push_str("U+"); |
| 1507 |
|
| 1508 |
let codepoint: u32 = if char.is_ascii() { |
| 1509 |
char.into() |
| 1510 |
} else { |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
let s = String::from(char); |
| 1515 |
let bytes = s.as_bytes(); |
| 1516 |
|
| 1517 |
|
| 1518 |
let first = bytes[0]; |
| 1519 |
let codepoint = first & (0xFF >> (first.leading_ones() + 1)); |
| 1520 |
let mut codepoint = u32::from(codepoint); |
| 1521 |
|
| 1522 |
|
| 1523 |
for byte in bytes.iter().skip(1) { |
| 1524 |
codepoint <<= 6; |
| 1525 |
codepoint += u32::from(*byte) & 0x3F; |
| 1526 |
} |
| 1527 |
|
| 1528 |
codepoint |
| 1529 |
}; |
| 1530 |
|
| 1531 |
write!(unicode, "{codepoint:0>4x}").unwrap(); |
| 1532 |
} |
| 1533 |
|
| 1534 |
unicode.push(')'); |
| 1535 |
unicode |
| 1536 |
} else { |
| 1537 |
String::new() |
| 1538 |
}; |
| 1539 |
|
| 1540 |
|
| 1541 |
let dec = if encoding.is_ascii_compatible() && grapheme.len() == 1 { |
| 1542 |
format!(" Dec {}", grapheme.as_bytes()[0]) |
| 1543 |
} else { |
| 1544 |
String::new() |
| 1545 |
}; |
| 1546 |
|
| 1547 |
let hex = { |
| 1548 |
let mut encoder = encoding.new_encoder(); |
| 1549 |
let max_encoded_len = encoder |
| 1550 |
.max_buffer_length_from_utf8_without_replacement(grapheme.len()) |
| 1551 |
.unwrap(); |
| 1552 |
let mut bytes = Vec::with_capacity(max_encoded_len); |
| 1553 |
let mut current_byte = 0; |
| 1554 |
let mut hex = String::new(); |
| 1555 |
|
| 1556 |
for (i, char) in grapheme.chars().enumerate() { |
| 1557 |
if i != 0 { |
| 1558 |
hex.push_str(" +"); |
| 1559 |
} |
| 1560 |
|
| 1561 |
let (result, _input_bytes_read) = encoder.encode_from_utf8_to_vec_without_replacement( |
| 1562 |
&char.to_string(), |
| 1563 |
&mut bytes, |
| 1564 |
true, |
| 1565 |
); |
| 1566 |
|
| 1567 |
if let encoding::EncoderResult::Unmappable(char) = result { |
| 1568 |
bail!("{char:?} cannot be mapped to {}", encoding.name()); |
| 1569 |
} |
| 1570 |
|
| 1571 |
for byte in &bytes[current_byte..] { |
| 1572 |
write!(hex, " {byte:0>2x}").unwrap(); |
| 1573 |
} |
| 1574 |
|
| 1575 |
current_byte = bytes.len(); |
| 1576 |
} |
| 1577 |
|
| 1578 |
hex |
| 1579 |
}; |
| 1580 |
|
| 1581 |
cx.editor |
| 1582 |
.set_status(format!("\"{printable}\"{unicode}{dec} Hex{hex}")); |
| 1583 |
|
| 1584 |
Ok(()) |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
fn reload(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1589 |
if event != PromptEvent::Validate { |
| 1590 |
return Ok(()); |
| 1591 |
} |
| 1592 |
|
| 1593 |
let scrolloff = cx.editor.config().scrolloff; |
| 1594 |
let trust_full = doc_trust_full(cx.editor); |
| 1595 |
let (view, doc) = current!(cx.editor); |
| 1596 |
doc.reload(view, &cx.editor.diff_providers, trust_full) |
| 1597 |
.map(|_| { |
| 1598 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 1599 |
})?; |
| 1600 |
if let Some(path) = doc.path().map(ToOwned::to_owned) { |
| 1601 |
cx.editor |
| 1602 |
.language_servers |
| 1603 |
.file_event_handler |
| 1604 |
.file_changed(path); |
| 1605 |
} |
| 1606 |
Ok(()) |
| 1607 |
} |
| 1608 |
|
| 1609 |
fn reload_all(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1610 |
if event != PromptEvent::Validate { |
| 1611 |
return Ok(()); |
| 1612 |
} |
| 1613 |
|
| 1614 |
let scrolloff = cx.editor.config().scrolloff; |
| 1615 |
let view_id = view!(cx.editor).id; |
| 1616 |
|
| 1617 |
let docs_view_ids: Vec<(DocumentId, Vec<ViewId>)> = cx |
| 1618 |
.editor |
| 1619 |
.documents_mut() |
| 1620 |
.map(|doc| { |
| 1621 |
let mut view_ids: Vec<_> = doc.selections().keys().cloned().collect(); |
| 1622 |
|
| 1623 |
if view_ids.is_empty() { |
| 1624 |
doc.ensure_view_init(view_id); |
| 1625 |
view_ids.push(view_id); |
| 1626 |
}; |
| 1627 |
|
| 1628 |
(doc.id(), view_ids) |
| 1629 |
}) |
| 1630 |
.collect(); |
| 1631 |
|
| 1632 |
for (doc_id, view_ids) in docs_view_ids { |
| 1633 |
let doc = doc_mut!(cx.editor, &doc_id); |
| 1634 |
|
| 1635 |
|
| 1636 |
let view = view_mut!(cx.editor, view_ids[0]); |
| 1637 |
|
| 1638 |
|
| 1639 |
view.sync_changes(doc); |
| 1640 |
|
| 1641 |
|
| 1642 |
let trust_full = cx |
| 1643 |
.editor |
| 1644 |
.workspace_trust |
| 1645 |
.query( |
| 1646 |
doc.workspace_root(), |
| 1647 |
helix_loader::workspace_trust::TrustQuery::Git, |
| 1648 |
) |
| 1649 |
.is_trusted(); |
| 1650 |
if let Err(error) = doc.reload(view, &cx.editor.diff_providers, trust_full) { |
| 1651 |
cx.editor.set_error(format!("{}", error)); |
| 1652 |
continue; |
| 1653 |
} |
| 1654 |
|
| 1655 |
if let Some(path) = doc.path().map(ToOwned::to_owned) { |
| 1656 |
cx.editor |
| 1657 |
.language_servers |
| 1658 |
.file_event_handler |
| 1659 |
.file_changed(path); |
| 1660 |
} |
| 1661 |
|
| 1662 |
for view_id in view_ids { |
| 1663 |
let view = view_mut!(cx.editor, view_id); |
| 1664 |
if view.doc.eq(&doc_id) { |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
view.sync_changes(doc); |
| 1672 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 1673 |
} |
| 1674 |
} |
| 1675 |
} |
| 1676 |
|
| 1677 |
Ok(()) |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
fn update(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1682 |
if event != PromptEvent::Validate { |
| 1683 |
return Ok(()); |
| 1684 |
} |
| 1685 |
|
| 1686 |
let (_view, doc) = current!(cx.editor); |
| 1687 |
if doc.is_modified() { |
| 1688 |
write_impl( |
| 1689 |
cx, |
| 1690 |
None, |
| 1691 |
WriteOptions { |
| 1692 |
force: false, |
| 1693 |
auto_format: !args.has_flag(WRITE_NO_FORMAT_FLAG.name), |
| 1694 |
code_actions: !args.has_flag(WRITE_NO_CODE_ACTIONS_FLAG.name), |
| 1695 |
}, |
| 1696 |
) |
| 1697 |
} else { |
| 1698 |
Ok(()) |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
fn lsp_workspace_command( |
| 1703 |
cx: &mut compositor::Context, |
| 1704 |
args: Args, |
| 1705 |
event: PromptEvent, |
| 1706 |
) -> anyhow::Result<()> { |
| 1707 |
if event != PromptEvent::Validate { |
| 1708 |
return Ok(()); |
| 1709 |
} |
| 1710 |
|
| 1711 |
let doc = doc!(cx.editor); |
| 1712 |
let ls_id_commands = doc |
| 1713 |
.language_servers_with_feature(LanguageServerFeature::WorkspaceCommand) |
| 1714 |
.flat_map(|ls| { |
| 1715 |
ls.capabilities() |
| 1716 |
.execute_command_provider |
| 1717 |
.iter() |
| 1718 |
.flat_map(|options| options.commands.iter()) |
| 1719 |
.map(|command| (ls.id(), command)) |
| 1720 |
}); |
| 1721 |
|
| 1722 |
if args.is_empty() { |
| 1723 |
let commands = ls_id_commands |
| 1724 |
.map(|(ls_id, command)| { |
| 1725 |
( |
| 1726 |
ls_id, |
| 1727 |
helix_lsp::lsp::Command { |
| 1728 |
title: command.clone(), |
| 1729 |
command: command.clone(), |
| 1730 |
arguments: None, |
| 1731 |
}, |
| 1732 |
) |
| 1733 |
}) |
| 1734 |
.collect::<Vec<_>>(); |
| 1735 |
let callback = async move { |
| 1736 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 1737 |
move |_editor: &mut Editor, compositor: &mut Compositor| { |
| 1738 |
let columns = [ui::PickerColumn::new( |
| 1739 |
"title", |
| 1740 |
|(_ls_id, command): &(_, helix_lsp::lsp::Command), _| { |
| 1741 |
command.title.as_str().into() |
| 1742 |
}, |
| 1743 |
)]; |
| 1744 |
let picker = ui::Picker::new( |
| 1745 |
columns, |
| 1746 |
0, |
| 1747 |
commands, |
| 1748 |
(), |
| 1749 |
move |cx, (ls_id, command), _action| { |
| 1750 |
cx.editor.execute_lsp_command(command.clone(), *ls_id); |
| 1751 |
}, |
| 1752 |
); |
| 1753 |
compositor.push(Box::new(overlaid(picker))) |
| 1754 |
}, |
| 1755 |
)); |
| 1756 |
Ok(call) |
| 1757 |
}; |
| 1758 |
cx.jobs.callback(callback); |
| 1759 |
} else { |
| 1760 |
let command = args[0].to_string(); |
| 1761 |
let matches: Vec<_> = ls_id_commands |
| 1762 |
.filter(|(_ls_id, c)| *c == &command) |
| 1763 |
.collect(); |
| 1764 |
|
| 1765 |
match matches.as_slice() { |
| 1766 |
[(ls_id, _command)] => { |
| 1767 |
let arguments = args |
| 1768 |
.get(1) |
| 1769 |
.map(|rest| { |
| 1770 |
serde_json::Deserializer::from_str(rest) |
| 1771 |
.into_iter() |
| 1772 |
.collect::<Result<Vec<Value>, _>>() |
| 1773 |
.map_err(|err| anyhow!("failed to parse arguments: {err}")) |
| 1774 |
}) |
| 1775 |
.transpose()? |
| 1776 |
.filter(|args| !args.is_empty()); |
| 1777 |
|
| 1778 |
cx.editor.execute_lsp_command( |
| 1779 |
helix_lsp::lsp::Command { |
| 1780 |
title: command.clone(), |
| 1781 |
arguments, |
| 1782 |
command, |
| 1783 |
}, |
| 1784 |
*ls_id, |
| 1785 |
); |
| 1786 |
} |
| 1787 |
[] => { |
| 1788 |
cx.editor.set_status(format!( |
| 1789 |
"`{command}` is not supported for any language server" |
| 1790 |
)); |
| 1791 |
} |
| 1792 |
_ => { |
| 1793 |
cx.editor.set_status(format!( |
| 1794 |
"`{command}` supported by multiple language servers" |
| 1795 |
)); |
| 1796 |
} |
| 1797 |
} |
| 1798 |
} |
| 1799 |
Ok(()) |
| 1800 |
} |
| 1801 |
|
| 1802 |
fn lsp_restart(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1803 |
if event != PromptEvent::Validate { |
| 1804 |
return Ok(()); |
| 1805 |
} |
| 1806 |
|
| 1807 |
let editor_config = cx.editor.config.load(); |
| 1808 |
let doc = doc!(cx.editor); |
| 1809 |
let config = doc |
| 1810 |
.language_config() |
| 1811 |
.context("LSP not defined for the current document")?; |
| 1812 |
|
| 1813 |
let language_servers: Vec<_> = config |
| 1814 |
.language_servers |
| 1815 |
.iter() |
| 1816 |
.map(|ls| ls.name.as_str()) |
| 1817 |
.collect(); |
| 1818 |
let language_servers = if args.is_empty() { |
| 1819 |
language_servers |
| 1820 |
} else { |
| 1821 |
let (valid, invalid): (Vec<_>, Vec<_>) = args |
| 1822 |
.iter() |
| 1823 |
.map(|arg| arg.as_ref()) |
| 1824 |
.partition(|name| language_servers.contains(name)); |
| 1825 |
if !invalid.is_empty() { |
| 1826 |
let s = if invalid.len() == 1 { "" } else { "s" }; |
| 1827 |
bail!("Unknown language server{s}: {}", invalid.join(", ")); |
| 1828 |
} |
| 1829 |
valid |
| 1830 |
}; |
| 1831 |
|
| 1832 |
let mut errors = Vec::new(); |
| 1833 |
for server in language_servers.iter() { |
| 1834 |
match cx |
| 1835 |
.editor |
| 1836 |
.language_servers |
| 1837 |
.restart_server( |
| 1838 |
server, |
| 1839 |
config, |
| 1840 |
doc.path(), |
| 1841 |
&editor_config.workspace_lsp_roots, |
| 1842 |
editor_config.lsp.snippets, |
| 1843 |
) |
| 1844 |
.transpose() |
| 1845 |
{ |
| 1846 |
|
| 1847 |
|
| 1848 |
Err(helix_lsp::Error::ExecutableNotFound(_)) |
| 1849 |
if !args.iter().any(|arg| arg == server) => {} |
| 1850 |
Err(err) => errors.push(err.to_string()), |
| 1851 |
_ => (), |
| 1852 |
} |
| 1853 |
} |
| 1854 |
|
| 1855 |
|
| 1856 |
let document_ids_to_refresh: Vec<DocumentId> = cx |
| 1857 |
.editor |
| 1858 |
.documents() |
| 1859 |
.filter_map(|doc| match doc.language_config() { |
| 1860 |
Some(config) |
| 1861 |
if config.language_servers.iter().any(|ls| { |
| 1862 |
language_servers |
| 1863 |
.iter() |
| 1864 |
.any(|restarted_ls| restarted_ls == &ls.name) |
| 1865 |
}) => |
| 1866 |
{ |
| 1867 |
Some(doc.id()) |
| 1868 |
} |
| 1869 |
_ => None, |
| 1870 |
}) |
| 1871 |
.collect(); |
| 1872 |
|
| 1873 |
for document_id in document_ids_to_refresh { |
| 1874 |
cx.editor.refresh_language_servers(document_id); |
| 1875 |
} |
| 1876 |
|
| 1877 |
if errors.is_empty() { |
| 1878 |
Ok(()) |
| 1879 |
} else { |
| 1880 |
Err(anyhow::anyhow!( |
| 1881 |
"Error restarting language servers: {}", |
| 1882 |
errors.join(", ") |
| 1883 |
)) |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
fn lsp_stop(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 1888 |
if event != PromptEvent::Validate { |
| 1889 |
return Ok(()); |
| 1890 |
} |
| 1891 |
let doc = doc!(cx.editor); |
| 1892 |
|
| 1893 |
let language_servers: Vec<_> = doc |
| 1894 |
.language_servers() |
| 1895 |
.map(|ls| ls.name().to_string()) |
| 1896 |
.collect(); |
| 1897 |
let language_servers = if args.is_empty() { |
| 1898 |
language_servers |
| 1899 |
} else { |
| 1900 |
let (valid, invalid): (Vec<_>, Vec<_>) = args |
| 1901 |
.iter() |
| 1902 |
.map(|arg| arg.to_string()) |
| 1903 |
.partition(|name| language_servers.contains(name)); |
| 1904 |
if !invalid.is_empty() { |
| 1905 |
let s = if invalid.len() == 1 { "" } else { "s" }; |
| 1906 |
bail!("Unknown language server{s}: {}", invalid.join(", ")); |
| 1907 |
} |
| 1908 |
valid |
| 1909 |
}; |
| 1910 |
|
| 1911 |
for ls_name in &language_servers { |
| 1912 |
cx.editor.language_servers.stop(ls_name); |
| 1913 |
|
| 1914 |
for doc in cx.editor.documents_mut() { |
| 1915 |
if let Some(client) = doc.remove_language_server_by_name(ls_name) { |
| 1916 |
doc.clear_diagnostics_for_language_server(client.id()); |
| 1917 |
doc.reset_all_inlay_hints(); |
| 1918 |
doc.inlay_hints_oudated = true; |
| 1919 |
} |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
Ok(()) |
| 1924 |
} |
| 1925 |
|
| 1926 |
fn tree_sitter_scopes( |
| 1927 |
cx: &mut compositor::Context, |
| 1928 |
_args: Args, |
| 1929 |
event: PromptEvent, |
| 1930 |
) -> anyhow::Result<()> { |
| 1931 |
if event != PromptEvent::Validate { |
| 1932 |
return Ok(()); |
| 1933 |
} |
| 1934 |
|
| 1935 |
let (view, doc) = current!(cx.editor); |
| 1936 |
let text = doc.text().slice(..); |
| 1937 |
|
| 1938 |
let pos = doc.selection(view.id).primary().cursor(text); |
| 1939 |
let scopes = indent::get_scopes(doc.syntax(), text, pos); |
| 1940 |
|
| 1941 |
let contents = format!("```json\n{:?}\n````", scopes); |
| 1942 |
|
| 1943 |
let callback = async move { |
| 1944 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 1945 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 1946 |
let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); |
| 1947 |
let popup = Popup::new("hover", contents).auto_close(true); |
| 1948 |
compositor.replace_or_push("hover", popup); |
| 1949 |
}, |
| 1950 |
)); |
| 1951 |
Ok(call) |
| 1952 |
}; |
| 1953 |
|
| 1954 |
cx.jobs.callback(callback); |
| 1955 |
|
| 1956 |
Ok(()) |
| 1957 |
} |
| 1958 |
|
| 1959 |
fn tree_sitter_highlight_name( |
| 1960 |
cx: &mut compositor::Context, |
| 1961 |
_args: Args, |
| 1962 |
event: PromptEvent, |
| 1963 |
) -> anyhow::Result<()> { |
| 1964 |
if event != PromptEvent::Validate { |
| 1965 |
return Ok(()); |
| 1966 |
} |
| 1967 |
|
| 1968 |
let (view, doc) = current_ref!(cx.editor); |
| 1969 |
let Some(syntax) = doc.syntax() else { |
| 1970 |
return Ok(()); |
| 1971 |
}; |
| 1972 |
let text = doc.text().slice(..); |
| 1973 |
let cursor = doc.selection(view.id).primary().cursor(text); |
| 1974 |
let byte = text.char_to_byte(cursor) as u32; |
| 1975 |
|
| 1976 |
let range = { |
| 1977 |
|
| 1978 |
let row = text.char_to_line(doc.view_offset(view.id).anchor.min(text.len_chars())); |
| 1979 |
|
| 1980 |
let last_line = text.len_lines().saturating_sub(1); |
| 1981 |
let height = view.inner_area(doc).height; |
| 1982 |
let last_visible_line = (row + height as usize).saturating_sub(1).min(last_line); |
| 1983 |
let start = text.line_to_byte(row.min(last_line)) as u32; |
| 1984 |
let end = text.line_to_byte(last_visible_line + 1) as u32; |
| 1985 |
|
| 1986 |
start..end |
| 1987 |
}; |
| 1988 |
|
| 1989 |
let loader = cx.editor.syn_loader.load(); |
| 1990 |
let mut highlighter = syntax.highlighter(text, &loader, range); |
| 1991 |
let mut highlights = Vec::new(); |
| 1992 |
|
| 1993 |
while highlighter.next_event_offset() <= byte { |
| 1994 |
let (event, new_highlights) = highlighter.advance(); |
| 1995 |
if event == helix_core::syntax::HighlightEvent::Refresh { |
| 1996 |
highlights.clear(); |
| 1997 |
} |
| 1998 |
highlights.extend(new_highlights); |
| 1999 |
} |
| 2000 |
|
| 2001 |
let content = highlights |
| 2002 |
.into_iter() |
| 2003 |
.fold(String::new(), |mut acc, highlight| { |
| 2004 |
if !acc.is_empty() { |
| 2005 |
acc.push_str(", "); |
| 2006 |
} |
| 2007 |
acc.push_str(cx.editor.theme.scope(highlight)); |
| 2008 |
acc |
| 2009 |
}); |
| 2010 |
|
| 2011 |
let callback = async move { |
| 2012 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 2013 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 2014 |
let content = ui::Markdown::new(content, editor.syn_loader.clone()); |
| 2015 |
let popup = Popup::new("hover", content).auto_close(true); |
| 2016 |
compositor.replace_or_push("hover", popup); |
| 2017 |
}, |
| 2018 |
)); |
| 2019 |
Ok(call) |
| 2020 |
}; |
| 2021 |
|
| 2022 |
cx.jobs.callback(callback); |
| 2023 |
|
| 2024 |
Ok(()) |
| 2025 |
} |
| 2026 |
|
| 2027 |
fn tree_sitter_layers( |
| 2028 |
cx: &mut compositor::Context, |
| 2029 |
_args: Args, |
| 2030 |
event: PromptEvent, |
| 2031 |
) -> anyhow::Result<()> { |
| 2032 |
if event != PromptEvent::Validate { |
| 2033 |
return Ok(()); |
| 2034 |
} |
| 2035 |
|
| 2036 |
let (view, doc) = current_ref!(cx.editor); |
| 2037 |
let Some(syntax) = doc.syntax() else { |
| 2038 |
bail!("Syntax information is not available"); |
| 2039 |
}; |
| 2040 |
|
| 2041 |
let loader: &helix_core::syntax::Loader = &cx.editor.syn_loader.load(); |
| 2042 |
let text = doc.text().slice(..); |
| 2043 |
let cursor = doc.selection(view.id).primary().cursor(text); |
| 2044 |
let byte = text.char_to_byte(cursor) as u32; |
| 2045 |
let languages = |
| 2046 |
syntax |
| 2047 |
.layers_for_byte_range(byte, byte) |
| 2048 |
.fold(String::new(), |mut acc, layer| { |
| 2049 |
if !acc.is_empty() { |
| 2050 |
acc.push_str(", "); |
| 2051 |
} |
| 2052 |
acc.push_str( |
| 2053 |
&loader |
| 2054 |
.language(syntax.layer(layer).language) |
| 2055 |
.config() |
| 2056 |
.language_id, |
| 2057 |
); |
| 2058 |
acc |
| 2059 |
}); |
| 2060 |
|
| 2061 |
let callback = async move { |
| 2062 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 2063 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 2064 |
let content = ui::Markdown::new(languages, editor.syn_loader.clone()); |
| 2065 |
let popup = Popup::new("hover", content).auto_close(true); |
| 2066 |
compositor.replace_or_push("hover", popup); |
| 2067 |
}, |
| 2068 |
)); |
| 2069 |
Ok(call) |
| 2070 |
}; |
| 2071 |
|
| 2072 |
cx.jobs.callback(callback); |
| 2073 |
|
| 2074 |
Ok(()) |
| 2075 |
} |
| 2076 |
|
| 2077 |
fn vsplit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2078 |
if event != PromptEvent::Validate { |
| 2079 |
return Ok(()); |
| 2080 |
} |
| 2081 |
|
| 2082 |
if args.is_empty() { |
| 2083 |
split(cx.editor, Action::VerticalSplit); |
| 2084 |
} else { |
| 2085 |
open_impl(cx, args, Action::VerticalSplit)?; |
| 2086 |
} |
| 2087 |
|
| 2088 |
Ok(()) |
| 2089 |
} |
| 2090 |
|
| 2091 |
fn hsplit(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2092 |
if event != PromptEvent::Validate { |
| 2093 |
return Ok(()); |
| 2094 |
} |
| 2095 |
|
| 2096 |
if args.is_empty() { |
| 2097 |
split(cx.editor, Action::HorizontalSplit); |
| 2098 |
} else { |
| 2099 |
open_impl(cx, args, Action::HorizontalSplit)?; |
| 2100 |
} |
| 2101 |
|
| 2102 |
Ok(()) |
| 2103 |
} |
| 2104 |
|
| 2105 |
fn vsplit_new(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2106 |
if event != PromptEvent::Validate { |
| 2107 |
return Ok(()); |
| 2108 |
} |
| 2109 |
|
| 2110 |
cx.editor.new_file(Action::VerticalSplit); |
| 2111 |
|
| 2112 |
Ok(()) |
| 2113 |
} |
| 2114 |
|
| 2115 |
fn hsplit_new(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2116 |
if event != PromptEvent::Validate { |
| 2117 |
return Ok(()); |
| 2118 |
} |
| 2119 |
|
| 2120 |
cx.editor.new_file(Action::HorizontalSplit); |
| 2121 |
|
| 2122 |
Ok(()) |
| 2123 |
} |
| 2124 |
|
| 2125 |
fn debug_eval(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2126 |
if event != PromptEvent::Validate { |
| 2127 |
return Ok(()); |
| 2128 |
} |
| 2129 |
|
| 2130 |
if let Some(debugger) = cx.editor.debug_adapters.get_active_client() { |
| 2131 |
let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) { |
| 2132 |
(Some(frame), Some(thread_id)) => (frame, thread_id), |
| 2133 |
_ => { |
| 2134 |
bail!("Cannot find current stack frame to access variables") |
| 2135 |
} |
| 2136 |
}; |
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
let frame_id = debugger.stack_frames[&thread_id][frame].id; |
| 2141 |
let response = helix_lsp::block_on(debugger.eval(args.join(" "), Some(frame_id)))?; |
| 2142 |
cx.editor.set_status(response.result); |
| 2143 |
} |
| 2144 |
Ok(()) |
| 2145 |
} |
| 2146 |
|
| 2147 |
fn debug_start(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2148 |
if event != PromptEvent::Validate { |
| 2149 |
return Ok(()); |
| 2150 |
} |
| 2151 |
|
| 2152 |
let mut args: Vec<_> = args.into_iter().collect(); |
| 2153 |
let name = match args.len() { |
| 2154 |
0 => None, |
| 2155 |
_ => Some(args.remove(0)), |
| 2156 |
}; |
| 2157 |
dap_start_impl(cx, name.as_deref(), None, Some(args)) |
| 2158 |
} |
| 2159 |
|
| 2160 |
fn debug_remote( |
| 2161 |
cx: &mut compositor::Context, |
| 2162 |
args: Args, |
| 2163 |
event: PromptEvent, |
| 2164 |
) -> anyhow::Result<()> { |
| 2165 |
if event != PromptEvent::Validate { |
| 2166 |
return Ok(()); |
| 2167 |
} |
| 2168 |
|
| 2169 |
let mut args: Vec<_> = args.into_iter().collect(); |
| 2170 |
let address = match args.len() { |
| 2171 |
0 => None, |
| 2172 |
_ => Some(args.remove(0).parse()?), |
| 2173 |
}; |
| 2174 |
let name = match args.len() { |
| 2175 |
0 => None, |
| 2176 |
_ => Some(args.remove(0)), |
| 2177 |
}; |
| 2178 |
dap_start_impl(cx, name.as_deref(), address, Some(args)) |
| 2179 |
} |
| 2180 |
|
| 2181 |
fn tutor(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2182 |
if event != PromptEvent::Validate { |
| 2183 |
return Ok(()); |
| 2184 |
} |
| 2185 |
|
| 2186 |
let path = helix_loader::runtime_file(Path::new("tutor")); |
| 2187 |
cx.editor.open(&path, Action::Replace)?; |
| 2188 |
|
| 2189 |
doc_mut!(cx.editor).set_path(None); |
| 2190 |
Ok(()) |
| 2191 |
} |
| 2192 |
|
| 2193 |
fn abort_goto_line_number_preview(cx: &mut compositor::Context) { |
| 2194 |
if let Some(last_selection) = cx.editor.last_selection.take() { |
| 2195 |
let scrolloff = cx.editor.config().scrolloff; |
| 2196 |
|
| 2197 |
let (view, doc) = current!(cx.editor); |
| 2198 |
doc.set_selection(view.id, last_selection); |
| 2199 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
fn update_goto_line_number_preview(cx: &mut compositor::Context, args: Args) -> anyhow::Result<()> { |
| 2204 |
cx.editor.last_selection.get_or_insert_with(|| { |
| 2205 |
let (view, doc) = current!(cx.editor); |
| 2206 |
doc.selection(view.id).clone() |
| 2207 |
}); |
| 2208 |
|
| 2209 |
let scrolloff = cx.editor.config().scrolloff; |
| 2210 |
let line = args[0].parse::<usize>()?; |
| 2211 |
goto_line_without_jumplist( |
| 2212 |
cx.editor, |
| 2213 |
NonZeroUsize::new(line), |
| 2214 |
if cx.editor.mode == Mode::Select { |
| 2215 |
Movement::Extend |
| 2216 |
} else { |
| 2217 |
Movement::Move |
| 2218 |
}, |
| 2219 |
); |
| 2220 |
|
| 2221 |
let (view, doc) = current!(cx.editor); |
| 2222 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2223 |
|
| 2224 |
Ok(()) |
| 2225 |
} |
| 2226 |
|
| 2227 |
pub(super) fn goto_line_number( |
| 2228 |
cx: &mut compositor::Context, |
| 2229 |
args: Args, |
| 2230 |
event: PromptEvent, |
| 2231 |
) -> anyhow::Result<()> { |
| 2232 |
match event { |
| 2233 |
PromptEvent::Abort => abort_goto_line_number_preview(cx), |
| 2234 |
PromptEvent::Validate => { |
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
update_goto_line_number_preview(cx, args)?; |
| 2239 |
|
| 2240 |
let last_selection = cx |
| 2241 |
.editor |
| 2242 |
.last_selection |
| 2243 |
.take() |
| 2244 |
.expect("update_goto_line_number_preview should always set last_selection"); |
| 2245 |
|
| 2246 |
let (view, doc) = current!(cx.editor); |
| 2247 |
view.push_jump(doc, (doc.id(), last_selection)); |
| 2248 |
} |
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
PromptEvent::Update if args.is_empty() => abort_goto_line_number_preview(cx), |
| 2254 |
PromptEvent::Update => update_goto_line_number_preview(cx, args)?, |
| 2255 |
} |
| 2256 |
|
| 2257 |
Ok(()) |
| 2258 |
} |
| 2259 |
|
| 2260 |
|
| 2261 |
fn get_option(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2262 |
if event != PromptEvent::Validate { |
| 2263 |
return Ok(()); |
| 2264 |
} |
| 2265 |
|
| 2266 |
let key = &args[0].to_lowercase(); |
| 2267 |
let key_error = || anyhow::anyhow!("Unknown key `{}`", key); |
| 2268 |
|
| 2269 |
let config = serde_json::json!(cx.editor.config().deref()); |
| 2270 |
let pointer = format!("/{}", key.replace('.', "/")); |
| 2271 |
let value = config.pointer(&pointer).ok_or_else(key_error)?; |
| 2272 |
|
| 2273 |
cx.editor.set_status(value.to_string()); |
| 2274 |
Ok(()) |
| 2275 |
} |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
fn set_option(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2280 |
if event != PromptEvent::Validate { |
| 2281 |
return Ok(()); |
| 2282 |
} |
| 2283 |
|
| 2284 |
let (key, arg) = (&args[0].to_lowercase(), args[1].trim()); |
| 2285 |
|
| 2286 |
let key_error = || anyhow::anyhow!("Unknown key `{}`", key); |
| 2287 |
let field_error = |_| anyhow::anyhow!("Could not parse field `{}`", arg); |
| 2288 |
|
| 2289 |
let mut config = serde_json::json!(&cx.editor.config().deref()); |
| 2290 |
let pointer = format!("/{}", key.replace('.', "/")); |
| 2291 |
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; |
| 2292 |
|
| 2293 |
*value = if value.is_string() { |
| 2294 |
|
| 2295 |
Value::String(arg.to_string()) |
| 2296 |
} else { |
| 2297 |
arg.parse().map_err(field_error)? |
| 2298 |
}; |
| 2299 |
let config = serde_json::from_value(config).map_err(field_error)?; |
| 2300 |
|
| 2301 |
cx.editor |
| 2302 |
.config_events |
| 2303 |
.0 |
| 2304 |
.send(ConfigEvent::Update(config))?; |
| 2305 |
Ok(()) |
| 2306 |
} |
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
fn toggle_option( |
| 2312 |
cx: &mut compositor::Context, |
| 2313 |
args: Args, |
| 2314 |
event: PromptEvent, |
| 2315 |
) -> anyhow::Result<()> { |
| 2316 |
if event != PromptEvent::Validate { |
| 2317 |
return Ok(()); |
| 2318 |
} |
| 2319 |
|
| 2320 |
let key = &args[0].to_lowercase(); |
| 2321 |
|
| 2322 |
let key_error = || anyhow::anyhow!("Unknown key `{}`", key); |
| 2323 |
|
| 2324 |
let mut config = serde_json::json!(&cx.editor.config().deref()); |
| 2325 |
let pointer = format!("/{}", key.replace('.', "/")); |
| 2326 |
let value = config.pointer_mut(&pointer).ok_or_else(key_error)?; |
| 2327 |
|
| 2328 |
*value = match value { |
| 2329 |
Value::Bool(ref value) => { |
| 2330 |
ensure!( |
| 2331 |
args.len() == 1, |
| 2332 |
"Bad arguments. For boolean configurations use: `:toggle {key}`" |
| 2333 |
); |
| 2334 |
Value::Bool(!value) |
| 2335 |
} |
| 2336 |
Value::String(ref value) => { |
| 2337 |
ensure!( |
| 2338 |
args.len() == 2, |
| 2339 |
"Bad arguments. For string configurations use: `:toggle {key} val1 val2 ...`", |
| 2340 |
); |
| 2341 |
|
| 2342 |
let values: Vec<_> = command_line::Tokenizer::new(&args[1], true) |
| 2343 |
.map(|res| res.map(|token| token.content)) |
| 2344 |
.collect::<Result<_, _>>() |
| 2345 |
.map_err(|err| anyhow!("failed to parse values: {err}"))?; |
| 2346 |
|
| 2347 |
Value::String( |
| 2348 |
values |
| 2349 |
.iter() |
| 2350 |
.skip_while(|e| *e != value) |
| 2351 |
.nth(1) |
| 2352 |
.map(AsRef::as_ref) |
| 2353 |
.unwrap_or_else(|| &values[0]) |
| 2354 |
.to_string(), |
| 2355 |
) |
| 2356 |
} |
| 2357 |
Value::Null => bail!("Configuration {key} cannot be toggled"), |
| 2358 |
Value::Number(_) | Value::Array(_) | Value::Object(_) => { |
| 2359 |
ensure!( |
| 2360 |
args.len() == 2, |
| 2361 |
"Bad arguments. For {kind} configurations use: `:toggle {key} val1 val2 ...`", |
| 2362 |
kind = match value { |
| 2363 |
Value::Number(_) => "number", |
| 2364 |
Value::Array(_) => "array", |
| 2365 |
Value::Object(_) => "object", |
| 2366 |
_ => unreachable!(), |
| 2367 |
} |
| 2368 |
); |
| 2369 |
|
| 2370 |
|
| 2371 |
let values: Vec<Value> = serde_json::Deserializer::from_str(&args[1]) |
| 2372 |
.into_iter() |
| 2373 |
.collect::<Result<_, _>>() |
| 2374 |
.map_err(|err| anyhow!("failed to parse value: {err}"))?; |
| 2375 |
|
| 2376 |
if let Some(wrongly_typed_value) = values |
| 2377 |
.iter() |
| 2378 |
.find(|v| std::mem::discriminant(*v) != std::mem::discriminant(&*value)) |
| 2379 |
{ |
| 2380 |
bail!("value '{wrongly_typed_value}' has a different type than '{value}'"); |
| 2381 |
} |
| 2382 |
|
| 2383 |
values |
| 2384 |
.iter() |
| 2385 |
.skip_while(|e| *e != value) |
| 2386 |
.nth(1) |
| 2387 |
.unwrap_or(&values[0]) |
| 2388 |
.clone() |
| 2389 |
} |
| 2390 |
}; |
| 2391 |
|
| 2392 |
let status = format!("'{key}' is now set to {value}"); |
| 2393 |
let config = serde_json::from_value(config) |
| 2394 |
.map_err(|err| anyhow::anyhow!("Failed to parse config: {err}"))?; |
| 2395 |
|
| 2396 |
cx.editor |
| 2397 |
.config_events |
| 2398 |
.0 |
| 2399 |
.send(ConfigEvent::Update(config))?; |
| 2400 |
cx.editor.set_status(status); |
| 2401 |
Ok(()) |
| 2402 |
} |
| 2403 |
|
| 2404 |
|
| 2405 |
fn language(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2406 |
if event != PromptEvent::Validate { |
| 2407 |
return Ok(()); |
| 2408 |
} |
| 2409 |
|
| 2410 |
if args.is_empty() { |
| 2411 |
let doc = doc!(cx.editor); |
| 2412 |
let language = &doc.language_name().unwrap_or(DEFAULT_LANGUAGE_NAME); |
| 2413 |
cx.editor.set_status(language.to_string()); |
| 2414 |
return Ok(()); |
| 2415 |
} |
| 2416 |
|
| 2417 |
let doc = doc_mut!(cx.editor); |
| 2418 |
|
| 2419 |
let loader = cx.editor.syn_loader.load(); |
| 2420 |
if &args[0] == DEFAULT_LANGUAGE_NAME { |
| 2421 |
doc.set_language(None, &loader) |
| 2422 |
} else { |
| 2423 |
doc.set_language_by_language_id(&args[0], &loader)?; |
| 2424 |
} |
| 2425 |
doc.detect_indent_and_line_ending(); |
| 2426 |
|
| 2427 |
let id = doc.id(); |
| 2428 |
cx.editor.refresh_language_servers(id); |
| 2429 |
let doc = doc_mut!(cx.editor); |
| 2430 |
let diagnostics = |
| 2431 |
Editor::doc_diagnostics(&cx.editor.language_servers, &cx.editor.diagnostics, doc); |
| 2432 |
doc.replace_diagnostics(diagnostics, &[], None); |
| 2433 |
Ok(()) |
| 2434 |
} |
| 2435 |
|
| 2436 |
fn sort(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2437 |
if event != PromptEvent::Validate { |
| 2438 |
return Ok(()); |
| 2439 |
} |
| 2440 |
|
| 2441 |
let scrolloff = cx.editor.config().scrolloff; |
| 2442 |
let (view, doc) = current!(cx.editor); |
| 2443 |
let text = doc.text().slice(..); |
| 2444 |
|
| 2445 |
let selection = doc.selection(view.id); |
| 2446 |
|
| 2447 |
if selection.len() == 1 { |
| 2448 |
bail!("Sorting requires multiple selections. Hint: split selection first"); |
| 2449 |
} |
| 2450 |
|
| 2451 |
let mut fragments: Vec<_> = selection |
| 2452 |
.slices(text) |
| 2453 |
.map(|fragment| fragment.chunks().collect()) |
| 2454 |
.collect(); |
| 2455 |
|
| 2456 |
fragments.sort_by( |
| 2457 |
match (args.has_flag("insensitive"), args.has_flag("reverse")) { |
| 2458 |
(true, true) => |a: &Tendril, b: &Tendril| b.to_lowercase().cmp(&a.to_lowercase()), |
| 2459 |
(true, false) => |a: &Tendril, b: &Tendril| a.to_lowercase().cmp(&b.to_lowercase()), |
| 2460 |
(false, true) => |a: &Tendril, b: &Tendril| b.cmp(a), |
| 2461 |
(false, false) => |a: &Tendril, b: &Tendril| a.cmp(b), |
| 2462 |
}, |
| 2463 |
); |
| 2464 |
|
| 2465 |
let transaction = Transaction::change( |
| 2466 |
doc.text(), |
| 2467 |
selection |
| 2468 |
.into_iter() |
| 2469 |
.zip(fragments) |
| 2470 |
.map(|(s, fragment)| (s.from(), s.to(), Some(fragment))), |
| 2471 |
); |
| 2472 |
|
| 2473 |
doc.apply(&transaction, view.id); |
| 2474 |
doc.append_changes_to_history(view); |
| 2475 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2476 |
|
| 2477 |
Ok(()) |
| 2478 |
} |
| 2479 |
|
| 2480 |
fn reflow(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2481 |
if event != PromptEvent::Validate { |
| 2482 |
return Ok(()); |
| 2483 |
} |
| 2484 |
|
| 2485 |
let scrolloff = cx.editor.config().scrolloff; |
| 2486 |
let (view, doc) = current!(cx.editor); |
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
let text_width: usize = args |
| 2493 |
.first() |
| 2494 |
.map(|num| num.parse::<usize>()) |
| 2495 |
.transpose()? |
| 2496 |
.unwrap_or_else(|| doc.text_width()); |
| 2497 |
|
| 2498 |
let rope = doc.text(); |
| 2499 |
|
| 2500 |
let selection = doc.selection(view.id); |
| 2501 |
let transaction = Transaction::change_by_selection(rope, selection, |range| { |
| 2502 |
let fragment = range.fragment(rope.slice(..)); |
| 2503 |
let reflowed_text = helix_core::wrap::reflow_hard_wrap(&fragment, text_width); |
| 2504 |
|
| 2505 |
(range.from(), range.to(), Some(reflowed_text)) |
| 2506 |
}); |
| 2507 |
|
| 2508 |
doc.apply(&transaction, view.id); |
| 2509 |
doc.append_changes_to_history(view); |
| 2510 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2511 |
|
| 2512 |
Ok(()) |
| 2513 |
} |
| 2514 |
|
| 2515 |
fn tree_sitter_subtree( |
| 2516 |
cx: &mut compositor::Context, |
| 2517 |
_args: Args, |
| 2518 |
event: PromptEvent, |
| 2519 |
) -> anyhow::Result<()> { |
| 2520 |
if event != PromptEvent::Validate { |
| 2521 |
return Ok(()); |
| 2522 |
} |
| 2523 |
|
| 2524 |
let (view, doc) = current!(cx.editor); |
| 2525 |
|
| 2526 |
if let Some(syntax) = doc.syntax() { |
| 2527 |
let primary_selection = doc.selection(view.id).primary(); |
| 2528 |
let text = doc.text(); |
| 2529 |
let from = text.char_to_byte(primary_selection.from()) as u32; |
| 2530 |
let to = text.char_to_byte(primary_selection.to()) as u32; |
| 2531 |
if let Some(selected_node) = syntax.descendant_for_byte_range(from, to) { |
| 2532 |
let mut contents = String::from("```tsq\n"); |
| 2533 |
helix_core::syntax::pretty_print_tree(&mut contents, selected_node)?; |
| 2534 |
contents.push_str("\n```"); |
| 2535 |
|
| 2536 |
let callback = async move { |
| 2537 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 2538 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 2539 |
let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); |
| 2540 |
let popup = Popup::new("hover", contents).auto_close(true); |
| 2541 |
compositor.replace_or_push("hover", popup); |
| 2542 |
}, |
| 2543 |
)); |
| 2544 |
Ok(call) |
| 2545 |
}; |
| 2546 |
|
| 2547 |
cx.jobs.callback(callback); |
| 2548 |
} |
| 2549 |
} |
| 2550 |
|
| 2551 |
Ok(()) |
| 2552 |
} |
| 2553 |
|
| 2554 |
fn open_config( |
| 2555 |
cx: &mut compositor::Context, |
| 2556 |
_args: Args, |
| 2557 |
event: PromptEvent, |
| 2558 |
) -> anyhow::Result<()> { |
| 2559 |
if event != PromptEvent::Validate { |
| 2560 |
return Ok(()); |
| 2561 |
} |
| 2562 |
|
| 2563 |
cx.editor |
| 2564 |
.open(&helix_loader::config_file(), Action::Replace)?; |
| 2565 |
Ok(()) |
| 2566 |
} |
| 2567 |
|
| 2568 |
fn open_workspace_config( |
| 2569 |
cx: &mut compositor::Context, |
| 2570 |
_args: Args, |
| 2571 |
event: PromptEvent, |
| 2572 |
) -> anyhow::Result<()> { |
| 2573 |
if event != PromptEvent::Validate { |
| 2574 |
return Ok(()); |
| 2575 |
} |
| 2576 |
|
| 2577 |
cx.editor |
| 2578 |
.open(&helix_loader::workspace_config_file(), Action::Replace)?; |
| 2579 |
Ok(()) |
| 2580 |
} |
| 2581 |
|
| 2582 |
fn open_log(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2583 |
if event != PromptEvent::Validate { |
| 2584 |
return Ok(()); |
| 2585 |
} |
| 2586 |
|
| 2587 |
cx.editor.open(&helix_loader::log_file(), Action::Replace)?; |
| 2588 |
Ok(()) |
| 2589 |
} |
| 2590 |
|
| 2591 |
fn refresh_config( |
| 2592 |
cx: &mut compositor::Context, |
| 2593 |
_args: Args, |
| 2594 |
event: PromptEvent, |
| 2595 |
) -> anyhow::Result<()> { |
| 2596 |
if event != PromptEvent::Validate { |
| 2597 |
return Ok(()); |
| 2598 |
} |
| 2599 |
|
| 2600 |
cx.editor.config_events.0.send(ConfigEvent::Refresh)?; |
| 2601 |
Ok(()) |
| 2602 |
} |
| 2603 |
|
| 2604 |
fn append_output( |
| 2605 |
cx: &mut compositor::Context, |
| 2606 |
args: Args, |
| 2607 |
event: PromptEvent, |
| 2608 |
) -> anyhow::Result<()> { |
| 2609 |
if event != PromptEvent::Validate { |
| 2610 |
return Ok(()); |
| 2611 |
} |
| 2612 |
|
| 2613 |
shell(cx, &args.join(" "), &ShellBehavior::Append); |
| 2614 |
Ok(()) |
| 2615 |
} |
| 2616 |
|
| 2617 |
fn insert_output( |
| 2618 |
cx: &mut compositor::Context, |
| 2619 |
args: Args, |
| 2620 |
event: PromptEvent, |
| 2621 |
) -> anyhow::Result<()> { |
| 2622 |
if event != PromptEvent::Validate { |
| 2623 |
return Ok(()); |
| 2624 |
} |
| 2625 |
|
| 2626 |
shell(cx, &args.join(" "), &ShellBehavior::Insert); |
| 2627 |
Ok(()) |
| 2628 |
} |
| 2629 |
|
| 2630 |
fn pipe_to(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2631 |
pipe_impl(cx, args, event, &ShellBehavior::Ignore) |
| 2632 |
} |
| 2633 |
|
| 2634 |
fn pipe(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2635 |
pipe_impl(cx, args, event, &ShellBehavior::Replace) |
| 2636 |
} |
| 2637 |
|
| 2638 |
fn pipe_impl( |
| 2639 |
cx: &mut compositor::Context, |
| 2640 |
args: Args, |
| 2641 |
event: PromptEvent, |
| 2642 |
behavior: &ShellBehavior, |
| 2643 |
) -> anyhow::Result<()> { |
| 2644 |
if event != PromptEvent::Validate { |
| 2645 |
return Ok(()); |
| 2646 |
} |
| 2647 |
|
| 2648 |
shell(cx, &args.join(" "), behavior); |
| 2649 |
Ok(()) |
| 2650 |
} |
| 2651 |
|
| 2652 |
fn run_shell_command( |
| 2653 |
cx: &mut compositor::Context, |
| 2654 |
args: Args, |
| 2655 |
event: PromptEvent, |
| 2656 |
) -> anyhow::Result<()> { |
| 2657 |
if event != PromptEvent::Validate { |
| 2658 |
return Ok(()); |
| 2659 |
} |
| 2660 |
|
| 2661 |
let shell = cx.editor.config().shell.clone(); |
| 2662 |
let args = args.join(" "); |
| 2663 |
|
| 2664 |
let callback = async move { |
| 2665 |
let output = shell_impl_async(&shell, &args, None).await?; |
| 2666 |
let call: job::Callback = Callback::EditorCompositor(Box::new( |
| 2667 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 2668 |
if !output.trim().is_empty() { |
| 2669 |
let contents = ui::Markdown::new( |
| 2670 |
format!("```sh\n{}\n```", output.trim_end()), |
| 2671 |
editor.syn_loader.clone(), |
| 2672 |
); |
| 2673 |
let popup = Popup::new("shell", contents).position(Some( |
| 2674 |
helix_core::Position::new(editor.cursor().0.unwrap_or_default().row, 2), |
| 2675 |
)); |
| 2676 |
compositor.replace_or_push("shell", popup); |
| 2677 |
} |
| 2678 |
editor.set_status("Command run"); |
| 2679 |
}, |
| 2680 |
)); |
| 2681 |
Ok(call) |
| 2682 |
}; |
| 2683 |
cx.jobs.callback(callback); |
| 2684 |
|
| 2685 |
Ok(()) |
| 2686 |
} |
| 2687 |
|
| 2688 |
fn reset_diff_change( |
| 2689 |
cx: &mut compositor::Context, |
| 2690 |
_args: Args, |
| 2691 |
event: PromptEvent, |
| 2692 |
) -> anyhow::Result<()> { |
| 2693 |
if event != PromptEvent::Validate { |
| 2694 |
return Ok(()); |
| 2695 |
} |
| 2696 |
|
| 2697 |
let editor = &mut cx.editor; |
| 2698 |
let scrolloff = editor.config().scrolloff; |
| 2699 |
|
| 2700 |
let (view, doc) = current!(editor); |
| 2701 |
let Some(handle) = doc.diff_handle() else { |
| 2702 |
bail!("Diff is not available in the current buffer") |
| 2703 |
}; |
| 2704 |
|
| 2705 |
let diff = handle.load(); |
| 2706 |
let doc_text = doc.text().slice(..); |
| 2707 |
let diff_base = diff.diff_base(); |
| 2708 |
let mut changes = 0; |
| 2709 |
|
| 2710 |
let transaction = Transaction::change( |
| 2711 |
doc.text(), |
| 2712 |
diff.hunks_intersecting_line_ranges(doc.selection(view.id).line_ranges(doc_text)) |
| 2713 |
.map(|hunk| { |
| 2714 |
changes += 1; |
| 2715 |
let start = diff_base.line_to_char(hunk.before.start as usize); |
| 2716 |
let end = diff_base.line_to_char(hunk.before.end as usize); |
| 2717 |
let text: Tendril = diff_base.slice(start..end).chunks().collect(); |
| 2718 |
( |
| 2719 |
doc_text.line_to_char(hunk.after.start as usize), |
| 2720 |
doc_text.line_to_char(hunk.after.end as usize), |
| 2721 |
(!text.is_empty()).then_some(text), |
| 2722 |
) |
| 2723 |
}), |
| 2724 |
); |
| 2725 |
if changes == 0 { |
| 2726 |
bail!("There are no changes under any selection"); |
| 2727 |
} |
| 2728 |
|
| 2729 |
drop(diff); |
| 2730 |
doc.apply(&transaction, view.id); |
| 2731 |
doc.append_changes_to_history(view); |
| 2732 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2733 |
cx.editor.set_status(format!( |
| 2734 |
"Reset {changes} change{}", |
| 2735 |
if changes == 1 { "" } else { "s" } |
| 2736 |
)); |
| 2737 |
Ok(()) |
| 2738 |
} |
| 2739 |
|
| 2740 |
fn clear_register( |
| 2741 |
cx: &mut compositor::Context, |
| 2742 |
args: Args, |
| 2743 |
event: PromptEvent, |
| 2744 |
) -> anyhow::Result<()> { |
| 2745 |
if event != PromptEvent::Validate { |
| 2746 |
return Ok(()); |
| 2747 |
} |
| 2748 |
|
| 2749 |
if args.is_empty() { |
| 2750 |
cx.editor.registers.clear(); |
| 2751 |
cx.editor.set_status("All registers cleared"); |
| 2752 |
return Ok(()); |
| 2753 |
} |
| 2754 |
|
| 2755 |
ensure!( |
| 2756 |
args[0].chars().count() == 1, |
| 2757 |
format!("Invalid register {}", &args[0]) |
| 2758 |
); |
| 2759 |
let register = args[0].chars().next().unwrap_or_default(); |
| 2760 |
if cx.editor.registers.remove(register) { |
| 2761 |
cx.editor |
| 2762 |
.set_status(format!("Register {} cleared", register)); |
| 2763 |
} else { |
| 2764 |
cx.editor |
| 2765 |
.set_error(format!("Register {} not found", register)); |
| 2766 |
} |
| 2767 |
Ok(()) |
| 2768 |
} |
| 2769 |
|
| 2770 |
fn set_register( |
| 2771 |
cx: &mut compositor::Context, |
| 2772 |
args: Args, |
| 2773 |
event: PromptEvent, |
| 2774 |
) -> anyhow::Result<()> { |
| 2775 |
if event != PromptEvent::Validate { |
| 2776 |
return Ok(()); |
| 2777 |
} |
| 2778 |
|
| 2779 |
ensure!( |
| 2780 |
args[0].chars().count() == 1, |
| 2781 |
format!("Invalid register {}", &args[0]) |
| 2782 |
); |
| 2783 |
|
| 2784 |
let register = args[0].chars().next().unwrap_or_default(); |
| 2785 |
cx.editor.registers.write(register, vec![args[1].into()]) |
| 2786 |
} |
| 2787 |
|
| 2788 |
fn redraw(cx: &mut compositor::Context, _args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2789 |
if event != PromptEvent::Validate { |
| 2790 |
return Ok(()); |
| 2791 |
} |
| 2792 |
|
| 2793 |
let callback = Box::pin(async move { |
| 2794 |
let call: job::Callback = |
| 2795 |
job::Callback::EditorCompositor(Box::new(|_editor, compositor| { |
| 2796 |
compositor.need_full_redraw(); |
| 2797 |
})); |
| 2798 |
|
| 2799 |
Ok(call) |
| 2800 |
}); |
| 2801 |
|
| 2802 |
cx.jobs.callback(callback); |
| 2803 |
|
| 2804 |
Ok(()) |
| 2805 |
} |
| 2806 |
|
| 2807 |
#[derive(Debug, Clone, Copy)] |
| 2808 |
pub struct MoveBufferOptions { |
| 2809 |
pub force: bool, |
| 2810 |
} |
| 2811 |
|
| 2812 |
fn move_buffer(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2813 |
if event != PromptEvent::Validate { |
| 2814 |
return Ok(()); |
| 2815 |
} |
| 2816 |
|
| 2817 |
let new_path: PathBuf = args.first().unwrap().into(); |
| 2818 |
move_buffer_impl(cx, new_path, MoveBufferOptions { force: false }) |
| 2819 |
} |
| 2820 |
|
| 2821 |
fn force_move_buffer( |
| 2822 |
cx: &mut compositor::Context, |
| 2823 |
args: Args, |
| 2824 |
event: PromptEvent, |
| 2825 |
) -> anyhow::Result<()> { |
| 2826 |
if event != PromptEvent::Validate { |
| 2827 |
return Ok(()); |
| 2828 |
} |
| 2829 |
|
| 2830 |
let new_path: PathBuf = args.first().unwrap().into(); |
| 2831 |
move_buffer_impl(cx, new_path, MoveBufferOptions { force: true }) |
| 2832 |
} |
| 2833 |
|
| 2834 |
fn move_buffer_impl( |
| 2835 |
cx: &mut compositor::Context, |
| 2836 |
new_path: PathBuf, |
| 2837 |
options: MoveBufferOptions, |
| 2838 |
) -> anyhow::Result<()> { |
| 2839 |
let doc = doc!(cx.editor); |
| 2840 |
let old_path = doc |
| 2841 |
.path() |
| 2842 |
.map(ToOwned::to_owned) |
| 2843 |
.context("Scratch buffer cannot be moved. Use :write instead")?; |
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
let new_path = old_path |
| 2848 |
.file_name() |
| 2849 |
.filter(|_| new_path.is_dir()) |
| 2850 |
.map(|old_file_name| new_path.join(old_file_name)) |
| 2851 |
.unwrap_or(new_path); |
| 2852 |
|
| 2853 |
if old_path.exists() { |
| 2854 |
if let Some(parent) = new_path.parent() { |
| 2855 |
if !parent.exists() { |
| 2856 |
if options.force { |
| 2857 |
std::fs::DirBuilder::new().recursive(true).create(parent)?; |
| 2858 |
} else { |
| 2859 |
bail!( |
| 2860 |
"can't move file, parent directory does not exist (use :mv! to create it)" |
| 2861 |
) |
| 2862 |
} |
| 2863 |
} |
| 2864 |
} |
| 2865 |
} |
| 2866 |
|
| 2867 |
if let Err(err) = cx.editor.move_path(&old_path, new_path.as_ref()) { |
| 2868 |
bail!("Could not move file: {err}"); |
| 2869 |
} |
| 2870 |
Ok(()) |
| 2871 |
} |
| 2872 |
|
| 2873 |
fn yank_diagnostic( |
| 2874 |
cx: &mut compositor::Context, |
| 2875 |
args: Args, |
| 2876 |
event: PromptEvent, |
| 2877 |
) -> anyhow::Result<()> { |
| 2878 |
if event != PromptEvent::Validate { |
| 2879 |
return Ok(()); |
| 2880 |
} |
| 2881 |
|
| 2882 |
let reg = match args.first() { |
| 2883 |
Some(s) => { |
| 2884 |
ensure!(s.chars().count() == 1, format!("Invalid register {s}")); |
| 2885 |
s.chars().next().unwrap() |
| 2886 |
} |
| 2887 |
None => '+', |
| 2888 |
}; |
| 2889 |
|
| 2890 |
let (view, doc) = current_ref!(cx.editor); |
| 2891 |
let primary = doc.selection(view.id).primary(); |
| 2892 |
|
| 2893 |
|
| 2894 |
let diag: Vec<_> = doc |
| 2895 |
.diagnostics() |
| 2896 |
.iter() |
| 2897 |
.filter(|d| primary.overlaps(&helix_core::Range::new(d.range.start, d.range.end))) |
| 2898 |
.map(|d| d.message.clone()) |
| 2899 |
.collect(); |
| 2900 |
let n = diag.len(); |
| 2901 |
if n == 0 { |
| 2902 |
bail!("No diagnostics under primary selection"); |
| 2903 |
} |
| 2904 |
|
| 2905 |
cx.editor.registers.write(reg, diag)?; |
| 2906 |
cx.editor.set_status(format!( |
| 2907 |
"Yanked {n} diagnostic{} to register {reg}", |
| 2908 |
if n == 1 { "" } else { "s" } |
| 2909 |
)); |
| 2910 |
Ok(()) |
| 2911 |
} |
| 2912 |
|
| 2913 |
fn read(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2914 |
if event != PromptEvent::Validate { |
| 2915 |
return Ok(()); |
| 2916 |
} |
| 2917 |
|
| 2918 |
let scrolloff = cx.editor.config().scrolloff; |
| 2919 |
let (view, doc) = current!(cx.editor); |
| 2920 |
|
| 2921 |
let filename = args.first().unwrap(); |
| 2922 |
let path = helix_stdx::path::expand_tilde(PathBuf::from(filename.to_string())); |
| 2923 |
|
| 2924 |
ensure!( |
| 2925 |
path.exists() && path.is_file(), |
| 2926 |
"path is not a file: {:?}", |
| 2927 |
path |
| 2928 |
); |
| 2929 |
|
| 2930 |
let file = std::fs::File::open(path).map_err(|err| anyhow!("error opening file: {}", err))?; |
| 2931 |
let mut reader = BufReader::new(file); |
| 2932 |
let (contents, _, _) = read_to_string(&mut reader, Some(doc.encoding())) |
| 2933 |
.map_err(|err| anyhow!("error reading file: {}", err))?; |
| 2934 |
let contents = Tendril::from(contents); |
| 2935 |
let selection = doc.selection(view.id); |
| 2936 |
let transaction = Transaction::insert(doc.text(), selection, contents); |
| 2937 |
doc.apply(&transaction, view.id); |
| 2938 |
doc.append_changes_to_history(view); |
| 2939 |
view.ensure_cursor_in_view(doc, scrolloff); |
| 2940 |
|
| 2941 |
Ok(()) |
| 2942 |
} |
| 2943 |
|
| 2944 |
fn echo(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { |
| 2945 |
if event != PromptEvent::Validate { |
| 2946 |
return Ok(()); |
| 2947 |
} |
| 2948 |
|
| 2949 |
let output = args.into_iter().fold(String::new(), |mut acc, arg| { |
| 2950 |
if !acc.is_empty() { |
| 2951 |
acc.push(' '); |
| 2952 |
} |
| 2953 |
acc.push_str(&arg); |
| 2954 |
acc |
| 2955 |
}); |
| 2956 |
cx.editor.set_status(output); |
| 2957 |
|
| 2958 |
Ok(()) |
| 2959 |
} |
| 2960 |
|
| 2961 |
fn noop(_cx: &mut compositor::Context, _args: Args, _event: PromptEvent) -> anyhow::Result<()> { |
| 2962 |
Ok(()) |
| 2963 |
} |
| 2964 |
|
| 2965 |
|
| 2966 |
const BUFFER_CLOSE_OTHERS_SIGNATURE: Signature = Signature { |
| 2967 |
positionals: (0, Some(0)), |
| 2968 |
flags: &[Flag { |
| 2969 |
name: "skip-visible", |
| 2970 |
alias: Some('s'), |
| 2971 |
doc: "don't close buffers that are visible", |
| 2972 |
..Flag::DEFAULT |
| 2973 |
}], |
| 2974 |
..Signature::DEFAULT |
| 2975 |
}; |
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
pub const SHELL_SIGNATURE: Signature = Signature { |
| 2982 |
positionals: (1, Some(2)), |
| 2983 |
raw_after: Some(1), |
| 2984 |
..Signature::DEFAULT |
| 2985 |
}; |
| 2986 |
|
| 2987 |
pub const SHELL_COMPLETER: CommandCompleter = CommandCompleter::positional(&[ |
| 2988 |
|
| 2989 |
completers::program, |
| 2990 |
|
| 2991 |
completers::repeating_filenames, |
| 2992 |
]); |
| 2993 |
|
| 2994 |
const WRITE_NO_FORMAT_FLAG: Flag = Flag { |
| 2995 |
name: "no-format", |
| 2996 |
doc: "skip auto-formatting", |
| 2997 |
..Flag::DEFAULT |
| 2998 |
}; |
| 2999 |
|
| 3000 |
const WRITE_NO_CODE_ACTIONS_FLAG: Flag = Flag { |
| 3001 |
name: "no-code-actions", |
| 3002 |
doc: "skip code actions on save", |
| 3003 |
..Flag::DEFAULT |
| 3004 |
}; |
| 3005 |
|
| 3006 |
pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ |
| 3007 |
TypableCommand { |
| 3008 |
name: "exit", |
| 3009 |
aliases: &["x", "xit"], |
| 3010 |
doc: "Write changes to disk if the buffer is modified and then quit. Accepts an optional path (:exit some/path.txt).", |
| 3011 |
fun: exit, |
| 3012 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3013 |
signature: Signature { |
| 3014 |
positionals: (0, Some(1)), |
| 3015 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3016 |
..Signature::DEFAULT |
| 3017 |
}, |
| 3018 |
}, |
| 3019 |
TypableCommand { |
| 3020 |
name: "exit!", |
| 3021 |
aliases: &["x!", "xit!"], |
| 3022 |
doc: "Force write changes to disk, creating necessary subdirectories, if the buffer is modified and then quit. Accepts an optional path (:exit! some/path.txt).", |
| 3023 |
fun: force_exit, |
| 3024 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3025 |
signature: Signature { |
| 3026 |
positionals: (0, Some(1)), |
| 3027 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3028 |
..Signature::DEFAULT |
| 3029 |
}, |
| 3030 |
}, |
| 3031 |
TypableCommand { |
| 3032 |
name: "quit", |
| 3033 |
aliases: &["q"], |
| 3034 |
doc: "Close the current view.", |
| 3035 |
fun: quit, |
| 3036 |
completer: CommandCompleter::none(), |
| 3037 |
signature: Signature { |
| 3038 |
positionals: (0, Some(0)), |
| 3039 |
..Signature::DEFAULT |
| 3040 |
}, |
| 3041 |
}, |
| 3042 |
TypableCommand { |
| 3043 |
name: "quit!", |
| 3044 |
aliases: &["q!"], |
| 3045 |
doc: "Force close the current view, ignoring unsaved changes.", |
| 3046 |
fun: force_quit, |
| 3047 |
completer: CommandCompleter::none(), |
| 3048 |
signature: Signature { |
| 3049 |
positionals: (0, Some(0)), |
| 3050 |
..Signature::DEFAULT |
| 3051 |
}, |
| 3052 |
}, |
| 3053 |
TypableCommand { |
| 3054 |
name: "open", |
| 3055 |
aliases: &["o", "edit", "e"], |
| 3056 |
doc: "Open a file from disk into the current view.", |
| 3057 |
fun: open, |
| 3058 |
completer: CommandCompleter::all(completers::filename), |
| 3059 |
signature: Signature { |
| 3060 |
positionals: (1, None), |
| 3061 |
..Signature::DEFAULT |
| 3062 |
}, |
| 3063 |
}, |
| 3064 |
TypableCommand { |
| 3065 |
name: "buffer-close", |
| 3066 |
aliases: &["bc", "bclose"], |
| 3067 |
doc: "Close the current buffer.", |
| 3068 |
fun: buffer_close, |
| 3069 |
completer: CommandCompleter::all(completers::buffer), |
| 3070 |
signature: Signature { |
| 3071 |
positionals: (0, None), |
| 3072 |
..Signature::DEFAULT |
| 3073 |
}, |
| 3074 |
}, |
| 3075 |
TypableCommand { |
| 3076 |
name: "buffer-close!", |
| 3077 |
aliases: &["bc!", "bclose!"], |
| 3078 |
doc: "Close the current buffer forcefully, ignoring unsaved changes.", |
| 3079 |
fun: force_buffer_close, |
| 3080 |
completer: CommandCompleter::all(completers::buffer), |
| 3081 |
signature: Signature { |
| 3082 |
positionals: (0, None), |
| 3083 |
..Signature::DEFAULT |
| 3084 |
}, |
| 3085 |
}, |
| 3086 |
TypableCommand { |
| 3087 |
name: "buffer-close-others", |
| 3088 |
aliases: &["bco", "bcloseother"], |
| 3089 |
doc: "Close all buffers but the currently focused one.", |
| 3090 |
fun: buffer_close_others, |
| 3091 |
completer: CommandCompleter::none(), |
| 3092 |
signature: BUFFER_CLOSE_OTHERS_SIGNATURE, |
| 3093 |
}, |
| 3094 |
TypableCommand { |
| 3095 |
name: "buffer-close-others!", |
| 3096 |
aliases: &["bco!", "bcloseother!"], |
| 3097 |
doc: "Force close all buffers but the currently focused one.", |
| 3098 |
fun: force_buffer_close_others, |
| 3099 |
completer: CommandCompleter::none(), |
| 3100 |
signature: BUFFER_CLOSE_OTHERS_SIGNATURE, |
| 3101 |
}, |
| 3102 |
TypableCommand { |
| 3103 |
name: "buffer-close-all", |
| 3104 |
aliases: &["bca", "bcloseall"], |
| 3105 |
doc: "Close all buffers without quitting.", |
| 3106 |
fun: buffer_close_all, |
| 3107 |
completer: CommandCompleter::none(), |
| 3108 |
signature: Signature { |
| 3109 |
positionals: (0, Some(0)), |
| 3110 |
..Signature::DEFAULT |
| 3111 |
}, |
| 3112 |
}, |
| 3113 |
TypableCommand { |
| 3114 |
name: "buffer-close-all!", |
| 3115 |
aliases: &["bca!", "bcloseall!"], |
| 3116 |
doc: "Force close all buffers ignoring unsaved changes without quitting.", |
| 3117 |
fun: force_buffer_close_all, |
| 3118 |
completer: CommandCompleter::none(), |
| 3119 |
signature: Signature { |
| 3120 |
positionals: (0, Some(0)), |
| 3121 |
..Signature::DEFAULT |
| 3122 |
}, |
| 3123 |
}, |
| 3124 |
TypableCommand { |
| 3125 |
name: "buffer-next", |
| 3126 |
aliases: &["bn", "bnext"], |
| 3127 |
doc: "Goto next buffer.", |
| 3128 |
fun: buffer_next, |
| 3129 |
completer: CommandCompleter::none(), |
| 3130 |
signature: Signature { |
| 3131 |
positionals: (0, Some(0)), |
| 3132 |
..Signature::DEFAULT |
| 3133 |
}, |
| 3134 |
}, |
| 3135 |
TypableCommand { |
| 3136 |
name: "buffer-previous", |
| 3137 |
aliases: &["bp", "bprev"], |
| 3138 |
doc: "Goto previous buffer.", |
| 3139 |
fun: buffer_previous, |
| 3140 |
completer: CommandCompleter::none(), |
| 3141 |
signature: Signature { |
| 3142 |
positionals: (0, Some(0)), |
| 3143 |
..Signature::DEFAULT |
| 3144 |
}, |
| 3145 |
}, |
| 3146 |
TypableCommand { |
| 3147 |
name: "write", |
| 3148 |
aliases: &["w"], |
| 3149 |
doc: "Write changes to disk. Accepts an optional path (:write some/path.txt)", |
| 3150 |
fun: write, |
| 3151 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3152 |
signature: Signature { |
| 3153 |
positionals: (0, Some(1)), |
| 3154 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3155 |
..Signature::DEFAULT |
| 3156 |
}, |
| 3157 |
}, |
| 3158 |
TypableCommand { |
| 3159 |
name: "write!", |
| 3160 |
aliases: &["w!"], |
| 3161 |
doc: "Force write changes to disk creating necessary subdirectories. Accepts an optional path (:write! some/path.txt)", |
| 3162 |
fun: force_write, |
| 3163 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3164 |
signature: Signature { |
| 3165 |
positionals: (0, Some(1)), |
| 3166 |
flags: &[WRITE_NO_FORMAT_FLAG,WRITE_NO_CODE_ACTIONS_FLAG], |
| 3167 |
..Signature::DEFAULT |
| 3168 |
}, |
| 3169 |
}, |
| 3170 |
TypableCommand { |
| 3171 |
name: "write-buffer-close", |
| 3172 |
aliases: &["wbc"], |
| 3173 |
doc: "Write changes to disk and closes the buffer. Accepts an optional path (:write-buffer-close some/path.txt)", |
| 3174 |
fun: write_buffer_close, |
| 3175 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3176 |
signature: Signature { |
| 3177 |
positionals: (0, Some(1)), |
| 3178 |
flags: &[WRITE_NO_FORMAT_FLAG,WRITE_NO_CODE_ACTIONS_FLAG], |
| 3179 |
..Signature::DEFAULT |
| 3180 |
}, |
| 3181 |
}, |
| 3182 |
TypableCommand { |
| 3183 |
name: "write-buffer-close!", |
| 3184 |
aliases: &["wbc!"], |
| 3185 |
doc: "Force write changes to disk creating necessary subdirectories and closes the buffer. Accepts an optional path (:write-buffer-close! some/path.txt)", |
| 3186 |
fun: force_write_buffer_close, |
| 3187 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3188 |
signature: Signature { |
| 3189 |
positionals: (0, Some(1)), |
| 3190 |
flags: &[WRITE_NO_FORMAT_FLAG,WRITE_NO_CODE_ACTIONS_FLAG], |
| 3191 |
..Signature::DEFAULT |
| 3192 |
}, |
| 3193 |
}, |
| 3194 |
TypableCommand { |
| 3195 |
name: "new", |
| 3196 |
aliases: &["n"], |
| 3197 |
doc: "Create a new scratch buffer.", |
| 3198 |
fun: new_file, |
| 3199 |
completer: CommandCompleter::none(), |
| 3200 |
signature: Signature { |
| 3201 |
positionals: (0, Some(0)), |
| 3202 |
..Signature::DEFAULT |
| 3203 |
}, |
| 3204 |
}, |
| 3205 |
TypableCommand { |
| 3206 |
name: "format", |
| 3207 |
aliases: &["fmt"], |
| 3208 |
doc: "Format the file using an external formatter or language server.", |
| 3209 |
fun: format, |
| 3210 |
completer: CommandCompleter::none(), |
| 3211 |
signature: Signature { |
| 3212 |
positionals: (0, Some(0)), |
| 3213 |
..Signature::DEFAULT |
| 3214 |
}, |
| 3215 |
}, |
| 3216 |
TypableCommand { |
| 3217 |
name: "indent-style", |
| 3218 |
aliases: &[], |
| 3219 |
doc: "Set the indentation style for editing. ('t' for tabs or 1-16 for number of spaces.)", |
| 3220 |
fun: set_indent_style, |
| 3221 |
completer: CommandCompleter::none(), |
| 3222 |
signature: Signature { |
| 3223 |
positionals: (0, Some(1)), |
| 3224 |
..Signature::DEFAULT |
| 3225 |
}, |
| 3226 |
}, |
| 3227 |
TypableCommand { |
| 3228 |
name: "line-ending", |
| 3229 |
aliases: &[], |
| 3230 |
#[cfg(not(feature = "unicode-lines"))] |
| 3231 |
doc: "Set the document's default line ending. Options: crlf, lf.", |
| 3232 |
#[cfg(feature = "unicode-lines")] |
| 3233 |
doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.", |
| 3234 |
fun: set_line_ending, |
| 3235 |
completer: CommandCompleter::none(), |
| 3236 |
signature: Signature { |
| 3237 |
positionals: (0, Some(1)), |
| 3238 |
..Signature::DEFAULT |
| 3239 |
}, |
| 3240 |
}, |
| 3241 |
TypableCommand { |
| 3242 |
name: "earlier", |
| 3243 |
aliases: &["ear"], |
| 3244 |
doc: "Jump back to an earlier point in edit history. Accepts a number of steps or a time span.", |
| 3245 |
fun: earlier, |
| 3246 |
completer: CommandCompleter::none(), |
| 3247 |
signature: Signature { |
| 3248 |
positionals: (0, Some(1)), |
| 3249 |
..Signature::DEFAULT |
| 3250 |
}, |
| 3251 |
}, |
| 3252 |
TypableCommand { |
| 3253 |
name: "later", |
| 3254 |
aliases: &["lat"], |
| 3255 |
doc: "Jump to a later point in edit history. Accepts a number of steps or a time span.", |
| 3256 |
fun: later, |
| 3257 |
completer: CommandCompleter::none(), |
| 3258 |
signature: Signature { |
| 3259 |
positionals: (0, Some(1)), |
| 3260 |
..Signature::DEFAULT |
| 3261 |
}, |
| 3262 |
}, |
| 3263 |
TypableCommand { |
| 3264 |
name: "write-quit", |
| 3265 |
aliases: &["wq"], |
| 3266 |
doc: "Write changes to disk and close the current view. Accepts an optional path (:wq some/path.txt)", |
| 3267 |
fun: write_quit, |
| 3268 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3269 |
signature: Signature { |
| 3270 |
positionals: (0, Some(1)), |
| 3271 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3272 |
..Signature::DEFAULT |
| 3273 |
}, |
| 3274 |
}, |
| 3275 |
TypableCommand { |
| 3276 |
name: "write-quit!", |
| 3277 |
aliases: &["wq!"], |
| 3278 |
doc: "Write changes to disk and close the current view forcefully. Accepts an optional path (:wq! some/path.txt)", |
| 3279 |
fun: force_write_quit, |
| 3280 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 3281 |
signature: Signature { |
| 3282 |
positionals: (0, Some(1)), |
| 3283 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3284 |
..Signature::DEFAULT |
| 3285 |
}, |
| 3286 |
}, |
| 3287 |
TypableCommand { |
| 3288 |
name: "write-all", |
| 3289 |
aliases: &["wa"], |
| 3290 |
doc: "Write changes from all buffers to disk.", |
| 3291 |
fun: write_all, |
| 3292 |
completer: CommandCompleter::none(), |
| 3293 |
signature: Signature { |
| 3294 |
positionals: (0, Some(0)), |
| 3295 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3296 |
..Signature::DEFAULT |
| 3297 |
}, |
| 3298 |
}, |
| 3299 |
TypableCommand { |
| 3300 |
name: "write-all!", |
| 3301 |
aliases: &["wa!"], |
| 3302 |
doc: "Forcefully write changes from all buffers to disk creating necessary subdirectories.", |
| 3303 |
fun: force_write_all, |
| 3304 |
completer: CommandCompleter::none(), |
| 3305 |
signature: Signature { |
| 3306 |
positionals: (0, Some(0)), |
| 3307 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3308 |
..Signature::DEFAULT |
| 3309 |
}, |
| 3310 |
}, |
| 3311 |
TypableCommand { |
| 3312 |
name: "write-quit-all", |
| 3313 |
aliases: &["wqa", "xa"], |
| 3314 |
doc: "Write changes from all buffers to disk and close all views.", |
| 3315 |
fun: write_all_quit, |
| 3316 |
completer: CommandCompleter::none(), |
| 3317 |
signature: Signature { |
| 3318 |
positionals: (0, Some(0)), |
| 3319 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3320 |
..Signature::DEFAULT |
| 3321 |
}, |
| 3322 |
}, |
| 3323 |
TypableCommand { |
| 3324 |
name: "write-quit-all!", |
| 3325 |
aliases: &["wqa!", "xa!"], |
| 3326 |
doc: "Forcefully write changes from all buffers to disk, creating necessary subdirectories, and close all views (ignoring unsaved changes).", |
| 3327 |
fun: force_write_all_quit, |
| 3328 |
completer: CommandCompleter::none(), |
| 3329 |
signature: Signature { |
| 3330 |
positionals: (0, Some(0)), |
| 3331 |
flags: &[WRITE_NO_FORMAT_FLAG, WRITE_NO_CODE_ACTIONS_FLAG], |
| 3332 |
..Signature::DEFAULT |
| 3333 |
}, |
| 3334 |
}, |
| 3335 |
TypableCommand { |
| 3336 |
name: "quit-all", |
| 3337 |
aliases: &["qa"], |
| 3338 |
doc: "Close all views.", |
| 3339 |
fun: quit_all, |
| 3340 |
completer: CommandCompleter::none(), |
| 3341 |
signature: Signature { |
| 3342 |
positionals: (0, Some(0)), |
| 3343 |
..Signature::DEFAULT |
| 3344 |
}, |
| 3345 |
}, |
| 3346 |
TypableCommand { |
| 3347 |
name: "quit-all!", |
| 3348 |
aliases: &["qa!"], |
| 3349 |
doc: "Force close all views ignoring unsaved changes.", |
| 3350 |
fun: force_quit_all, |
| 3351 |
completer: CommandCompleter::none(), |
| 3352 |
signature: Signature { |
| 3353 |
positionals: (0, Some(0)), |
| 3354 |
..Signature::DEFAULT |
| 3355 |
}, |
| 3356 |
}, |
| 3357 |
TypableCommand { |
| 3358 |
name: "cquit", |
| 3359 |
aliases: &["cq"], |
| 3360 |
doc: "Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2).", |
| 3361 |
fun: cquit, |
| 3362 |
completer: CommandCompleter::none(), |
| 3363 |
signature: Signature { |
| 3364 |
positionals: (0, Some(1)), |
| 3365 |
..Signature::DEFAULT |
| 3366 |
}, |
| 3367 |
}, |
| 3368 |
TypableCommand { |
| 3369 |
name: "cquit!", |
| 3370 |
aliases: &["cq!"], |
| 3371 |
doc: "Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2).", |
| 3372 |
fun: force_cquit, |
| 3373 |
completer: CommandCompleter::none(), |
| 3374 |
signature: Signature { |
| 3375 |
positionals: (0, Some(1)), |
| 3376 |
..Signature::DEFAULT |
| 3377 |
}, |
| 3378 |
}, |
| 3379 |
TypableCommand { |
| 3380 |
name: "theme", |
| 3381 |
aliases: &[], |
| 3382 |
doc: "Change the editor theme (show current theme if no name specified).", |
| 3383 |
fun: theme, |
| 3384 |
completer: CommandCompleter::positional(&[completers::theme]), |
| 3385 |
signature: Signature { |
| 3386 |
positionals: (0, Some(1)), |
| 3387 |
..Signature::DEFAULT |
| 3388 |
}, |
| 3389 |
}, |
| 3390 |
TypableCommand { |
| 3391 |
name: "yank-join", |
| 3392 |
aliases: &[], |
| 3393 |
doc: "Yank joined selections. A separator can be provided as first argument. Default value is newline.", |
| 3394 |
fun: yank_joined, |
| 3395 |
completer: CommandCompleter::none(), |
| 3396 |
signature: Signature { |
| 3397 |
positionals: (0, Some(1)), |
| 3398 |
..Signature::DEFAULT |
| 3399 |
}, |
| 3400 |
}, |
| 3401 |
TypableCommand { |
| 3402 |
name: "clipboard-yank", |
| 3403 |
aliases: &[], |
| 3404 |
doc: "Yank main selection into system clipboard.", |
| 3405 |
fun: yank_main_selection_to_clipboard, |
| 3406 |
completer: CommandCompleter::none(), |
| 3407 |
signature: Signature { |
| 3408 |
positionals: (0, Some(0)), |
| 3409 |
..Signature::DEFAULT |
| 3410 |
}, |
| 3411 |
}, |
| 3412 |
TypableCommand { |
| 3413 |
name: "clipboard-yank-join", |
| 3414 |
aliases: &[], |
| 3415 |
doc: "Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline.", |
| 3416 |
fun: yank_joined_to_clipboard, |
| 3417 |
completer: CommandCompleter::none(), |
| 3418 |
signature: Signature { |
| 3419 |
positionals: (0, Some(1)), |
| 3420 |
..Signature::DEFAULT |
| 3421 |
}, |
| 3422 |
}, |
| 3423 |
TypableCommand { |
| 3424 |
name: "primary-clipboard-yank", |
| 3425 |
aliases: &[], |
| 3426 |
doc: "Yank main selection into system primary clipboard.", |
| 3427 |
fun: yank_main_selection_to_primary_clipboard, |
| 3428 |
completer: CommandCompleter::none(), |
| 3429 |
signature: Signature { |
| 3430 |
positionals: (0, Some(0)), |
| 3431 |
..Signature::DEFAULT |
| 3432 |
}, |
| 3433 |
}, |
| 3434 |
TypableCommand { |
| 3435 |
name: "primary-clipboard-yank-join", |
| 3436 |
aliases: &[], |
| 3437 |
doc: "Yank joined selections into system primary clipboard. A separator can be provided as first argument. Default value is newline.", |
| 3438 |
fun: yank_joined_to_primary_clipboard, |
| 3439 |
completer: CommandCompleter::none(), |
| 3440 |
signature: Signature { |
| 3441 |
positionals: (0, Some(1)), |
| 3442 |
..Signature::DEFAULT |
| 3443 |
}, |
| 3444 |
}, |
| 3445 |
TypableCommand { |
| 3446 |
name: "clipboard-paste-after", |
| 3447 |
aliases: &[], |
| 3448 |
doc: "Paste system clipboard after selections.", |
| 3449 |
fun: paste_clipboard_after, |
| 3450 |
completer: CommandCompleter::none(), |
| 3451 |
signature: Signature { |
| 3452 |
positionals: (0, Some(0)), |
| 3453 |
..Signature::DEFAULT |
| 3454 |
}, |
| 3455 |
}, |
| 3456 |
TypableCommand { |
| 3457 |
name: "clipboard-paste-before", |
| 3458 |
aliases: &[], |
| 3459 |
doc: "Paste system clipboard before selections.", |
| 3460 |
fun: paste_clipboard_before, |
| 3461 |
completer: CommandCompleter::none(), |
| 3462 |
signature: Signature { |
| 3463 |
positionals: (0, Some(0)), |
| 3464 |
..Signature::DEFAULT |
| 3465 |
}, |
| 3466 |
}, |
| 3467 |
TypableCommand { |
| 3468 |
name: "clipboard-paste-replace", |
| 3469 |
aliases: &[], |
| 3470 |
doc: "Replace selections with content of system clipboard.", |
| 3471 |
fun: replace_selections_with_clipboard, |
| 3472 |
completer: CommandCompleter::none(), |
| 3473 |
signature: Signature { |
| 3474 |
positionals: (0, Some(0)), |
| 3475 |
..Signature::DEFAULT |
| 3476 |
}, |
| 3477 |
}, |
| 3478 |
TypableCommand { |
| 3479 |
name: "primary-clipboard-paste-after", |
| 3480 |
aliases: &[], |
| 3481 |
doc: "Paste primary clipboard after selections.", |
| 3482 |
fun: paste_primary_clipboard_after, |
| 3483 |
completer: CommandCompleter::none(), |
| 3484 |
signature: Signature { |
| 3485 |
positionals: (0, Some(0)), |
| 3486 |
..Signature::DEFAULT |
| 3487 |
}, |
| 3488 |
}, |
| 3489 |
TypableCommand { |
| 3490 |
name: "primary-clipboard-paste-before", |
| 3491 |
aliases: &[], |
| 3492 |
doc: "Paste primary clipboard before selections.", |
| 3493 |
fun: paste_primary_clipboard_before, |
| 3494 |
completer: CommandCompleter::none(), |
| 3495 |
signature: Signature { |
| 3496 |
positionals: (0, Some(0)), |
| 3497 |
..Signature::DEFAULT |
| 3498 |
}, |
| 3499 |
}, |
| 3500 |
TypableCommand { |
| 3501 |
name: "primary-clipboard-paste-replace", |
| 3502 |
aliases: &[], |
| 3503 |
doc: "Replace selections with content of system primary clipboard.", |
| 3504 |
fun: replace_selections_with_primary_clipboard, |
| 3505 |
completer: CommandCompleter::none(), |
| 3506 |
signature: Signature { |
| 3507 |
positionals: (0, Some(0)), |
| 3508 |
..Signature::DEFAULT |
| 3509 |
}, |
| 3510 |
}, |
| 3511 |
TypableCommand { |
| 3512 |
name: "show-clipboard-provider", |
| 3513 |
aliases: &[], |
| 3514 |
doc: "Show clipboard provider name in status bar.", |
| 3515 |
fun: show_clipboard_provider, |
| 3516 |
completer: CommandCompleter::none(), |
| 3517 |
signature: Signature { |
| 3518 |
positionals: (0, Some(0)), |
| 3519 |
..Signature::DEFAULT |
| 3520 |
}, |
| 3521 |
}, |
| 3522 |
TypableCommand { |
| 3523 |
name: "change-current-directory", |
| 3524 |
aliases: &["cd"], |
| 3525 |
doc: "Change the current working directory.", |
| 3526 |
fun: change_current_directory, |
| 3527 |
completer: CommandCompleter::positional(&[completers::directory]), |
| 3528 |
signature: Signature { |
| 3529 |
positionals: (1, Some(1)), |
| 3530 |
..Signature::DEFAULT |
| 3531 |
}, |
| 3532 |
}, |
| 3533 |
TypableCommand { |
| 3534 |
name: "show-directory-stack", |
| 3535 |
aliases: &[], |
| 3536 |
doc: "Show the directory stack as a <space> delimited string.", |
| 3537 |
fun: show_directory_stack, |
| 3538 |
completer: CommandCompleter::none(), |
| 3539 |
signature: Signature { |
| 3540 |
positionals: (0, Some(0)), |
| 3541 |
..Signature::DEFAULT |
| 3542 |
}, |
| 3543 |
}, |
| 3544 |
TypableCommand { |
| 3545 |
name: "push-directory", |
| 3546 |
aliases: &["pushd"], |
| 3547 |
doc: "Save and then change the current directory.", |
| 3548 |
fun: push_directory, |
| 3549 |
completer: CommandCompleter::positional(&[completers::directory]), |
| 3550 |
signature: Signature { |
| 3551 |
positionals: (1, Some(1)), |
| 3552 |
..Signature::DEFAULT |
| 3553 |
}, |
| 3554 |
}, |
| 3555 |
TypableCommand { |
| 3556 |
name: "pop-directory", |
| 3557 |
aliases: &["popd"], |
| 3558 |
doc: "Remove the top entry from the directory stack, and cd to the new top directory..", |
| 3559 |
fun: pop_directory, |
| 3560 |
completer: CommandCompleter::none(), |
| 3561 |
signature: Signature { |
| 3562 |
positionals: (0, Some(0)), |
| 3563 |
..Signature::DEFAULT |
| 3564 |
}, |
| 3565 |
}, |
| 3566 |
TypableCommand { |
| 3567 |
name: "show-directory", |
| 3568 |
aliases: &["pwd"], |
| 3569 |
doc: "Show the current working directory.", |
| 3570 |
fun: show_current_directory, |
| 3571 |
completer: CommandCompleter::none(), |
| 3572 |
signature: Signature { |
| 3573 |
positionals: (0, Some(0)), |
| 3574 |
..Signature::DEFAULT |
| 3575 |
}, |
| 3576 |
}, |
| 3577 |
TypableCommand { |
| 3578 |
name: "encoding", |
| 3579 |
aliases: &[], |
| 3580 |
doc: "Set encoding. Based on `https://encoding.spec.whatwg.org`.", |
| 3581 |
fun: set_encoding, |
| 3582 |
completer: CommandCompleter::none(), |
| 3583 |
signature: Signature { |
| 3584 |
positionals: (0, Some(1)), |
| 3585 |
..Signature::DEFAULT |
| 3586 |
}, |
| 3587 |
}, |
| 3588 |
TypableCommand { |
| 3589 |
name: "character-info", |
| 3590 |
aliases: &["char"], |
| 3591 |
doc: "Get info about the character under the primary cursor.", |
| 3592 |
fun: get_character_info, |
| 3593 |
completer: CommandCompleter::none(), |
| 3594 |
signature: Signature { |
| 3595 |
positionals: (0, Some(0)), |
| 3596 |
..Signature::DEFAULT |
| 3597 |
}, |
| 3598 |
}, |
| 3599 |
TypableCommand { |
| 3600 |
name: "reload", |
| 3601 |
aliases: &["rl"], |
| 3602 |
doc: "Discard changes and reload from the source file.", |
| 3603 |
fun: reload, |
| 3604 |
completer: CommandCompleter::none(), |
| 3605 |
signature: Signature { |
| 3606 |
positionals: (0, Some(0)), |
| 3607 |
..Signature::DEFAULT |
| 3608 |
}, |
| 3609 |
}, |
| 3610 |
TypableCommand { |
| 3611 |
name: "reload-all", |
| 3612 |
aliases: &["rla"], |
| 3613 |
doc: "Discard changes and reload all documents from the source files.", |
| 3614 |
fun: reload_all, |
| 3615 |
completer: CommandCompleter::none(), |
| 3616 |
signature: Signature { |
| 3617 |
positionals: (0, Some(0)), |
| 3618 |
..Signature::DEFAULT |
| 3619 |
}, |
| 3620 |
}, |
| 3621 |
TypableCommand { |
| 3622 |
name: "update", |
| 3623 |
aliases: &["u"], |
| 3624 |
doc: "Write changes only if the file has been modified.", |
| 3625 |
fun: update, |
| 3626 |
completer: CommandCompleter::none(), |
| 3627 |
signature: Signature { |
| 3628 |
positionals: (0, Some(0)), |
| 3629 |
flags: &[WRITE_NO_FORMAT_FLAG], |
| 3630 |
..Signature::DEFAULT |
| 3631 |
}, |
| 3632 |
}, |
| 3633 |
TypableCommand { |
| 3634 |
name: "lsp-workspace-command", |
| 3635 |
aliases: &[], |
| 3636 |
doc: "Open workspace command picker", |
| 3637 |
fun: lsp_workspace_command, |
| 3638 |
completer: CommandCompleter::positional(&[completers::lsp_workspace_command]), |
| 3639 |
signature: Signature { |
| 3640 |
positionals: (0, None), |
| 3641 |
raw_after: Some(1), |
| 3642 |
..Signature::DEFAULT |
| 3643 |
}, |
| 3644 |
}, |
| 3645 |
TypableCommand { |
| 3646 |
name: "lsp-restart", |
| 3647 |
aliases: &[], |
| 3648 |
doc: "Restarts the given language servers, or all language servers that are used by the current file if no arguments are supplied", |
| 3649 |
fun: lsp_restart, |
| 3650 |
completer: CommandCompleter::all(completers::configured_language_servers), |
| 3651 |
signature: Signature { |
| 3652 |
positionals: (0, None), |
| 3653 |
..Signature::DEFAULT |
| 3654 |
}, |
| 3655 |
}, |
| 3656 |
TypableCommand { |
| 3657 |
name: "lsp-stop", |
| 3658 |
aliases: &[], |
| 3659 |
doc: "Stops the given language servers, or all language servers that are used by the current file if no arguments are supplied", |
| 3660 |
fun: lsp_stop, |
| 3661 |
completer: CommandCompleter::all(completers::active_language_servers), |
| 3662 |
signature: Signature { |
| 3663 |
positionals: (0, None), |
| 3664 |
..Signature::DEFAULT |
| 3665 |
}, |
| 3666 |
}, |
| 3667 |
TypableCommand { |
| 3668 |
name: "tree-sitter-scopes", |
| 3669 |
aliases: &[], |
| 3670 |
doc: "Display tree sitter scopes, primarily for theming and development.", |
| 3671 |
fun: tree_sitter_scopes, |
| 3672 |
completer: CommandCompleter::none(), |
| 3673 |
signature: Signature { |
| 3674 |
positionals: (0, Some(0)), |
| 3675 |
..Signature::DEFAULT |
| 3676 |
}, |
| 3677 |
}, |
| 3678 |
TypableCommand { |
| 3679 |
name: "tree-sitter-highlight-name", |
| 3680 |
aliases: &[], |
| 3681 |
doc: "Display name of tree-sitter highlight scope under the cursor.", |
| 3682 |
fun: tree_sitter_highlight_name, |
| 3683 |
completer: CommandCompleter::none(), |
| 3684 |
signature: Signature { |
| 3685 |
positionals: (0, Some(0)), |
| 3686 |
..Signature::DEFAULT |
| 3687 |
}, |
| 3688 |
}, |
| 3689 |
TypableCommand { |
| 3690 |
name: "tree-sitter-layers", |
| 3691 |
aliases: &[], |
| 3692 |
doc: "Display language names of tree-sitter injection layers under the cursor.", |
| 3693 |
fun: tree_sitter_layers, |
| 3694 |
completer: CommandCompleter::none(), |
| 3695 |
signature: Signature { |
| 3696 |
positionals: (0, Some(0)), |
| 3697 |
..Signature::DEFAULT |
| 3698 |
}, |
| 3699 |
}, |
| 3700 |
TypableCommand { |
| 3701 |
name: "debug-start", |
| 3702 |
aliases: &["dbg"], |
| 3703 |
doc: "Start a debug session from a given template with given parameters.", |
| 3704 |
fun: debug_start, |
| 3705 |
completer: CommandCompleter::none(), |
| 3706 |
signature: Signature { |
| 3707 |
positionals: (0, None), |
| 3708 |
..Signature::DEFAULT |
| 3709 |
}, |
| 3710 |
}, |
| 3711 |
TypableCommand { |
| 3712 |
name: "debug-remote", |
| 3713 |
aliases: &["dbg-tcp"], |
| 3714 |
doc: "Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters.", |
| 3715 |
fun: debug_remote, |
| 3716 |
completer: CommandCompleter::none(), |
| 3717 |
signature: Signature { |
| 3718 |
positionals: (0, None), |
| 3719 |
..Signature::DEFAULT |
| 3720 |
}, |
| 3721 |
}, |
| 3722 |
TypableCommand { |
| 3723 |
name: "debug-eval", |
| 3724 |
aliases: &[], |
| 3725 |
doc: "Evaluate expression in current debug context.", |
| 3726 |
fun: debug_eval, |
| 3727 |
completer: CommandCompleter::none(), |
| 3728 |
signature: Signature { |
| 3729 |
positionals: (1, Some(1)), |
| 3730 |
..Signature::DEFAULT |
| 3731 |
}, |
| 3732 |
}, |
| 3733 |
TypableCommand { |
| 3734 |
name: "vsplit", |
| 3735 |
aliases: &["vs"], |
| 3736 |
doc: "Open the file in a vertical split.", |
| 3737 |
fun: vsplit, |
| 3738 |
completer: CommandCompleter::all(completers::filename), |
| 3739 |
signature: Signature { |
| 3740 |
positionals: (0, None), |
| 3741 |
..Signature::DEFAULT |
| 3742 |
}, |
| 3743 |
}, |
| 3744 |
TypableCommand { |
| 3745 |
name: "vsplit-new", |
| 3746 |
aliases: &["vnew"], |
| 3747 |
doc: "Open a scratch buffer in a vertical split.", |
| 3748 |
fun: vsplit_new, |
| 3749 |
completer: CommandCompleter::none(), |
| 3750 |
signature: Signature { |
| 3751 |
positionals: (0, Some(0)), |
| 3752 |
..Signature::DEFAULT |
| 3753 |
}, |
| 3754 |
}, |
| 3755 |
TypableCommand { |
| 3756 |
name: "hsplit", |
| 3757 |
aliases: &["hs", "sp"], |
| 3758 |
doc: "Open the file in a horizontal split.", |
| 3759 |
fun: hsplit, |
| 3760 |
completer: CommandCompleter::all(completers::filename), |
| 3761 |
signature: Signature { |
| 3762 |
positionals: (0, None), |
| 3763 |
..Signature::DEFAULT |
| 3764 |
}, |
| 3765 |
}, |
| 3766 |
TypableCommand { |
| 3767 |
name: "hsplit-new", |
| 3768 |
aliases: &["hnew"], |
| 3769 |
doc: "Open a scratch buffer in a horizontal split.", |
| 3770 |
fun: hsplit_new, |
| 3771 |
completer: CommandCompleter::none(), |
| 3772 |
signature: Signature { |
| 3773 |
positionals: (0, Some(0)), |
| 3774 |
..Signature::DEFAULT |
| 3775 |
}, |
| 3776 |
}, |
| 3777 |
TypableCommand { |
| 3778 |
name: "tutor", |
| 3779 |
aliases: &[], |
| 3780 |
doc: "Open the tutorial.", |
| 3781 |
fun: tutor, |
| 3782 |
completer: CommandCompleter::none(), |
| 3783 |
signature: Signature { |
| 3784 |
positionals: (0, Some(0)), |
| 3785 |
..Signature::DEFAULT |
| 3786 |
}, |
| 3787 |
}, |
| 3788 |
TypableCommand { |
| 3789 |
name: "goto", |
| 3790 |
aliases: &["g"], |
| 3791 |
doc: "Goto line number.", |
| 3792 |
fun: goto_line_number, |
| 3793 |
completer: CommandCompleter::none(), |
| 3794 |
signature: Signature { |
| 3795 |
positionals: (1, Some(1)), |
| 3796 |
..Signature::DEFAULT |
| 3797 |
}, |
| 3798 |
}, |
| 3799 |
TypableCommand { |
| 3800 |
name: "set-language", |
| 3801 |
aliases: &["lang"], |
| 3802 |
doc: "Set the language of current buffer (show current language if no value specified).", |
| 3803 |
fun: language, |
| 3804 |
completer: CommandCompleter::positional(&[completers::language]), |
| 3805 |
signature: Signature { |
| 3806 |
positionals: (0, Some(1)), |
| 3807 |
..Signature::DEFAULT |
| 3808 |
}, |
| 3809 |
}, |
| 3810 |
TypableCommand { |
| 3811 |
name: "set-option", |
| 3812 |
aliases: &["set"], |
| 3813 |
doc: "Set a config option at runtime.\nFor example to disable smart case search, use `:set search.smart-case false`.", |
| 3814 |
fun: set_option, |
| 3815 |
|
| 3816 |
completer: CommandCompleter::positional(&[completers::setting]), |
| 3817 |
signature: Signature { |
| 3818 |
positionals: (2, Some(2)), |
| 3819 |
raw_after: Some(1), |
| 3820 |
..Signature::DEFAULT |
| 3821 |
}, |
| 3822 |
}, |
| 3823 |
TypableCommand { |
| 3824 |
name: "toggle-option", |
| 3825 |
aliases: &["toggle"], |
| 3826 |
doc: "Toggle a config option at runtime.\nFor example to toggle smart case search, use `:toggle search.smart-case`.", |
| 3827 |
fun: toggle_option, |
| 3828 |
completer: CommandCompleter::positional(&[completers::setting]), |
| 3829 |
signature: Signature { |
| 3830 |
positionals: (1, None), |
| 3831 |
raw_after: Some(1), |
| 3832 |
..Signature::DEFAULT |
| 3833 |
}, |
| 3834 |
}, |
| 3835 |
TypableCommand { |
| 3836 |
name: "get-option", |
| 3837 |
aliases: &["get"], |
| 3838 |
doc: "Get the current value of a config option.", |
| 3839 |
fun: get_option, |
| 3840 |
completer: CommandCompleter::positional(&[completers::setting]), |
| 3841 |
signature: Signature { |
| 3842 |
positionals: (1, Some(1)), |
| 3843 |
..Signature::DEFAULT |
| 3844 |
}, |
| 3845 |
}, |
| 3846 |
TypableCommand { |
| 3847 |
name: "sort", |
| 3848 |
aliases: &[], |
| 3849 |
doc: "Sort ranges in selection.", |
| 3850 |
fun: sort, |
| 3851 |
completer: CommandCompleter::none(), |
| 3852 |
signature: Signature { |
| 3853 |
positionals: (0, Some(0)), |
| 3854 |
flags: &[ |
| 3855 |
Flag { |
| 3856 |
name: "insensitive", |
| 3857 |
alias: Some('i'), |
| 3858 |
doc: "sort the ranges case-insensitively", |
| 3859 |
..Flag::DEFAULT |
| 3860 |
}, |
| 3861 |
Flag { |
| 3862 |
name: "reverse", |
| 3863 |
alias: Some('r'), |
| 3864 |
doc: "sort ranges in reverse order", |
| 3865 |
..Flag::DEFAULT |
| 3866 |
}, |
| 3867 |
], |
| 3868 |
..Signature::DEFAULT |
| 3869 |
}, |
| 3870 |
}, |
| 3871 |
TypableCommand { |
| 3872 |
name: "reflow", |
| 3873 |
aliases: &[], |
| 3874 |
doc: "Hard-wrap the current selection of lines to a given width.", |
| 3875 |
fun: reflow, |
| 3876 |
completer: CommandCompleter::none(), |
| 3877 |
signature: Signature { |
| 3878 |
positionals: (0, Some(1)), |
| 3879 |
..Signature::DEFAULT |
| 3880 |
}, |
| 3881 |
}, |
| 3882 |
TypableCommand { |
| 3883 |
name: "tree-sitter-subtree", |
| 3884 |
aliases: &["ts-subtree"], |
| 3885 |
doc: "Display the smallest tree-sitter subtree that spans the primary selection, primarily for debugging queries.", |
| 3886 |
fun: tree_sitter_subtree, |
| 3887 |
completer: CommandCompleter::none(), |
| 3888 |
signature: Signature { |
| 3889 |
positionals: (0, Some(0)), |
| 3890 |
..Signature::DEFAULT |
| 3891 |
}, |
| 3892 |
}, |
| 3893 |
TypableCommand { |
| 3894 |
name: "config-reload", |
| 3895 |
aliases: &[], |
| 3896 |
doc: "Refresh user config.", |
| 3897 |
fun: refresh_config, |
| 3898 |
completer: CommandCompleter::none(), |
| 3899 |
signature: Signature { |
| 3900 |
positionals: (0, Some(0)), |
| 3901 |
..Signature::DEFAULT |
| 3902 |
}, |
| 3903 |
}, |
| 3904 |
TypableCommand { |
| 3905 |
name: "config-open", |
| 3906 |
aliases: &[], |
| 3907 |
doc: "Open the user config.toml file.", |
| 3908 |
fun: open_config, |
| 3909 |
completer: CommandCompleter::none(), |
| 3910 |
signature: Signature { |
| 3911 |
positionals: (0, Some(0)), |
| 3912 |
..Signature::DEFAULT |
| 3913 |
}, |
| 3914 |
}, |
| 3915 |
TypableCommand { |
| 3916 |
name: "config-open-workspace", |
| 3917 |
aliases: &[], |
| 3918 |
doc: "Open the workspace config.toml file.", |
| 3919 |
fun: open_workspace_config, |
| 3920 |
completer: CommandCompleter::none(), |
| 3921 |
signature: Signature { |
| 3922 |
positionals: (0, Some(0)), |
| 3923 |
..Signature::DEFAULT |
| 3924 |
}, |
| 3925 |
}, |
| 3926 |
TypableCommand { |
| 3927 |
name: "log-open", |
| 3928 |
aliases: &[], |
| 3929 |
doc: "Open the helix log file.", |
| 3930 |
fun: open_log, |
| 3931 |
completer: CommandCompleter::none(), |
| 3932 |
signature: Signature { |
| 3933 |
positionals: (0, Some(0)), |
| 3934 |
..Signature::DEFAULT |
| 3935 |
}, |
| 3936 |
}, |
| 3937 |
TypableCommand { |
| 3938 |
name: "insert-output", |
| 3939 |
aliases: &[], |
| 3940 |
doc: "Run shell command, inserting output before each selection.", |
| 3941 |
fun: insert_output, |
| 3942 |
completer: SHELL_COMPLETER, |
| 3943 |
signature: SHELL_SIGNATURE, |
| 3944 |
}, |
| 3945 |
TypableCommand { |
| 3946 |
name: "append-output", |
| 3947 |
aliases: &[], |
| 3948 |
doc: "Run shell command, appending output after each selection.", |
| 3949 |
fun: append_output, |
| 3950 |
completer: SHELL_COMPLETER, |
| 3951 |
signature: SHELL_SIGNATURE, |
| 3952 |
}, |
| 3953 |
TypableCommand { |
| 3954 |
name: "pipe", |
| 3955 |
aliases: &["|"], |
| 3956 |
doc: "Pipe each selection to the shell command.", |
| 3957 |
fun: pipe, |
| 3958 |
completer: SHELL_COMPLETER, |
| 3959 |
signature: SHELL_SIGNATURE, |
| 3960 |
}, |
| 3961 |
TypableCommand { |
| 3962 |
name: "pipe-to", |
| 3963 |
aliases: &[], |
| 3964 |
doc: "Pipe each selection to the shell command, ignoring output.", |
| 3965 |
fun: pipe_to, |
| 3966 |
completer: SHELL_COMPLETER, |
| 3967 |
signature: SHELL_SIGNATURE, |
| 3968 |
}, |
| 3969 |
TypableCommand { |
| 3970 |
name: "run-shell-command", |
| 3971 |
aliases: &["sh", "!"], |
| 3972 |
doc: "Run a shell command", |
| 3973 |
fun: run_shell_command, |
| 3974 |
completer: SHELL_COMPLETER, |
| 3975 |
signature: SHELL_SIGNATURE, |
| 3976 |
}, |
| 3977 |
TypableCommand { |
| 3978 |
name: "reset-diff-change", |
| 3979 |
aliases: &["diffget", "diffg"], |
| 3980 |
doc: "Reset the diff change at the cursor position.", |
| 3981 |
fun: reset_diff_change, |
| 3982 |
completer: CommandCompleter::none(), |
| 3983 |
signature: Signature { |
| 3984 |
positionals: (0, Some(0)), |
| 3985 |
..Signature::DEFAULT |
| 3986 |
}, |
| 3987 |
}, |
| 3988 |
TypableCommand { |
| 3989 |
name: "clear-register", |
| 3990 |
aliases: &[], |
| 3991 |
doc: "Clear given register. If no argument is provided, clear all registers.", |
| 3992 |
fun: clear_register, |
| 3993 |
completer: CommandCompleter::all(completers::register), |
| 3994 |
signature: Signature { |
| 3995 |
positionals: (0, Some(1)), |
| 3996 |
..Signature::DEFAULT |
| 3997 |
}, |
| 3998 |
}, |
| 3999 |
TypableCommand { |
| 4000 |
name: "set-register", |
| 4001 |
aliases: &[], |
| 4002 |
doc: "Set contents of the given register.", |
| 4003 |
fun: set_register, |
| 4004 |
completer: CommandCompleter::positional(&[completers::register, completers::none]), |
| 4005 |
signature: Signature { |
| 4006 |
positionals: (2, Some(2)), |
| 4007 |
raw_after: Some(1), |
| 4008 |
..Signature::DEFAULT |
| 4009 |
}, |
| 4010 |
}, |
| 4011 |
TypableCommand { |
| 4012 |
name: "redraw", |
| 4013 |
aliases: &[], |
| 4014 |
doc: "Clear and re-render the whole UI", |
| 4015 |
fun: redraw, |
| 4016 |
completer: CommandCompleter::none(), |
| 4017 |
signature: Signature { |
| 4018 |
positionals: (0, Some(0)), |
| 4019 |
..Signature::DEFAULT |
| 4020 |
}, |
| 4021 |
}, |
| 4022 |
TypableCommand { |
| 4023 |
name: "move", |
| 4024 |
aliases: &["mv"], |
| 4025 |
doc: "Move the current buffer and its corresponding file to a different path", |
| 4026 |
fun: move_buffer, |
| 4027 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 4028 |
signature: Signature { |
| 4029 |
positionals: (1, Some(1)), |
| 4030 |
..Signature::DEFAULT |
| 4031 |
}, |
| 4032 |
}, |
| 4033 |
TypableCommand { |
| 4034 |
name: "move!", |
| 4035 |
aliases: &["mv!"], |
| 4036 |
doc: "Move the current buffer and its corresponding file to a different path creating necessary subdirectories", |
| 4037 |
fun: force_move_buffer, |
| 4038 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 4039 |
signature: Signature { |
| 4040 |
positionals: (1, Some(1)), |
| 4041 |
..Signature::DEFAULT |
| 4042 |
}, |
| 4043 |
}, |
| 4044 |
TypableCommand { |
| 4045 |
name: "yank-diagnostic", |
| 4046 |
aliases: &[], |
| 4047 |
doc: "Yank diagnostic(s) under primary cursor to register, or clipboard by default", |
| 4048 |
fun: yank_diagnostic, |
| 4049 |
completer: CommandCompleter::all(completers::register), |
| 4050 |
signature: Signature { |
| 4051 |
positionals: (0, Some(1)), |
| 4052 |
..Signature::DEFAULT |
| 4053 |
}, |
| 4054 |
}, |
| 4055 |
TypableCommand { |
| 4056 |
name: "read", |
| 4057 |
aliases: &["r"], |
| 4058 |
doc: "Load a file into buffer", |
| 4059 |
fun: read, |
| 4060 |
completer: CommandCompleter::positional(&[completers::filename]), |
| 4061 |
signature: Signature { |
| 4062 |
positionals: (1, Some(1)), |
| 4063 |
..Signature::DEFAULT |
| 4064 |
}, |
| 4065 |
}, |
| 4066 |
TypableCommand { |
| 4067 |
name: "echo", |
| 4068 |
aliases: &[], |
| 4069 |
doc: "Prints the given arguments to the statusline.", |
| 4070 |
fun: echo, |
| 4071 |
completer: CommandCompleter::none(), |
| 4072 |
signature: Signature { |
| 4073 |
positionals: (1, None), |
| 4074 |
..Signature::DEFAULT |
| 4075 |
}, |
| 4076 |
}, |
| 4077 |
TypableCommand { |
| 4078 |
name: "noop", |
| 4079 |
aliases: &[], |
| 4080 |
doc: "Does nothing.", |
| 4081 |
fun: noop, |
| 4082 |
completer: CommandCompleter::none(), |
| 4083 |
signature: Signature { |
| 4084 |
positionals: (0, None), |
| 4085 |
..Signature::DEFAULT |
| 4086 |
}, |
| 4087 |
}, |
| 4088 |
TypableCommand { |
| 4089 |
name: "workspace-trust", |
| 4090 |
aliases: &[], |
| 4091 |
doc: "Allow language servers and local config for the current workspace.", |
| 4092 |
fun: trust_workspace, |
| 4093 |
completer: CommandCompleter::none(), |
| 4094 |
signature: Signature { positionals: (0, None), ..Signature::DEFAULT }, |
| 4095 |
}, |
| 4096 |
TypableCommand { |
| 4097 |
name: "workspace-untrust", |
| 4098 |
aliases: &[], |
| 4099 |
doc: "Revoke the current workspace's trust grant or exclusion.", |
| 4100 |
fun: untrust_workspace, |
| 4101 |
completer: CommandCompleter::none(), |
| 4102 |
signature: Signature { positionals: (0, None), ..Signature::DEFAULT }, |
| 4103 |
}, |
| 4104 |
TypableCommand { |
| 4105 |
name: "workspace-exclude", |
| 4106 |
aliases: &[], |
| 4107 |
doc: "Mark the current workspace as never-prompt. Never prompts for trust again.", |
| 4108 |
fun: exclude_workspace, |
| 4109 |
completer: CommandCompleter::none(), |
| 4110 |
signature: Signature { positionals: (0, None), ..Signature::DEFAULT }, |
| 4111 |
} |
| 4112 |
]; |
| 4113 |
|
| 4114 |
pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> = |
| 4115 |
Lazy::new(|| { |
| 4116 |
TYPABLE_COMMAND_LIST |
| 4117 |
.iter() |
| 4118 |
.flat_map(|cmd| { |
| 4119 |
std::iter::once((cmd.name, cmd)) |
| 4120 |
.chain(cmd.aliases.iter().map(move |&alias| (alias, cmd))) |
| 4121 |
}) |
| 4122 |
.collect() |
| 4123 |
}); |
| 4124 |
|
| 4125 |
fn execute_command_line( |
| 4126 |
cx: &mut compositor::Context, |
| 4127 |
input: &str, |
| 4128 |
event: PromptEvent, |
| 4129 |
) -> anyhow::Result<()> { |
| 4130 |
let (command, rest, _) = command_line::split(input); |
| 4131 |
if command.is_empty() { |
| 4132 |
return Ok(()); |
| 4133 |
} |
| 4134 |
|
| 4135 |
|
| 4136 |
if command.parse::<usize>().is_ok() && rest.trim().is_empty() { |
| 4137 |
let cmd = TYPABLE_COMMAND_MAP.get("goto").unwrap(); |
| 4138 |
return execute_command(cx, cmd, command, event); |
| 4139 |
} |
| 4140 |
|
| 4141 |
match typed::TYPABLE_COMMAND_MAP.get(command) { |
| 4142 |
Some(cmd) => execute_command(cx, cmd, rest, event), |
| 4143 |
None if event == PromptEvent::Validate => Err(anyhow!("no such command: '{command}'")), |
| 4144 |
None => Ok(()), |
| 4145 |
} |
| 4146 |
} |
| 4147 |
|
| 4148 |
pub(super) fn execute_command( |
| 4149 |
cx: &mut compositor::Context, |
| 4150 |
cmd: &TypableCommand, |
| 4151 |
args: &str, |
| 4152 |
event: PromptEvent, |
| 4153 |
) -> anyhow::Result<()> { |
| 4154 |
let args = if event == PromptEvent::Validate { |
| 4155 |
Args::parse(args, cmd.signature, true, |token| { |
| 4156 |
expansion::expand(cx.editor, token).map_err(|err| err.into()) |
| 4157 |
}) |
| 4158 |
.map_err(|err| anyhow!("'{}': {err}", cmd.name))? |
| 4159 |
} else { |
| 4160 |
Args::parse(args, cmd.signature, false, |token| Ok(token.content)) |
| 4161 |
.expect("arg parsing cannot fail when validation is turned off") |
| 4162 |
}; |
| 4163 |
|
| 4164 |
(cmd.fun)(cx, args, event).map_err(|err| anyhow!("'{}': {err}", cmd.name)) |
| 4165 |
} |
| 4166 |
|
| 4167 |
#[allow(clippy::unnecessary_unwrap)] |
| 4168 |
pub(super) fn command_mode(cx: &mut Context) { |
| 4169 |
let mut prompt = Prompt::new( |
| 4170 |
":".into(), |
| 4171 |
Some(':'), |
| 4172 |
complete_command_line, |
| 4173 |
move |cx: &mut compositor::Context, input: &str, event: PromptEvent| { |
| 4174 |
if let Err(err) = execute_command_line(cx, input, event) { |
| 4175 |
cx.editor.set_error(err.to_string()); |
| 4176 |
} |
| 4177 |
}, |
| 4178 |
); |
| 4179 |
prompt.doc_fn = Box::new(command_line_doc); |
| 4180 |
|
| 4181 |
|
| 4182 |
prompt.recalculate_completion(cx.editor); |
| 4183 |
cx.push_layer(Box::new(prompt)); |
| 4184 |
} |
| 4185 |
|
| 4186 |
fn command_line_doc(input: &str) -> Option<Cow<'_, str>> { |
| 4187 |
let (command, _, _) = command_line::split(input); |
| 4188 |
let command = TYPABLE_COMMAND_MAP.get(command)?; |
| 4189 |
|
| 4190 |
if command.aliases.is_empty() && command.signature.flags.is_empty() { |
| 4191 |
return Some(Cow::Borrowed(command.doc)); |
| 4192 |
} |
| 4193 |
|
| 4194 |
let mut doc = command.doc.to_string(); |
| 4195 |
|
| 4196 |
if !command.aliases.is_empty() { |
| 4197 |
write!(doc, "\nAliases: {}", command.aliases.join(", ")).unwrap(); |
| 4198 |
} |
| 4199 |
|
| 4200 |
if !command.signature.flags.is_empty() { |
| 4201 |
const ARG_PLACEHOLDER: &str = " <arg>"; |
| 4202 |
|
| 4203 |
fn flag_len(flag: &Flag) -> usize { |
| 4204 |
let name_len = flag.name.len(); |
| 4205 |
let alias_len = if let Some(alias) = flag.alias { |
| 4206 |
"/-".len() + alias.len_utf8() |
| 4207 |
} else { |
| 4208 |
0 |
| 4209 |
}; |
| 4210 |
let arg_len = if flag.completions.is_some() { |
| 4211 |
ARG_PLACEHOLDER.len() |
| 4212 |
} else { |
| 4213 |
0 |
| 4214 |
}; |
| 4215 |
name_len + alias_len + arg_len |
| 4216 |
} |
| 4217 |
|
| 4218 |
doc.push_str("\nFlags:"); |
| 4219 |
|
| 4220 |
let max_flag_len = command.signature.flags.iter().map(flag_len).max().unwrap(); |
| 4221 |
|
| 4222 |
for flag in command.signature.flags { |
| 4223 |
let mut buf = [0u8; 4]; |
| 4224 |
let this_flag_len = flag_len(flag); |
| 4225 |
write!( |
| 4226 |
doc, |
| 4227 |
"\n --{flag_text}{spacer:spacing$} {doc}", |
| 4228 |
doc = flag.doc, |
| 4229 |
|
| 4230 |
|
| 4231 |
spacer = "", |
| 4232 |
spacing = max_flag_len - this_flag_len, |
| 4233 |
flag_text = format_args!( |
| 4234 |
"{}{}{}{}", |
| 4235 |
flag.name, |
| 4236 |
|
| 4237 |
|
| 4238 |
if flag.alias.is_some() { "/-" } else { "" }, |
| 4239 |
if let Some(alias) = flag.alias { |
| 4240 |
alias.encode_utf8(&mut buf) |
| 4241 |
} else { |
| 4242 |
"" |
| 4243 |
}, |
| 4244 |
if flag.completions.is_some() { |
| 4245 |
ARG_PLACEHOLDER |
| 4246 |
} else { |
| 4247 |
"" |
| 4248 |
} |
| 4249 |
), |
| 4250 |
) |
| 4251 |
.unwrap(); |
| 4252 |
} |
| 4253 |
} |
| 4254 |
|
| 4255 |
Some(Cow::Owned(doc)) |
| 4256 |
} |
| 4257 |
|
| 4258 |
fn complete_command_line(editor: &Editor, input: &str) -> Vec<ui::prompt::Completion> { |
| 4259 |
let (command, rest, complete_command) = command_line::split(input); |
| 4260 |
|
| 4261 |
if complete_command { |
| 4262 |
fuzzy_match( |
| 4263 |
input, |
| 4264 |
TYPABLE_COMMAND_LIST.iter().map(|command| command.name), |
| 4265 |
false, |
| 4266 |
) |
| 4267 |
.into_iter() |
| 4268 |
.map(|(name, _)| (0.., name.into())) |
| 4269 |
.collect() |
| 4270 |
} else { |
| 4271 |
TYPABLE_COMMAND_MAP |
| 4272 |
.get(command) |
| 4273 |
.map_or_else(Vec::new, |cmd| { |
| 4274 |
let args_offset = command.len() + 1; |
| 4275 |
complete_command_args(editor, cmd.signature, &cmd.completer, rest, args_offset) |
| 4276 |
}) |
| 4277 |
} |
| 4278 |
} |
| 4279 |
|
| 4280 |
pub fn complete_command_args( |
| 4281 |
editor: &Editor, |
| 4282 |
signature: Signature, |
| 4283 |
completer: &CommandCompleter, |
| 4284 |
input: &str, |
| 4285 |
offset: usize, |
| 4286 |
) -> Vec<ui::prompt::Completion> { |
| 4287 |
use command_line::{CompletionState, ExpansionKind, Tokenizer}; |
| 4288 |
|
| 4289 |
|
| 4290 |
|
| 4291 |
|
| 4292 |
let cursor = input.len(); |
| 4293 |
let prefix = &input[..cursor]; |
| 4294 |
let mut tokenizer = Tokenizer::new(prefix, false); |
| 4295 |
let mut args = Args::new(signature, false); |
| 4296 |
let mut final_token = None; |
| 4297 |
let mut is_last_token = true; |
| 4298 |
|
| 4299 |
while let Some(token) = args |
| 4300 |
.read_token(&mut tokenizer) |
| 4301 |
.expect("arg parsing cannot fail when validation is turned off") |
| 4302 |
{ |
| 4303 |
final_token = Some(token.clone()); |
| 4304 |
args.push(token.content) |
| 4305 |
.expect("arg parsing cannot fail when validation is turned off"); |
| 4306 |
if tokenizer.pos() >= cursor { |
| 4307 |
is_last_token = false; |
| 4308 |
} |
| 4309 |
} |
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
|
| 4314 |
|
| 4315 |
|
| 4316 |
let token = if is_last_token { |
| 4317 |
let token = Token::empty_at(prefix.len()); |
| 4318 |
args.push(token.content.clone()).unwrap(); |
| 4319 |
token |
| 4320 |
} else { |
| 4321 |
final_token.unwrap() |
| 4322 |
}; |
| 4323 |
|
| 4324 |
|
| 4325 |
if token.is_terminated { |
| 4326 |
return Vec::new(); |
| 4327 |
} |
| 4328 |
|
| 4329 |
match token.kind { |
| 4330 |
TokenKind::Unquoted | TokenKind::Quoted(_) => { |
| 4331 |
match args.completion_state() { |
| 4332 |
CompletionState::Positional => { |
| 4333 |
|
| 4334 |
|
| 4335 |
let n = args |
| 4336 |
.len() |
| 4337 |
.checked_sub(1) |
| 4338 |
.expect("completion state to be positional"); |
| 4339 |
let completer = completer.for_argument_number(n); |
| 4340 |
|
| 4341 |
completer(editor, &token.content) |
| 4342 |
.into_iter() |
| 4343 |
.map(|(range, span)| quote_completion(&token, range, span, offset)) |
| 4344 |
.collect() |
| 4345 |
} |
| 4346 |
CompletionState::Flag(_) => fuzzy_match( |
| 4347 |
token.content.trim_start_matches('-'), |
| 4348 |
signature.flags.iter().map(|flag| flag.name), |
| 4349 |
false, |
| 4350 |
) |
| 4351 |
.into_iter() |
| 4352 |
.map(|(name, _)| ((offset + token.content_start).., format!("--{name}").into())) |
| 4353 |
.collect(), |
| 4354 |
CompletionState::FlagArgument(flag) => fuzzy_match( |
| 4355 |
&token.content, |
| 4356 |
flag.completions |
| 4357 |
.expect("flags in FlagArgument always have completions"), |
| 4358 |
false, |
| 4359 |
) |
| 4360 |
.into_iter() |
| 4361 |
.map(|(value, _)| ((offset + token.content_start).., (*value).into())) |
| 4362 |
.collect(), |
| 4363 |
} |
| 4364 |
} |
| 4365 |
TokenKind::Expand | TokenKind::Expansion(ExpansionKind::Shell) => { |
| 4366 |
|
| 4367 |
let arg_completer = matches!(args.completion_state(), CompletionState::Positional) |
| 4368 |
.then(|| { |
| 4369 |
let n = args |
| 4370 |
.len() |
| 4371 |
.checked_sub(1) |
| 4372 |
.expect("completion state to be positional"); |
| 4373 |
completer.for_argument_number(n) |
| 4374 |
}); |
| 4375 |
complete_expand(editor, &token, arg_completer, offset + token.content_start) |
| 4376 |
} |
| 4377 |
TokenKind::Expansion(ExpansionKind::Variable) => { |
| 4378 |
complete_variable_expansion(&token.content, offset + token.content_start) |
| 4379 |
} |
| 4380 |
TokenKind::Expansion(ExpansionKind::Unicode) => Vec::new(), |
| 4381 |
TokenKind::Expansion(ExpansionKind::Register) => { |
| 4382 |
complete_register_expansion(editor, &token.content, offset + token.content_start) |
| 4383 |
} |
| 4384 |
TokenKind::ExpansionKind => { |
| 4385 |
complete_expansion_kind(&token.content, offset + token.content_start) |
| 4386 |
} |
| 4387 |
} |
| 4388 |
} |
| 4389 |
|
| 4390 |
|
| 4391 |
|
| 4392 |
|
| 4393 |
|
| 4394 |
|
| 4395 |
|
| 4396 |
|
| 4397 |
fn quote_completion<'a>( |
| 4398 |
token: &Token, |
| 4399 |
range: ops::RangeFrom<usize>, |
| 4400 |
mut span: Span<'a>, |
| 4401 |
offset: usize, |
| 4402 |
) -> (ops::RangeFrom<usize>, Span<'a>) { |
| 4403 |
fn replace<'a>(text: Cow<'a, str>, from: char, to: &str) -> Cow<'a, str> { |
| 4404 |
if text.contains(from) { |
| 4405 |
Cow::Owned(text.replace(from, to)) |
| 4406 |
} else { |
| 4407 |
text |
| 4408 |
} |
| 4409 |
} |
| 4410 |
|
| 4411 |
match token.kind { |
| 4412 |
TokenKind::Unquoted if span.content.contains([' ', '\t', '%']) => { |
| 4413 |
span.content = Cow::Owned(format!( |
| 4414 |
"'{}{}'", |
| 4415 |
|
| 4416 |
replace(token.content[..range.start].into(), '\'', "''"), |
| 4417 |
replace(span.content, '\'', "''") |
| 4418 |
)); |
| 4419 |
|
| 4420 |
|
| 4421 |
|
| 4422 |
((offset + token.content_start).., span) |
| 4423 |
} |
| 4424 |
TokenKind::Quoted(quote) => { |
| 4425 |
span.content = replace(span.content, quote.char(), quote.escape()); |
| 4426 |
((range.start + offset + token.content_start).., span) |
| 4427 |
} |
| 4428 |
TokenKind::Expand => { |
| 4429 |
|
| 4430 |
|
| 4431 |
span.content = replace(span.content, '"', "\"\""); |
| 4432 |
((range.start + offset).., span) |
| 4433 |
} |
| 4434 |
_ => ((range.start + offset + token.content_start).., span), |
| 4435 |
} |
| 4436 |
} |
| 4437 |
|
| 4438 |
fn complete_expand( |
| 4439 |
editor: &Editor, |
| 4440 |
token: &Token, |
| 4441 |
completer: Option<&Completer>, |
| 4442 |
offset: usize, |
| 4443 |
) -> Vec<ui::prompt::Completion> { |
| 4444 |
use command_line::{ExpansionKind, Tokenizer}; |
| 4445 |
|
| 4446 |
let mut start = 0; |
| 4447 |
|
| 4448 |
|
| 4449 |
while let Some(idx) = token.content[start..].find('%') { |
| 4450 |
let idx = start + idx; |
| 4451 |
if token.content.as_bytes().get(idx + '%'.len_utf8()).copied() == Some(b'%') { |
| 4452 |
|
| 4453 |
start = idx + ('%'.len_utf8() * 2); |
| 4454 |
} else { |
| 4455 |
let mut tokenizer = Tokenizer::new(&token.content[idx..], false); |
| 4456 |
let token = tokenizer |
| 4457 |
.parse_percent_token() |
| 4458 |
.map(|token| token.expect("arg parser cannot fail when validation is disabled")); |
| 4459 |
start = idx + tokenizer.pos(); |
| 4460 |
|
| 4461 |
|
| 4462 |
|
| 4463 |
|
| 4464 |
|
| 4465 |
|
| 4466 |
let Some(token) = token.filter(|t| !t.is_terminated) else { |
| 4467 |
continue; |
| 4468 |
}; |
| 4469 |
|
| 4470 |
let local_offset = offset + idx + token.content_start; |
| 4471 |
match token.kind { |
| 4472 |
TokenKind::Expansion(ExpansionKind::Variable) => { |
| 4473 |
return complete_variable_expansion(&token.content, local_offset); |
| 4474 |
} |
| 4475 |
TokenKind::Expansion(ExpansionKind::Shell) => { |
| 4476 |
return complete_expand(editor, &token, None, local_offset); |
| 4477 |
} |
| 4478 |
TokenKind::ExpansionKind => { |
| 4479 |
return complete_expansion_kind(&token.content, local_offset); |
| 4480 |
} |
| 4481 |
_ => continue, |
| 4482 |
} |
| 4483 |
} |
| 4484 |
} |
| 4485 |
|
| 4486 |
match completer { |
| 4487 |
|
| 4488 |
Some(completer) if start == 0 => completer(editor, &token.content) |
| 4489 |
.into_iter() |
| 4490 |
.map(|(range, span)| quote_completion(token, range, span, offset)) |
| 4491 |
.collect(), |
| 4492 |
_ => Vec::new(), |
| 4493 |
} |
| 4494 |
} |
| 4495 |
|
| 4496 |
fn complete_variable_expansion(content: &str, offset: usize) -> Vec<ui::prompt::Completion> { |
| 4497 |
use expansion::Variable; |
| 4498 |
|
| 4499 |
fuzzy_match( |
| 4500 |
content, |
| 4501 |
Variable::VARIANTS.iter().map(Variable::as_str), |
| 4502 |
false, |
| 4503 |
) |
| 4504 |
.into_iter() |
| 4505 |
.map(|(name, _)| (offset.., (*name).into())) |
| 4506 |
.collect() |
| 4507 |
} |
| 4508 |
|
| 4509 |
fn complete_register_expansion( |
| 4510 |
editor: &Editor, |
| 4511 |
content: &str, |
| 4512 |
offset: usize, |
| 4513 |
) -> Vec<ui::prompt::Completion> { |
| 4514 |
let register_names: Vec<String> = editor |
| 4515 |
.registers |
| 4516 |
.iter_preview() |
| 4517 |
.map(|(ch, _)| ch.to_string()) |
| 4518 |
.collect(); |
| 4519 |
fuzzy_match(content, register_names, false) |
| 4520 |
.into_iter() |
| 4521 |
.map(|(name, _)| (offset.., name.to_string().into())) |
| 4522 |
.collect() |
| 4523 |
} |
| 4524 |
|
| 4525 |
fn complete_expansion_kind(content: &str, offset: usize) -> Vec<ui::prompt::Completion> { |
| 4526 |
use command_line::ExpansionKind; |
| 4527 |
|
| 4528 |
fuzzy_match( |
| 4529 |
content, |
| 4530 |
|
| 4531 |
ExpansionKind::VARIANTS |
| 4532 |
.iter() |
| 4533 |
.skip(1) |
| 4534 |
.map(ExpansionKind::as_str), |
| 4535 |
false, |
| 4536 |
) |
| 4537 |
.into_iter() |
| 4538 |
.map(|(name, _)| (offset.., (*name).into())) |
| 4539 |
.collect() |
| 4540 |
} |
| 4541 |
|
| 4542 |
fn current_workspace(cx: &compositor::Context) -> std::path::PathBuf { |
| 4543 |
let (_, doc) = current_ref!(cx.editor); |
| 4544 |
doc.workspace_root().to_path_buf() |
| 4545 |
} |
| 4546 |
|
| 4547 |
|
| 4548 |
|
| 4549 |
fn doc_trust_full(editor: &helix_view::Editor) -> bool { |
| 4550 |
let (_, doc) = current_ref!(editor); |
| 4551 |
editor |
| 4552 |
.workspace_trust |
| 4553 |
.query( |
| 4554 |
doc.workspace_root(), |
| 4555 |
helix_loader::workspace_trust::TrustQuery::Git, |
| 4556 |
) |
| 4557 |
.is_trusted() |
| 4558 |
} |
| 4559 |
|
| 4560 |
fn trust_workspace( |
| 4561 |
cx: &mut compositor::Context, |
| 4562 |
args: Args<'_>, |
| 4563 |
event: PromptEvent, |
| 4564 |
) -> anyhow::Result<()> { |
| 4565 |
if event != PromptEvent::Validate { |
| 4566 |
return Ok(()); |
| 4567 |
} |
| 4568 |
|
| 4569 |
let workspace = current_workspace(cx); |
| 4570 |
cx.editor.workspace_trust.trust(&workspace); |
| 4571 |
|
| 4572 |
cx.editor.config_events.0.send(ConfigEvent::Refresh)?; |
| 4573 |
|
| 4574 |
lsp_restart(cx, args, event) |
| 4575 |
} |
| 4576 |
|
| 4577 |
fn untrust_workspace( |
| 4578 |
cx: &mut compositor::Context, |
| 4579 |
_args: Args<'_>, |
| 4580 |
event: PromptEvent, |
| 4581 |
) -> anyhow::Result<()> { |
| 4582 |
if event != PromptEvent::Validate { |
| 4583 |
return Ok(()); |
| 4584 |
} |
| 4585 |
|
| 4586 |
let workspace = current_workspace(cx); |
| 4587 |
cx.editor.workspace_trust.untrust(&workspace); |
| 4588 |
|
| 4589 |
|
| 4590 |
|
| 4591 |
cx.editor.config_events.0.send(ConfigEvent::Refresh)?; |
| 4592 |
Ok(()) |
| 4593 |
} |
| 4594 |
|
| 4595 |
fn exclude_workspace( |
| 4596 |
cx: &mut compositor::Context, |
| 4597 |
_args: Args<'_>, |
| 4598 |
event: PromptEvent, |
| 4599 |
) -> anyhow::Result<()> { |
| 4600 |
if event != PromptEvent::Validate { |
| 4601 |
return Ok(()); |
| 4602 |
} |
| 4603 |
|
| 4604 |
let workspace = current_workspace(cx); |
| 4605 |
cx.editor.workspace_trust.exclude(&workspace); |
| 4606 |
cx.editor.config_events.0.send(ConfigEvent::Refresh)?; |
| 4607 |
Ok(()) |
| 4608 |
} |