Skip to content

Commit 37acea8

Browse files
authored
Merge pull request #19400 from Shourya742/2025-03-20-fix-syntax-highlighting
Fix missing syntax highlighting for `&raw const` / `&raw mut` in all files.
2 parents c85fcd2 + cfac2c3 commit 37acea8

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

crates/ide/src/syntax_highlighting.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,7 @@ pub(crate) fn highlight(
222222
};
223223

224224
let mut hl = highlights::Highlights::new(root.text_range());
225-
let krate = match sema.scope(&root) {
226-
Some(it) => it.krate(),
227-
None => return hl.to_vec(),
228-
};
225+
let krate = sema.scope(&root).map(|it| it.krate());
229226
traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight);
230227
hl.to_vec()
231228
}
@@ -235,7 +232,7 @@ fn traverse(
235232
sema: &Semantics<'_, RootDatabase>,
236233
config: HighlightConfig,
237234
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
238-
krate: hir::Crate,
235+
krate: Option<hir::Crate>,
239236
range_to_highlight: TextRange,
240237
) {
241238
let is_unlinked = sema.file_to_module_def(file_id).is_none();
@@ -498,7 +495,7 @@ fn string_injections(
498495
sema: &Semantics<'_, RootDatabase>,
499496
config: HighlightConfig,
500497
file_id: EditionedFileId,
501-
krate: hir::Crate,
498+
krate: Option<hir::Crate>,
502499
token: SyntaxToken,
503500
descended_token: &SyntaxToken,
504501
) -> ControlFlow<()> {

crates/ide/src/syntax_highlighting/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
pub(super) fn highlight_format_string(
1616
stack: &mut Highlights,
1717
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
18-
krate: hir::Crate,
18+
krate: Option<hir::Crate>,
1919
string: &ast::String,
2020
expanded_string: &ast::String,
2121
edition: Edition,

crates/ide/src/syntax_highlighting/highlight.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(super) fn token(
6363

6464
pub(super) fn name_like(
6565
sema: &Semantics<'_, RootDatabase>,
66-
krate: hir::Crate,
66+
krate: Option<hir::Crate>,
6767
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
6868
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
6969
syntactic_name_ref_highlighting: bool,
@@ -272,7 +272,7 @@ fn keyword(token: SyntaxToken, kind: SyntaxKind) -> Highlight {
272272

273273
fn highlight_name_ref(
274274
sema: &Semantics<'_, RootDatabase>,
275-
krate: hir::Crate,
275+
krate: Option<hir::Crate>,
276276
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
277277
binding_hash: &mut Option<u64>,
278278
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
@@ -401,9 +401,10 @@ fn highlight_name_ref(
401401
NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
402402
let mut h = HlTag::Symbol(SymbolKind::Module).into();
403403

404-
if resolved_krate != krate {
405-
h |= HlMod::Library
404+
if krate.as_ref().is_some_and(|krate| resolved_krate != *krate) {
405+
h |= HlMod::Library;
406406
}
407+
407408
let is_public = decl.visibility(db) == hir::Visibility::Public;
408409
if is_public {
409410
h |= HlMod::Public
@@ -431,7 +432,7 @@ fn highlight_name(
431432
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
432433
binding_hash: &mut Option<u64>,
433434
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
434-
krate: hir::Crate,
435+
krate: Option<hir::Crate>,
435436
name: ast::Name,
436437
edition: Edition,
437438
) -> Highlight {
@@ -476,7 +477,7 @@ fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
476477

477478
pub(super) fn highlight_def(
478479
sema: &Semantics<'_, RootDatabase>,
479-
krate: hir::Crate,
480+
krate: Option<hir::Crate>,
480481
def: Definition,
481482
edition: Edition,
482483
is_ref: bool,
@@ -660,7 +661,7 @@ pub(super) fn highlight_def(
660661
};
661662

662663
let def_crate = def.krate(db);
663-
let is_from_other_crate = def_crate != Some(krate);
664+
let is_from_other_crate = def_crate != krate;
664665
let is_from_builtin_crate = def_crate.is_some_and(|def_crate| def_crate.is_builtin(db));
665666
let is_builtin = matches!(
666667
def,
@@ -681,7 +682,7 @@ pub(super) fn highlight_def(
681682

682683
fn highlight_method_call_by_name_ref(
683684
sema: &Semantics<'_, RootDatabase>,
684-
krate: hir::Crate,
685+
krate: Option<hir::Crate>,
685686
name_ref: &ast::NameRef,
686687
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
687688
) -> Option<Highlight> {
@@ -691,7 +692,7 @@ fn highlight_method_call_by_name_ref(
691692

692693
fn highlight_method_call(
693694
sema: &Semantics<'_, RootDatabase>,
694-
krate: hir::Crate,
695+
krate: Option<hir::Crate>,
695696
method_call: &ast::MethodCallExpr,
696697
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
697698
) -> Option<Highlight> {
@@ -718,7 +719,7 @@ fn highlight_method_call(
718719
}
719720

720721
let def_crate = func.module(sema.db).krate();
721-
let is_from_other_crate = def_crate != krate;
722+
let is_from_other_crate = krate.as_ref().map_or(false, |krate| def_crate != *krate);
722723
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
723724
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
724725

@@ -791,7 +792,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
791792
fn highlight_name_ref_by_syntax(
792793
name: ast::NameRef,
793794
sema: &Semantics<'_, RootDatabase>,
794-
krate: hir::Crate,
795+
krate: Option<hir::Crate>,
795796
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
796797
) -> Highlight {
797798
let default = HlTag::UnresolvedReference;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
.parameter { color: #94BFF3; }
18+
.text { color: #DCDCCC; }
19+
.type { color: #7CB8BB; }
20+
.builtin_type { color: #8CD0D3; }
21+
.type_param { color: #DFAF8F; }
22+
.attribute { color: #94BFF3; }
23+
.numeric_literal { color: #BFEBBF; }
24+
.bool_literal { color: #BFE6EB; }
25+
.macro { color: #94BFF3; }
26+
.proc_macro { color: #94BFF3; text-decoration: underline; }
27+
.derive { color: #94BFF3; font-style: italic; }
28+
.module { color: #AFD8AF; }
29+
.value_param { color: #DCDCCC; }
30+
.variable { color: #DCDCCC; }
31+
.format_specifier { color: #CC696B; }
32+
.mutable { text-decoration: underline; }
33+
.escape_sequence { color: #94BFF3; }
34+
.keyword { color: #F0DFAF; font-weight: bold; }
35+
.control { font-style: italic; }
36+
.reference { font-style: italic; font-weight: bold; }
37+
.const { font-weight: bolder; }
38+
.unsafe { color: #BC8383; }
39+
40+
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
41+
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
42+
</style>
43+
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
44+
<span class="keyword">let</span> <span class="variable declaration">x</span> <span class="operator">=</span> <span class="operator">&</span><span class="keyword">raw</span> <span class="keyword">mut</span> <span class="numeric_literal">5</span><span class="semicolon">;</span>
45+
<span class="brace">}</span>
46+
</code></pre>

crates/ide/src/syntax_highlighting/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1420,3 +1420,18 @@ fn template() {}
14201420
false,
14211421
);
14221422
}
1423+
1424+
#[test]
1425+
fn issue_19357() {
1426+
check_highlighting(
1427+
r#"
1428+
//- /foo.rs
1429+
fn main() {
1430+
let x = &raw mut 5;
1431+
}
1432+
//- /main.rs
1433+
"#,
1434+
expect_file!["./test_data/highlight_issue_19357.html"],
1435+
false,
1436+
);
1437+
}

0 commit comments

Comments
 (0)