mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
[ty] Implemented "go to definition" support for import statements (#19428)
Some checks are pending
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
This PR extends the "go to declaration" and "go to definition" functionality to support import statements — both standard imports and "from" import forms. --------- Co-authored-by: UnboundVariable <unbound@gmail.com>
This commit is contained in:
parent
93a9fabb26
commit
0acc273286
5 changed files with 469 additions and 31 deletions
|
@ -610,6 +610,229 @@ def another_helper():
|
|||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_import_as_alias_name() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
"
|
||||
import mymodule.submodule as su<CURSOR>b
|
||||
print(sub.helper())
|
||||
",
|
||||
)
|
||||
.source(
|
||||
"mymodule/__init__.py",
|
||||
"
|
||||
# Main module init
|
||||
",
|
||||
)
|
||||
.source(
|
||||
"mymodule/submodule.py",
|
||||
r#"
|
||||
FOO = 0
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r"
|
||||
info[goto-declaration]: Declaration
|
||||
--> mymodule/submodule.py:1:1
|
||||
|
|
||||
1 |
|
||||
| ^
|
||||
2 | FOO = 0
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:30
|
||||
|
|
||||
2 | import mymodule.submodule as sub
|
||||
| ^^^
|
||||
3 | print(sub.helper())
|
||||
|
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_import_as_alias_name_on_module() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
"
|
||||
import mymodule.submod<CURSOR>ule as sub
|
||||
print(sub.helper())
|
||||
",
|
||||
)
|
||||
.source(
|
||||
"mymodule/__init__.py",
|
||||
"
|
||||
# Main module init
|
||||
",
|
||||
)
|
||||
.source(
|
||||
"mymodule/submodule.py",
|
||||
r#"
|
||||
FOO = 0
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r"
|
||||
info[goto-declaration]: Declaration
|
||||
--> mymodule/submodule.py:1:1
|
||||
|
|
||||
1 |
|
||||
| ^
|
||||
2 | FOO = 0
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:17
|
||||
|
|
||||
2 | import mymodule.submodule as sub
|
||||
| ^^^^^^^^^
|
||||
3 | print(sub.helper())
|
||||
|
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_from_import_symbol_original() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
from mypackage.utils import hel<CURSOR>per as h
|
||||
result = h("/a", "/b")
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/__init__.py",
|
||||
r#"
|
||||
# Package init
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/utils.py",
|
||||
r#"
|
||||
def helper(a, b):
|
||||
return a + "/" + b
|
||||
|
||||
def another_helper(path):
|
||||
return "processed"
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> mypackage/utils.py:2:5
|
||||
|
|
||||
2 | def helper(a, b):
|
||||
| ^^^^^^
|
||||
3 | return a + "/" + b
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:29
|
||||
|
|
||||
2 | from mypackage.utils import helper as h
|
||||
| ^^^^^^
|
||||
3 | result = h("/a", "/b")
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_from_import_symbol_alias() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
from mypackage.utils import helper as h<CURSOR>
|
||||
result = h("/a", "/b")
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/__init__.py",
|
||||
r#"
|
||||
# Package init
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/utils.py",
|
||||
r#"
|
||||
def helper(a, b):
|
||||
return a + "/" + b
|
||||
|
||||
def another_helper(path):
|
||||
return "processed"
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> mypackage/utils.py:2:5
|
||||
|
|
||||
2 | def helper(a, b):
|
||||
| ^^^^^^
|
||||
3 | return a + "/" + b
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:39
|
||||
|
|
||||
2 | from mypackage.utils import helper as h
|
||||
| ^
|
||||
3 | result = h("/a", "/b")
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_from_import_module() {
|
||||
let test = CursorTest::builder()
|
||||
.source(
|
||||
"main.py",
|
||||
r#"
|
||||
from mypackage.ut<CURSOR>ils import helper as h
|
||||
result = h("/a", "/b")
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/__init__.py",
|
||||
r#"
|
||||
# Package init
|
||||
"#,
|
||||
)
|
||||
.source(
|
||||
"mypackage/utils.py",
|
||||
r#"
|
||||
def helper(a, b):
|
||||
return a + "/" + b
|
||||
|
||||
def another_helper(path):
|
||||
return "processed"
|
||||
"#,
|
||||
)
|
||||
.build();
|
||||
|
||||
assert_snapshot!(test.goto_declaration(), @r#"
|
||||
info[goto-declaration]: Declaration
|
||||
--> mypackage/utils.py:1:1
|
||||
|
|
||||
1 |
|
||||
| ^
|
||||
2 | def helper(a, b):
|
||||
3 | return a + "/" + b
|
||||
|
|
||||
info: Source
|
||||
--> main.py:2:16
|
||||
|
|
||||
2 | from mypackage.utils import helper as h
|
||||
| ^^^^^
|
||||
3 | result = h("/a", "/b")
|
||||
|
|
||||
"#);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_declaration_instance_attribute() {
|
||||
let test = cursor_test(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue