mirror of https://github.com/astral-sh/ruff
print function names for bare Signatures opportunistically
This commit is contained in:
parent
dcc451d4d2
commit
42abe02eac
|
|
@ -1090,7 +1090,7 @@ def ab(a: int):
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assert_snapshot!(test.hover(), @r"
|
assert_snapshot!(test.hover(), @r"
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
b: int
|
b: int
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
|
|
@ -1099,7 +1099,7 @@ def ab(a: int):
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
```python
|
```python
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
b: int
|
b: int
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
|
|
@ -1226,7 +1226,7 @@ def ab(a: int, *, c: int):
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assert_snapshot!(test.hover(), @r"
|
assert_snapshot!(test.hover(), @r"
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
*,
|
*,
|
||||||
b: int
|
b: int
|
||||||
|
|
@ -1236,7 +1236,7 @@ def ab(a: int, *, c: int):
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
```python
|
```python
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
*,
|
*,
|
||||||
b: int
|
b: int
|
||||||
|
|
@ -1301,7 +1301,7 @@ def ab(a: int, *, c: int):
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
assert_snapshot!(test.hover(), @r"
|
assert_snapshot!(test.hover(), @r"
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
*,
|
*,
|
||||||
c: int
|
c: int
|
||||||
|
|
@ -1311,7 +1311,7 @@ def ab(a: int, *, c: int):
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
```python
|
```python
|
||||||
(
|
def ab(
|
||||||
a: int,
|
a: int,
|
||||||
*,
|
*,
|
||||||
c: int
|
c: int
|
||||||
|
|
@ -1363,11 +1363,11 @@ def ab(a: int, *, c: int):
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_snapshot!(test.hover(), @r#"
|
assert_snapshot!(test.hover(), @r#"
|
||||||
(
|
def foo(
|
||||||
a: int,
|
a: int,
|
||||||
b
|
b
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
(
|
def foo(
|
||||||
a: str,
|
a: str,
|
||||||
b
|
b
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
|
|
@ -1376,11 +1376,11 @@ def ab(a: int, *, c: int):
|
||||||
|
|
||||||
---------------------------------------------
|
---------------------------------------------
|
||||||
```python
|
```python
|
||||||
(
|
def foo(
|
||||||
a: int,
|
a: int,
|
||||||
b
|
b
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
(
|
def foo(
|
||||||
a: str,
|
a: str,
|
||||||
b
|
b
|
||||||
) -> Unknown
|
) -> Unknown
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
|
|
||||||
use crate::Db;
|
use crate::Db;
|
||||||
use crate::module_resolver::file_to_module;
|
use crate::module_resolver::file_to_module;
|
||||||
|
use crate::semantic_index::definition::Definition;
|
||||||
use crate::semantic_index::{scope::ScopeKind, semantic_index};
|
use crate::semantic_index::{scope::ScopeKind, semantic_index};
|
||||||
use crate::types::class::{ClassLiteral, ClassType, GenericAlias};
|
use crate::types::class::{ClassLiteral, ClassType, GenericAlias};
|
||||||
use crate::types::function::{FunctionType, OverloadLiteral};
|
use crate::types::function::{FunctionType, OverloadLiteral};
|
||||||
|
|
@ -40,6 +41,9 @@ pub struct DisplaySettings<'db> {
|
||||||
pub qualified: Rc<FxHashMap<&'db str, QualificationLevel>>,
|
pub qualified: Rc<FxHashMap<&'db str, QualificationLevel>>,
|
||||||
/// Whether long unions and literals are displayed in full
|
/// Whether long unions and literals are displayed in full
|
||||||
pub preserve_full_unions: bool,
|
pub preserve_full_unions: bool,
|
||||||
|
/// Disallow Signature printing to introduce a name
|
||||||
|
/// (presumably because we rendered one already)
|
||||||
|
pub disallow_signature_name: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'db> DisplaySettings<'db> {
|
impl<'db> DisplaySettings<'db> {
|
||||||
|
|
@ -59,6 +63,14 @@ impl<'db> DisplaySettings<'db> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn disallow_signature_name(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
disallow_signature_name: true,
|
||||||
|
..self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn truncate_long_unions(self) -> Self {
|
pub fn truncate_long_unions(self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -473,7 +485,7 @@ impl Display for DisplayRepresentation<'_> {
|
||||||
type_parameters = type_parameters,
|
type_parameters = type_parameters,
|
||||||
signature = signature
|
signature = signature
|
||||||
.bind_self(self.db, Some(typing_self_ty))
|
.bind_self(self.db, Some(typing_self_ty))
|
||||||
.display_with(self.db, self.settings.clone())
|
.display_with(self.db, self.settings.disallow_signature_name())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
signatures => {
|
signatures => {
|
||||||
|
|
@ -768,7 +780,7 @@ impl Display for DisplayOverloadLiteral<'_> {
|
||||||
"def {name}{type_parameters}{signature}",
|
"def {name}{type_parameters}{signature}",
|
||||||
name = self.literal.name(self.db),
|
name = self.literal.name(self.db),
|
||||||
type_parameters = type_parameters,
|
type_parameters = type_parameters,
|
||||||
signature = signature.display_with(self.db, self.settings.clone())
|
signature = signature.display_with(self.db, self.settings.disallow_signature_name())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -810,7 +822,8 @@ impl Display for DisplayFunctionType<'_> {
|
||||||
"def {name}{type_parameters}{signature}",
|
"def {name}{type_parameters}{signature}",
|
||||||
name = self.ty.name(self.db),
|
name = self.ty.name(self.db),
|
||||||
type_parameters = type_parameters,
|
type_parameters = type_parameters,
|
||||||
signature = signature.display_with(self.db, self.settings.clone())
|
signature =
|
||||||
|
signature.display_with(self.db, self.settings.disallow_signature_name())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
signatures => {
|
signatures => {
|
||||||
|
|
@ -1081,6 +1094,7 @@ impl<'db> Signature<'db> {
|
||||||
settings: DisplaySettings<'db>,
|
settings: DisplaySettings<'db>,
|
||||||
) -> DisplaySignature<'db> {
|
) -> DisplaySignature<'db> {
|
||||||
DisplaySignature {
|
DisplaySignature {
|
||||||
|
definition: self.definition(),
|
||||||
parameters: self.parameters(),
|
parameters: self.parameters(),
|
||||||
return_ty: self.return_ty,
|
return_ty: self.return_ty,
|
||||||
db,
|
db,
|
||||||
|
|
@ -1090,6 +1104,7 @@ impl<'db> Signature<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct DisplaySignature<'db> {
|
pub(crate) struct DisplaySignature<'db> {
|
||||||
|
definition: Option<Definition<'db>>,
|
||||||
parameters: &'db Parameters<'db>,
|
parameters: &'db Parameters<'db>,
|
||||||
return_ty: Option<Type<'db>>,
|
return_ty: Option<Type<'db>>,
|
||||||
db: &'db dyn Db,
|
db: &'db dyn Db,
|
||||||
|
|
@ -1111,6 +1126,18 @@ impl DisplaySignature<'_> {
|
||||||
/// Internal method to write signature with the signature writer
|
/// Internal method to write signature with the signature writer
|
||||||
fn write_signature(&self, writer: &mut SignatureWriter) -> fmt::Result {
|
fn write_signature(&self, writer: &mut SignatureWriter) -> fmt::Result {
|
||||||
let multiline = self.settings.multiline && self.parameters.len() > 1;
|
let multiline = self.settings.multiline && self.parameters.len() > 1;
|
||||||
|
// If we're multiline printing and a name hasn't been emitted, try to
|
||||||
|
// make one up to make things more pretty
|
||||||
|
if multiline && !self.settings.disallow_signature_name {
|
||||||
|
writer.write_str("def ")?;
|
||||||
|
if let Some(definition) = self.definition
|
||||||
|
&& let Some(name) = definition.name(self.db)
|
||||||
|
{
|
||||||
|
writer.write_str(&name)?;
|
||||||
|
} else {
|
||||||
|
writer.write_str("_")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
// Opening parenthesis
|
// Opening parenthesis
|
||||||
writer.write_char('(')?;
|
writer.write_char('(')?;
|
||||||
if multiline {
|
if multiline {
|
||||||
|
|
@ -1979,7 +2006,7 @@ mod tests {
|
||||||
Some(Type::none(&db))
|
Some(Type::none(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
x=int,
|
x=int,
|
||||||
y: str = str
|
y: str = str
|
||||||
) -> None
|
) -> None
|
||||||
|
|
@ -1997,7 +2024,7 @@ mod tests {
|
||||||
Some(Type::none(&db))
|
Some(Type::none(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
/
|
/
|
||||||
|
|
@ -2016,7 +2043,7 @@ mod tests {
|
||||||
Some(Type::none(&db))
|
Some(Type::none(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
x,
|
x,
|
||||||
/,
|
/,
|
||||||
y
|
y
|
||||||
|
|
@ -2035,7 +2062,7 @@ mod tests {
|
||||||
Some(Type::none(&db))
|
Some(Type::none(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
*,
|
*,
|
||||||
x,
|
x,
|
||||||
y
|
y
|
||||||
|
|
@ -2054,7 +2081,7 @@ mod tests {
|
||||||
Some(Type::none(&db))
|
Some(Type::none(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
x,
|
x,
|
||||||
*,
|
*,
|
||||||
y
|
y
|
||||||
|
|
@ -2093,7 +2120,7 @@ mod tests {
|
||||||
Some(KnownClass::Bytes.to_instance(&db))
|
Some(KnownClass::Bytes.to_instance(&db))
|
||||||
),
|
),
|
||||||
@r"
|
@r"
|
||||||
(
|
def _(
|
||||||
a,
|
a,
|
||||||
b: int,
|
b: int,
|
||||||
c=Literal[1],
|
c=Literal[1],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue