mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
Implement basic Documentation source to syntax range mapping
This commit is contained in:
parent
9df78ec4a4
commit
9a327311e4
5 changed files with 149 additions and 36 deletions
|
@ -1,6 +1,10 @@
|
|||
//! A higher level attributes based on TokenTree, with also some shortcuts.
|
||||
|
||||
use std::{ops, sync::Arc};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
ops::{self, Range},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use base_db::CrateId;
|
||||
use cfg::{CfgExpr, CfgOptions};
|
||||
|
@ -12,7 +16,7 @@ use mbe::ast_to_token_tree;
|
|||
use smallvec::{smallvec, SmallVec};
|
||||
use syntax::{
|
||||
ast::{self, AstNode, AttrsOwner},
|
||||
match_ast, AstToken, SmolStr, SyntaxNode,
|
||||
match_ast, AstToken, SmolStr, SyntaxNode, TextRange, TextSize,
|
||||
};
|
||||
use tt::Subtree;
|
||||
|
||||
|
@ -451,6 +455,54 @@ impl AttrsWithOwner {
|
|||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn docs_with_rangemap(
|
||||
&self,
|
||||
db: &dyn DefDatabase,
|
||||
) -> Option<(Documentation, DocsRangeMap)> {
|
||||
// FIXME: code duplication in `docs` above
|
||||
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? {
|
||||
AttrInput::Literal(s) => Some((s, attr.index)),
|
||||
AttrInput::TokenTree(_) => None,
|
||||
});
|
||||
let indent = docs
|
||||
.clone()
|
||||
.flat_map(|(s, _)| s.lines())
|
||||
.filter(|line| !line.chars().all(|c| c.is_whitespace()))
|
||||
.map(|line| line.chars().take_while(|c| c.is_whitespace()).count())
|
||||
.min()
|
||||
.unwrap_or(0);
|
||||
let mut buf = String::new();
|
||||
let mut mapping = Vec::new();
|
||||
for (doc, idx) in docs {
|
||||
// str::lines doesn't yield anything for the empty string
|
||||
if !doc.is_empty() {
|
||||
for line in doc.split('\n') {
|
||||
let line = line.trim_end();
|
||||
let (offset, line) = match line.char_indices().nth(indent) {
|
||||
Some((offset, _)) => (offset, &line[offset..]),
|
||||
None => (0, line),
|
||||
};
|
||||
let buf_offset = buf.len();
|
||||
buf.push_str(line);
|
||||
mapping.push((
|
||||
Range { start: buf_offset, end: buf.len() },
|
||||
idx,
|
||||
Range { start: offset, end: line.len() },
|
||||
));
|
||||
buf.push('\n');
|
||||
}
|
||||
} else {
|
||||
buf.push('\n');
|
||||
}
|
||||
}
|
||||
buf.pop();
|
||||
if buf.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some((Documentation(buf), DocsRangeMap { mapping, source: self.source_map(db).attrs }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inner_attributes(
|
||||
|
@ -507,6 +559,59 @@ impl AttrSourceMap {
|
|||
}
|
||||
}
|
||||
|
||||
/// A struct to map text ranges from [`Documentation`] back to TextRanges in the syntax tree.
|
||||
pub struct DocsRangeMap {
|
||||
source: Vec<InFile<Either<ast::Attr, ast::Comment>>>,
|
||||
// (docstring-line-range, attr_index, attr-string-range)
|
||||
// a mapping from the text range of a line of the [`Documentation`] to the attribute index and
|
||||
// the original (untrimmed) syntax doc line
|
||||
mapping: Vec<(Range<usize>, u32, Range<usize>)>,
|
||||
}
|
||||
|
||||
impl DocsRangeMap {
|
||||
pub fn map(&self, range: Range<usize>) -> Option<InFile<TextRange>> {
|
||||
let found = self
|
||||
.mapping
|
||||
.binary_search_by(|(probe, ..)| {
|
||||
if probe.contains(&range.start) {
|
||||
Ordering::Equal
|
||||
} else {
|
||||
probe.start.cmp(&range.end)
|
||||
}
|
||||
})
|
||||
.ok()?;
|
||||
let (line_docs_range, idx, original_line_src_range) = self.mapping[found].clone();
|
||||
if range.end > line_docs_range.end {
|
||||
return None;
|
||||
}
|
||||
|
||||
let relative_range = Range {
|
||||
start: range.start - line_docs_range.start,
|
||||
end: range.end - line_docs_range.start,
|
||||
};
|
||||
let range_len = TextSize::from((range.end - range.start) as u32);
|
||||
|
||||
let &InFile { file_id, value: ref source } = &self.source[idx as usize];
|
||||
match source {
|
||||
Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
|
||||
// as well as for whats done in syntax highlight doc injection
|
||||
Either::Right(comment) => {
|
||||
let text_range = comment.syntax().text_range();
|
||||
let range = TextRange::at(
|
||||
text_range.start()
|
||||
+ TextSize::from(
|
||||
(comment.prefix().len()
|
||||
+ original_line_src_range.start
|
||||
+ relative_range.start) as u32,
|
||||
),
|
||||
text_range.len().min(range_len),
|
||||
);
|
||||
Some(InFile { file_id, value: range })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Attr {
|
||||
index: u32,
|
||||
|
|
|
@ -32,6 +32,7 @@ pub(crate) fn goto_definition(
|
|||
let parent = token.parent()?;
|
||||
if let Some(comment) = ast::Comment::cast(token) {
|
||||
let docs = doc_owner_to_def(&sema, &parent)?.docs(db)?;
|
||||
|
||||
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment, docs)?;
|
||||
let def = doc_owner_to_def(&sema, &parent)?;
|
||||
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! "Recursive" Syntax highlighting for code in doctests and fixtures.
|
||||
|
||||
use std::{mem, ops::Range};
|
||||
use std::mem;
|
||||
|
||||
use either::Either;
|
||||
use hir::{HasAttrs, InFile, Semantics};
|
||||
|
@ -139,8 +139,28 @@ pub(super) fn doc_comment(
|
|||
// Replace the original, line-spanning comment ranges by new, only comment-prefix
|
||||
// spanning comment ranges.
|
||||
let mut new_comments = Vec::new();
|
||||
let mut intra_doc_links = Vec::new();
|
||||
let mut string;
|
||||
|
||||
if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
|
||||
extract_definitions_from_markdown(docs.as_str())
|
||||
.into_iter()
|
||||
.filter_map(|(range, link, ns)| {
|
||||
let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?;
|
||||
let InFile { file_id, value: range } = doc_mapping.map(range)?;
|
||||
(file_id == node.file_id).then(|| (range, def))
|
||||
})
|
||||
.for_each(|(range, def)| {
|
||||
hl.add(HlRange {
|
||||
range,
|
||||
highlight: module_def_to_hl_tag(def)
|
||||
| HlMod::Documentation
|
||||
| HlMod::Injected
|
||||
| HlMod::IntraDocLink,
|
||||
binding_hash: None,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
for attr in attributes.by_key("doc").attrs() {
|
||||
let InFile { file_id, value: src } = attrs_source_map.source_of(&attr);
|
||||
if file_id != node.file_id {
|
||||
|
@ -186,25 +206,7 @@ pub(super) fn doc_comment(
|
|||
is_doctest = is_codeblock && is_rust;
|
||||
continue;
|
||||
}
|
||||
None if !is_doctest => {
|
||||
intra_doc_links.extend(
|
||||
extract_definitions_from_markdown(line)
|
||||
.into_iter()
|
||||
.filter_map(|(range, link, ns)| {
|
||||
Some(range).zip(resolve_doc_path_for_def(sema.db, def, &link, ns))
|
||||
})
|
||||
.map(|(Range { start, end }, def)| {
|
||||
(
|
||||
def,
|
||||
TextRange::at(
|
||||
prev_range_start + TextSize::from(start as u32),
|
||||
TextSize::from((end - start) as u32),
|
||||
),
|
||||
)
|
||||
}),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
None if !is_doctest => continue,
|
||||
None => (),
|
||||
}
|
||||
|
||||
|
@ -223,17 +225,6 @@ pub(super) fn doc_comment(
|
|||
}
|
||||
}
|
||||
|
||||
for (def, range) in intra_doc_links {
|
||||
hl.add(HlRange {
|
||||
range,
|
||||
highlight: module_def_to_hl_tag(def)
|
||||
| HlMod::Documentation
|
||||
| HlMod::Injected
|
||||
| HlMod::IntraDocLink,
|
||||
binding_hash: None,
|
||||
});
|
||||
}
|
||||
|
||||
if new_comments.is_empty() {
|
||||
return; // no need to run an analysis on an empty file
|
||||
}
|
||||
|
|
|
@ -100,10 +100,18 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
<span class="brace">}</span>
|
||||
|
||||
<span class="comment documentation">/// </span><span class="struct documentation intra_doc_link injected">[`Foo`](Foo)</span><span class="comment documentation"> is a struct</span>
|
||||
<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>
|
||||
<span class="comment documentation">/// This function is > </span><span class="function documentation intra_doc_link injected">[`all_the_links`](all_the_links)</span><span class="comment documentation"> <</span>
|
||||
<span class="comment documentation">/// [`noop`](noop) is a macro below</span>
|
||||
<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>
|
||||
<span class="comment documentation">///</span>
|
||||
<span class="comment documentation">/// [`Item`]: module::Item</span>
|
||||
<span class="comment documentation">/// [mix_and_match]: ThisShouldntResolve</span>
|
||||
<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>
|
||||
|
||||
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">module</span> <span class="brace">{</span>
|
||||
<span class="keyword">pub</span> <span class="keyword">struct</span> <span class="struct declaration">Item</span><span class="semicolon">;</span>
|
||||
<span class="brace">}</span>
|
||||
|
||||
<span class="comment documentation">/// ```</span>
|
||||
<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>
|
||||
<span class="comment documentation">/// ```</span>
|
||||
|
|
|
@ -544,10 +544,18 @@ impl Foo {
|
|||
}
|
||||
|
||||
/// [`Foo`](Foo) is a struct
|
||||
/// [`all_the_links`](all_the_links) is this function
|
||||
/// This function is > [`all_the_links`](all_the_links) <
|
||||
/// [`noop`](noop) is a macro below
|
||||
/// [`Item`] is a struct in the module [`module`]
|
||||
///
|
||||
/// [`Item`]: module::Item
|
||||
/// [mix_and_match]: ThisShouldntResolve
|
||||
pub fn all_the_links() {}
|
||||
|
||||
pub mod module {
|
||||
pub struct Item;
|
||||
}
|
||||
|
||||
/// ```
|
||||
/// noop!(1);
|
||||
/// ```
|
||||
|
|
Loading…
Reference in a new issue