mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +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,4 +1,5 @@
|
|||
use ruff_text_size::{TextRange, TextSize};
|
||||
use std::path::Path;
|
||||
|
||||
pub mod all;
|
||||
pub mod call_path;
|
||||
|
@ -49,3 +50,36 @@ where
|
|||
T::range(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum PySourceType {
|
||||
#[default]
|
||||
Python,
|
||||
Stub,
|
||||
Jupyter,
|
||||
}
|
||||
|
||||
impl PySourceType {
|
||||
pub const fn is_python(&self) -> bool {
|
||||
matches!(self, PySourceType::Python)
|
||||
}
|
||||
|
||||
pub const fn is_stub(&self) -> bool {
|
||||
matches!(self, PySourceType::Stub)
|
||||
}
|
||||
|
||||
pub const fn is_jupyter(&self) -> bool {
|
||||
matches!(self, PySourceType::Jupyter)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Path> for PySourceType {
|
||||
fn from(path: &Path) -> Self {
|
||||
match path.extension() {
|
||||
Some(ext) if ext == "pyi" => PySourceType::Stub,
|
||||
Some(ext) if ext == "ipynb" => PySourceType::Jupyter,
|
||||
_ => PySourceType::Python,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue