Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover flatshapes #35

Merged
merged 26 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e72a4f9
:construction: add some shapes
AucaCoyan Jun 21, 2023
8ae7105
:construction: add str interpolation boolean
AucaCoyan Jun 22, 2023
1392222
:construction: keep on lining
AucaCoyan Jun 23, 2023
c965da8
:sparkles: resolve some shapes and internal calls
AucaCoyan Jun 25, 2023
f7555d4
:twisted_rightwards_arrows: merge `upstream/main` into this branch
AucaCoyan Jun 29, 2023
b3180dc
:arrow_up: upgrade `nu` dependencies
AucaCoyan Jun 29, 2023
4ab533f
:rotating_light: fix clippy
AucaCoyan Jun 29, 2023
03078d3
:recycle: add `write_only_if_have_comments` fn
AucaCoyan Jun 29, 2023
6b49ff1
refactor the decl ids in an enumeration
amtoine Jul 8, 2023
cbda62d
remove useless comments
amtoine Jul 8, 2023
2cceede
make Clippy happy
amtoine Jul 8, 2023
7faac01
:twisted_rightwards_arrows: bring the changes of upstream/cover-flats…
AucaCoyan Jul 21, 2023
e0e4087
:construction: advance on write comments function
AucaCoyan Jul 21, 2023
1670dae
:bug: remove the merge marker and add link to license
AucaCoyan Jul 21, 2023
37d6bd6
:package: update `nu` packages
Oct 1, 2023
8863caf
:fire: remove string interpolation
Oct 1, 2023
c47174c
Merge remote-tracking branch 'upstream/main' into cover-flatshapes
Oct 7, 2023
93955e5
:bug: fix space in signature
Oct 17, 2023
4e5f322
:rotating_light: Fix clippy and update `nu` dependencies
AucaCoyan Dec 15, 2023
c48df19
:memo: Update nushell version in the README
AucaCoyan Dec 15, 2023
dc7735e
:white_check_mark: Fix failing tests
AucaCoyan Dec 15, 2023
581febd
:recycle: Refactor the boolean
AucaCoyan Dec 15, 2023
63b851e
:fire: Remove files
AucaCoyan Dec 15, 2023
1f385cf
Update docstring on `formatting.rs`
AucaCoyan Dec 16, 2023
07f5076
:fire: Remove unused comment
AucaCoyan Dec 16, 2023
8d8c2e5
:fire: Remove no longer used comment
AucaCoyan Dec 16, 2023
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
Prev Previous commit
Next Next commit
🔥 remove string interpolation
  • Loading branch information
Auca Maillot committed Oct 1, 2023
commit 8863caf1b1f10f7070576ce05d7ec96fa9289ecc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[discord-url]: https://discord.gg/NtAbbGn
[ci-badge]: https://github.com/nushell/nufmt/actions/workflows/main.yml/badge.svg
[ci-url]: https://github.com/nushell/nufmt/actions/workflows/main.yml
[nushell-badge]: https://img.shields.io/badge/nushell-v0.81.0-green
[nushell-badge]: https://img.shields.io/badge/nushell-v0.85.0-green
[nushell-url]: https://crates.io/crates/nu

</div>
Expand Down
30 changes: 16 additions & 14 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
impl DeclId {
const If: usize = 22;
const Let: usize = 30;
const Def: usize = 5;
const ExportDefEnv: usize = 6;
}

Expand Down Expand Up @@ -45,7 +46,6 @@
let mut out: Vec<u8> = vec![];
let mut start = 0;
let end_of_file = contents.len();
let mut inside_string_interpolation = false;

for (span, shape) in flat.clone() {
if span.start > start {
Expand All @@ -59,7 +59,7 @@
let printable = String::from_utf8_lossy(skipped_contents).to_string();
trace!("contents: {:?}", printable);

out = write_only_if_have_comments(skipped_contents, out, true);
out = write_only_if_have_hastag_or_equal(skipped_contents, out, true);
}

let mut bytes = working_set.get_span_contents(span);
Expand All @@ -71,8 +71,6 @@
FlatShape::Int | FlatShape::Nothing => out.extend(bytes),
FlatShape::StringInterpolation => {
out.extend(bytes);
inside_string_interpolation = !inside_string_interpolation;
trace!("inside_string_interpolation 🚚: {inside_string_interpolation}");
}
FlatShape::List | FlatShape::Record => {
bytes = trim_ascii_whitespace(bytes);
Expand All @@ -81,15 +79,9 @@
FlatShape::Block | FlatShape::Closure => {
bytes = trim_ascii_whitespace(bytes);
out.extend(bytes);
if !inside_string_interpolation {
out.extend(b" ");
}
}
FlatShape::String => {
out.extend(bytes);
if !inside_string_interpolation {
out.extend(b" ");
}
}
FlatShape::Pipe => {
out.extend(b"| ");
Expand All @@ -108,6 +100,10 @@
out.extend(bytes);
out.extend(b" ");
}
FlatShape::Signature => {

Check failure on line 103 in src/formatting.rs

View workflow job for this annotation

GitHub Actions / lint all rust files

unreachable pattern

Check failure on line 103 in src/formatting.rs

View workflow job for this annotation

GitHub Actions / test rust files (ubuntu-latest)

unreachable pattern
out.extend(bytes);
out = insert_newline(out);
}
FlatShape::Garbage => {
error!("found garbage 😢 {content}");
out.extend(bytes);
Expand All @@ -128,7 +124,7 @@
let printable = String::from_utf8_lossy(remaining_contents).to_string();
trace!("contents: {:?}", printable);

out = write_only_if_have_comments(remaining_contents, out, false);
out = write_only_if_have_hastag_or_equal(remaining_contents, out, false);
}

start = span.end + 1;
Expand All @@ -143,7 +139,7 @@
bytes
}

/// given a list of `bytes` and a `out`put to write only the bytes if they contain `#`
/// given a list of `bytes` and a `out`put to write only the bytes if they contain `#` or `=`
///
/// One tiny little detail: the order of bytes is important to nufmt.
/// It is not the same to have
Expand All @@ -155,7 +151,7 @@
/// `out` + \n + `bytes`
///
/// That's why `bytes_before_content` bool is for
AucaCoyan marked this conversation as resolved.
Show resolved Hide resolved
fn write_only_if_have_comments(
fn write_only_if_have_hastag_or_equal(
bytes: &[u8],
mut out: Vec<u8>,
bytes_before_content: bool,
Expand All @@ -169,6 +165,9 @@
out = insert_newline(out);
out.extend(trim_ascii_whitespace(bytes));
}
} else if bytes.contains(&b'=') {
out.extend(trim_ascii_whitespace(bytes));
out.extend(b" ");
} else {
trace!("The contents doesn't have a '#'. Skipping.");
}
Expand All @@ -180,6 +179,7 @@
out = match declid {
DeclId::If => insert_newline(out),
DeclId::Let => insert_newline(out),
DeclId::Def => insert_newline(out),
DeclId::ExportDefEnv | _ => out,
};
out.extend(c_bytes);
Expand Down Expand Up @@ -212,7 +212,9 @@
/// and afterwards include the new lines and indentation correctly
/// according to the configuration
fn trim_ascii_whitespace(x: &[u8]) -> &[u8] {
let Some(from) = x.iter().position(|x| !x.is_ascii_whitespace()) else { return &x[0..0] };
let Some(from) = x.iter().position(|x| !x.is_ascii_whitespace()) else {
return &x[0..0];
};
let to = x.iter().rposition(|x| !x.is_ascii_whitespace()).unwrap();
let result = &x[from..=to];
let printable = String::from_utf8_lossy(result).to_string();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ myfunc(one)
#[test]
fn remove_additional_lines() {
let input = "let one = 1\n\n\n";
let expected = "let one = 1\n";
let expected = "let one = 1";
run_test(input, expected);
}

Expand Down
Loading