Skip to content

Commit 7c5aead

Browse files
authored
Enhance formatting and repair capabilities in NuShell (#124)
1 parent 074930a commit 7c5aead

21 files changed

Lines changed: 911 additions & 22 deletions

src/formatting.rs

Lines changed: 792 additions & 18 deletions
Large diffs are not rendered by default.

src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use nu_formatter::Mode;
1919
use rayon::iter::{IntoParallelIterator, ParallelIterator};
2020
use std::convert::TryFrom;
2121
use std::{
22+
collections::BTreeMap,
2223
io::{self, Write},
2324
path::{Path, PathBuf},
2425
};
@@ -334,9 +335,22 @@ fn discover_nu_files(
334335
})
335336
.collect();
336337

338+
let nu_files = deduplicate_discovered_files(nu_files);
339+
337340
Ok((nu_files, invalid_paths))
338341
}
339342

343+
fn deduplicate_discovered_files(paths: Vec<PathBuf>) -> Vec<PathBuf> {
344+
let mut unique: BTreeMap<PathBuf, PathBuf> = BTreeMap::new();
345+
346+
for path in paths {
347+
let key = path.canonicalize().unwrap_or_else(|_| path.clone());
348+
unique.entry(key).or_insert(path);
349+
}
350+
351+
unique.into_values().collect()
352+
}
353+
340354
/// Build override rules for excluded patterns
341355
fn build_overrides(excludes: &[String]) -> Result<ignore::overrides::Override, ConfigError> {
342356
let mut builder = OverrideBuilder::new(".");
@@ -466,4 +480,29 @@ mod tests {
466480
let exit_code = display_diagnostic_and_compute_exit_code(&results, check_mode);
467481
assert_eq!(exit_code, expected);
468482
}
483+
484+
#[test]
485+
fn discover_nu_files_deduplicates_overlapping_paths() {
486+
let dir = tempdir().unwrap();
487+
let nested = dir.path().join("nested");
488+
fs::create_dir_all(&nested).unwrap();
489+
490+
let target = nested.join("a.nu");
491+
fs::write(&target, "let x = 1").unwrap();
492+
493+
let (files, invalid) =
494+
discover_nu_files(vec![dir.path().to_path_buf(), nested.clone()], &[])
495+
.expect("discovery should succeed");
496+
497+
assert!(invalid.is_empty());
498+
499+
let canonical_target = target.canonicalize().unwrap();
500+
let matches = files
501+
.iter()
502+
.filter_map(|path| path.canonicalize().ok())
503+
.filter(|path| *path == canonical_target)
504+
.count();
505+
506+
assert_eq!(matches, 1, "file should be discovered exactly once");
507+
}
469508
}

tests/fixtures/expected/comment.nu

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let z = 3 # inline comment
77
# consecutive
88
# comments
99
def foo [] { 1 }
10-
def bar [] {
11-
# comment inside block
12-
print "hello" }
10+
def bar [] {
11+
# comment inside block
12+
print "hello"
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo []: nothing -> nothing {
2+
let xs = [1 2 3]
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def foo []: nothing -> nothing {
2+
for h in [1 2 3] {
3+
print $h
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def foo []: nothing -> nothing {
2+
let x = (bar
3+
"4444444444444444444444444444444444444444"
4+
"5555555555555555555555555555555555555555"
5+
)
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def foo []: bool -> bool {
2+
let yesno: bool = ($in | str trim) == "yes"
3+
$yesno
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def foo []: nothing -> nothing {
2+
let result = $items | from json | if ($in | default [] | where value == "ERR" | is-empty) {
3+
$in # test
4+
} else {
5+
null
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo [] {
2+
if true { 1 } else { 2 }
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo [] {
2+
if true { 1 } else { 2 }
3+
}

0 commit comments

Comments
 (0)