Skip to content

Commit e0ff4be

Browse files
committed
Auto merge of rust-lang#12913 - Veykril:attr-merge, r=Veykril
fix: Fix ast-id up when merging raw attributes Fixes rust-lang/rust-analyzer#12912
2 parents fb5e496 + 618cfd7 commit e0ff4be

File tree

6 files changed

+167
-10
lines changed

6 files changed

+167
-10
lines changed

crates/hir-def/src/attr.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,24 @@ impl RawAttrs {
124124

125125
pub(crate) fn merge(&self, other: Self) -> Self {
126126
// FIXME: This needs to fixup `AttrId`s
127-
match (&self.entries, &other.entries) {
127+
match (&self.entries, other.entries) {
128128
(None, None) => Self::EMPTY,
129-
(Some(entries), None) | (None, Some(entries)) => {
130-
Self { entries: Some(entries.clone()) }
131-
}
129+
(None, entries @ Some(_)) => Self { entries },
130+
(Some(entries), None) => Self { entries: Some(entries.clone()) },
132131
(Some(a), Some(b)) => {
133-
Self { entries: Some(a.iter().chain(b.iter()).cloned().collect()) }
132+
let last_ast_index = a.last().map_or(0, |it| it.id.ast_index + 1);
133+
Self {
134+
entries: Some(
135+
a.iter()
136+
.cloned()
137+
.chain(b.iter().map(|it| {
138+
let mut it = it.clone();
139+
it.id.ast_index += last_ast_index;
140+
it
141+
}))
142+
.collect(),
143+
),
144+
}
134145
}
135146
}
136147
}

crates/ide/src/syntax_highlighting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod html;
1313
#[cfg(test)]
1414
mod tests;
1515

16-
use hir::{InFile, Name, Semantics};
16+
use hir::{Name, Semantics};
1717
use ide_db::{FxHashMap, RootDatabase};
1818
use syntax::{
1919
ast, AstNode, AstToken, NodeOrToken, SyntaxKind::*, SyntaxNode, TextRange, WalkEvent, T,
@@ -325,7 +325,7 @@ fn traverse(
325325
Leave(NodeOrToken::Node(node)) => {
326326
// Doc comment highlighting injection, we do this when leaving the node
327327
// so that we overwrite the highlighting of the doc comment itself.
328-
inject::doc_comment(hl, sema, InFile::new(file_id.into(), &node));
328+
inject::doc_comment(hl, sema, file_id, &node);
329329
continue;
330330
}
331331
};

crates/ide/src/syntax_highlighting/inject.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::mem;
55
use either::Either;
66
use hir::{InFile, Semantics};
77
use ide_db::{
8-
active_parameter::ActiveParameter, defs::Definition, rust_doc::is_rust_fence, SymbolKind,
8+
active_parameter::ActiveParameter, base_db::FileId, defs::Definition, rust_doc::is_rust_fence,
9+
SymbolKind,
910
};
1011
use syntax::{
1112
ast::{self, AstNode, IsString, QuoteOffsets},
@@ -81,16 +82,18 @@ pub(super) fn ra_fixture(
8182
const RUSTDOC_FENCE_LENGTH: usize = 3;
8283
const RUSTDOC_FENCES: [&str; 2] = ["```", "~~~"];
8384

84-
/// Injection of syntax highlighting of doctests.
85+
/// Injection of syntax highlighting of doctests and intra doc links.
8586
pub(super) fn doc_comment(
8687
hl: &mut Highlights,
8788
sema: &Semantics<'_, RootDatabase>,
88-
InFile { file_id: src_file_id, value: node }: InFile<&SyntaxNode>,
89+
src_file_id: FileId,
90+
node: &SyntaxNode,
8991
) {
9092
let (attributes, def) = match doc_attributes(sema, node) {
9193
Some(it) => it,
9294
None => return,
9395
};
96+
let src_file_id = src_file_id.into();
9497

9598
// Extract intra-doc links and emit highlights for them.
9699
if let Some((docs, doc_mapping)) = attributes.docs_with_rangemap(sema.db) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
<style>
3+
body { margin: 0; }
4+
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
5+
6+
.lifetime { color: #DFAF8F; font-style: italic; }
7+
.label { color: #DFAF8F; font-style: italic; }
8+
.comment { color: #7F9F7F; }
9+
.documentation { color: #629755; }
10+
.intra_doc_link { font-style: italic; }
11+
.injected { opacity: 0.65 ; }
12+
.struct, .enum { color: #7CB8BB; }
13+
.enum_variant { color: #BDE0F3; }
14+
.string_literal { color: #CC9393; }
15+
.field { color: #94BFF3; }
16+
.function { color: #93E0E3; }
17+
.function.unsafe { color: #BC8383; }
18+
.trait.unsafe { color: #BC8383; }
19+
.operator.unsafe { color: #BC8383; }
20+
.mutable.unsafe { color: #BC8383; text-decoration: underline; }
21+
.keyword.unsafe { color: #BC8383; font-weight: bold; }
22+
.macro.unsafe { color: #BC8383; }
23+
.parameter { color: #94BFF3; }
24+
.text { color: #DCDCCC; }
25+
.type { color: #7CB8BB; }
26+
.builtin_type { color: #8CD0D3; }
27+
.type_param { color: #DFAF8F; }
28+
.attribute { color: #94BFF3; }
29+
.numeric_literal { color: #BFEBBF; }
30+
.bool_literal { color: #BFE6EB; }
31+
.macro { color: #94BFF3; }
32+
.derive { color: #94BFF3; font-style: italic; }
33+
.module { color: #AFD8AF; }
34+
.value_param { color: #DCDCCC; }
35+
.variable { color: #DCDCCC; }
36+
.format_specifier { color: #CC696B; }
37+
.mutable { text-decoration: underline; }
38+
.escape_sequence { color: #94BFF3; }
39+
.keyword { color: #F0DFAF; font-weight: bold; }
40+
.control { font-style: italic; }
41+
.reference { font-style: italic; font-weight: bold; }
42+
43+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
44+
</style>
45+
<pre><code><span class="comment documentation">//! </span><span class="struct documentation injected intra_doc_link">[Struct]</span>
46+
<span class="comment documentation">//! This is an intra doc injection test for modules</span>
47+
<span class="comment documentation">//! </span><span class="struct documentation injected intra_doc_link">[Struct]</span>
48+
<span class="comment documentation">//! This is an intra doc injection test for modules</span>
49+
50+
<span class="keyword">pub</span> <span class="keyword">struct</span> <span class="struct declaration public">Struct</span><span class="semicolon">;</span>
51+
</code></pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
<style>
3+
body { margin: 0; }
4+
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
5+
6+
.lifetime { color: #DFAF8F; font-style: italic; }
7+
.label { color: #DFAF8F; font-style: italic; }
8+
.comment { color: #7F9F7F; }
9+
.documentation { color: #629755; }
10+
.intra_doc_link { font-style: italic; }
11+
.injected { opacity: 0.65 ; }
12+
.struct, .enum { color: #7CB8BB; }
13+
.enum_variant { color: #BDE0F3; }
14+
.string_literal { color: #CC9393; }
15+
.field { color: #94BFF3; }
16+
.function { color: #93E0E3; }
17+
.function.unsafe { color: #BC8383; }
18+
.trait.unsafe { color: #BC8383; }
19+
.operator.unsafe { color: #BC8383; }
20+
.mutable.unsafe { color: #BC8383; text-decoration: underline; }
21+
.keyword.unsafe { color: #BC8383; font-weight: bold; }
22+
.macro.unsafe { color: #BC8383; }
23+
.parameter { color: #94BFF3; }
24+
.text { color: #DCDCCC; }
25+
.type { color: #7CB8BB; }
26+
.builtin_type { color: #8CD0D3; }
27+
.type_param { color: #DFAF8F; }
28+
.attribute { color: #94BFF3; }
29+
.numeric_literal { color: #BFEBBF; }
30+
.bool_literal { color: #BFE6EB; }
31+
.macro { color: #94BFF3; }
32+
.derive { color: #94BFF3; font-style: italic; }
33+
.module { color: #AFD8AF; }
34+
.value_param { color: #DCDCCC; }
35+
.variable { color: #DCDCCC; }
36+
.format_specifier { color: #CC696B; }
37+
.mutable { text-decoration: underline; }
38+
.escape_sequence { color: #94BFF3; }
39+
.keyword { color: #F0DFAF; font-weight: bold; }
40+
.control { font-style: italic; }
41+
.reference { font-style: italic; font-weight: bold; }
42+
43+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
44+
</style>
45+
<pre><code><span class="comment documentation">/// </span><span class="struct documentation injected intra_doc_link">[crate::foo::Struct]</span>
46+
<span class="comment documentation">/// This is an intra doc injection test for modules</span>
47+
<span class="comment documentation">/// </span><span class="struct documentation injected intra_doc_link">[crate::foo::Struct]</span>
48+
<span class="comment documentation">/// This is an intra doc injection test for modules</span>
49+
<span class="keyword">mod</span> <span class="module declaration">foo</span><span class="semicolon">;</span>
50+
</code></pre>

crates/ide/src/syntax_highlighting/tests.rs

+42
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,48 @@ fn main() {
914914
);
915915
}
916916

917+
#[test]
918+
fn test_mod_hl_injection() {
919+
check_highlighting(
920+
r##"
921+
//- /foo.rs
922+
//! [Struct]
923+
//! This is an intra doc injection test for modules
924+
//! [Struct]
925+
//! This is an intra doc injection test for modules
926+
927+
pub struct Struct;
928+
//- /lib.rs crate:foo
929+
/// [crate::foo::Struct]
930+
/// This is an intra doc injection test for modules
931+
/// [crate::foo::Struct]
932+
/// This is an intra doc injection test for modules
933+
mod foo;
934+
"##,
935+
expect_file!["./test_data/highlight_module_docs_inline.html"],
936+
false,
937+
);
938+
check_highlighting(
939+
r##"
940+
//- /lib.rs crate:foo
941+
/// [crate::foo::Struct]
942+
/// This is an intra doc injection test for modules
943+
/// [crate::foo::Struct]
944+
/// This is an intra doc injection test for modules
945+
mod foo;
946+
//- /foo.rs
947+
//! [Struct]
948+
//! This is an intra doc injection test for modules
949+
//! [Struct]
950+
//! This is an intra doc injection test for modules
951+
952+
pub struct Struct;
953+
"##,
954+
expect_file!["./test_data/highlight_module_docs_outline.html"],
955+
false,
956+
);
957+
}
958+
917959
#[test]
918960
#[cfg_attr(
919961
all(unix, not(target_pointer_width = "64")),

0 commit comments

Comments
 (0)