diff options
author | Blaž Hrastnik | 2021-02-26 06:52:43 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-02-26 06:52:43 +0000 |
commit | 1ae7c4339194c6f988202b24cd910d8c2edea737 (patch) | |
tree | f7aea3b0e26fc59f1bcb5693ee540fe31af8a8fa /helix-term | |
parent | 7162632eb739557ededaee902b23e48a577817b4 (diff) |
commands: = as range formatting (via lsp)
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/commands.rs | 41 | ||||
-rw-r--r-- | helix-term/src/keymap.rs | 1 |
2 files changed, 42 insertions, 0 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c5c2ecf1..898981ae 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -888,6 +888,47 @@ pub fn unindent(cx: &mut Context) { doc.append_changes_to_history(); } +pub fn format_selections(cx: &mut Context) { + use helix_lsp::lsp; + let doc = cx.doc(); + + // via lsp if available + // else via tree-sitter indentation calculations + + // TODO: blocking here is not ideal + + let ranges: Vec<lsp::Range> = doc + .selection() + .ranges() + .iter() + .map(|range| { + helix_lsp::util::range_to_lsp_range(doc.text().slice(..), doc.selection().primary()) + }) + .collect(); + + for range in ranges { + let language_server = match doc.language_server.as_ref() { + Some(language_server) => language_server, + None => return, + }; + + // TODO: handle fails + // TODO: concurrent map + let edits = smol::block_on(language_server.text_document_range_formatting( + doc.identifier(), + range, + lsp::FormattingOptions::default(), + )) + .unwrap_or_default(); + + let transaction = helix_lsp::util::generate_transaction_from_edits(&doc.state, edits); + + doc.apply(&transaction); + } + + doc.append_changes_to_history(); +} + // pub fn save(cx: &mut Context) { diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index ac4a2bf2..589740fc 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -178,6 +178,7 @@ pub fn default() -> Keymaps { vec![key!('p')] => commands::paste, vec![key!('>')] => commands::indent, vec![key!('<')] => commands::unindent, + vec![key!('=')] => commands::format_selections, vec![key!(':')] => commands::command_mode, vec![Key { code: KeyCode::Esc, |