mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Merge #8848
8848: Attach comments to ast::Impl r=Veykril a=Veykril bors r+ Fixes #8847 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
92abc56bc9
@ -19,14 +19,10 @@ use syntax::{
|
|||||||
pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
|
pub(crate) fn matching_brace(file: &SourceFile, offset: TextSize) -> Option<TextSize> {
|
||||||
const BRACES: &[SyntaxKind] =
|
const BRACES: &[SyntaxKind] =
|
||||||
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
|
&[T!['{'], T!['}'], T!['['], T![']'], T!['('], T![')'], T![<], T![>], T![|], T![|]];
|
||||||
let (brace_token, brace_idx) = file
|
let (brace_token, brace_idx) = file.syntax().token_at_offset(offset).find_map(|node| {
|
||||||
.syntax()
|
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
|
||||||
.token_at_offset(offset)
|
Some((node, idx))
|
||||||
.filter_map(|node| {
|
})?;
|
||||||
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
|
|
||||||
Some((node, idx))
|
|
||||||
})
|
|
||||||
.next()?;
|
|
||||||
let parent = brace_token.parent()?;
|
let parent = brace_token.parent()?;
|
||||||
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
|
if brace_token.kind() == T![|] && !ast::ParamList::can_cast(parent.kind()) {
|
||||||
cov_mark::hit!(pipes_not_braces);
|
cov_mark::hit!(pipes_not_braces);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::base_db::{CrateId, FileId, FilePosition};
|
use ide_db::{
|
||||||
use ide_db::RootDatabase;
|
base_db::{CrateId, FileId, FilePosition},
|
||||||
|
RootDatabase,
|
||||||
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::find_node_at_offset,
|
algo::find_node_at_offset,
|
||||||
|
@ -37,13 +37,25 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||||||
|
|
||||||
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
|
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
|
||||||
</style>
|
</style>
|
||||||
<pre><code><span class="comment documentation">/// ```</span>
|
<pre><code><span class="comment documentation">//! This is a module to test doc injection.</span>
|
||||||
|
<span class="comment documentation">//! ```</span>
|
||||||
|
<span class="comment documentation">//! </span><span class="keyword injected">fn</span><span class="none injected"> </span><span class="function declaration injected">test</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="none injected"> </span><span class="brace injected">{</span><span class="brace injected">}</span>
|
||||||
|
<span class="comment documentation">//! ```</span>
|
||||||
|
|
||||||
|
<span class="comment documentation">/// ```</span>
|
||||||
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="semicolon injected">;</span>
|
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="semicolon injected">;</span>
|
||||||
<span class="comment documentation">/// ```</span>
|
<span class="comment documentation">/// ```</span>
|
||||||
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="brace">{</span>
|
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="brace">{</span>
|
||||||
<span class="field declaration">bar</span><span class="colon">:</span> <span class="builtin_type">bool</span><span class="comma">,</span>
|
<span class="field declaration">bar</span><span class="colon">:</span> <span class="builtin_type">bool</span><span class="comma">,</span>
|
||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
|
|
||||||
|
<span class="comment documentation">/// This is an impl with a code block.</span>
|
||||||
|
<span class="comment documentation">///</span>
|
||||||
|
<span class="comment documentation">/// ```</span>
|
||||||
|
<span class="comment documentation">/// </span><span class="keyword injected">fn</span><span class="none injected"> </span><span class="function declaration injected">foo</span><span class="parenthesis injected">(</span><span class="parenthesis injected">)</span><span class="none injected"> </span><span class="brace injected">{</span>
|
||||||
|
<span class="comment documentation">///</span>
|
||||||
|
<span class="comment documentation">/// </span><span class="brace injected">}</span>
|
||||||
|
<span class="comment documentation">/// ```</span>
|
||||||
<span class="keyword">impl</span> <span class="struct">Foo</span> <span class="brace">{</span>
|
<span class="keyword">impl</span> <span class="struct">Foo</span> <span class="brace">{</span>
|
||||||
<span class="comment documentation">/// ```</span>
|
<span class="comment documentation">/// ```</span>
|
||||||
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Call me</span>
|
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Call me</span>
|
||||||
|
@ -524,6 +524,11 @@ fn main() {
|
|||||||
fn test_highlight_doc_comment() {
|
fn test_highlight_doc_comment() {
|
||||||
check_highlighting(
|
check_highlighting(
|
||||||
r#"
|
r#"
|
||||||
|
//! This is a module to test doc injection.
|
||||||
|
//! ```
|
||||||
|
//! fn test() {}
|
||||||
|
//! ```
|
||||||
|
|
||||||
/// ```
|
/// ```
|
||||||
/// let _ = "early doctests should not go boom";
|
/// let _ = "early doctests should not go boom";
|
||||||
/// ```
|
/// ```
|
||||||
@ -531,6 +536,13 @@ struct Foo {
|
|||||||
bar: bool,
|
bar: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is an impl with a code block.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// fn foo() {
|
||||||
|
///
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
impl Foo {
|
impl Foo {
|
||||||
/// ```
|
/// ```
|
||||||
/// let _ = "Call me
|
/// let _ = "Call me
|
||||||
|
@ -147,8 +147,8 @@ fn n_attached_trivias<'a>(
|
|||||||
trivias: impl Iterator<Item = (SyntaxKind, &'a str)>,
|
trivias: impl Iterator<Item = (SyntaxKind, &'a str)>,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
match kind {
|
match kind {
|
||||||
MACRO_CALL | MACRO_RULES | MACRO_DEF | CONST | TYPE_ALIAS | STRUCT | UNION | ENUM
|
CONST | ENUM | FN | IMPL | MACRO_CALL | MACRO_DEF | MACRO_RULES | MODULE | RECORD_FIELD
|
||||||
| VARIANT | FN | TRAIT | MODULE | RECORD_FIELD | STATIC | USE => {
|
| STATIC | STRUCT | TRAIT | TUPLE_FIELD | TYPE_ALIAS | UNION | USE | VARIANT => {
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
let mut trivias = trivias.enumerate().peekable();
|
let mut trivias = trivias.enumerate().peekable();
|
||||||
|
|
||||||
|
@ -127,9 +127,9 @@ SOURCE_FILE@0..764
|
|||||||
WHITESPACE@537..538 "\n"
|
WHITESPACE@537..538 "\n"
|
||||||
R_CURLY@538..539 "}"
|
R_CURLY@538..539 "}"
|
||||||
WHITESPACE@539..541 "\n\n"
|
WHITESPACE@539..541 "\n\n"
|
||||||
COMMENT@541..601 "// https://github.com ..."
|
IMPL@541..763
|
||||||
WHITESPACE@601..602 "\n"
|
COMMENT@541..601 "// https://github.com ..."
|
||||||
IMPL@602..763
|
WHITESPACE@601..602 "\n"
|
||||||
IMPL_KW@602..606 "impl"
|
IMPL_KW@602..606 "impl"
|
||||||
WHITESPACE@606..607 " "
|
WHITESPACE@606..607 " "
|
||||||
PATH_TYPE@607..615
|
PATH_TYPE@607..615
|
||||||
|
Loading…
Reference in New Issue
Block a user