diff options
author | Lukas Wirth <[email protected]> | 2021-03-17 16:24:16 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-17 16:24:16 +0000 |
commit | 5481c78f32cdf4fd61dab239d7c4675929fd2ba8 (patch) | |
tree | de13e9e2b264161d9b695a746a794cfa56a0a515 /crates/ide/src | |
parent | edf11480ceae1ef77d7084604011c0ef6f692c72 (diff) |
Check whether cursor is on and not between intra doc links in goto_definition
Diffstat (limited to 'crates/ide/src')
-rw-r--r-- | crates/ide/src/goto_definition.rs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index 5072ecea0..598b47e41 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::ops::Range; | ||
2 | |||
1 | use either::Either; | 3 | use either::Either; |
2 | use hir::{HasAttrs, ModuleDef, Semantics}; | 4 | use hir::{HasAttrs, ModuleDef, Semantics}; |
3 | use ide_db::{ | 5 | use ide_db::{ |
@@ -5,7 +7,8 @@ use ide_db::{ | |||
5 | RootDatabase, | 7 | RootDatabase, |
6 | }; | 8 | }; |
7 | use syntax::{ | 9 | use syntax::{ |
8 | ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextSize, TokenAtOffset, T, | 10 | ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextRange, TextSize, |
11 | TokenAtOffset, T, | ||
9 | }; | 12 | }; |
10 | 13 | ||
11 | use crate::{ | 14 | use crate::{ |
@@ -92,15 +95,18 @@ fn extract_positioned_link_from_comment( | |||
92 | position: FilePosition, | 95 | position: FilePosition, |
93 | comment: &ast::Comment, | 96 | comment: &ast::Comment, |
94 | ) -> Option<(String, Option<hir::Namespace>)> { | 97 | ) -> Option<(String, Option<hir::Namespace>)> { |
95 | let comment_range = comment.syntax().text_range(); | ||
96 | let doc_comment = comment.doc_comment()?; | 98 | let doc_comment = comment.doc_comment()?; |
99 | let comment_start = | ||
100 | comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32); | ||
97 | let def_links = extract_definitions_from_markdown(doc_comment); | 101 | let def_links = extract_definitions_from_markdown(doc_comment); |
98 | let start = comment_range.start() + TextSize::from(comment.prefix().len() as u32); | 102 | let (def_link, ns, _) = def_links.into_iter().find(|&(_, _, Range { start, end })| { |
99 | let (def_link, ns, _) = def_links.iter().min_by_key(|(_, _, def_link_range)| { | 103 | TextRange::at( |
100 | let matched_position = start + TextSize::from(def_link_range.start as u32); | 104 | comment_start + TextSize::from(start as u32), |
101 | position.offset.checked_sub(matched_position).unwrap_or_else(|| comment_range.end()) | 105 | TextSize::from((end - start) as u32), |
106 | ) | ||
107 | .contains(position.offset) | ||
102 | })?; | 108 | })?; |
103 | Some((def_link.to_string(), *ns)) | 109 | Some((def_link, ns)) |
104 | } | 110 | } |
105 | 111 | ||
106 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { | 112 | fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { |
@@ -1134,7 +1140,7 @@ fn foo<'foo>(_: &'foo ()) { | |||
1134 | fn goto_def_for_intra_doc_link_same_file() { | 1140 | fn goto_def_for_intra_doc_link_same_file() { |
1135 | check( | 1141 | check( |
1136 | r#" | 1142 | r#" |
1137 | /// Blah, [`bar`](bar) .. [`foo`](foo)$0 has [`bar`](bar) | 1143 | /// Blah, [`bar`](bar) .. [`foo`](foo$0) has [`bar`](bar) |
1138 | pub fn bar() { } | 1144 | pub fn bar() { } |
1139 | 1145 | ||
1140 | /// You might want to see [`std::fs::read()`] too. | 1146 | /// You might want to see [`std::fs::read()`] too. |