@@ -888,10 +888,34 @@ mod cmd {
888888 }
889889 tokio::spawn(doc.save());
890890 }
891+
891892 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
892893 editor.new_file(Action::Replace);
893894 }
894895
896+ fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
897+ let (view, doc) = editor.current();
898+
899+ if let Some(language_server) = doc.language_server() {
900+ // TODO: await, no blocking
901+ let transaction = helix_lsp::block_on(
902+ language_server
903+ .text_document_formatting(doc.identifier(), lsp::FormattingOptions::default()),
904+ )
905+ .map(|edits| {
906+ helix_lsp::util::generate_transaction_from_edits(
907+ doc.text(),
908+ edits,
909+ language_server.offset_encoding(),
910+ )
911+ });
912+
913+ if let Ok(transaction) = transaction {
914+ doc.apply(&transaction, view.id);
915+ }
916+ }
917+ }
918+
895919 pub const COMMAND_LIST: &[Command] = &[
896920 Command {
897921 name: "quit",
@@ -928,6 +952,13 @@ mod cmd {
928952 fun: new_file,
929953 completer: Some(completers::filename),
930954 },
955+ Command {
956+ name: "format",
957+ alias: Some("fmt"),
958+ doc: "Format the file using a formatter.",
959+ fun: format,
960+ completer: None,
961+ },
931962 ];
932963
933964 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {