Skip to content

Commit d867b1b

Browse files
authored
Formatter uses stdin instead of filepath (#46)
1 parent 10d2a1e commit d867b1b

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/formatter/clang.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ impl ClangFormatter {
8585
Some(p)
8686
}
8787

88-
fn get_command(&self, u: &Path) -> Command {
88+
fn get_command(&self, f: &str, u: &Path) -> Option<Command> {
8989
let mut c = Command::new(self.path.as_str());
9090
if let Some(wd) = self.working_dir.as_ref() {
9191
c.current_dir(wd.as_str());
9292
}
93-
c.args([u.to_str().unwrap(), "--output-replacements-xml"]);
94-
c
93+
c.stdin(File::open(u).ok()?);
94+
c.args(["--output-replacements-xml", format!("--assume-filename={f}").as_str()]);
95+
Some(c)
9596
}
9697

9798
fn output_to_textedit(&self, output: &str, content: &str) -> Option<Vec<TextEdit>> {
@@ -107,9 +108,10 @@ impl ClangFormatter {
107108
}
108109

109110
impl ProtoFormatter for ClangFormatter {
110-
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>> {
111+
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
111112
let p = self.get_temp_file_path(content)?;
112-
let output = self.get_command(p.as_ref()).output().ok()?;
113+
let mut cmd = self.get_command(filename, p.as_ref())?;
114+
let output = cmd.output().ok()?;
113115
if !output.status.success() {
114116
tracing::error!(
115117
status = output.status.code(),
@@ -120,12 +122,12 @@ impl ProtoFormatter for ClangFormatter {
120122
self.output_to_textedit(&String::from_utf8_lossy(&output.stdout), content)
121123
}
122124

123-
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>> {
125+
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
124126
let p = self.get_temp_file_path(content)?;
125127
let start = r.start.line + 1;
126128
let end = r.end.line + 1;
127129
let output = self
128-
.get_command(p.as_ref())
130+
.get_command(filename, p.as_ref())?
129131
.args(["--lines", format!("{start}:{end}").as_str()])
130132
.output()
131133
.ok()?;

src/formatter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ use async_lsp::lsp_types::{Range, TextEdit};
33
pub mod clang;
44

55
pub trait ProtoFormatter: Sized {
6-
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>>;
7-
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>>;
6+
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
7+
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
88
}

src/lsp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ impl LanguageServer for ProtoLanguageServer {
375375
let response = self
376376
.state
377377
.get_formatter()
378-
.and_then(|f| f.format_document(content.as_str()));
378+
.and_then(|f| f.format_document(uri.path(), content.as_str()));
379379

380380
Box::pin(async move { Ok(response) })
381381
}
@@ -390,7 +390,7 @@ impl LanguageServer for ProtoLanguageServer {
390390
let response = self
391391
.state
392392
.get_formatter()
393-
.and_then(|f| f.format_document_range(&params.range, content.as_str()));
393+
.and_then(|f| f.format_document_range(&params.range, uri.path(), content.as_str()));
394394

395395
Box::pin(async move { Ok(response) })
396396
}

0 commit comments

Comments
 (0)