-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsConstant.kt
76 lines (59 loc) · 2.59 KB
/
RsConstant.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.lang.core.psi.ext
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import com.intellij.psi.search.SearchScope
import com.intellij.psi.stubs.IStubElementType
import org.rust.ide.icons.RsIcons
import org.rust.lang.core.macros.RsExpandedElement
import org.rust.lang.core.psi.RsConstant
import org.rust.lang.core.psi.RsElementTypes.DEFAULT
import org.rust.lang.core.psi.RsPsiImplUtil
import org.rust.lang.core.stubs.RsConstantStub
import org.rust.lang.core.types.ty.Mutability
import javax.swing.Icon
enum class RsConstantKind {
STATIC,
MUT_STATIC,
CONST
}
val RsConstant.isMut: Boolean get() = greenStub?.isMut ?: (mut != null)
val RsConstant.isConst: Boolean get() = greenStub?.isConst ?: (const != null)
val RsConstant.kind: RsConstantKind get() = when {
isMut -> RsConstantKind.MUT_STATIC
isConst -> RsConstantKind.CONST
else -> RsConstantKind.STATIC
}
val RsConstant.default: PsiElement?
get() = node.findChildByType(DEFAULT)?.psi
val RsConstant.mutability: Mutability get() = Mutability.valueOf(isMut)
val RsConstant.nameLikeElement: PsiElement
get() = nameIdentifier ?: underscore ?: error("Constant without name: `$text`")
abstract class RsConstantImplMixin : RsStubbedNamedElementImpl<RsConstantStub>, RsConstant {
constructor(node: ASTNode) : super(node)
constructor(stub: RsConstantStub, nodeType: IStubElementType<*, *>) : super(stub, nodeType)
override fun getIcon(flags: Int): Icon {
val baseIcon = when (kind) {
RsConstantKind.CONST -> {
val owner = owner
val icon = when (owner) {
is RsAbstractableOwner.Trait -> if (isAbstract) RsIcons.ABSTRACT_ASSOC_CONSTANT else RsIcons.ASSOC_CONSTANT
is RsAbstractableOwner.Impl -> RsIcons.ASSOC_CONSTANT
else -> RsIcons.CONSTANT
}
if (owner.isImplOrTrait && !owner.isInherentImpl) return icon
icon
}
RsConstantKind.MUT_STATIC -> RsIcons.MUT_STATIC
RsConstantKind.STATIC -> RsIcons.STATIC
}
return iconWithVisibility(flags, baseIcon)
}
override val isAbstract: Boolean get() = expr == null
override val crateRelativePath: String? get() = RsPsiImplUtil.crateRelativePath(this)
override fun getContext(): PsiElement? = RsExpandedElement.getContextImpl(this)
override fun getUseScope(): SearchScope = RsPsiImplUtil.getDeclarationUseScope(this) ?: super.getUseScope()
}