@@ -6,7 +6,8 @@ use crate::job::Job;
66use super::*;
77
88use helix_core::fuzzy::fuzzy_match;
9−use helix_core::{encoding, line_ending, shellwords::Shellwords};
9+use helix_core::{encoding, line_ending, path::get_canonicalized_path, shellwords::Shellwords};
10+use helix_lsp::{OffsetEncoding, Url};
1011use helix_view::document::DEFAULT_LANGUAGE_NAME;
1112use helix_view::editor::{Action, CloseError, ConfigEvent};
1213use serde_json::Value;
@@ -2408,6 +2409,80 @@ fn redraw(
24082409 Ok(())
24092410}
24102411
2412+fn move_buffer(
2413+ cx: &mut compositor::Context,
2414+ args: &[Cow<str>],
2415+ event: PromptEvent,
2416+) -> anyhow::Result<()> {
2417+ if event != PromptEvent::Validate {
2418+ return Ok(());
2419+ }
2420+
2421+ ensure!(args.len() == 1, format!(":move takes one argument"));
2422+ let doc = doc!(cx.editor);
2423+
2424+ let new_path = get_canonicalized_path(&PathBuf::from(args.first().unwrap().to_string()));
2425+ let old_path = doc
2426+ .path()
2427+ .ok_or_else(|| anyhow!("Scratch buffer cannot be moved. Use :write instead"))?
2428+ .clone();
2429+ let old_path_as_url = doc.url().unwrap();
2430+ let new_path_as_url = Url::from_file_path(&new_path).unwrap();
2431+
2432+ let edits: Vec<(
2433+ helix_lsp::Result<helix_lsp::lsp::WorkspaceEdit>,
2434+ OffsetEncoding,
2435+ String,
2436+ )> = doc
2437+ .language_servers()
2438+ .map(|lsp| {
2439+ (
2440+ lsp.prepare_file_rename(&old_path_as_url, &new_path_as_url),
2441+ lsp.offset_encoding(),
2442+ lsp.name().to_owned(),
2443+ )
2444+ })
2445+ .filter(|(f, _, _)| f.is_some())
2446+ .map(|(f, encoding, name)| (helix_lsp::block_on(f.unwrap()), encoding, name))
2447+ .collect();
2448+
2449+ for (lsp_reply, encoding, name) in edits {
2450+ match lsp_reply {
2451+ Ok(edit) => {
2452+ if let Err(e) = apply_workspace_edit(cx.editor, encoding, &edit) {
2453+ log::error!(
2454+ ":move command failed to apply edits from lsp {}: {:?}",
2455+ name,
2456+ e
2457+ );
2458+ };
2459+ }
2460+ Err(e) => {
2461+ log::error!("LSP {} failed to treat willRename request: {:?}", name, e);
2462+ }
2463+ };
2464+ }
2465+
2466+ let doc = doc_mut!(cx.editor);
2467+
2468+ doc.set_path(Some(new_path.as_path()));
2469+ if let Err(e) = std::fs::rename(&old_path, &new_path) {
2470+ doc.set_path(Some(old_path.as_path()));
2471+ bail!("Could not move file: {}", e);
2472+ };
2473+
2474+ doc.language_servers().for_each(|lsp| {
2475+ lsp.did_file_rename(&old_path_as_url, &new_path_as_url);
2476+ });
2477+
2478+ cx.editor
2479+ .language_servers
2480+ .file_event_handler
2481+ .file_changed(new_path);
2482+
2483+ Ok(())
2484+}
2485+
24112486pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
24122487 TypableCommand {
24132488 name: "quit",
@@ -3008,6 +3083,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
30083083 fun: redraw,
30093084 signature: CommandSignature::none(),
30103085 },
3086+ TypableCommand {
3087+ name: "move",
3088+ aliases: &[],
3089+ doc: "Move the current buffer and its corresponding file to a different path",
3090+ fun: move_buffer,
3091+ signature: CommandSignature::positional(&[completers::filename]),
3092+ },
30113093];
30123094
30133095pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =