mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:42:10 +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
|
@ -21,6 +21,7 @@ use ruff::rules::{
|
|||
use ruff::settings::configuration::Configuration;
|
||||
use ruff::settings::options::Options;
|
||||
use ruff::settings::{defaults, flags, Settings};
|
||||
use ruff_python_ast::PySourceType;
|
||||
use ruff_python_codegen::Stylist;
|
||||
use ruff_python_formatter::{format_module, format_node, PyFormatOptions};
|
||||
use ruff_python_index::{CommentRangesBuilder, Indexer};
|
||||
|
@ -262,7 +263,9 @@ impl Workspace {
|
|||
}
|
||||
|
||||
pub fn format(&self, contents: &str) -> Result<String, Error> {
|
||||
let printed = format_module(contents, PyFormatOptions::default()).map_err(into_error)?;
|
||||
// TODO(konstin): Add an options for py/pyi to the UI (1/2)
|
||||
let options = PyFormatOptions::from_source_type(PySourceType::default());
|
||||
let printed = format_module(contents, options).map_err(into_error)?;
|
||||
|
||||
Ok(printed.into_code())
|
||||
}
|
||||
|
@ -278,13 +281,10 @@ impl Workspace {
|
|||
let comment_ranges = comment_ranges.finish();
|
||||
let module = parse_tokens(tokens, Mode::Module, ".").map_err(into_error)?;
|
||||
|
||||
let formatted = format_node(
|
||||
&module,
|
||||
&comment_ranges,
|
||||
contents,
|
||||
PyFormatOptions::default(),
|
||||
)
|
||||
.map_err(into_error)?;
|
||||
// TODO(konstin): Add an options for py/pyi to the UI (2/2)
|
||||
let options = PyFormatOptions::from_source_type(PySourceType::default());
|
||||
let formatted =
|
||||
format_node(&module, &comment_ranges, contents, options).map_err(into_error)?;
|
||||
|
||||
Ok(format!("{formatted}"))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue