Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion crates/red_knot_ide/src/hover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::goto::find_goto_target;
use crate::goto::{find_goto_target, GotoTarget};
use crate::{Db, MarkupKind, RangedValue};
use red_knot_python_semantic::types::Type;
use red_knot_python_semantic::SemanticModel;
Expand All @@ -12,6 +12,12 @@ pub fn hover(db: &dyn Db, file: File, offset: TextSize) -> Option<RangedValue<Ho
let parsed = parsed_module(db.upcast(), file);
let goto_target = find_goto_target(parsed, offset)?;

if let GotoTarget::Expression(expr) = goto_target {
if expr.is_literal_expr() {
return None;
}
}

let model = SemanticModel::new(db.upcast(), file);
let ty = goto_target.inferred_type(&model)?;

Expand Down Expand Up @@ -496,6 +502,57 @@ mod tests {
");
}

#[test]
fn hover_whitespace() {
let test = cursor_test(
r#"
class C:
<CURSOR>
foo: str = 'bar'
"#,
);

assert_snapshot!(test.hover(), @"Hover provided no content");
}

#[test]
fn hover_literal_int() {
let test = cursor_test(
r#"
print(
0 + 1<CURSOR>
)
"#,
);

assert_snapshot!(test.hover(), @"Hover provided no content");
}

#[test]
fn hover_literal_ellipsis() {
let test = cursor_test(
r#"
print(
.<CURSOR>..
)
"#,
);

assert_snapshot!(test.hover(), @"Hover provided no content");
}

#[test]
fn hover_docstring() {
let test = cursor_test(
r#"
def f():
"""Lorem ipsum dolor sit amet.<CURSOR>"""
"#,
);

assert_snapshot!(test.hover(), @"Hover provided no content");
}

impl CursorTest {
fn hover(&self) -> String {
use std::fmt::Write;
Expand Down
13 changes: 13 additions & 0 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ impl Expr {
}

impl ExprRef<'_> {
/// See [`Expr::is_literal_expr`].
pub fn is_literal_expr(&self) -> bool {
matches!(
self,
ExprRef::StringLiteral(_)
| ExprRef::BytesLiteral(_)
| ExprRef::NumberLiteral(_)
| ExprRef::BooleanLiteral(_)
| ExprRef::NoneLiteral(_)
| ExprRef::EllipsisLiteral(_)
)
}

pub fn precedence(&self) -> OperatorPrecedence {
OperatorPrecedence::from(self)
}
Expand Down
Loading