aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-03-17 16:24:16 +0000
committerLukas Wirth <[email protected]>2021-03-17 16:24:16 +0000
commit5481c78f32cdf4fd61dab239d7c4675929fd2ba8 (patch)
treede13e9e2b264161d9b695a746a794cfa56a0a515 /crates/ide/src
parentedf11480ceae1ef77d7084604011c0ef6f692c72 (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.rs22
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 @@
1use std::ops::Range;
2
1use either::Either; 3use either::Either;
2use hir::{HasAttrs, ModuleDef, Semantics}; 4use hir::{HasAttrs, ModuleDef, Semantics};
3use ide_db::{ 5use ide_db::{
@@ -5,7 +7,8 @@ use ide_db::{
5 RootDatabase, 7 RootDatabase,
6}; 8};
7use syntax::{ 9use 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
11use crate::{ 14use 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
106fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> { 112fn 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)
1138pub fn bar() { } 1144pub fn bar() { }
1139 1145
1140/// You might want to see [`std::fs::read()`] too. 1146/// You might want to see [`std::fs::read()`] too.