mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-04 04:39:16 +00:00
Basic resolution for ADT
This commit is contained in:
parent
a094d5c621
commit
7125192c1e
68
crates/ra_ide_api/src/goto_type_definition.rs
Normal file
68
crates/ra_ide_api/src/goto_type_definition.rs
Normal file
@ -0,0 +1,68 @@
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{
|
||||
AstNode, ast,
|
||||
algo::find_token_at_offset
|
||||
};
|
||||
|
||||
use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
|
||||
|
||||
pub(crate) fn goto_type_definition(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let file = db.parse(position.file_id);
|
||||
|
||||
let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| {
|
||||
token
|
||||
.parent()
|
||||
.ancestors()
|
||||
.find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())
|
||||
})?;
|
||||
|
||||
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, node, None);
|
||||
|
||||
let ty: hir::Ty = if let Some(ty) = ast::Expr::cast(node).and_then(|e| analyzer.type_of(db, e))
|
||||
{
|
||||
ty
|
||||
} else if let Some(ty) = ast::Pat::cast(node).and_then(|p| analyzer.type_of_pat(db, p)) {
|
||||
ty
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if let Some((adt_def, _)) = ty.as_adt() {
|
||||
let nav = NavigationTarget::from_adt_def(db, adt_def);
|
||||
return Some(RangeInfo::new(node.range(), vec![nav]));
|
||||
};
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::mock_analysis::analysis_and_position;
|
||||
|
||||
fn check_goto(fixture: &str, expected: &str) {
|
||||
let (analysis, pos) = analysis_and_position(fixture);
|
||||
|
||||
let mut navs = analysis.goto_type_definition(pos).unwrap().unwrap().info;
|
||||
assert_eq!(navs.len(), 1);
|
||||
let nav = navs.pop().unwrap();
|
||||
nav.assert_match(expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn goto_type_definition_works_simple() {
|
||||
check_goto(
|
||||
"
|
||||
//- /lib.rs
|
||||
struct Foo;
|
||||
fn foo() {
|
||||
let f: Foo;
|
||||
f<|>
|
||||
}
|
||||
",
|
||||
"Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)",
|
||||
);
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ mod status;
|
||||
mod completion;
|
||||
mod runnables;
|
||||
mod goto_definition;
|
||||
mod goto_type_definition;
|
||||
mod extend_selection;
|
||||
mod hover;
|
||||
mod call_info;
|
||||
@ -416,6 +417,13 @@ impl Analysis {
|
||||
self.with_db(|db| impls::goto_implementation(db, position))
|
||||
}
|
||||
|
||||
pub fn goto_type_definition(
|
||||
&self,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
||||
self.with_db(|db| goto_type_definition::goto_type_definition(db, position))
|
||||
}
|
||||
|
||||
/// Finds all usages of the reference at point.
|
||||
pub fn find_all_refs(
|
||||
&self,
|
||||
|
@ -2,7 +2,7 @@ use lsp_types::{
|
||||
CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions,
|
||||
ExecuteCommandOptions, FoldingRangeProviderCapability, RenameOptions, RenameProviderCapability,
|
||||
ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
|
||||
TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability,
|
||||
TextDocumentSyncOptions, ImplementationProviderCapability, GenericCapability, TypeDefinitionProviderCapability
|
||||
};
|
||||
|
||||
pub fn server_capabilities() -> ServerCapabilities {
|
||||
@ -23,7 +23,7 @@ pub fn server_capabilities() -> ServerCapabilities {
|
||||
trigger_characters: Some(vec!["(".to_string(), ",".to_string(), ")".to_string()]),
|
||||
}),
|
||||
definition_provider: Some(true),
|
||||
type_definition_provider: None,
|
||||
type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)),
|
||||
implementation_provider: Some(ImplementationProviderCapability::Simple(true)),
|
||||
references_provider: Some(true),
|
||||
document_highlight_provider: Some(true),
|
||||
|
@ -306,6 +306,7 @@ fn on_request(
|
||||
.on::<req::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
|
||||
.on::<req::GotoDefinition>(handlers::handle_goto_definition)?
|
||||
.on::<req::GotoImplementation>(handlers::handle_goto_implementation)?
|
||||
.on::<req::GotoTypeDefinition>(handlers::handle_goto_type_definition)?
|
||||
.on::<req::ParentModule>(handlers::handle_parent_module)?
|
||||
.on::<req::Runnables>(handlers::handle_runnables)?
|
||||
.on::<req::DecorationsRequest>(handlers::handle_decorations)?
|
||||
|
@ -288,6 +288,26 @@ pub fn handle_goto_implementation(
|
||||
Ok(Some(req::GotoDefinitionResponse::Link(res)))
|
||||
}
|
||||
|
||||
pub fn handle_goto_type_definition(
|
||||
world: ServerWorld,
|
||||
params: req::TextDocumentPositionParams,
|
||||
) -> Result<Option<req::GotoTypeDefinitionResponse>> {
|
||||
let position = params.try_conv_with(&world)?;
|
||||
let line_index = world.analysis().file_line_index(position.file_id);
|
||||
let nav_info = match world.analysis().goto_type_definition(position)? {
|
||||
None => return Ok(None),
|
||||
Some(it) => it,
|
||||
};
|
||||
let nav_range = nav_info.range;
|
||||
let res = nav_info
|
||||
.info
|
||||
.into_iter()
|
||||
.map(|nav| RangeInfo::new(nav_range, nav))
|
||||
.map(|nav| to_location_link(&nav, &world, &line_index))
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(Some(req::GotoDefinitionResponse::Link(res)))
|
||||
}
|
||||
|
||||
pub fn handle_parent_module(
|
||||
world: ServerWorld,
|
||||
params: req::TextDocumentPositionParams,
|
||||
|
Loading…
Reference in New Issue
Block a user