aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaž Hrastnik2022-11-08 14:15:38 +0000
committerBlaž Hrastnik2022-11-09 09:17:23 +0000
commitd4f5cab7b53d4e10cc1907156876441744eff646 (patch)
treeeaa2414ab440862d6540a6f4cc80c53f5e844463
parentdee5b2a983f6a334753be48730868e8dd651b505 (diff)
Re-enable format_selections for a single selection range
-rw-r--r--helix-term/src/commands.rs49
1 files changed, 25 insertions, 24 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 4e3c321c..7e4969c1 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -3748,7 +3748,7 @@ fn format_selections(cx: &mut Context) {
let (view, doc) = current!(cx.editor);
// via lsp if available
- // else via tree-sitter indentation calculations
+ // TODO: else via tree-sitter indentation calculations
let language_server = match doc.language_server() {
Some(language_server) => language_server,
@@ -3761,33 +3761,34 @@ fn format_selections(cx: &mut Context) {
.map(|range| range_to_lsp_range(doc.text(), *range, language_server.offset_encoding()))
.collect();
- // TODO: all of the TODO's and commented code inside the loop,
- // to make this actually work.
- for _range in ranges {
- let _language_server = match doc.language_server() {
- Some(language_server) => language_server,
- None => return,
- };
- // TODO: handle fails
- // TODO: concurrent map
+ if ranges.len() != 1 {
+ cx.editor
+ .set_error("format_selections only supports a single selection for now");
+ return;
+ }
- // TODO: need to block to get the formatting
+ // TODO: handle fails
+ // TODO: concurrent map over all ranges
- // let edits = block_on(language_server.text_document_range_formatting(
- // doc.identifier(),
- // range,
- // lsp::FormattingOptions::default(),
- // ))
- // .unwrap_or_default();
+ let range = ranges[0];
- // let transaction = helix_lsp::util::generate_transaction_from_edits(
- // doc.text(),
- // edits,
- // language_server.offset_encoding(),
- // );
+ let edits = tokio::task::block_in_place(|| {
+ helix_lsp::block_on(language_server.text_document_range_formatting(
+ doc.identifier(),
+ range,
+ lsp::FormattingOptions::default(),
+ None,
+ ))
+ })
+ .unwrap_or_default();
- // apply_transaction(&transaction, doc, view);
- }
+ let transaction = helix_lsp::util::generate_transaction_from_edits(
+ doc.text(),
+ edits,
+ language_server.offset_encoding(),
+ );
+
+ apply_transaction(&transaction, doc, view);
}
fn join_selections_inner(cx: &mut Context, select_space: bool) {