mirror of
https://github.com/RGBCube/alejandra
synced 2025-08-01 21:17:45 +00:00
refactor: remove layout and config
This commit is contained in:
parent
bafc3ac5a5
commit
55cb958dff
27 changed files with 440 additions and 741 deletions
|
@ -35,24 +35,20 @@ pub fn parse(args: Vec<String>) -> clap::ArgMatches {
|
||||||
.get_matches_from(args)
|
.get_matches_from(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stdin(config: alejandra_engine::config::Config) -> std::io::Result<()> {
|
pub fn stdin() -> std::io::Result<()> {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
eprintln!("Formatting stdin, run with --help to see all options.");
|
eprintln!("Formatting stdin, run with --help to see all options.");
|
||||||
let mut stdin = String::new();
|
let mut stdin = String::new();
|
||||||
std::io::stdin().read_to_string(&mut stdin).unwrap();
|
std::io::stdin().read_to_string(&mut stdin).unwrap();
|
||||||
|
|
||||||
let stdout =
|
let stdout = alejandra_engine::format::string("stdin".to_string(), stdin)?;
|
||||||
alejandra_engine::format::string(&config, "stdin".to_string(), stdin)?;
|
|
||||||
print!("{}", stdout);
|
print!("{}", stdout);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simple(
|
pub fn simple(paths: Vec<String>) -> std::io::Result<()> {
|
||||||
config: alejandra_engine::config::Config,
|
|
||||||
paths: Vec<String>,
|
|
||||||
) -> std::io::Result<()> {
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
|
||||||
eprintln!("Formatting {} files.", paths.len());
|
eprintln!("Formatting {} files.", paths.len());
|
||||||
|
@ -60,14 +56,12 @@ pub fn simple(
|
||||||
let (results, errors): (Vec<_>, Vec<_>) = paths
|
let (results, errors): (Vec<_>, Vec<_>) = paths
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|path| -> std::io::Result<bool> {
|
.map(|path| -> std::io::Result<bool> {
|
||||||
alejandra_engine::format::file(&config, path.to_string()).map(
|
alejandra_engine::format::file(path.to_string()).map(|changed| {
|
||||||
|changed| {
|
if changed {
|
||||||
if changed {
|
eprintln!("Formatted: {}", &path);
|
||||||
eprintln!("Formatted: {}", &path);
|
}
|
||||||
}
|
changed
|
||||||
changed
|
})
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.partition(Result::is_ok);
|
.partition(Result::is_ok);
|
||||||
|
|
||||||
|
@ -80,10 +74,7 @@ pub fn simple(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tui(
|
pub fn tui(paths: Vec<String>) -> std::io::Result<()> {
|
||||||
config: alejandra_engine::config::Config,
|
|
||||||
paths: Vec<String>,
|
|
||||||
) -> std::io::Result<()> {
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use termion::{input::TermRead, raw::IntoRawMode};
|
use termion::{input::TermRead, raw::IntoRawMode};
|
||||||
|
|
||||||
|
@ -137,7 +128,7 @@ pub fn tui(
|
||||||
let sender_finished = sender;
|
let sender_finished = sender;
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
paths.into_par_iter().for_each_with(sender_paths, |sender, path| {
|
paths.into_par_iter().for_each_with(sender_paths, |sender, path| {
|
||||||
let result = alejandra_engine::format::file(&config, path.clone());
|
let result = alejandra_engine::format::file(path.clone());
|
||||||
|
|
||||||
if let Err(error) = sender
|
if let Err(error) = sender
|
||||||
.send(Event::FormattedPath(FormattedPath { path, result }))
|
.send(Event::FormattedPath(FormattedPath { path, result }))
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub fn nix_files(include: Vec<&str>, exclude: Vec<&str>) -> Vec<String> {
|
||||||
exclude.iter().flat_map(nix_files_in_path).collect();
|
exclude.iter().flat_map(nix_files_in_path).collect();
|
||||||
|
|
||||||
let mut paths: Vec<String> =
|
let mut paths: Vec<String> =
|
||||||
include.difference(&exclude).map(|path| path.clone()).collect();
|
include.difference(&exclude).cloned().collect();
|
||||||
|
|
||||||
paths.sort();
|
paths.sort();
|
||||||
paths
|
paths
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let matches = alejandra_cli::cli::parse(std::env::args().collect());
|
let matches = alejandra_cli::cli::parse(std::env::args().collect());
|
||||||
|
|
||||||
let config = alejandra_engine::config::Config::default();
|
|
||||||
|
|
||||||
match matches.values_of("include") {
|
match matches.values_of("include") {
|
||||||
Some(include) => {
|
Some(include) => {
|
||||||
let include = include.collect();
|
let include = include.collect();
|
||||||
|
@ -18,13 +16,13 @@ fn main() -> std::io::Result<()> {
|
||||||
&& atty::is(atty::Stream::Stdin)
|
&& atty::is(atty::Stream::Stdin)
|
||||||
&& atty::is(atty::Stream::Stdout)
|
&& atty::is(atty::Stream::Stdout)
|
||||||
{
|
{
|
||||||
alejandra_cli::cli::tui(config, paths)?;
|
alejandra_cli::cli::tui(paths)?;
|
||||||
} else {
|
} else {
|
||||||
alejandra_cli::cli::simple(config, paths)?;
|
alejandra_cli::cli::simple(paths)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
alejandra_cli::cli::stdin(config)?;
|
alejandra_cli::cli::stdin()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,39 +13,46 @@ pub enum Step {
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct BuildCtx {
|
pub struct BuildCtx {
|
||||||
pub config: crate::config::Config,
|
|
||||||
pub force_wide: bool,
|
pub force_wide: bool,
|
||||||
pub indentation: usize,
|
pub indentation: usize,
|
||||||
pub pos_new: crate::position::Position,
|
pub pos_new: crate::position::Position,
|
||||||
pub pos_old: crate::position::Position,
|
pub pos_old: crate::position::Position,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
pub vertical: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuildCtx {
|
impl BuildCtx {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
config: crate::config::Config,
|
|
||||||
force_wide: bool,
|
force_wide: bool,
|
||||||
path: String,
|
path: String,
|
||||||
pos_new: crate::position::Position,
|
pos_new: crate::position::Position,
|
||||||
pos_old: crate::position::Position,
|
pos_old: crate::position::Position,
|
||||||
|
vertical: bool,
|
||||||
) -> BuildCtx {
|
) -> BuildCtx {
|
||||||
BuildCtx { config, force_wide, indentation: 0, path, pos_new, pos_old }
|
BuildCtx {
|
||||||
|
force_wide,
|
||||||
|
indentation: 0,
|
||||||
|
path,
|
||||||
|
pos_new,
|
||||||
|
pos_old,
|
||||||
|
vertical,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(
|
pub fn build(
|
||||||
config: &crate::config::Config,
|
|
||||||
element: rnix::SyntaxElement,
|
element: rnix::SyntaxElement,
|
||||||
force_wide: bool,
|
force_wide: bool,
|
||||||
path: String,
|
path: String,
|
||||||
|
vertical: bool,
|
||||||
) -> Option<rowan::GreenNode> {
|
) -> Option<rowan::GreenNode> {
|
||||||
let mut builder = rowan::GreenNodeBuilder::new();
|
let mut builder = rowan::GreenNodeBuilder::new();
|
||||||
let mut build_ctx = BuildCtx::new(
|
let mut build_ctx = BuildCtx::new(
|
||||||
config.clone(),
|
|
||||||
force_wide,
|
force_wide,
|
||||||
path,
|
path,
|
||||||
crate::position::Position::default(),
|
crate::position::Position::default(),
|
||||||
crate::position::Position::default(),
|
crate::position::Position::default(),
|
||||||
|
vertical,
|
||||||
);
|
);
|
||||||
|
|
||||||
build_step(
|
build_step(
|
||||||
|
@ -267,15 +274,10 @@ fn format_wider(
|
||||||
) {
|
) {
|
||||||
match element {
|
match element {
|
||||||
rnix::SyntaxElement::Node(node) => {
|
rnix::SyntaxElement::Node(node) => {
|
||||||
let layout = if fits_in_single_line(build_ctx, node.clone().into())
|
|
||||||
{
|
|
||||||
crate::config::Layout::Wide
|
|
||||||
} else {
|
|
||||||
crate::config::Layout::Tall
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut build_ctx_clone = build_ctx.clone();
|
let mut build_ctx_clone = build_ctx.clone();
|
||||||
build_ctx_clone.config = build_ctx.config.with_layout(layout);
|
build_ctx_clone.vertical =
|
||||||
|
!fits_in_single_line(build_ctx, node.clone().into());
|
||||||
|
|
||||||
format(builder, &mut build_ctx_clone, element);
|
format(builder, &mut build_ctx_clone, element);
|
||||||
}
|
}
|
||||||
rnix::SyntaxElement::Token(_) => {
|
rnix::SyntaxElement::Token(_) => {
|
||||||
|
@ -289,12 +291,7 @@ pub fn fits_in_single_line(
|
||||||
node: rnix::SyntaxElement,
|
node: rnix::SyntaxElement,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let line = build_ctx.pos_new.line;
|
let line = build_ctx.pos_new.line;
|
||||||
let maybe_green_node = build(
|
let maybe_green_node = build(node, true, build_ctx.path.clone(), false);
|
||||||
&build_ctx.config.with_layout(crate::config::Layout::Wide),
|
|
||||||
node,
|
|
||||||
true,
|
|
||||||
build_ctx.path.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
match maybe_green_node {
|
match maybe_green_node {
|
||||||
Some(_) => build_ctx.pos_new.line == line,
|
Some(_) => build_ctx.pos_new.line == line,
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub enum Layout {
|
|
||||||
Tall,
|
|
||||||
Wide,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Config {
|
|
||||||
layout: Layout,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Config {
|
|
||||||
fn default() -> Config {
|
|
||||||
Config { layout: Layout::Tall }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn layout(&self) -> &Layout {
|
|
||||||
&self.layout
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_layout(&self, layout: Layout) -> Config {
|
|
||||||
Config { layout }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +1,4 @@
|
||||||
pub fn string(
|
pub fn string(path: String, string: String) -> std::io::Result<String> {
|
||||||
config: &crate::config::Config,
|
|
||||||
path: String,
|
|
||||||
string: String,
|
|
||||||
) -> std::io::Result<String> {
|
|
||||||
let tokens = rnix::tokenizer::Tokenizer::new(&string);
|
let tokens = rnix::tokenizer::Tokenizer::new(&string);
|
||||||
let ast = rnix::parser::parse(tokens);
|
let ast = rnix::parser::parse(tokens);
|
||||||
|
|
||||||
|
@ -15,33 +11,26 @@ pub fn string(
|
||||||
}
|
}
|
||||||
|
|
||||||
let green_node =
|
let green_node =
|
||||||
crate::builder::build(config, ast.node().into(), false, path).unwrap();
|
crate::builder::build(ast.node().into(), false, path, true).unwrap();
|
||||||
|
|
||||||
Ok(green_node.to_string())
|
Ok(green_node.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn string_or_passthrough(
|
pub fn string_or_passthrough(path: String, before: String) -> String {
|
||||||
config: &crate::config::Config,
|
match crate::format::string(path, before.clone()) {
|
||||||
path: String,
|
|
||||||
before: String,
|
|
||||||
) -> String {
|
|
||||||
match crate::format::string(config, path, before.clone()) {
|
|
||||||
Ok(after) => after,
|
Ok(after) => after,
|
||||||
Err(_) => before,
|
Err(_) => before,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file(
|
pub fn file(path: String) -> std::io::Result<bool> {
|
||||||
config: &crate::config::Config,
|
|
||||||
path: String,
|
|
||||||
) -> std::io::Result<bool> {
|
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
let input = std::fs::read_to_string(&path)?;
|
let input = std::fs::read_to_string(&path)?;
|
||||||
let input_clone = input.clone();
|
let input_clone = input.clone();
|
||||||
let input_bytes = input_clone.as_bytes();
|
let input_bytes = input_clone.as_bytes();
|
||||||
|
|
||||||
let output = crate::format::string(config, path.clone(), input)?;
|
let output = crate::format::string(path.clone(), input)?;
|
||||||
let output_bytes = output.as_bytes();
|
let output_bytes = output.as_bytes();
|
||||||
|
|
||||||
let changed = input_bytes != output_bytes;
|
let changed = input_bytes != output_bytes;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
pub mod builder;
|
pub mod builder;
|
||||||
pub mod children;
|
pub mod children;
|
||||||
pub mod config;
|
|
||||||
pub mod format;
|
pub mod format;
|
||||||
pub mod parsers;
|
pub mod parsers;
|
||||||
pub mod position;
|
pub mod position;
|
||||||
|
|
|
@ -1,23 +1,13 @@
|
||||||
use std::collections::LinkedList;
|
use std::collections::LinkedList;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Argument {
|
pub struct Argument {
|
||||||
pub comments_before: LinkedList<String>,
|
pub comments_before: LinkedList<String>,
|
||||||
pub item: Option<rnix::SyntaxElement>,
|
pub item: Option<rnix::SyntaxElement>,
|
||||||
pub comment_after: Option<String>,
|
pub comment_after: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Argument {
|
#[derive(Debug, Default)]
|
||||||
fn default() -> Argument {
|
|
||||||
Argument {
|
|
||||||
comments_before: LinkedList::new(),
|
|
||||||
item: None,
|
|
||||||
comment_after: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Pattern {
|
pub struct Pattern {
|
||||||
pub initial_at: Option<rnix::SyntaxElement>,
|
pub initial_at: Option<rnix::SyntaxElement>,
|
||||||
pub comments_after_initial_at: LinkedList<String>,
|
pub comments_after_initial_at: LinkedList<String>,
|
||||||
|
@ -27,19 +17,6 @@ pub struct Pattern {
|
||||||
pub end_at: Option<rnix::SyntaxElement>,
|
pub end_at: Option<rnix::SyntaxElement>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Pattern {
|
|
||||||
fn default() -> Pattern {
|
|
||||||
Pattern {
|
|
||||||
initial_at: None,
|
|
||||||
comments_after_initial_at: LinkedList::new(),
|
|
||||||
arguments: LinkedList::new(),
|
|
||||||
comments_before_curly_b_close: LinkedList::new(),
|
|
||||||
comments_before_end_at: LinkedList::new(),
|
|
||||||
end_at: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(
|
pub fn parse(
|
||||||
build_ctx: &crate::builder::BuildCtx,
|
build_ctx: &crate::builder::BuildCtx,
|
||||||
node: &rnix::SyntaxNode,
|
node: &rnix::SyntaxNode,
|
||||||
|
|
|
@ -8,21 +8,17 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// left
|
// left
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
|
||||||
crate::config::Layout::Tall => {
|
if vertical {
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
}
|
} else {
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -39,29 +35,26 @@ pub fn rule(
|
||||||
|
|
||||||
// right
|
// right
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if let rnix::SyntaxKind::TOKEN_COMMENT
|
||||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.element.kind()
|
||||||
| rnix::SyntaxKind::TOKEN_WHITESPACE = child_prev.element.kind()
|
{
|
||||||
{
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
} else if let rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
} else if let rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_STRING = child.element.kind()
|
||||||
| rnix::SyntaxKind::NODE_STRING = child.element.kind()
|
{
|
||||||
{
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
};
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
};
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -8,11 +8,9 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// with
|
// with
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
|
@ -39,13 +37,10 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ;
|
// ;
|
||||||
|
@ -66,49 +61,40 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if {
|
||||||
if {
|
matches!(
|
||||||
matches!(
|
child.element.kind(),
|
||||||
child.element.kind(),
|
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
||||||
rnix::SyntaxKind::NODE_ASSERT | rnix::SyntaxKind::NODE_WITH
|
)
|
||||||
)
|
} {
|
||||||
} {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
} else if comment
|
||||||
child.element,
|
|| !matches!(
|
||||||
));
|
child.element.kind(),
|
||||||
} else if comment
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
|| !matches!(
|
| rnix::SyntaxKind::NODE_IDENT
|
||||||
child.element.kind(),
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_IDENT
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_LITERAL
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
)
|
||||||
| rnix::SyntaxKind::NODE_LITERAL
|
{
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
)
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
{
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
} else {
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -27,14 +27,10 @@ pub fn rule(
|
||||||
})
|
})
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let layout = if items_count > 1
|
let vertical = items_count > 1
|
||||||
|| children.has_comments()
|
|| children.has_comments()
|
||||||
|| children.has_newlines()
|
|| children.has_newlines()
|
||||||
{
|
|| build_ctx.vertical;
|
||||||
&crate::config::Layout::Tall
|
|
||||||
} else {
|
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// rec
|
// rec
|
||||||
let child = children.peek_next().unwrap();
|
let child = children.peek_next().unwrap();
|
||||||
|
@ -66,11 +62,8 @@ pub fn rule(
|
||||||
// {
|
// {
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut item_index: usize = 0;
|
let mut item_index: usize = 0;
|
||||||
|
@ -107,23 +100,18 @@ pub fn rule(
|
||||||
|
|
||||||
// item
|
// item
|
||||||
item_index += 1;
|
item_index += 1;
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
} else {
|
||||||
}
|
if item_index > 1 {
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
if item_index > 1 {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
}
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
children.move_next();
|
children.move_next();
|
||||||
inline_next_comment = true;
|
inline_next_comment = true;
|
||||||
}
|
}
|
||||||
|
@ -131,13 +119,10 @@ pub fn rule(
|
||||||
|
|
||||||
// }
|
// }
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
|
|
@ -16,39 +16,32 @@ pub fn rule_with_configuration(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
let kind = child.element.kind();
|
||||||
let kind = child.element.kind();
|
|
||||||
|
|
||||||
if (parent_kind == "bin_op_and_or_default"
|
if (parent_kind == "bin_op_and_or_default"
|
||||||
&& matches!(
|
&& matches!(
|
||||||
kind,
|
kind,
|
||||||
rnix::SyntaxKind::NODE_BIN_OP
|
rnix::SyntaxKind::NODE_BIN_OP
|
||||||
| rnix::SyntaxKind::NODE_OR_DEFAULT
|
| rnix::SyntaxKind::NODE_OR_DEFAULT
|
||||||
))
|
))
|
||||||
|| (parent_kind == "select"
|
|| (parent_kind == "select"
|
||||||
&& matches!(kind, rnix::SyntaxKind::NODE_SELECT))
|
&& matches!(kind, rnix::SyntaxKind::NODE_SELECT))
|
||||||
{
|
{
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
}
|
}
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -63,13 +56,9 @@ pub fn rule_with_configuration(
|
||||||
|
|
||||||
// operator
|
// operator
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {}
|
} else if parent_kind == "bin_op_and_or_default" {
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
if parent_kind == "bin_op_and_or_default" {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
@ -94,13 +83,10 @@ pub fn rule_with_configuration(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -8,22 +8,17 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// ${
|
// ${
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -38,13 +33,10 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -59,13 +51,10 @@ pub fn rule(
|
||||||
|
|
||||||
// }
|
// }
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
|
|
@ -8,20 +8,15 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// inherit
|
// inherit
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -37,35 +32,27 @@ pub fn rule(
|
||||||
|
|
||||||
if let Some(child) = children.get_next() {
|
if let Some(child) = children.get_next() {
|
||||||
// expr
|
// expr
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
} else {
|
||||||
}
|
if let rnix::SyntaxKind::TOKEN_SEMICOLON = child.element.kind()
|
||||||
crate::config::Layout::Wide => {
|
{
|
||||||
if let rnix::SyntaxKind::TOKEN_SEMICOLON =
|
} else {
|
||||||
child.element.kind()
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
{
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
}
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -8,21 +8,16 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// a
|
// a
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -76,39 +71,36 @@ pub fn rule(
|
||||||
let mut dedent = false;
|
let mut dedent = false;
|
||||||
steps.push_back(crate::builder::Step::Format(child_equal.element));
|
steps.push_back(crate::builder::Step::Format(child_equal.element));
|
||||||
|
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if !comments_before.is_empty() || !comments_after.is_empty() {
|
||||||
if !comments_before.is_empty() || !comments_after.is_empty() {
|
dedent = true;
|
||||||
dedent = true;
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
} else if matches!(
|
||||||
} else if matches!(
|
child_expr.element.kind(),
|
||||||
child_expr.element.kind(),
|
rnix::SyntaxKind::NODE_ASSERT
|
||||||
rnix::SyntaxKind::NODE_ASSERT
|
| rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
| rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_LAMBDA
|
||||||
| rnix::SyntaxKind::NODE_LAMBDA
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
| rnix::SyntaxKind::NODE_WITH
|
||||||
| rnix::SyntaxKind::NODE_WITH
|
) || (matches!(
|
||||||
) || (matches!(
|
child_expr.element.kind(),
|
||||||
child_expr.element.kind(),
|
rnix::SyntaxKind::NODE_APPLY
|
||||||
rnix::SyntaxKind::NODE_APPLY
|
) && !newlines)
|
||||||
) && !newlines)
|
{
|
||||||
{
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
} else {
|
|
||||||
dedent = true;
|
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
} else {
|
||||||
|
dedent = true;
|
||||||
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -119,19 +111,14 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child_expr.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
if !comments_after.is_empty() {
|
||||||
child_expr.element,
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
));
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
if !comments_after.is_empty() {
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child_expr.element));
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Format(child_expr.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
|
|
@ -8,21 +8,16 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// a
|
// a
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let rnix::SyntaxKind::TOKEN_COMMENT
|
if let rnix::SyntaxKind::TOKEN_COMMENT
|
||||||
|
@ -61,53 +56,46 @@ pub fn rule(
|
||||||
|
|
||||||
// c
|
// c
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if comment
|
||||||
if comment
|
|| !matches!(
|
||||||
|| !matches!(
|
child.element.kind(),
|
||||||
child.element.kind(),
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_LAMBDA
|
||||||
| rnix::SyntaxKind::NODE_LAMBDA
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_LITERAL
|
||||||
| rnix::SyntaxKind::NODE_LITERAL
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
)
|
||||||
)
|
{
|
||||||
{
|
let should_indent = !matches!(
|
||||||
let should_indent = !matches!(
|
child.element.kind(),
|
||||||
child.element.kind(),
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_LAMBDA
|
||||||
| rnix::SyntaxKind::NODE_LAMBDA
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
) && build_ctx.pos_new.column > 1;
|
||||||
) && build_ctx.pos_new.column > 1;
|
|
||||||
|
|
||||||
if should_indent {
|
if should_indent {
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
}
|
|
||||||
|
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
if should_indent {
|
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
|
||||||
child.element,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
|
if should_indent {
|
||||||
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -20,23 +20,16 @@ pub fn rule(
|
||||||
})
|
})
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
let layout = if items_count > 1
|
let vertical = items_count > 1
|
||||||
|| children.has_comments()
|
|| children.has_comments()
|
||||||
|| children.has_newlines()
|
|| children.has_newlines()
|
||||||
{
|
|| build_ctx.vertical;
|
||||||
&crate::config::Layout::Tall
|
|
||||||
} else {
|
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// let
|
// let
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut item_index: usize = 0;
|
let mut item_index: usize = 0;
|
||||||
|
@ -71,19 +64,15 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
item_index += 1;
|
item_index += 1;
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
children.move_next();
|
children.move_next();
|
||||||
|
@ -91,15 +80,12 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// in
|
// in
|
||||||
|
@ -120,27 +106,24 @@ pub fn rule(
|
||||||
// in
|
// in
|
||||||
let mut dedent = false;
|
let mut dedent = false;
|
||||||
steps.push_back(crate::builder::Step::Format(child_in.element));
|
steps.push_back(crate::builder::Step::Format(child_in.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if child_comments.is_empty()
|
||||||
if child_comments.is_empty()
|
&& matches!(
|
||||||
&& matches!(
|
child_expr.element.kind(),
|
||||||
child_expr.element.kind(),
|
rnix::SyntaxKind::NODE_ATTR_SET
|
||||||
rnix::SyntaxKind::NODE_ATTR_SET
|
| rnix::SyntaxKind::NODE_LET_IN
|
||||||
| rnix::SyntaxKind::NODE_LET_IN
|
| rnix::SyntaxKind::NODE_LIST
|
||||||
| rnix::SyntaxKind::NODE_LIST
|
| rnix::SyntaxKind::NODE_PAREN
|
||||||
| rnix::SyntaxKind::NODE_PAREN
|
| rnix::SyntaxKind::NODE_STRING
|
||||||
| rnix::SyntaxKind::NODE_STRING
|
)
|
||||||
)
|
{
|
||||||
{
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
} else {
|
||||||
} else {
|
dedent = true;
|
||||||
dedent = true;
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -151,19 +134,14 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child_expr.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
if dedent {
|
||||||
child_expr.element,
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
));
|
|
||||||
if dedent {
|
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
steps.push_back(crate::builder::Step::Format(child_expr.element));
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
|
steps.push_back(crate::builder::Step::Format(child_expr.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
steps
|
steps
|
||||||
|
|
|
@ -16,20 +16,15 @@ pub fn rule(
|
||||||
.count()
|
.count()
|
||||||
- 2;
|
- 2;
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// [
|
// [
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut item_index: usize = 0;
|
let mut item_index: usize = 0;
|
||||||
|
@ -73,21 +68,17 @@ pub fn rule(
|
||||||
|
|
||||||
// item
|
// item
|
||||||
item_index += 1;
|
item_index += 1;
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
} else {
|
||||||
}
|
if item_index > 1 {
|
||||||
crate::config::Layout::Wide => {
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
if item_index > 1 {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
}
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
children.move_next();
|
children.move_next();
|
||||||
|
@ -97,13 +88,10 @@ pub fn rule(
|
||||||
|
|
||||||
// ]
|
// ]
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
|
|
@ -27,10 +27,8 @@ pub fn default(
|
||||||
let mut children = crate::children::Children::new(build_ctx, node);
|
let mut children = crate::children::Children::new(build_ctx, node);
|
||||||
|
|
||||||
while let Some(child) = children.get_next() {
|
while let Some(child) = children.get_next() {
|
||||||
let step = match build_ctx.config.layout() {
|
let step = match build_ctx.vertical {
|
||||||
crate::config::Layout::Tall => {
|
true => crate::builder::Step::FormatWider(child.element),
|
||||||
crate::builder::Step::FormatWider(child.element)
|
|
||||||
}
|
|
||||||
_ => crate::builder::Step::Format(child.element),
|
_ => crate::builder::Step::Format(child.element),
|
||||||
};
|
};
|
||||||
steps.push_back(step);
|
steps.push_back(step);
|
||||||
|
|
|
@ -11,22 +11,13 @@ pub fn rule(
|
||||||
let has_comments_or_newlines =
|
let has_comments_or_newlines =
|
||||||
children.has_comments() || children.has_newlines();
|
children.has_comments() || children.has_newlines();
|
||||||
|
|
||||||
let layout = if has_comments_or_newlines {
|
let vertical = has_comments_or_newlines || build_ctx.vertical;
|
||||||
&crate::config::Layout::Tall
|
|
||||||
} else {
|
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// (
|
// (
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical && has_comments_or_newlines {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
if has_comments_or_newlines {
|
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -41,17 +32,14 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if has_comments_or_newlines {
|
||||||
if has_comments_or_newlines {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
}
|
||||||
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
|
} else {
|
||||||
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -66,15 +54,10 @@ pub fn rule(
|
||||||
|
|
||||||
// )
|
// )
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical && has_comments_or_newlines {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
if has_comments_or_newlines {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
|
|
@ -8,20 +8,15 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -44,13 +39,10 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
|
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
children.move_prev();
|
children.move_prev();
|
||||||
|
|
||||||
|
|
|
@ -8,21 +8,16 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if children.has_next() {
|
if children.has_next() {
|
||||||
|
@ -90,15 +85,10 @@ pub fn rule(
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
}
|
}
|
||||||
|
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
} else {
|
||||||
child.element,
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
));
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if dedent {
|
if dedent {
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
|
|
|
@ -29,25 +29,18 @@ pub fn rule(
|
||||||
|
|
||||||
let arguments_count_for_tall = if has_ellipsis { 2 } else { 1 };
|
let arguments_count_for_tall = if has_ellipsis { 2 } else { 1 };
|
||||||
|
|
||||||
let layout = if has_comments
|
let vertical = has_comments
|
||||||
|| arguments_count > arguments_count_for_tall
|
|| arguments_count > arguments_count_for_tall
|
||||||
|| (arguments_count > 0 && has_comments_between_curly_b)
|
|| (arguments_count > 0 && has_comments_between_curly_b)
|
||||||
{
|
|| build_ctx.vertical;
|
||||||
&crate::config::Layout::Tall
|
|
||||||
} else {
|
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// x @
|
// x @
|
||||||
if let Some(element) = &pattern.initial_at {
|
if let Some(element) = &pattern.initial_at {
|
||||||
let element = element.clone();
|
let element = element.clone();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,26 +62,17 @@ pub fn rule(
|
||||||
rnix::SyntaxKind::TOKEN_CURLY_B_OPEN,
|
rnix::SyntaxKind::TOKEN_CURLY_B_OPEN,
|
||||||
"{".to_string(),
|
"{".to_string(),
|
||||||
));
|
));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
}
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// arguments
|
// arguments
|
||||||
let mut index = 0;
|
for (index, argument) in pattern.arguments.into_iter().enumerate() {
|
||||||
for argument in pattern.arguments {
|
if vertical {
|
||||||
match layout {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
} else if index > 0 {
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
steps.push_back(crate::builder::Step::Whitespace);
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
if index > 0 {
|
|
||||||
steps.push_back(crate::builder::Step::Whitespace);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -103,33 +87,25 @@ pub fn rule(
|
||||||
// argument
|
// argument
|
||||||
let element = argument.item.unwrap();
|
let element = argument.item.unwrap();
|
||||||
let element_kind = element.kind();
|
let element_kind = element.kind();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(element));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// ,
|
// ,
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
if !matches!(element_kind, rnix::SyntaxKind::TOKEN_ELLIPSIS) {
|
||||||
if !matches!(element_kind, rnix::SyntaxKind::TOKEN_ELLIPSIS) {
|
steps.push_back(crate::builder::Step::Token(
|
||||||
steps.push_back(crate::builder::Step::Token(
|
rnix::SyntaxKind::TOKEN_COMMA,
|
||||||
rnix::SyntaxKind::TOKEN_COMMA,
|
",".to_string(),
|
||||||
",".to_string(),
|
));
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
if index + 1 < arguments_count {
|
|
||||||
steps.push_back(crate::builder::Step::Token(
|
|
||||||
rnix::SyntaxKind::TOKEN_COMMA,
|
|
||||||
",".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else if index + 1 < arguments_count {
|
||||||
|
steps.push_back(crate::builder::Step::Token(
|
||||||
|
rnix::SyntaxKind::TOKEN_COMMA,
|
||||||
|
",".to_string(),
|
||||||
|
));
|
||||||
};
|
};
|
||||||
|
|
||||||
// possible inline comment
|
// possible inline comment
|
||||||
|
@ -142,8 +118,6 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Comment(text));
|
steps.push_back(crate::builder::Step::Comment(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
index += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -156,16 +130,13 @@ pub fn rule(
|
||||||
}
|
}
|
||||||
|
|
||||||
// }
|
// }
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
if arguments_count > 0 || has_comments_before_curly_b_close {
|
||||||
if arguments_count > 0 || has_comments_before_curly_b_close {
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
crate::config::Layout::Wide => {}
|
}
|
||||||
};
|
|
||||||
steps.push_back(crate::builder::Step::Token(
|
steps.push_back(crate::builder::Step::Token(
|
||||||
rnix::SyntaxKind::TOKEN_CURLY_B_OPEN,
|
rnix::SyntaxKind::TOKEN_CURLY_B_OPEN,
|
||||||
"}".to_string(),
|
"}".to_string(),
|
||||||
|
@ -188,13 +159,10 @@ pub fn rule(
|
||||||
|
|
||||||
// @ x
|
// @ x
|
||||||
if let Some(element) = pattern.end_at {
|
if let Some(element) = pattern.end_at {
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,9 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
while children.has_next() {
|
while children.has_next() {
|
||||||
children.drain_comments_and_newlines(|element| match element {
|
children.drain_comments_and_newlines(|element| match element {
|
||||||
|
@ -25,17 +23,13 @@ pub fn rule(
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(child) = children.get_next() {
|
if let Some(child) = children.get_next() {
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,16 +19,12 @@ pub fn rule(
|
||||||
|
|
||||||
if text == "\"" {
|
if text == "\"" {
|
||||||
while let Some(child) = children.get_next() {
|
while let Some(child) = children.get_next() {
|
||||||
match build_ctx.config.layout() {
|
if build_ctx.vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(
|
||||||
steps.push_back(crate::builder::Step::FormatWider(
|
child.element,
|
||||||
child.element,
|
));
|
||||||
));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps
|
|
||||||
.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -8,22 +8,17 @@ pub fn rule(
|
||||||
build_ctx, node, true,
|
build_ctx, node, true,
|
||||||
);
|
);
|
||||||
|
|
||||||
let layout = if children.has_comments() || children.has_newlines() {
|
let vertical = children.has_comments()
|
||||||
&crate::config::Layout::Tall
|
|| children.has_newlines()
|
||||||
} else {
|
|| build_ctx.vertical;
|
||||||
build_ctx.config.layout()
|
|
||||||
};
|
|
||||||
|
|
||||||
// ${
|
// ${
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Indent);
|
||||||
steps.push_back(crate::builder::Step::Indent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -38,13 +33,10 @@ pub fn rule(
|
||||||
|
|
||||||
// expr
|
// expr
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
||||||
steps.push_back(crate::builder::Step::FormatWider(child.element));
|
} else {
|
||||||
}
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
crate::config::Layout::Wide => {
|
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**/
|
// /**/
|
||||||
|
@ -59,13 +51,10 @@ pub fn rule(
|
||||||
|
|
||||||
// }
|
// }
|
||||||
let child = children.get_next().unwrap();
|
let child = children.get_next().unwrap();
|
||||||
match layout {
|
if vertical {
|
||||||
crate::config::Layout::Tall => {
|
steps.push_back(crate::builder::Step::Dedent);
|
||||||
steps.push_back(crate::builder::Step::Dedent);
|
steps.push_back(crate::builder::Step::NewLine);
|
||||||
steps.push_back(crate::builder::Step::NewLine);
|
steps.push_back(crate::builder::Step::Pad);
|
||||||
steps.push_back(crate::builder::Step::Pad);
|
|
||||||
}
|
|
||||||
crate::config::Layout::Wide => {}
|
|
||||||
}
|
}
|
||||||
steps.push_back(crate::builder::Step::Format(child.element));
|
steps.push_back(crate::builder::Step::Format(child.element));
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@ use std::io::Write;
|
||||||
fn cases() {
|
fn cases() {
|
||||||
let should_update = std::env::var("UPDATE").is_ok();
|
let should_update = std::env::var("UPDATE").is_ok();
|
||||||
|
|
||||||
let config = alejandra_engine::config::Config::default();
|
|
||||||
|
|
||||||
let cases: std::collections::HashSet<String> =
|
let cases: std::collections::HashSet<String> =
|
||||||
std::fs::read_dir("tests/cases")
|
std::fs::read_dir("tests/cases")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -17,7 +15,6 @@ fn cases() {
|
||||||
let path_out = format!("tests/cases/{}/out", case);
|
let path_out = format!("tests/cases/{}/out", case);
|
||||||
let content_in = std::fs::read_to_string(path_in.clone()).unwrap();
|
let content_in = std::fs::read_to_string(path_in.clone()).unwrap();
|
||||||
let content_got = alejandra_engine::format::string_or_passthrough(
|
let content_got = alejandra_engine::format::string_or_passthrough(
|
||||||
&config,
|
|
||||||
path_in,
|
path_in,
|
||||||
content_in.clone(),
|
content_in.clone(),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue