Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit bc871c7

Browse files
authored
Merge pull request #1497 from Xanewok/no-file-lines
Pass --file-lines to rustfmt only if specified
2 parents 36def18 + 0716da4 commit bc871c7

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

rls/src/actions/format.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,13 @@ fn rustfmt_args(config: &Config, config_path: &Path) -> Vec<String> {
195195
"--quiet".into(),
196196
];
197197

198-
args.push("--file-lines".into());
199-
let file_lines_json = config.file_lines().to_json_spans();
200-
let lines: String = serde_json::to_string(&file_lines_json).unwrap();
201-
args.push(lines);
198+
// Otherwise --file-lines [] are treated as no lines rather than FileLines::all()
199+
if config.file_lines().files().count() > 0 {
200+
args.push("--file-lines".into());
201+
let file_lines_json = config.file_lines().to_json_spans();
202+
let lines = serde_json::to_string(&file_lines_json).unwrap();
203+
args.push(lines);
204+
}
202205

203206
args.push("--config-path".into());
204207
args.push(config_path.to_str().map(ToOwned::to_owned).unwrap());
@@ -211,6 +214,8 @@ mod tests {
211214
use super::*;
212215
use crate::config::FmtConfig;
213216
use lsp_types::{Position, Range, TextEdit};
217+
use rustfmt_nightly::FileLines;
218+
use std::str::FromStr;
214219

215220
#[test]
216221
fn calc_text_edits() {
@@ -244,4 +249,20 @@ mod tests {
244249
vec![(0, 0, 1, 0, "struct Upper;\n"), (2, 0, 3, 0, "struct Lower;\n")],
245250
);
246251
}
252+
253+
#[test]
254+
fn no_empty_file_lines() {
255+
let config_with_lines = {
256+
let mut config = Config::default();
257+
config.set().file_lines(
258+
FileLines::from_str(r#"[{ "file": "stdin", "range": [0, 5] }]"#).unwrap(),
259+
);
260+
config
261+
};
262+
let args = rustfmt_args(&config_with_lines, Path::new("dummy"));
263+
assert!(args.join(" ").find("--file-lines").is_some());
264+
265+
let args = rustfmt_args(&Config::default(), Path::new("dummy"));
266+
assert_eq!(args.join(" ").find("--file-lines"), None);
267+
}
247268
}

0 commit comments

Comments
 (0)