From 37d417868e575fe3da5ee23e7a35db88d2e7bc97 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Thu, 24 Jun 2021 00:22:02 +0200 Subject: [PATCH] move goto_declaration fall back into handlers --- crates/ide/src/goto_declaration.rs | 64 +++++++++++----------------- crates/rust-analyzer/src/handlers.rs | 4 +- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/crates/ide/src/goto_declaration.rs b/crates/ide/src/goto_declaration.rs index fb23efdc8241..e390616365ff 100644 --- a/crates/ide/src/goto_declaration.rs +++ b/crates/ide/src/goto_declaration.rs @@ -5,46 +5,41 @@ use ide_db::{ }; use syntax::{ast, match_ast, AstNode, SyntaxKind::*, T}; -use crate::{goto_definition, FilePosition, NavigationTarget, RangeInfo}; +use crate::{FilePosition, NavigationTarget, RangeInfo}; // Feature: Go to Declaration // -// Navigates to the declaration of an identifier. This is the same as the definition except for -// modules where this goes to the identifier of the declaration instead of the contents. +// Navigates to the declaration of an identifier. pub(crate) fn goto_declaration( db: &RootDatabase, position: FilePosition, ) -> Option>> { let sema = Semantics::new(db); let file = sema.parse(position.file_id).syntax().clone(); - let res = (|| { - // try - let original_token = file - .token_at_offset(position.offset) - .find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate]))?; - let token = sema.descend_into_macros(original_token.clone()); - let parent = token.parent()?; - let def = match_ast! { - match parent { - ast::NameRef(name_ref) => { - let name_kind = NameRefClass::classify(&sema, &name_ref)?; - name_kind.referenced(sema.db) - }, - ast::Name(name) => { - NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db) - }, - _ => return None, - } - }; - match def { - Definition::ModuleDef(hir::ModuleDef::Module(module)) => Some(RangeInfo::new( - original_token.text_range(), - vec![NavigationTarget::from_module_to_decl(db, module)], - )), + let original_token = file + .token_at_offset(position.offset) + .find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate]))?; + let token = sema.descend_into_macros(original_token.clone()); + let parent = token.parent()?; + let def = match_ast! { + match parent { + ast::NameRef(name_ref) => { + let name_kind = NameRefClass::classify(&sema, &name_ref)?; + name_kind.referenced(sema.db) + }, + ast::Name(name) => { + NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db) + }, _ => return None, } - })(); - res.or_else(|| goto_definition::goto_definition(db, position)) + }; + match def { + Definition::ModuleDef(hir::ModuleDef::Module(module)) => Some(RangeInfo::new( + original_token.text_range(), + vec![NavigationTarget::from_module_to_decl(db, module)], + )), + _ => return None, + } } #[cfg(test)] @@ -90,17 +85,6 @@ mod foo { // ^^^ use self$0; } -"#, - ) - } - - #[test] - fn goto_decl_falls_back_to_goto_def() { - check( - r#" -struct Foo; - // ^^^ -use self::Foo$0; "#, ) } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 3e3a6fc6a521..bfed068fccb5 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -561,9 +561,9 @@ pub(crate) fn handle_goto_declaration( params: lsp_types::request::GotoDeclarationParams, ) -> Result> { let _p = profile::span("handle_goto_declaration"); - let position = from_proto::file_position(&snap, params.text_document_position_params)?; + let position = from_proto::file_position(&snap, params.text_document_position_params.clone())?; let nav_info = match snap.analysis.goto_declaration(position)? { - None => return Ok(None), + None => return handle_goto_definition(snap, params), Some(it) => it, }; let src = FileRange { file_id: position.file_id, range: nav_info.range };