-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1216 from tweag/lsp/formatting
Add support for formatting capabilities to the LSP
- Loading branch information
Showing
5 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::process; | ||
|
||
use lsp_server::{ErrorCode, RequestId, Response, ResponseError}; | ||
use lsp_types::{DocumentFormattingParams, Position, Range, TextEdit}; | ||
|
||
use crate::server::{self, Server}; | ||
|
||
/// Handle the LSP formatting request from a client using an external binary as a formatter. | ||
/// If this succeds, it sends a reponse to the server and returns `Ok(..)`, otherwise, | ||
/// it only returns an `Err(..)`. | ||
pub fn handle_format_document( | ||
params: DocumentFormattingParams, | ||
id: RequestId, | ||
server: &mut Server, | ||
) -> Result<(), ResponseError> { | ||
let document_id = params.text_document.uri.to_file_path().unwrap(); | ||
let file_id = server.cache.id_of(document_id).unwrap(); | ||
let text = server.cache.files().source(file_id).clone(); | ||
let document_length = text.lines().count() as u32; | ||
let last_line_length = text.lines().rev().next().unwrap().len() as u32; | ||
|
||
let formatting_command = server::FORMATTING_COMMAND; | ||
let Ok(mut topiary) = process::Command::new(formatting_command[0]) | ||
.args(&formatting_command[1..]) | ||
.stdin(process::Stdio::piped()) | ||
.stdout(process::Stdio::piped()) | ||
.stderr(process::Stdio::piped()) | ||
.spawn() else { | ||
let message = "Executing topiary failed"; | ||
return Err(ResponseError { | ||
code: ErrorCode::InternalError as i32, | ||
message: String::from(message), | ||
data: None, | ||
}); | ||
}; | ||
|
||
let mut stdin = topiary.stdin.take().unwrap(); | ||
|
||
std::thread::spawn(move || { | ||
let mut text_bytes = text.as_bytes(); | ||
std::io::copy(&mut text_bytes, &mut stdin).unwrap(); | ||
}); | ||
|
||
let output = topiary.wait_with_output().unwrap(); | ||
|
||
if !output.status.success() { | ||
let error = String::from_utf8_lossy(&output.stderr); | ||
return Err(ResponseError { | ||
code: ErrorCode::InternalError as i32, | ||
message: error.to_string(), | ||
data: None, | ||
}); | ||
} | ||
|
||
let new_text = String::from_utf8(output.stdout).unwrap(); | ||
|
||
let result = Some(vec![TextEdit { | ||
range: Range { | ||
start: Position { | ||
line: 0, | ||
character: 0, | ||
}, | ||
end: Position { | ||
line: document_length - 1, | ||
character: last_line_length, | ||
}, | ||
}, | ||
new_text, | ||
}]); | ||
server.reply(Response::new_ok(id, result)); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod completion; | ||
pub mod formatting; | ||
pub mod goto; | ||
pub mod hover; | ||
pub mod symbols; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters