Skip to content

Commit 1032533

Browse files
committed
Formatter uses stdin instead of filepath
In the current version, at least on Nvim and OSX, .clang-format in the root of the repo was being ignored because clang-format looks for a config file relative to the source file, not the working directory. This change allows .clang-format in the root of a workspace to be properly picked up with the addition of the assume-filename flag.
1 parent 10d2a1e commit 1032533

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

src/formatter/clang.rs

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

88-
fn get_command(&self, u: &Path) -> Command {
88+
fn get_command(&self, f: &str, u: &Path) -> 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"]);
93+
c.stdin(File::open(u).unwrap());
94+
c.args(["--output-replacements-xml", format!("--assume-filename={f}").as_str()]);
9495
c
9596
}
9697

@@ -107,9 +108,9 @@ 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 output = self.get_command(filename, p.as_ref()).output().ok()?;
113114
if !output.status.success() {
114115
tracing::error!(
115116
status = output.status.code(),
@@ -120,12 +121,12 @@ impl ProtoFormatter for ClangFormatter {
120121
self.output_to_textedit(&String::from_utf8_lossy(&output.stdout), content)
121122
}
122123

123-
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>> {
124+
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
124125
let p = self.get_temp_file_path(content)?;
125126
let start = r.start.line + 1;
126127
let end = r.end.line + 1;
127128
let output = self
128-
.get_command(p.as_ref())
129+
.get_command(filename, p.as_ref())
129130
.args(["--lines", format!("{start}:{end}").as_str()])
130131
.output()
131132
.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)