ruff/crates/ty_server/tests/e2e/inlay_hints.rs
Matthew Mckee 8d6dc7d3a3
[ty] Refactor inlay hints structure to use separate parts (#20052)
## Summary

Our internal inlay hints structure (`ty_ide::InlayHint`) now more
closely resembles `lsp_types::InlayHint`.

This mainly allows us to convert to `lsp_types::InlayHint` with less
hassle, but it also allows us to manage the different parts of the inlay
hint better, which in the future will allow us to implement features
like goto on the type part of the type inlay hint.

It also really isn't important to store a specific `Type` instance in
the `InlayHintContent`. So we remove this and use `InlayHintLabel`
instead which just shows the representation of the type (along with
other information).

We see a similar structure used in rust-analyzer too.
2025-08-26 10:21:31 +05:30

106 lines
2.6 KiB
Rust

use anyhow::Result;
use lsp_types::{Position, Range, notification::PublishDiagnostics};
use ruff_db::system::SystemPath;
use ty_server::ClientOptions;
use crate::TestServerBuilder;
/// Tests that the default value of inlay hints settings is correct i.e., they're all enabled
/// by default.
#[test]
fn default_inlay_hints() -> Result<()> {
let workspace_root = SystemPath::new("src");
let foo = SystemPath::new("src/foo.py");
let foo_content = "\
x = 1
def foo(a: int) -> int:
return a + 1
foo(1)
";
let mut server = TestServerBuilder::new()?
.with_initialization_options(ClientOptions::default())
.with_workspace(workspace_root, None)?
.with_file(foo, foo_content)?
.enable_inlay_hints(true)
.build()?
.wait_until_workspaces_are_initialized()?;
server.open_text_document(foo, &foo_content, 1);
let _ = server.await_notification::<PublishDiagnostics>()?;
let hints = server
.inlay_hints_request(foo, Range::new(Position::new(0, 0), Position::new(6, 0)))?
.unwrap();
insta::assert_json_snapshot!(hints, @r#"
[
{
"position": {
"line": 0,
"character": 1
},
"label": [
{
"value": ": "
},
{
"value": "Literal[1]"
}
],
"kind": 1
},
{
"position": {
"line": 5,
"character": 4
},
"label": [
{
"value": "a"
},
{
"value": "="
}
],
"kind": 2
}
]
"#);
Ok(())
}
/// Tests that disabling variable types inlay hints works correctly.
#[test]
fn variable_inlay_hints_disabled() -> Result<()> {
let workspace_root = SystemPath::new("src");
let foo = SystemPath::new("src/foo.py");
let foo_content = "x = 1";
let mut server = TestServerBuilder::new()?
.with_initialization_options(
ClientOptions::default().with_variable_types_inlay_hints(false),
)
.with_workspace(workspace_root, None)?
.with_file(foo, foo_content)?
.enable_inlay_hints(true)
.build()?
.wait_until_workspaces_are_initialized()?;
server.open_text_document(foo, &foo_content, 1);
let _ = server.await_notification::<PublishDiagnostics>()?;
let hints = server
.inlay_hints_request(foo, Range::new(Position::new(0, 0), Position::new(0, 5)))?
.unwrap();
assert!(
hints.is_empty(),
"Expected no inlay hints, but found: {hints:?}"
);
Ok(())
}