Skip to content

Commit fae0b5c

Browse files
UnboundVariableUnboundVariable
andauthored
[ty] Initial implementation of declaration and definition providers. (#19371)
This PR implements "go to definition" and "go to declaration" functionality for name nodes only. Future PRs will add support for attributes, module names in import statements, keyword argument names, etc. This PR: * Registers a declaration and definition request handler for the language server. * Splits out the `goto_type_definition` into its own module. The `goto` module contains functionality that is common to `goto_type_definition`, `goto_declaration` and `goto_definition`. * Roughs in a new module `stub_mapping` that is not yet implemented. It will be responsible for mapping a definition in a stub file to its corresponding definition(s) in an implementation (source) file. * Adds a new IDE support function `definitions_for_name` that collects all of the definitions associated with a name and resolves any imports (recursively) to find the original definitions associated with that name. * Adds a new `VisibleAncestorsIter` stuct that iterates up the scope hierarchy but skips scopes that are not visible to starting scope. --------- Co-authored-by: UnboundVariable <[email protected]>
1 parent cbe94b0 commit fae0b5c

File tree

16 files changed

+2244
-664
lines changed

16 files changed

+2244
-664
lines changed

crates/ty_ide/src/goto.rs

Lines changed: 135 additions & 658 deletions
Large diffs are not rendered by default.

crates/ty_ide/src/goto_declaration.rs

Lines changed: 813 additions & 0 deletions
Large diffs are not rendered by default.

crates/ty_ide/src/goto_definition.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::goto::find_goto_target;
2+
use crate::stub_mapping::StubMapper;
3+
use crate::{Db, NavigationTargets, RangedValue};
4+
use ruff_db::files::{File, FileRange};
5+
use ruff_db::parsed::parsed_module;
6+
use ruff_text_size::{Ranged, TextSize};
7+
8+
/// Navigate to the definition of a symbol.
9+
///
10+
/// A "definition" is the actual implementation of a symbol, potentially in a source file
11+
/// rather than a stub file. This differs from "declaration" which may navigate to stub files.
12+
/// When possible, this function will map from stub file declarations to their corresponding
13+
/// source file implementations using the `StubMapper`.
14+
pub fn goto_definition(
15+
db: &dyn Db,
16+
file: File,
17+
offset: TextSize,
18+
) -> Option<RangedValue<NavigationTargets>> {
19+
let module = parsed_module(db, file).load(db);
20+
let goto_target = find_goto_target(&module, offset)?;
21+
22+
// Create a StubMapper to map from stub files to source files
23+
let stub_mapper = StubMapper::new(db);
24+
25+
let definition_targets = goto_target.get_definition_targets(file, db, Some(&stub_mapper))?;
26+
27+
Some(RangedValue {
28+
range: FileRange::new(file, goto_target.range()),
29+
value: definition_targets,
30+
})
31+
}

0 commit comments

Comments
 (0)