red-knot: source_text, line_index, and parsed_module queries (#11822)

This commit is contained in:
Micha Reiser 2024-06-13 08:37:02 +01:00 committed by GitHub
parent efbf7b14b5
commit d4dd96d1f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 194 additions and 16 deletions

View file

@ -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)
}
}