Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/formatter/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ impl ClangFormatter {
Some(p)
}

fn get_command(&self, u: &Path) -> Command {
fn get_command(&self, f: &str, u: &Path) -> Command {
let mut c = Command::new(self.path.as_str());
if let Some(wd) = self.working_dir.as_ref() {
c.current_dir(wd.as_str());
}
c.args([u.to_str().unwrap(), "--output-replacements-xml"]);
c.stdin(File::open(u).unwrap());
c.args(["--output-replacements-xml", format!("--assume-filename={f}").as_str()]);
c
}

Expand All @@ -107,9 +108,9 @@ impl ClangFormatter {
}

impl ProtoFormatter for ClangFormatter {
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>> {
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
let p = self.get_temp_file_path(content)?;
let output = self.get_command(p.as_ref()).output().ok()?;
let output = self.get_command(filename, p.as_ref()).output().ok()?;
if !output.status.success() {
tracing::error!(
status = output.status.code(),
Expand All @@ -120,12 +121,12 @@ impl ProtoFormatter for ClangFormatter {
self.output_to_textedit(&String::from_utf8_lossy(&output.stdout), content)
}

fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>> {
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
let p = self.get_temp_file_path(content)?;
let start = r.start.line + 1;
let end = r.end.line + 1;
let output = self
.get_command(p.as_ref())
.get_command(filename, p.as_ref())
.args(["--lines", format!("{start}:{end}").as_str()])
.output()
.ok()?;
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use async_lsp::lsp_types::{Range, TextEdit};
pub mod clang;

pub trait ProtoFormatter: Sized {
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>>;
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>>;
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
}
4 changes: 2 additions & 2 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl LanguageServer for ProtoLanguageServer {
let response = self
.state
.get_formatter()
.and_then(|f| f.format_document(content.as_str()));
.and_then(|f| f.format_document(uri.path(), content.as_str()));

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

Box::pin(async move { Ok(response) })
}
Expand Down
Loading