Skip to content

Commit 22879fc

Browse files
committed
change from krate to Option<krate>
1 parent 31e412c commit 22879fc

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode {
20022002
///
20032003
/// Note that if you are wondering "what does this specific existing name mean?",
20042004
/// you'd better use the `resolve_` family of methods.
2005-
#[derive(Debug)]
2005+
#[derive(Debug, Clone)]
20062006
pub struct SemanticsScope<'a> {
20072007
pub db: &'a dyn HirDatabase,
20082008
file_id: HirFileId,

crates/ide/src/syntax_highlighting.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::ops::ControlFlow;
1717
use either::Either;
1818
use hir::{
1919
DefWithBody, HirFileIdExt, InFile, InRealFile, MacroFileIdExt, MacroKind, Name, Semantics,
20+
SemanticsScope,
2021
};
2122
use ide_db::{
2223
FxHashMap, FxHashSet, Ranker, RootDatabase, SymbolKind, base_db::salsa::AsDynDatabase,
@@ -222,10 +223,7 @@ pub(crate) fn highlight(
222223
};
223224

224225
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-
};
226+
let krate = sema.scope(&root);
229227
traverse(&mut hl, &sema, config, InRealFile::new(file_id, &root), krate, range_to_highlight);
230228
hl.to_vec()
231229
}
@@ -235,7 +233,7 @@ fn traverse(
235233
sema: &Semantics<'_, RootDatabase>,
236234
config: HighlightConfig,
237235
InRealFile { file_id, value: root }: InRealFile<&SyntaxNode>,
238-
krate: hir::Crate,
236+
krate: Option<SemanticsScope<'_>>,
239237
range_to_highlight: TextRange,
240238
) {
241239
let is_unlinked = sema.file_to_module_def(file_id).is_none();
@@ -421,7 +419,7 @@ fn traverse(
421419
sema,
422420
config,
423421
file_id,
424-
krate,
422+
krate.clone(),
425423
original_token,
426424
descended_token,
427425
);
@@ -446,7 +444,7 @@ fn traverse(
446444
NodeOrToken::Node(name_like) => {
447445
let hl = highlight::name_like(
448446
sema,
449-
krate,
447+
krate.clone(),
450448
bindings_shadow_count,
451449
&is_unsafe_node,
452450
config.syntactic_name_ref_highlighting,
@@ -498,7 +496,7 @@ fn string_injections(
498496
sema: &Semantics<'_, RootDatabase>,
499497
config: HighlightConfig,
500498
file_id: EditionedFileId,
501-
krate: hir::Crate,
499+
krate: Option<SemanticsScope<'_>>,
502500
token: SyntaxToken,
503501
descended_token: &SyntaxToken,
504502
) -> ControlFlow<()> {

crates/ide/src/syntax_highlighting/format.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Syntax highlighting for format macro strings.
2+
use hir::SemanticsScope;
23
use ide_db::{
34
SymbolKind,
45
defs::Definition,
@@ -15,7 +16,7 @@ use crate::{
1516
pub(super) fn highlight_format_string(
1617
stack: &mut Highlights,
1718
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
18-
krate: hir::Crate,
19+
krate: Option<SemanticsScope<'_>>,
1920
string: &ast::String,
2021
expanded_string: &ast::String,
2122
edition: Edition,
@@ -41,7 +42,13 @@ pub(super) fn highlight_format_string(
4142
if let Some(res) = res {
4243
stack.add(HlRange {
4344
range,
44-
highlight: highlight_def(sema, krate, Definition::from(res), edition, true),
45+
highlight: highlight_def(
46+
sema,
47+
krate.clone(),
48+
Definition::from(res),
49+
edition,
50+
true,
51+
),
4552
binding_hash: None,
4653
})
4754
}

crates/ide/src/syntax_highlighting/highlight.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::ops::ControlFlow;
44

55
use either::Either;
6-
use hir::{AsAssocItem, HasVisibility, MacroFileIdExt, Semantics};
6+
use hir::{AsAssocItem, HasVisibility, MacroFileIdExt, Semantics, SemanticsScope};
77
use ide_db::{
88
FxHashMap, RootDatabase, SymbolKind,
99
defs::{Definition, IdentClass, NameClass, NameRefClass},
@@ -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<SemanticsScope<'_>>,
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<SemanticsScope<'_>>,
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,
@@ -281,7 +281,9 @@ fn highlight_name_ref(
281281
edition: Edition,
282282
) -> Highlight {
283283
let db = sema.db;
284-
if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref, is_unsafe_node) {
284+
if let Some(res) =
285+
highlight_method_call_by_name_ref(sema, krate.clone(), &name_ref, is_unsafe_node)
286+
{
285287
return res;
286288
}
287289

@@ -401,9 +403,11 @@ fn highlight_name_ref(
401403
NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
402404
let mut h = HlTag::Symbol(SymbolKind::Module).into();
403405

404-
if resolved_krate != krate {
405-
h |= HlMod::Library
406+
if krate.as_ref().map(|it| resolved_krate != it.krate()).is_some_and(|differs| differs)
407+
{
408+
h |= HlMod::Library;
406409
}
410+
407411
let is_public = decl.visibility(db) == hir::Visibility::Public;
408412
if is_public {
409413
h |= HlMod::Public
@@ -431,7 +435,7 @@ fn highlight_name(
431435
bindings_shadow_count: Option<&mut FxHashMap<hir::Name, u32>>,
432436
binding_hash: &mut Option<u64>,
433437
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
434-
krate: hir::Crate,
438+
krate: Option<SemanticsScope<'_>>,
435439
name: ast::Name,
436440
edition: Edition,
437441
) -> Highlight {
@@ -476,7 +480,7 @@ fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
476480

477481
pub(super) fn highlight_def(
478482
sema: &Semantics<'_, RootDatabase>,
479-
krate: hir::Crate,
483+
krate: Option<SemanticsScope<'_>>,
480484
def: Definition,
481485
edition: Edition,
482486
is_ref: bool,
@@ -660,7 +664,7 @@ pub(super) fn highlight_def(
660664
};
661665

662666
let def_crate = def.krate(db);
663-
let is_from_other_crate = def_crate != Some(krate);
667+
let is_from_other_crate = krate.map_or(false, |it| def_crate != Some(it.krate()));
664668
let is_from_builtin_crate = def_crate.is_some_and(|def_crate| def_crate.is_builtin(db));
665669
let is_builtin = matches!(
666670
def,
@@ -681,7 +685,7 @@ pub(super) fn highlight_def(
681685

682686
fn highlight_method_call_by_name_ref(
683687
sema: &Semantics<'_, RootDatabase>,
684-
krate: hir::Crate,
688+
krate: Option<SemanticsScope<'_>>,
685689
name_ref: &ast::NameRef,
686690
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
687691
) -> Option<Highlight> {
@@ -691,7 +695,7 @@ fn highlight_method_call_by_name_ref(
691695

692696
fn highlight_method_call(
693697
sema: &Semantics<'_, RootDatabase>,
694-
krate: hir::Crate,
698+
krate: Option<SemanticsScope<'_>>,
695699
method_call: &ast::MethodCallExpr,
696700
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
697701
) -> Option<Highlight> {
@@ -718,7 +722,7 @@ fn highlight_method_call(
718722
}
719723

720724
let def_crate = func.module(sema.db).krate();
721-
let is_from_other_crate = def_crate != krate;
725+
let is_from_other_crate = krate.as_ref().map_or(false, |it| def_crate != it.krate());
722726
let is_from_builtin_crate = def_crate.is_builtin(sema.db);
723727
let is_public = func.visibility(sema.db) == hir::Visibility::Public;
724728

@@ -791,7 +795,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
791795
fn highlight_name_ref_by_syntax(
792796
name: ast::NameRef,
793797
sema: &Semantics<'_, RootDatabase>,
794-
krate: hir::Crate,
798+
krate: Option<SemanticsScope<'_>>,
795799
is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
796800
) -> Highlight {
797801
let default = HlTag::UnresolvedReference;

0 commit comments

Comments
 (0)