aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/doc_links.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-03-23 18:19:44 +0000
committerLukas Wirth <[email protected]>2021-03-23 18:57:04 +0000
commit8b0d0bd9c7488fedfd0bdd34a0a9cb04da3f143a (patch)
tree755b2f33124306b9ae9ad658da66b543806928eb /crates/ide/src/doc_links.rs
parent065a8e87cd015ccfe5c01ad6b603f4fdad18f768 (diff)
Show item info when hovering intra doc links
Diffstat (limited to 'crates/ide/src/doc_links.rs')
-rw-r--r--crates/ide/src/doc_links.rs129
1 files changed, 104 insertions, 25 deletions
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs
index c7c1f4fee..f12c9d442 100644
--- a/crates/ide/src/doc_links.rs
+++ b/crates/ide/src/doc_links.rs
@@ -1,4 +1,4 @@
1//! Resolves and rewrites links in markdown documentation. 1//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
2 2
3use std::{convert::TryFrom, iter::once, ops::Range}; 3use std::{convert::TryFrom, iter::once, ops::Range};
4 4
@@ -15,7 +15,10 @@ use ide_db::{
15 defs::{Definition, NameClass, NameRefClass}, 15 defs::{Definition, NameClass, NameRefClass},
16 RootDatabase, 16 RootDatabase,
17}; 17};
18use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; 18use syntax::{
19 ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextSize,
20 TokenAtOffset, T,
21};
19 22
20use crate::{FilePosition, Semantics}; 23use crate::{FilePosition, Semantics};
21 24
@@ -60,29 +63,6 @@ pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Defi
60 out 63 out
61} 64}
62 65
63pub(crate) fn extract_definitions_from_markdown(
64 markdown: &str,
65) -> Vec<(String, Option<hir::Namespace>, Range<usize>)> {
66 let mut res = vec![];
67 let mut cb = |link: BrokenLink| {
68 // These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
69 // this is fixed in the repo but not on the crates.io release yet
70 Some((
71 /*url*/ link.reference.to_owned().into(),
72 /*title*/ link.reference.to_owned().into(),
73 ))
74 };
75 let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
76 for (event, range) in doc.into_offset_iter() {
77 if let Event::Start(Tag::Link(_, target, title)) = event {
78 let link = if target.is_empty() { title } else { target };
79 let (link, ns) = parse_link(&link);
80 res.push((link.to_string(), ns, range));
81 }
82 }
83 res
84}
85
86/// Remove all links in markdown documentation. 66/// Remove all links in markdown documentation.
87pub(crate) fn remove_links(markdown: &str) -> String { 67pub(crate) fn remove_links(markdown: &str) -> String {
88 let mut drop_link = false; 68 let mut drop_link = false;
@@ -118,6 +98,105 @@ pub(crate) fn remove_links(markdown: &str) -> String {
118 out 98 out
119} 99}
120 100
101pub(crate) fn extract_definitions_from_markdown(
102 markdown: &str,
103) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
104 let mut res = vec![];
105 let mut cb = |link: BrokenLink| {
106 // These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
107 // this is fixed in the repo but not on the crates.io release yet
108 Some((
109 /*url*/ link.reference.to_owned().into(),
110 /*title*/ link.reference.to_owned().into(),
111 ))
112 };
113 let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
114 for (event, range) in doc.into_offset_iter() {
115 if let Event::Start(Tag::Link(_, target, title)) = event {
116 let link = if target.is_empty() { title } else { target };
117 let (link, ns) = parse_link(&link);
118 res.push((range, link.to_string(), ns));
119 }
120 }
121 res
122}
123
124/// Extracts a link from a comment at the given position returning the spanning range, link and
125/// optionally it's namespace.
126pub(crate) fn extract_positioned_link_from_comment(
127 position: TextSize,
128 comment: &ast::Comment,
129) -> Option<(TextRange, String, Option<hir::Namespace>)> {
130 let doc_comment = comment.doc_comment()?;
131 let comment_start =
132 comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
133 let def_links = extract_definitions_from_markdown(doc_comment);
134 let (range, def_link, ns) =
135 def_links.into_iter().find_map(|(Range { start, end }, def_link, ns)| {
136 let range = TextRange::at(
137 comment_start + TextSize::from(start as u32),
138 TextSize::from((end - start) as u32),
139 );
140 range.contains(position).then(|| (range, def_link, ns))
141 })?;
142 Some((range, def_link, ns))
143}
144
145/// Turns a syntax node into it's [`Definition`] if it can hold docs.
146pub(crate) fn doc_owner_to_def(
147 sema: &Semantics<RootDatabase>,
148 item: &SyntaxNode,
149) -> Option<Definition> {
150 let res: hir::ModuleDef = match_ast! {
151 match item {
152 ast::SourceFile(_it) => sema.scope(item).module()?.into(),
153 ast::Fn(it) => sema.to_def(&it)?.into(),
154 ast::Struct(it) => sema.to_def(&it)?.into(),
155 ast::Enum(it) => sema.to_def(&it)?.into(),
156 ast::Union(it) => sema.to_def(&it)?.into(),
157 ast::Trait(it) => sema.to_def(&it)?.into(),
158 ast::Const(it) => sema.to_def(&it)?.into(),
159 ast::Static(it) => sema.to_def(&it)?.into(),
160 ast::TypeAlias(it) => sema.to_def(&it)?.into(),
161 ast::Variant(it) => sema.to_def(&it)?.into(),
162 ast::Trait(it) => sema.to_def(&it)?.into(),
163 ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
164 ast::MacroRules(it) => return sema.to_def(&it).map(Definition::Macro),
165 ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
166 ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
167 _ => return None,
168 }
169 };
170 Some(Definition::ModuleDef(res))
171}
172
173pub(crate) fn resolve_doc_path_for_def(
174 db: &dyn HirDatabase,
175 def: Definition,
176 link: &str,
177 ns: Option<hir::Namespace>,
178) -> Option<hir::ModuleDef> {
179 match def {
180 Definition::ModuleDef(def) => match def {
181 ModuleDef::Module(it) => it.resolve_doc_path(db, &link, ns),
182 ModuleDef::Function(it) => it.resolve_doc_path(db, &link, ns),
183 ModuleDef::Adt(it) => it.resolve_doc_path(db, &link, ns),
184 ModuleDef::Variant(it) => it.resolve_doc_path(db, &link, ns),
185 ModuleDef::Const(it) => it.resolve_doc_path(db, &link, ns),
186 ModuleDef::Static(it) => it.resolve_doc_path(db, &link, ns),
187 ModuleDef::Trait(it) => it.resolve_doc_path(db, &link, ns),
188 ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, &link, ns),
189 ModuleDef::BuiltinType(_) => None,
190 },
191 Definition::Macro(it) => it.resolve_doc_path(db, &link, ns),
192 Definition::Field(it) => it.resolve_doc_path(db, &link, ns),
193 Definition::SelfType(_)
194 | Definition::Local(_)
195 | Definition::GenericParam(_)
196 | Definition::Label(_) => None,
197 }
198}
199
121// FIXME: 200// FIXME:
122// BUG: For Option::Some 201// BUG: For Option::Some
123// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some 202// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some