mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
red-knot: source_text
, line_index
, and parsed_module
queries (#11822)
This commit is contained in:
parent
efbf7b14b5
commit
d4dd96d1f4
9 changed files with 194 additions and 16 deletions
|
@ -1,3 +1,4 @@
|
|||
use std::ffi::OsStr;
|
||||
use std::path::Path;
|
||||
|
||||
pub use expression::*;
|
||||
|
@ -80,13 +81,25 @@ pub enum PySourceType {
|
|||
Ipynb,
|
||||
}
|
||||
|
||||
impl<P: AsRef<Path>> From<P> for PySourceType {
|
||||
fn from(path: P) -> Self {
|
||||
match path.as_ref().extension() {
|
||||
Some(ext) if ext == "py" => PySourceType::Python,
|
||||
Some(ext) if ext == "pyi" => PySourceType::Stub,
|
||||
Some(ext) if ext == "ipynb" => PySourceType::Ipynb,
|
||||
_ => PySourceType::Python,
|
||||
impl PySourceType {
|
||||
/// Infers the source type from the file extension.
|
||||
///
|
||||
/// Falls back to `Python` if the extension is not recognized.
|
||||
pub fn from_extension(extension: &str) -> Self {
|
||||
match extension {
|
||||
"py" => Self::Python,
|
||||
"pyi" => Self::Stub,
|
||||
"ipynb" => Self::Ipynb,
|
||||
_ => Self::Python,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: AsRef<Path>> From<P> for PySourceType {
|
||||
fn from(path: P) -> Self {
|
||||
path.as_ref()
|
||||
.extension()
|
||||
.and_then(OsStr::to_str)
|
||||
.map_or(Self::Python, Self::from_extension)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue