aboutsummaryrefslogtreecommitdiff
path: root/helix-view/src
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-05-29 15:00:15 +0000
committerBlaž Hrastnik2021-05-29 15:00:15 +0000
commit2c48d65b1565cf68a3efb5c9e91e9011526f43e7 (patch)
tree25b8f9e9a84800ce6e16282e783172bc7238ef31 /helix-view/src
parentd5466eddf59a2a447e0f5067b325a55fa6ba2d90 (diff)
Format document on save
Diffstat (limited to 'helix-view/src')
-rw-r--r--helix-view/src/document.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 814777f8..7d912ec0 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -116,6 +116,29 @@ impl Document {
Ok(doc)
}
+ // TODO: remove view_id dependency here
+ pub fn format(&mut self, view_id: ViewId) {
+ if let Some(language_server) = self.language_server() {
+ // TODO: await, no blocking
+ let transaction = helix_lsp::block_on(
+ language_server
+ .text_document_formatting(self.identifier(), lsp::FormattingOptions::default()),
+ )
+ .map(|edits| {
+ helix_lsp::util::generate_transaction_from_edits(
+ self.text(),
+ edits,
+ language_server.offset_encoding(),
+ )
+ });
+
+ if let Ok(transaction) = transaction {
+ self.apply(&transaction, view_id);
+ self.append_changes_to_history(view_id);
+ }
+ }
+ }
+
// TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
// or is that handled by the OS/async layer
pub fn save(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {