mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
for goto and hover pick the token based on a priority
This commit is contained in:
parent
4f7da04c67
commit
c82529a97f
3 changed files with 46 additions and 17 deletions
|
@ -3,7 +3,9 @@
|
|||
use hir::{db::AstDatabase, InFile};
|
||||
use ra_syntax::{
|
||||
ast::{self, DocCommentsOwner},
|
||||
match_ast, AstNode, SyntaxKind, SyntaxNode,
|
||||
match_ast, AstNode,
|
||||
SyntaxKind::*,
|
||||
SyntaxNode, SyntaxToken, TokenAtOffset,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -19,8 +21,7 @@ pub(crate) fn goto_definition(
|
|||
position: FilePosition,
|
||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let file = db.parse_or_expand(position.file_id.into())?;
|
||||
let original_token =
|
||||
file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?;
|
||||
let original_token = pick_best(file.token_at_offset(position.offset))?;
|
||||
let token = descend_into_macros(db, position.file_id, original_token.clone());
|
||||
|
||||
let nav_targets = match_ast! {
|
||||
|
@ -38,6 +39,17 @@ pub(crate) fn goto_definition(
|
|||
Some(RangeInfo::new(original_token.text_range(), nav_targets))
|
||||
}
|
||||
|
||||
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||
return tokens.max_by_key(priority);
|
||||
fn priority(n: &SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
IDENT | INT_NUMBER => 2,
|
||||
kind if kind.is_trivia() => 0,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum ReferenceResult {
|
||||
Exact(NavigationTarget),
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::db::AstDatabase;
|
||||
use ra_syntax::{ast, AstNode, SyntaxKind};
|
||||
use ra_syntax::{ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
|
||||
|
||||
use crate::{
|
||||
db::RootDatabase, display::ToNav, expand::descend_into_macros, FilePosition, NavigationTarget,
|
||||
|
@ -13,7 +13,7 @@ pub(crate) fn goto_type_definition(
|
|||
position: FilePosition,
|
||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let file = db.parse_or_expand(position.file_id.into())?;
|
||||
let token = file.token_at_offset(position.offset).find(|it| it.kind() == SyntaxKind::IDENT)?;
|
||||
let token = pick_best(file.token_at_offset(position.offset))?;
|
||||
let token = descend_into_macros(db, position.file_id, token);
|
||||
|
||||
let node = token.value.ancestors().find_map(|token| {
|
||||
|
@ -41,6 +41,17 @@ pub(crate) fn goto_type_definition(
|
|||
Some(RangeInfo::new(node.text_range(), vec![nav]))
|
||||
}
|
||||
|
||||
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||
return tokens.max_by_key(priority);
|
||||
fn priority(n: &SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
IDENT | INT_NUMBER => 2,
|
||||
kind if kind.is_trivia() => 0,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::mock_analysis::analysis_and_position;
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, InFile};
|
||||
use hir::{db::AstDatabase, Adt, HasSource, HirDisplay};
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{
|
||||
algo::find_covering_element,
|
||||
ast::{self, DocCommentsOwner},
|
||||
match_ast, AstNode, SyntaxToken,
|
||||
match_ast, AstNode,
|
||||
SyntaxKind::*,
|
||||
SyntaxToken, TokenAtOffset,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -156,17 +158,9 @@ fn hover_text_from_name_kind(
|
|||
|
||||
pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> {
|
||||
let file = db.parse_or_expand(position.file_id.into())?;
|
||||
file.token_at_offset(position.offset)
|
||||
.filter(|token| !token.kind().is_trivia())
|
||||
.map(|token| descend_into_macros(db, position.file_id, token))
|
||||
.find_map(|token| hover_token(db, position, token))
|
||||
}
|
||||
let token = pick_best(file.token_at_offset(position.offset))?;
|
||||
let token = descend_into_macros(db, position.file_id, token);
|
||||
|
||||
fn hover_token(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
token: InFile<SyntaxToken>,
|
||||
) -> Option<RangeInfo<HoverResult>> {
|
||||
let mut res = HoverResult::new();
|
||||
|
||||
let mut range = match_ast! {
|
||||
|
@ -226,6 +220,18 @@ fn hover_token(
|
|||
Some(RangeInfo::new(range, res))
|
||||
}
|
||||
|
||||
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
||||
return tokens.max_by_key(priority);
|
||||
fn priority(n: &SyntaxToken) -> usize {
|
||||
match n.kind() {
|
||||
IDENT | INT_NUMBER => 3,
|
||||
L_PAREN | R_PAREN => 2,
|
||||
kind if kind.is_trivia() => 0,
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
|
||||
let parse = db.parse(frange.file_id);
|
||||
let leaf_node = find_covering_element(parse.tree().syntax(), frange.range);
|
||||
|
|
Loading…
Reference in a new issue