Skip to content

🚧 add some shapes #32

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
2,329 changes: 2,214 additions & 115 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ edition = "2018"
authors = ["The NuShell Contributors"]
license = "MIT"
description = "Formats nushell extremely fast"
homepage = "https://github.com/fdncred/nufmt"
repository = "https://github.com/fdncred/nufmt"
homepage = "https://github.com/nushell/nufmt"
repository = "https://github.com/nushell/nufmt"
readme = "README.md"
keywords = ["nu", "nushell", "formatting", "cli"]
categories = ["command-line-utilities"]
@@ -18,8 +18,14 @@ anyhow = "1.0.71"
clap = { version = "4.3.0", optional = true, features = ["unicode", "derive"] }
env_logger = "0.10.0"
log = "0.4.17"
nu-parser = "0.81.0"
nu-protocol = "0.81.0"
nu-cli = { path = "../nushell/crates/nu-cli", version = "0.81.1", features = [
"plugin",
] }
nu-cmd-lang = { path = "../nushell/crates/nu-cmd-lang", version = "0.81.1" }
nu-command = { path = "../nushell/crates/nu-command", version = "0.81.1" }
nu-explore = { path = "../nushell/crates/nu-explore", version = "0.81.1" }
nu-parser = { path = "../nushell/crates/nu-parser", version = "0.81.1" }
nu-protocol = { path = "../nushell/crates/nu-protocol", version = "0.81.1" }

[dev-dependencies]
criterion = "0.5.1"
31 changes: 26 additions & 5 deletions src/formatting.rs
Original file line number Diff line number Diff line change
@@ -2,19 +2,23 @@
//!
//! It has functions to format slice of bytes and some help functions to separate concerns while doing the job.
use crate::config::Config;
use log::{info, trace};
use log::{error, info, trace};
use nu_parser::{flatten_block, parse, FlatShape};
use nu_protocol::{
ast::Block,
engine::{self, StateWorkingSet},
engine::{EngineState, StateWorkingSet},
Span,
};

fn get_engine_state() -> EngineState {
nu_cmd_lang::create_default_context()
}

/// format an array of bytes
///
/// Reading the file gives you a list of bytes
pub(crate) fn format_inner(contents: &[u8], _config: &Config) -> Vec<u8> {
let engine_state = engine::EngineState::new();
let engine_state = get_engine_state();
let mut working_set = StateWorkingSet::new(&engine_state);

let parsed_block = parse(&mut working_set, None, contents, false);
@@ -60,21 +64,38 @@ pub(crate) fn format_inner(contents: &[u8], _config: &Config) -> Vec<u8> {
trace!("shape contents: {:?}", &content);

match shape {
FlatShape::String | FlatShape::Int | FlatShape::Nothing => out.extend(c_bites),
FlatShape::Int | FlatShape::Nothing | FlatShape::Block => out.extend(c_bites),
FlatShape::List | FlatShape::Record => {
c_bites = trim_ascii_whitespace(c_bites);
let printable = String::from_utf8_lossy(c_bites).to_string();
trace!("stripped the whitespace, result: {:?}", printable);
out.extend(c_bites);
}
FlatShape::String => {
out.extend(c_bites);
// add a space after the string, so the parser doen't misleads the string into a long thingy
out.extend(b" ");
}
FlatShape::Pipe => {
out.extend(b"| ");
}
FlatShape::External | FlatShape::ExternalArg => {
FlatShape::InternalCall(declid) => {
trace!("Called Internal call with {declid}");
out.extend(c_bites);
// add a space after "external def", etc
out.extend(b" ");
}
FlatShape::External | FlatShape::ExternalArg | FlatShape::Signature => {
out.extend(c_bites);
out.extend(b" ");
}
FlatShape::VarDecl(varid) | FlatShape::Variable(varid) => {
trace!("Called variable or vardecl with {varid}");
out.extend(c_bites);
out.extend(b" ");
}
FlatShape::Garbage => {
error!("found garbage 😢 {content}");
out.extend(c_bites);
out = insert_newline(out);
}