mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-29 02:52:11 +00:00
Merge pull request #19400 from Shourya742/2025-03-20-fix-syntax-highlighting
Fix missing syntax highlighting for `&raw const` / `&raw mut` in all files.
This commit is contained in:
commit
37acea8052
5 changed files with 77 additions and 18 deletions
|
|
@ -222,10 +222,7 @@ pub(crate) fn highlight(
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut hl = highlights::Highlights::new(root.text_range());
|
let mut hl = highlights::Highlights::new(root.text_range());
|
||||||
let krate = match sema.scope(&root) {
|
let krate = sema.scope(&root).map(|it| it.krate());
|
||||||
Some(it) => it.krate(),
|
|
||||||
None => return hl.to_vec(),
|
|
||||||
};
|
|
||||||
traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight);
|
traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight);
|
||||||
hl.to_vec()
|
hl.to_vec()
|
||||||
}
|
}
|
||||||
|
|
@ -235,7 +232,7 @@ fn traverse(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
config: HighlightConfig,
|
config: HighlightConfig,
|
||||||
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
|
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
range_to_highlight: TextRange,
|
range_to_highlight: TextRange,
|
||||||
) {
|
) {
|
||||||
let is_unlinked = sema.file_to_module_def(file_id).is_none();
|
let is_unlinked = sema.file_to_module_def(file_id).is_none();
|
||||||
|
|
@ -498,7 +495,7 @@ fn string_injections(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
config: HighlightConfig,
|
config: HighlightConfig,
|
||||||
file_id: EditionedFileId,
|
file_id: EditionedFileId,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
token: SyntaxToken,
|
token: SyntaxToken,
|
||||||
descended_token: &SyntaxToken,
|
descended_token: &SyntaxToken,
|
||||||
) -> ControlFlow<()> {
|
) -> ControlFlow<()> {
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ use crate::{
|
||||||
pub(super) fn highlight_format_string(
|
pub(super) fn highlight_format_string(
|
||||||
stack: &mut Highlights,
|
stack: &mut Highlights,
|
||||||
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
|
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
string: &ast::String,
|
string: &ast::String,
|
||||||
expanded_string: &ast::String,
|
expanded_string: &ast::String,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ pub(super) fn token(
|
||||||
|
|
||||||
pub(super) fn name_like(
|
pub(super) fn name_like(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
syntactic_name_ref_highlighting: bool,
|
syntactic_name_ref_highlighting: bool,
|
||||||
|
|
@ -272,7 +272,7 @@ fn keyword(token: SyntaxToken, kind: SyntaxKind) -> Highlight {
|
||||||
|
|
||||||
fn highlight_name_ref(
|
fn highlight_name_ref(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
||||||
binding_hash: &mut Option<u64>,
|
binding_hash: &mut Option<u64>,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
|
|
@ -401,9 +401,10 @@ fn highlight_name_ref(
|
||||||
NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
|
NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
|
||||||
let mut h = HlTag::Symbol(SymbolKind::Module).into();
|
let mut h = HlTag::Symbol(SymbolKind::Module).into();
|
||||||
|
|
||||||
if resolved_krate != krate {
|
if krate.as_ref().is_some_and(|krate| resolved_krate != *krate) {
|
||||||
h |= HlMod::Library
|
h |= HlMod::Library;
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_public = decl.visibility(db) == hir::Visibility::Public;
|
let is_public = decl.visibility(db) == hir::Visibility::Public;
|
||||||
if is_public {
|
if is_public {
|
||||||
h |= HlMod::Public
|
h |= HlMod::Public
|
||||||
|
|
@ -431,7 +432,7 @@ fn highlight_name(
|
||||||
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
|
||||||
binding_hash: &mut Option<u64>,
|
binding_hash: &mut Option<u64>,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
name: ast::Name,
|
name: ast::Name,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
) -> Highlight {
|
) -> Highlight {
|
||||||
|
|
@ -476,7 +477,7 @@ fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
|
||||||
|
|
||||||
pub(super) fn highlight_def(
|
pub(super) fn highlight_def(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
def: Definition,
|
def: Definition,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
is_ref: bool,
|
is_ref: bool,
|
||||||
|
|
@ -660,7 +661,7 @@ pub(super) fn highlight_def(
|
||||||
};
|
};
|
||||||
|
|
||||||
let def_crate = def.krate(db);
|
let def_crate = def.krate(db);
|
||||||
let is_from_other_crate = def_crate != Some(krate);
|
let is_from_other_crate = def_crate != krate;
|
||||||
let is_from_builtin_crate = def_crate.is_some_and(|def_crate| def_crate.is_builtin(db));
|
let is_from_builtin_crate = def_crate.is_some_and(|def_crate| def_crate.is_builtin(db));
|
||||||
let is_builtin = matches!(
|
let is_builtin = matches!(
|
||||||
def,
|
def,
|
||||||
|
|
@ -681,7 +682,7 @@ pub(super) fn highlight_def(
|
||||||
|
|
||||||
fn highlight_method_call_by_name_ref(
|
fn highlight_method_call_by_name_ref(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
name_ref: &ast::NameRef,
|
name_ref: &ast::NameRef,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
) -> Option<Highlight> {
|
) -> Option<Highlight> {
|
||||||
|
|
@ -691,7 +692,7 @@ fn highlight_method_call_by_name_ref(
|
||||||
|
|
||||||
fn highlight_method_call(
|
fn highlight_method_call(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
method_call: &ast::MethodCallExpr,
|
method_call: &ast::MethodCallExpr,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
) -> Option<Highlight> {
|
) -> Option<Highlight> {
|
||||||
|
|
@ -718,7 +719,7 @@ fn highlight_method_call(
|
||||||
}
|
}
|
||||||
|
|
||||||
let def_crate = func.module(sema.db).krate();
|
let def_crate = func.module(sema.db).krate();
|
||||||
let is_from_other_crate = def_crate != krate;
|
let is_from_other_crate = krate.as_ref().map_or(false, |krate| def_crate != *krate);
|
||||||
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
|
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
|
||||||
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
|
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
|
||||||
|
|
||||||
|
|
@ -791,7 +792,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
|
||||||
fn highlight_name_ref_by_syntax(
|
fn highlight_name_ref_by_syntax(
|
||||||
name: ast::NameRef,
|
name: ast::NameRef,
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: hir::Crate,
|
krate: Option<hir::Crate>,
|
||||||
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
|
||||||
) -> Highlight {
|
) -> Highlight {
|
||||||
let default = HlTag::UnresolvedReference;
|
let default = HlTag::UnresolvedReference;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
|
||||||
|
<style>
|
||||||
|
body { margin: 0; }
|
||||||
|
pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padding: 0.4em; }
|
||||||
|
|
||||||
|
.lifetime { color: #DFAF8F; font-style: italic; }
|
||||||
|
.label { color: #DFAF8F; font-style: italic; }
|
||||||
|
.comment { color: #7F9F7F; }
|
||||||
|
.documentation { color: #629755; }
|
||||||
|
.intra_doc_link { font-style: italic; }
|
||||||
|
.injected { opacity: 0.65 ; }
|
||||||
|
.struct, .enum { color: #7CB8BB; }
|
||||||
|
.enum_variant { color: #BDE0F3; }
|
||||||
|
.string_literal { color: #CC9393; }
|
||||||
|
.field { color: #94BFF3; }
|
||||||
|
.function { color: #93E0E3; }
|
||||||
|
.parameter { color: #94BFF3; }
|
||||||
|
.text { color: #DCDCCC; }
|
||||||
|
.type { color: #7CB8BB; }
|
||||||
|
.builtin_type { color: #8CD0D3; }
|
||||||
|
.type_param { color: #DFAF8F; }
|
||||||
|
.attribute { color: #94BFF3; }
|
||||||
|
.numeric_literal { color: #BFEBBF; }
|
||||||
|
.bool_literal { color: #BFE6EB; }
|
||||||
|
.macro { color: #94BFF3; }
|
||||||
|
.proc_macro { color: #94BFF3; text-decoration: underline; }
|
||||||
|
.derive { color: #94BFF3; font-style: italic; }
|
||||||
|
.module { color: #AFD8AF; }
|
||||||
|
.value_param { color: #DCDCCC; }
|
||||||
|
.variable { color: #DCDCCC; }
|
||||||
|
.format_specifier { color: #CC696B; }
|
||||||
|
.mutable { text-decoration: underline; }
|
||||||
|
.escape_sequence { color: #94BFF3; }
|
||||||
|
.keyword { color: #F0DFAF; font-weight: bold; }
|
||||||
|
.control { font-style: italic; }
|
||||||
|
.reference { font-style: italic; font-weight: bold; }
|
||||||
|
.const { font-weight: bolder; }
|
||||||
|
.unsafe { color: #BC8383; }
|
||||||
|
|
||||||
|
.invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; }
|
||||||
|
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
|
||||||
|
</style>
|
||||||
|
<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>
|
||||||
|
<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>
|
||||||
|
<span class="brace">}</span>
|
||||||
|
</code></pre>
|
||||||
|
|
@ -1420,3 +1420,18 @@ fn template() {}
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn issue_19357() {
|
||||||
|
check_highlighting(
|
||||||
|
r#"
|
||||||
|
//- /foo.rs
|
||||||
|
fn main() {
|
||||||
|
let x = &raw mut 5;
|
||||||
|
}
|
||||||
|
//- /main.rs
|
||||||
|
"#,
|
||||||
|
expect_file!["./test_data/highlight_issue_19357.html"],
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue