aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-05-12 08:24:55 +0000
committerBlaž Hrastnik2021-05-12 08:24:55 +0000
commite4ff75b4d4eb793004e91279c82fde7766fc5a00 (patch)
treef9adbf7d37b194982e57fd7db5f8a63ef4e1ccc3
parent4eabb8d05462aebae11a09f17380c9774bccbeb0 (diff)
Add :fmt (formats the whole file).
-rw-r--r--helix-lsp/src/lib.rs2
-rw-r--r--helix-term/src/commands.rs31
2 files changed, 33 insertions, 0 deletions
diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs
index 1dbd6529..0a83c93a 100644
--- a/helix-lsp/src/lib.rs
+++ b/helix-lsp/src/lib.rs
@@ -20,6 +20,8 @@ use serde::{Deserialize, Serialize};
use tokio_stream::wrappers::UnboundedReceiverStream;
+pub use futures_executor::block_on;
+
#[derive(Error, Debug)]
pub enum Error {
#[error("protocol error: {0}")]
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index e26dbbaa..4a42bbba 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -888,10 +888,34 @@ mod cmd {
}
tokio::spawn(doc.save());
}
+
fn new_file(editor: &mut Editor, args: &[&str], event: PromptEvent) {
editor.new_file(Action::Replace);
}
+ fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
+ let (view, doc) = editor.current();
+
+ if let Some(language_server) = doc.language_server() {
+ // TODO: await, no blocking
+ let transaction = helix_lsp::block_on(
+ language_server
+ .text_document_formatting(doc.identifier(), lsp::FormattingOptions::default()),
+ )
+ .map(|edits| {
+ helix_lsp::util::generate_transaction_from_edits(
+ doc.text(),
+ edits,
+ language_server.offset_encoding(),
+ )
+ });
+
+ if let Ok(transaction) = transaction {
+ doc.apply(&transaction, view.id);
+ }
+ }
+ }
+
pub const COMMAND_LIST: &[Command] = &[
Command {
name: "quit",
@@ -928,6 +952,13 @@ mod cmd {
fun: new_file,
completer: Some(completers::filename),
},
+ Command {
+ name: "format",
+ alias: Some("fmt"),
+ doc: "Format the file using a formatter.",
+ fun: format,
+ completer: None,
+ },
];
pub static COMMANDS: Lazy<HashMap<&'static str, &'static Command>> = Lazy::new(|| {