on master help sign in
Add :fmt (formats the whole file).
e4ff75bBlaž Hrastnik
helix-lsp/src/lib.rs+2
@@ -20,6 +20,8 @@ use serde::{Deserialize, Serialize};
20
20
21use tokio_stream::wrappers::UnboundedReceiverStream;
21use tokio_stream::wrappers::UnboundedReceiverStream;
22
22
23+pub use futures_executor::block_on;
24+
23#[derive(Error, Debug)]
25#[derive(Error, Debug)]
24pub enum Error {
26pub enum Error {
25 #[error("protocol error: {0}")]
27 #[error("protocol error: {0}")]
helix-term/src/commands.rs+31
@@ -888,10 +888,34 @@ mod cmd {
888 }
888 }
889 tokio::spawn(doc.save());
889 tokio::spawn(doc.save());
890 }
890 }
891+
891 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
892 fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
892 editor.new_file(Action::Replace);
893 editor.new_file(Action::Replace);
893 }
894 }
894
895
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+
895 pub const COMMAND_LIST: &[Command] = &[
919 pub const COMMAND_LIST: &[Command] = &[
896 Command {
920 Command {
897 name: "quit",
921 name: "quit",
@@ -928,6 +952,13 @@ mod cmd {
928 fun: new_file,
952 fun: new_file,
929 completer: Some(completers::filename),
953 completer: Some(completers::filename),
930 },
954 },
955+ Command {
956+ name: "format",
957+ alias: Some("fmt"),
958+ doc: "Format the file using a formatter.",
959+ fun: format,
960+ completer: None,
961+ },
931 ];
962 ];
932
963
933 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {
964 pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {