diff options
author | Lukas Wirth <[email protected]> | 2021-03-30 16:20:43 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-30 16:20:43 +0100 |
commit | 9a327311e4a9b9102528751e052c63266c00c6bd (patch) | |
tree | 6cddc362ef8237570c0ce1f8fef73d9dcc9eec8a /crates/ide/src/syntax_highlighting/inject.rs | |
parent | 9df78ec4a4e41ca94b25f292aba90e266f104f02 (diff) |
Implement basic Documentation source to syntax range mapping
Diffstat (limited to 'crates/ide/src/syntax_highlighting/inject.rs')
-rw-r--r-- | crates/ide/src/syntax_highlighting/inject.rs | 55 |
1 files changed, 23 insertions, 32 deletions
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index b62d43256..504783f31 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! "Recursive" Syntax highlighting for code in doctests and fixtures. | 1 | //! "Recursive" Syntax highlighting for code in doctests and fixtures. |
2 | 2 | ||
3 | use std::{mem, ops::Range}; | 3 | use std::mem; |
4 | 4 | ||
5 | use either::Either; | 5 | use either::Either; |
6 | use hir::{HasAttrs, InFile, Semantics}; | 6 | use hir::{HasAttrs, InFile, Semantics}; |
@@ -139,8 +139,28 @@ pub(super) fn doc_comment( | |||
139 | // Replace the original, line-spanning comment ranges by new, only comment-prefix | 139 | // Replace the original, line-spanning comment ranges by new, only comment-prefix |
140 | // spanning comment ranges. | 140 | // spanning comment ranges. |
141 | let mut new_comments = Vec::new(); | 141 | let mut new_comments = Vec::new(); |
142 | let mut intra_doc_links = Vec::new(); | ||
143 | let mut string; | 142 | let mut string; |
143 | |||
144 | if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) { | ||
145 | extract_definitions_from_markdown(docs.as_str()) | ||
146 | .into_iter() | ||
147 | .filter_map(|(range, link, ns)| { | ||
148 | let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?; | ||
149 | let InFile { file_id, value: range } = doc_mapping.map(range)?; | ||
150 | (file_id == node.file_id).then(|| (range, def)) | ||
151 | }) | ||
152 | .for_each(|(range, def)| { | ||
153 | hl.add(HlRange { | ||
154 | range, | ||
155 | highlight: module_def_to_hl_tag(def) | ||
156 | | HlMod::Documentation | ||
157 | | HlMod::Injected | ||
158 | | HlMod::IntraDocLink, | ||
159 | binding_hash: None, | ||
160 | }) | ||
161 | }); | ||
162 | } | ||
163 | |||
144 | for attr in attributes.by_key("doc").attrs() { | 164 | for attr in attributes.by_key("doc").attrs() { |
145 | let InFile { file_id, value: src } = attrs_source_map.source_of(&attr); | 165 | let InFile { file_id, value: src } = attrs_source_map.source_of(&attr); |
146 | if file_id != node.file_id { | 166 | if file_id != node.file_id { |
@@ -186,25 +206,7 @@ pub(super) fn doc_comment( | |||
186 | is_doctest = is_codeblock && is_rust; | 206 | is_doctest = is_codeblock && is_rust; |
187 | continue; | 207 | continue; |
188 | } | 208 | } |
189 | None if !is_doctest => { | 209 | None if !is_doctest => continue, |
190 | intra_doc_links.extend( | ||
191 | extract_definitions_from_markdown(line) | ||
192 | .into_iter() | ||
193 | .filter_map(|(range, link, ns)| { | ||
194 | Some(range).zip(resolve_doc_path_for_def(sema.db, def, &link, ns)) | ||
195 | }) | ||
196 | .map(|(Range { start, end }, def)| { | ||
197 | ( | ||
198 | def, | ||
199 | TextRange::at( | ||
200 | prev_range_start + TextSize::from(start as u32), | ||
201 | TextSize::from((end - start) as u32), | ||
202 | ), | ||
203 | ) | ||
204 | }), | ||
205 | ); | ||
206 | continue; | ||
207 | } | ||
208 | None => (), | 210 | None => (), |
209 | } | 211 | } |
210 | 212 | ||
@@ -223,17 +225,6 @@ pub(super) fn doc_comment( | |||
223 | } | 225 | } |
224 | } | 226 | } |
225 | 227 | ||
226 | for (def, range) in intra_doc_links { | ||
227 | hl.add(HlRange { | ||
228 | range, | ||
229 | highlight: module_def_to_hl_tag(def) | ||
230 | | HlMod::Documentation | ||
231 | | HlMod::Injected | ||
232 | | HlMod::IntraDocLink, | ||
233 | binding_hash: None, | ||
234 | }); | ||
235 | } | ||
236 | |||
237 | if new_comments.is_empty() { | 228 | if new_comments.is_empty() { |
238 | return; // no need to run an analysis on an empty file | 229 | return; // no need to run an analysis on an empty file |
239 | } | 230 | } |