diff options
author | succcubbus <[email protected]> | 2019-12-13 21:00:05 +0000 |
---|---|---|
committer | succcubbus <[email protected]> | 2019-12-13 21:00:05 +0000 |
commit | c82529a97f10b1302d2944f1946bcb3479f64e2d (patch) | |
tree | 89cf9cec7e8314da5aaa122a7c4f39844f166d23 /crates/ra_ide/src/hover.rs | |
parent | 4f7da04c6735e1d00fceb6fc6f83542626ce03c9 (diff) |
for goto and hover pick the token based on a priority
Diffstat (limited to 'crates/ra_ide/src/hover.rs')
-rw-r--r-- | crates/ra_ide/src/hover.rs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 5b48b1998..51e320128 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -1,11 +1,13 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, InFile}; | 3 | use hir::{db::AstDatabase, Adt, HasSource, HirDisplay}; |
4 | use ra_db::SourceDatabase; | 4 | use ra_db::SourceDatabase; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | algo::find_covering_element, | 6 | algo::find_covering_element, |
7 | ast::{self, DocCommentsOwner}, | 7 | ast::{self, DocCommentsOwner}, |
8 | match_ast, AstNode, SyntaxToken, | 8 | match_ast, AstNode, |
9 | SyntaxKind::*, | ||
10 | SyntaxToken, TokenAtOffset, | ||
9 | }; | 11 | }; |
10 | 12 | ||
11 | use crate::{ | 13 | use crate::{ |
@@ -156,17 +158,9 @@ fn hover_text_from_name_kind( | |||
156 | 158 | ||
157 | pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> { | 159 | pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> { |
158 | let file = db.parse_or_expand(position.file_id.into())?; | 160 | let file = db.parse_or_expand(position.file_id.into())?; |
159 | file.token_at_offset(position.offset) | 161 | let token = pick_best(file.token_at_offset(position.offset))?; |
160 | .filter(|token| !token.kind().is_trivia()) | 162 | let token = descend_into_macros(db, position.file_id, token); |
161 | .map(|token| descend_into_macros(db, position.file_id, token)) | ||
162 | .find_map(|token| hover_token(db, position, token)) | ||
163 | } | ||
164 | 163 | ||
165 | fn hover_token( | ||
166 | db: &RootDatabase, | ||
167 | position: FilePosition, | ||
168 | token: InFile<SyntaxToken>, | ||
169 | ) -> Option<RangeInfo<HoverResult>> { | ||
170 | let mut res = HoverResult::new(); | 164 | let mut res = HoverResult::new(); |
171 | 165 | ||
172 | let mut range = match_ast! { | 166 | let mut range = match_ast! { |
@@ -226,6 +220,18 @@ fn hover_token( | |||
226 | Some(RangeInfo::new(range, res)) | 220 | Some(RangeInfo::new(range, res)) |
227 | } | 221 | } |
228 | 222 | ||
223 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | ||
224 | return tokens.max_by_key(priority); | ||
225 | fn priority(n: &SyntaxToken) -> usize { | ||
226 | match n.kind() { | ||
227 | IDENT | INT_NUMBER => 3, | ||
228 | L_PAREN | R_PAREN => 2, | ||
229 | kind if kind.is_trivia() => 0, | ||
230 | _ => 1, | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | |||
229 | pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { | 235 | pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { |
230 | let parse = db.parse(frange.file_id); | 236 | let parse = db.parse(frange.file_id); |
231 | let leaf_node = find_covering_element(parse.tree().syntax(), frange.range); | 237 | let leaf_node = find_covering_element(parse.tree().syntax(), frange.range); |