diff options
Diffstat (limited to 'crates/ra_lsp_server/src/main_loop/handlers.rs')
-rw-r--r-- | crates/ra_lsp_server/src/main_loop/handlers.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index d6f3dbe28..a2c12a4c1 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs | |||
@@ -6,13 +6,16 @@ use languageserver_types::{ | |||
6 | DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind, | 6 | DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind, |
7 | FoldingRangeParams, Location, MarkupContent, MarkupKind, MarkedString, Position, | 7 | FoldingRangeParams, Location, MarkupContent, MarkupKind, MarkedString, Position, |
8 | PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, | 8 | PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, |
9 | Range, | ||
9 | WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents, | 10 | WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents, |
11 | DocumentFormattingParams, | ||
10 | }; | 12 | }; |
11 | use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity}; | 13 | use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity}; |
12 | use ra_syntax::{TextUnit, text_utils::intersect}; | 14 | use ra_syntax::{TextUnit, text_utils::intersect}; |
13 | use ra_text_edit::text_utils::contains_offset_nonstrict; | 15 | use ra_text_edit::text_utils::contains_offset_nonstrict; |
14 | use rustc_hash::FxHashMap; | 16 | use rustc_hash::FxHashMap; |
15 | use serde_json::to_value; | 17 | use serde_json::to_value; |
18 | use std::io::Write; | ||
16 | 19 | ||
17 | use crate::{ | 20 | use crate::{ |
18 | conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith}, | 21 | conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith}, |
@@ -601,6 +604,40 @@ pub fn handle_references( | |||
601 | )) | 604 | )) |
602 | } | 605 | } |
603 | 606 | ||
607 | pub fn handle_formatting( | ||
608 | world: ServerWorld, | ||
609 | params: DocumentFormattingParams, | ||
610 | ) -> Result<Option<Vec<TextEdit>>> { | ||
611 | let file_id = params.text_document.try_conv_with(&world)?; | ||
612 | let file = world.analysis().file_text(file_id); | ||
613 | |||
614 | let file_line_index = world.analysis().file_line_index(file_id); | ||
615 | let end_position = TextUnit::of_str(&file).conv_with(&file_line_index); | ||
616 | |||
617 | use std::process; | ||
618 | let mut rustfmt = process::Command::new("rustfmt") | ||
619 | .stdin(process::Stdio::piped()) | ||
620 | .stdout(process::Stdio::piped()) | ||
621 | .spawn()?; | ||
622 | |||
623 | rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?; | ||
624 | |||
625 | let output = rustfmt.wait_with_output()?; | ||
626 | let captured_stdout = String::from_utf8(output.stdout)?; | ||
627 | if !output.status.success() { | ||
628 | failure::bail!( | ||
629 | "rustfmt exited with error code {}: {}.", | ||
630 | output.status, | ||
631 | captured_stdout, | ||
632 | ); | ||
633 | } | ||
634 | |||
635 | Ok(Some(vec![TextEdit { | ||
636 | range: Range::new(Position::new(0, 0), end_position), | ||
637 | new_text: captured_stdout, | ||
638 | }])) | ||
639 | } | ||
640 | |||
604 | pub fn handle_code_action( | 641 | pub fn handle_code_action( |
605 | world: ServerWorld, | 642 | world: ServerWorld, |
606 | params: req::CodeActionParams, | 643 | params: req::CodeActionParams, |