Skip to content

Commit 771c0d8

Browse files
author
Anatol Liu
committed
Add static semantic token modifier for associated functions with no &self
refactor logic into code_model.rs
1 parent 3baa526 commit 771c0d8

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

crates/hir/src/code_model.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_hash::FxHashSet;
4141
use stdx::impl_from;
4242
use syntax::{
4343
ast::{self, AttrsOwner, NameOwner},
44-
AstNode, SmolStr,
44+
AstNode, SmolStr, SyntaxKind,
4545
};
4646
use tt::{Ident, Leaf, Literal, TokenTree};
4747

@@ -788,8 +788,25 @@ impl Function {
788788
db.function_data(self.id).has_body
789789
}
790790

791-
pub fn source(self, db: &dyn HirDatabase) -> InFile<ast::Fn> {
792-
self.id.lookup(db.upcast()).source(db.upcast())
791+
/// whether this function is associated with some trait/impl
792+
pub fn is_associated(self, db: &dyn HirDatabase) -> bool {
793+
if let Some(_) = self.self_param(db) {
794+
return false;
795+
}
796+
797+
let fn_parent_kind = self
798+
.source(db)
799+
.value
800+
.syntax()
801+
.parent()
802+
.and_then(|s| s.parent())
803+
.and_then(|s| Some(s.kind()));
804+
805+
match fn_parent_kind {
806+
Some(SyntaxKind::IMPL) => true,
807+
Some(SyntaxKind::TRAIT) => true,
808+
_ => false,
809+
}
793810
}
794811
}
795812

crates/ide/src/syntax_highlighting.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,20 +746,8 @@ fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
746746
if func.is_unsafe(db) {
747747
h |= HighlightModifier::Unsafe;
748748
}
749-
if let None = func.self_param(db) {
750-
// if enclosing IMPL or TRAIT exists, this is a static method
751-
let fn_parent_kind = func
752-
.source(db)
753-
.value
754-
.syntax()
755-
.parent()
756-
.and_then(|s| s.parent())
757-
.and_then(|s| Some(s.kind()));
758-
if let Some(SyntaxKind::IMPL) = fn_parent_kind {
759-
h |= HighlightModifier::Static;
760-
} else if let Some(SyntaxKind::TRAIT) = fn_parent_kind {
761-
h |= HighlightModifier::Static;
762-
}
749+
if func.is_associated(db) {
750+
h |= HighlightModifier::Static;
763751
}
764752
return h;
765753
}

0 commit comments

Comments
 (0)