Skip to content

Commit

Permalink
Add print IR and print comments options
Browse files Browse the repository at this point in the history
Tested with `cargo run --bin ruff_python_formatter -- --print-ir --print-comments scratch.py`
  • Loading branch information
konstin committed Jun 2, 2023
1 parent 8cb8572 commit 0faf080
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
51 changes: 51 additions & 0 deletions crates/ruff_python_formatter/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#![allow(clippy::print_stdout)]

use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use clap::{command, Parser, ValueEnum};
use rustpython_parser::lexer::lex;
use rustpython_parser::{parse_tokens, Mode};

use ruff_formatter::SourceCode;
use ruff_python_ast::source_code::CommentRangesBuilder;

use crate::format_node;

#[derive(ValueEnum, Clone, Debug)]
pub enum Emit {
Expand All @@ -21,4 +31,45 @@ pub struct Cli {
/// a diff if formatting is required.
#[clap(long)]
pub check: bool,
#[clap(long)]
pub print_ir: bool,
#[clap(long)]
pub print_comments: bool,
}

pub fn format_and_debug_print(input: &str, cli: &Cli) -> Result<String> {
let mut tokens = Vec::new();
let mut comment_ranges = CommentRangesBuilder::default();

for result in lex(input, Mode::Module) {
let (token, range) = match result {
Ok((token, range)) => (token, range),
Err(err) => bail!("Source contains syntax errors {err:?}"),
};

comment_ranges.visit_token(&token, range);
tokens.push(Ok((token, range)));
}

let comment_ranges = comment_ranges.finish();

// Parse the AST.
let python_ast = parse_tokens(tokens, Mode::Module, "<filename>")
.with_context(|| "Syntax error in input")?;

let formatted = format_node(&python_ast, &comment_ranges, input)?;
if cli.print_ir {
println!("{}", formatted.document().display(SourceCode::new(input)));
}
if cli.print_comments {
println!(
"{:?}",
formatted.context().comments().debug(SourceCode::new(input))
);
}
Ok(formatted
.print()
.with_context(|| "Failed to print the formatter IR")?
.as_code()
.to_string())
}
19 changes: 9 additions & 10 deletions crates/ruff_python_formatter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::{fs, io};
use anyhow::{bail, Context, Result};
use clap::Parser as ClapParser;

use ruff_python_formatter::cli::{Cli, Emit};
use ruff_python_formatter::format_module;
use ruff_python_formatter::cli::{format_and_debug_print, Cli, Emit};

/// Read a `String` from `stdin`.
pub(crate) fn read_from_stdin() -> Result<String> {
Expand All @@ -26,23 +25,23 @@ fn main() -> Result<()> {
);
}
let input = read_from_stdin()?;
let formatted = format_module(&input)?;
let formatted = format_and_debug_print(&input, &cli)?;
if cli.check {
if formatted.as_code() == input {
if formatted == input {
return Ok(());
}
bail!("Content not correctly formatted")
}
stdout().lock().write_all(formatted.as_code().as_bytes())?;
stdout().lock().write_all(formatted.as_bytes())?;
} else {
for file in cli.files {
let unformatted = fs::read_to_string(&file)
for file in &cli.files {
let input = fs::read_to_string(file)
.with_context(|| format!("Could not read {}: ", file.display()))?;
let formatted = format_module(&unformatted)?;
let formatted = format_and_debug_print(&input, &cli)?;
match cli.emit {
Some(Emit::Stdout) => stdout().lock().write_all(formatted.as_code().as_bytes())?,
Some(Emit::Stdout) => stdout().lock().write_all(formatted.as_bytes())?,
None | Some(Emit::Files) => {
fs::write(&file, formatted.as_code().as_bytes()).with_context(|| {
fs::write(file, formatted.as_bytes()).with_context(|| {
format!("Could not write to {}, exiting", file.display())
})?;
}
Expand Down

0 comments on commit 0faf080

Please sign in to comment.