Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing syntax highlighting for &raw const / &raw mut in all files. #19400

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions crates/ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ pub(crate) fn highlight(
};

let mut hl = highlights::Highlights::new(root.text_range());
let krate = match sema.scope(&root) {
Some(it) => it.krate(),
None => return hl.to_vec(),
};
let krate = sema.scope(&root).map(|it| it.krate());
traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight);
hl.to_vec()
}
Expand All @@ -235,7 +232,7 @@ fn traverse(
sema: &Semantics<'_, RootDatabase>,
config: HighlightConfig,
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
krate: hir::Crate,
krate: Option<hir::Crate>,
range_to_highlight: TextRange,
) {
let is_unlinked = sema.file_to_module_def(file_id).is_none();
Expand Down Expand Up @@ -498,7 +495,7 @@ fn string_injections(
sema: &Semantics<'_, RootDatabase>,
config: HighlightConfig,
file_id: EditionedFileId,
krate: hir::Crate,
krate: Option<hir::Crate>,
token: SyntaxToken,
descended_token: &SyntaxToken,
) -> ControlFlow<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/syntax_highlighting/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
pub(super) fn highlight_format_string(
stack: &mut Highlights,
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
krate: hir::Crate,
krate: Option<hir::Crate>,
string: &ast::String,
expanded_string: &ast::String,
edition: Edition,
Expand Down
23 changes: 12 additions & 11 deletions crates/ide/src/syntax_highlighting/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(super) fn token(

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

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

if resolved_krate != krate {
h |= HlMod::Library
if krate.as_ref().is_some_and(|krate| resolved_krate != *krate) {
h |= HlMod::Library;
}

let is_public = decl.visibility(db) == hir::Visibility::Public;
if is_public {
h |= HlMod::Public
Expand Down Expand Up @@ -431,7 +432,7 @@ fn highlight_name(
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
binding_hash: &mut Option<u64>,
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
krate: hir::Crate,
krate: Option<hir::Crate>,
name: ast::Name,
edition: Edition,
) -> Highlight {
Expand Down Expand Up @@ -476,7 +477,7 @@ fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {

pub(super) fn highlight_def(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
krate: Option<hir::Crate>,
def: Definition,
edition: Edition,
is_ref: bool,
Expand Down Expand Up @@ -660,7 +661,7 @@ pub(super) fn highlight_def(
};

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_builtin = matches!(
def,
Expand All @@ -681,7 +682,7 @@ pub(super) fn highlight_def(

fn highlight_method_call_by_name_ref(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
krate: Option<hir::Crate>,
name_ref: &ast::NameRef,
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
) -> Option<Highlight> {
Expand All @@ -691,7 +692,7 @@ fn highlight_method_call_by_name_ref(

fn highlight_method_call(
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
krate: Option<hir::Crate>,
method_call: &ast::MethodCallExpr,
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
) -> Option<Highlight> {
Expand All @@ -718,7 +719,7 @@ fn highlight_method_call(
}

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_public = func.visibility(sema.db) == hir::Visibility::Public;

Expand Down Expand Up @@ -791,7 +792,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
fn highlight_name_ref_by_syntax(
name: ast::NameRef,
sema: &Semantics<'_, RootDatabase>,
krate: hir::Crate,
krate: Option<hir::Crate>,
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
) -> Highlight {
let default = HlTag::UnresolvedReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
15 changes: 15 additions & 0 deletions crates/ide/src/syntax_highlighting/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,3 +1420,18 @@ fn template() {}
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