mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-14 15:40:20 +00:00
Formatter: Add SourceType to context to enable special formatting for stub files (#6331)
**Summary** This adds the information whether we're in a .py python source file or in a .pyi stub file to enable people working on #5822 and related issues. I'm not completely happy with `Default` for something that depends on the input. **Test Plan** None, this is currently unused, i'm leaving this to first implementation of stub file specific formatting. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
fe97a2a302
commit
1031bb6550
12 changed files with 137 additions and 97 deletions
|
@ -1,14 +1,14 @@
|
|||
#![allow(clippy::print_stdout)]
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use clap::{command, Parser, ValueEnum};
|
||||
use ruff_python_parser::lexer::lex;
|
||||
use ruff_python_parser::{parse_tokens, Mode};
|
||||
|
||||
use ruff_formatter::SourceCode;
|
||||
use ruff_python_index::CommentRangesBuilder;
|
||||
use ruff_python_parser::lexer::lex;
|
||||
use ruff_python_parser::{parse_tokens, Mode};
|
||||
|
||||
use crate::{format_node, PyFormatOptions};
|
||||
|
||||
|
@ -37,7 +37,7 @@ pub struct Cli {
|
|||
pub print_comments: bool,
|
||||
}
|
||||
|
||||
pub fn format_and_debug_print(input: &str, cli: &Cli) -> Result<String> {
|
||||
pub fn format_and_debug_print(input: &str, cli: &Cli, source_type: &Path) -> Result<String> {
|
||||
let mut tokens = Vec::new();
|
||||
let mut comment_ranges = CommentRangesBuilder::default();
|
||||
|
||||
|
@ -57,13 +57,9 @@ pub fn format_and_debug_print(input: &str, cli: &Cli) -> Result<String> {
|
|||
let python_ast =
|
||||
parse_tokens(tokens, Mode::Module, "<filename>").context("Syntax error in input")?;
|
||||
|
||||
let formatted = format_node(
|
||||
&python_ast,
|
||||
&comment_ranges,
|
||||
input,
|
||||
PyFormatOptions::default(),
|
||||
)
|
||||
.context("Failed to format node")?;
|
||||
let options = PyFormatOptions::from_extension(source_type);
|
||||
let formatted = format_node(&python_ast, &comment_ranges, input, options)
|
||||
.context("Failed to format node")?;
|
||||
if cli.print_ir {
|
||||
println!("{}", formatted.document().display(SourceCode::new(input)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue