Skip to content

Commit 9a32731

Browse files
committed
Implement basic Documentation source to syntax range mapping
1 parent 9df78ec commit 9a32731

File tree

5 files changed

+149
-36
lines changed

5 files changed

+149
-36
lines changed

crates/hir_def/src/attr.rs

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! A higher level attributes based on TokenTree, with also some shortcuts.
22
3-
use std::{ops, sync::Arc};
3+
use std::{
4+
cmp::Ordering,
5+
ops::{self, Range},
6+
sync::Arc,
7+
};
48

59
use base_db::CrateId;
610
use cfg::{CfgExpr, CfgOptions};
@@ -12,7 +16,7 @@ use mbe::ast_to_token_tree;
1216
use smallvec::{smallvec, SmallVec};
1317
use syntax::{
1418
ast::{self, AstNode, AttrsOwner},
15-
match_ast, AstToken, SmolStr, SyntaxNode,
19+
match_ast, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
1620
};
1721
use tt::Subtree;
1822

@@ -451,6 +455,54 @@ impl AttrsWithOwner {
451455
.collect(),
452456
}
453457
}
458+
459+
pub fn docs_with_rangemap(
460+
&self,
461+
db: &dyn DefDatabase,
462+
) -> Option<(Documentation, DocsRangeMap)> {
463+
// FIXME: code duplication in `docs` above
464+
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? {
465+
AttrInput::Literal(s) => Some((s, attr.index)),
466+
AttrInput::TokenTree(_) => None,
467+
});
468+
let indent = docs
469+
.clone()
470+
.flat_map(|(s, _)| s.lines())
471+
.filter(|line| !line.chars().all(|c| c.is_whitespace()))
472+
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
473+
.min()
474+
.unwrap_or(0);
475+
let mut buf = String::new();
476+
let mut mapping = Vec::new();
477+
for (doc, idx) in docs {
478+
// str::lines doesn't yield anything for the empty string
479+
if !doc.is_empty() {
480+
for line in doc.split('\n') {
481+
let line = line.trim_end();
482+
let (offset, line) = match line.char_indices().nth(indent) {
483+
Some((offset, _)) => (offset, &line[offset..]),
484+
None => (0, line),
485+
};
486+
let buf_offset = buf.len();
487+
buf.push_str(line);
488+
mapping.push((
489+
Range { start: buf_offset, end: buf.len() },
490+
idx,
491+
Range { start: offset, end: line.len() },
492+
));
493+
buf.push('\n');
494+
}
495+
} else {
496+
buf.push('\n');
497+
}
498+
}
499+
buf.pop();
500+
if buf.is_empty() {
501+
None
502+
} else {
503+
Some((Documentation(buf), DocsRangeMap { mapping, source: self.source_map(db).attrs }))
504+
}
505+
}
454506
}
455507

456508
fn inner_attributes(
@@ -507,6 +559,59 @@ impl AttrSourceMap {
507559
}
508560
}
509561

562+
/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree.
563+
pub struct DocsRangeMap {
564+
source: Vec<InFile<Either<ast::Attr, ast::Comment>>>,
565+
// (docstring-line-range, attr_index, attr-string-range)
566+
// a mapping from the text range of a line of the [`Documentation`] to the attribute index and
567+
// the original (untrimmed) syntax doc line
568+
mapping: Vec<(Range<usize>, u32, Range<usize>)>,
569+
}
570+
571+
impl DocsRangeMap {
572+
pub fn map(&self, range: Range<usize>) -> Option<InFile<TextRange>> {
573+
let found = self
574+
.mapping
575+
.binary_search_by(|(probe, ..)| {
576+
if probe.contains(&range.start) {
577+
Ordering::Equal
578+
} else {
579+
probe.start.cmp(&range.end)
580+
}
581+
})
582+
.ok()?;
583+
let (line_docs_range, idx, original_line_src_range) = self.mapping[found].clone();
584+
if range.end > line_docs_range.end {
585+
return None;
586+
}
587+
588+
let relative_range = Range {
589+
start: range.start - line_docs_range.start,
590+
end: range.end - line_docs_range.start,
591+
};
592+
let range_len = TextSize::from((range.end - range.start) as u32);
593+
594+
let &InFile { file_id, value: ref source } = &self.source[idx as usize];
595+
match source {
596+
Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
597+
// as well as for whats done in syntax highlight doc injection
598+
Either::Right(comment) => {
599+
let text_range = comment.syntax().text_range();
600+
let range = TextRange::at(
601+
text_range.start()
602+
+ TextSize::from(
603+
(comment.prefix().len()
604+
+ original_line_src_range.start
605+
+ relative_range.start) as u32,
606+
),
607+
text_range.len().min(range_len),
608+
);
609+
Some(InFile { file_id, value: range })
610+
}
611+
}
612+
}
613+
}
614+
510615
#[derive(Debug, Clone, PartialEq, Eq)]
511616
pub struct Attr {
512617
index: u32,

crates/ide/src/goto_definition.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub(crate) fn goto_definition(
3232
let parent = token.parent()?;
3333
if let Some(comment) = ast::Comment::cast(token) {
3434
let docs = doc_owner_to_def(&sema, &parent)?.docs(db)?;
35+
3536
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment, docs)?;
3637
let def = doc_owner_to_def(&sema, &parent)?;
3738
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;

crates/ide/src/syntax_highlighting/inject.rs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! "Recursive" Syntax highlighting for code in doctests and fixtures.
22
3-
use std::{mem, ops::Range};
3+
use std::mem;
44

55
use either::Either;
66
use hir::{HasAttrs, InFile, Semantics};
@@ -139,8 +139,28 @@ pub(super) fn doc_comment(
139139
// Replace the original, line-spanning comment ranges by new, only comment-prefix
140140
// spanning comment ranges.
141141
let mut new_comments = Vec::new();
142-
let mut intra_doc_links = Vec::new();
143142
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+
144164
for attr in attributes.by_key("doc").attrs() {
145165
let InFile { file_id, value: src } = attrs_source_map.source_of(&attr);
146166
if file_id != node.file_id {
@@ -186,25 +206,7 @@ pub(super) fn doc_comment(
186206
is_doctest = is_codeblock && is_rust;
187207
continue;
188208
}
189-
None if !is_doctest => {
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-
}
209+
None if !is_doctest => continue,
208210
None => (),
209211
}
210212

@@ -223,17 +225,6 @@ pub(super) fn doc_comment(
223225
}
224226
}
225227

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-
237228
if new_comments.is_empty() {
238229
return; // no need to run an analysis on an empty file
239230
}

crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@
100100
<span class="brace">}</span>
101101

102102
<span class="comment documentation">/// </span><span class="struct documentation intra_doc_link injected">[`Foo`](Foo)</span><span class="comment documentation"> is a struct</span>
103-
<span class="comment documentation">/// </span><span class="function documentation intra_doc_link injected">[`all_the_links`](all_the_links)</span><span class="comment documentation"> is this function</span>
103+
<span class="comment documentation">/// This function is &gt; </span><span class="function documentation intra_doc_link injected">[`all_the_links`](all_the_links)</span><span class="comment documentation"> &lt;</span>
104104
<span class="comment documentation">/// [`noop`](noop) is a macro below</span>
105+
<span class="comment documentation">/// </span><span class="struct documentation intra_doc_link injected">[`Item`]</span><span class="comment documentation"> is a struct in the module </span><span class="module documentation intra_doc_link injected">[`module`]</span>
106+
<span class="comment documentation">///</span>
107+
<span class="comment documentation">/// [`Item`]: module::Item</span>
108+
<span class="comment documentation">/// [mix_and_match]: ThisShouldntResolve</span>
105109
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration">all_the_links</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span>
106110

111+
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">module</span> <span class="brace">{</span>
112+
<span class="keyword">pub</span> <span class="keyword">struct</span> <span class="struct declaration">Item</span><span class="semicolon">;</span>
113+
<span class="brace">}</span>
114+
107115
<span class="comment documentation">/// ```</span>
108116
<span class="comment documentation">/// </span><span class="macro injected">noop!</span><span class="parenthesis injected">(</span><span class="numeric_literal injected">1</span><span class="parenthesis injected">)</span><span class="semicolon injected">;</span>
109117
<span class="comment documentation">/// ```</span>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,18 @@ impl Foo {
544544
}
545545
546546
/// [`Foo`](Foo) is a struct
547-
/// [`all_the_links`](all_the_links) is this function
547+
/// This function is > [`all_the_links`](all_the_links) <
548548
/// [`noop`](noop) is a macro below
549+
/// [`Item`] is a struct in the module [`module`]
550+
///
551+
/// [`Item`]: module::Item
552+
/// [mix_and_match]: ThisShouldntResolve
549553
pub fn all_the_links() {}
550554
555+
pub mod module {
556+
pub struct Item;
557+
}
558+
551559
/// ```
552560
/// noop!(1);
553561
/// ```

0 commit comments

Comments
 (0)