From 2fa90e736b026ee979d9eb59178dc1f792228250 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 Aug 2018 11:12:42 +0300 Subject: better recovery for exprs --- crates/server/src/caps.rs | 6 +++++- crates/server/src/main_loop/handlers.rs | 19 +++++++++++++++++++ crates/server/src/main_loop/mod.rs | 4 ++++ crates/server/src/req.rs | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) (limited to 'crates/server/src') diff --git a/crates/server/src/caps.rs b/crates/server/src/caps.rs index 98499bb05..7456aea8a 100644 --- a/crates/server/src/caps.rs +++ b/crates/server/src/caps.rs @@ -5,6 +5,7 @@ use languageserver_types::{ TextDocumentSyncKind, ExecuteCommandOptions, CompletionOptions, + DocumentOnTypeFormattingOptions, }; pub fn server_capabilities() -> ServerCapabilities { @@ -35,7 +36,10 @@ pub fn server_capabilities() -> ServerCapabilities { code_lens_provider: None, document_formatting_provider: None, document_range_formatting_provider: None, - document_on_type_formatting_provider: None, + document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions { + first_trigger_character: "=".to_string(), + more_trigger_character: None, + }), rename_provider: None, color_provider: None, execute_command_provider: Some(ExecuteCommandOptions { diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index ec5421f06..ca5cd5ab1 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -314,6 +314,25 @@ pub fn handle_completion( Ok(Some(req::CompletionResponse::Array(items))) } +pub fn handle_on_type_formatting( + world: ServerWorld, + params: req::DocumentOnTypeFormattingParams, +) -> Result>> { + if params.ch != "=" { + return Ok(None); + } + + let file_id = params.text_document.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(file_id)?; + let offset = params.position.conv_with(&line_index); + let file = world.analysis().file_syntax(file_id)?; + let action = match libeditor::on_eq_typed(&file, offset) { + None => return Ok(None), + Some(action) => action, + }; + Ok(Some(action.edit.conv_with(&line_index))) +} + pub fn handle_execute_command( world: ServerWorld, mut params: req::ExecuteCommandParams, diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index da1121cb4..accb13878 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -31,6 +31,7 @@ use { handle_completion, handle_runnables, handle_decorations, + handle_on_type_formatting, }, }; @@ -161,6 +162,9 @@ fn on_request( handle_request_on_threadpool::( &mut req, pool, world, sender, handle_decorations, )?; + handle_request_on_threadpool::( + &mut req, pool, world, sender, handle_on_type_formatting, + )?; dispatch::handle_request::(&mut req, |params, resp| { io.send(RawMsg::Response(resp.into_response(Ok(None))?)); diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index 999fdb7c2..881069b1f 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -14,6 +14,7 @@ pub use languageserver_types::{ TextDocumentPositionParams, TextEdit, CompletionParams, CompletionResponse, + DocumentOnTypeFormattingParams, }; -- cgit v1.2.3