From 6164dbe8175c5be0f77372561a7052bbe2604ce6 Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Mon, 22 Aug 2022 18:58:44 +0400 Subject: [PATCH] refactor: impl getNameIdentifier() --- commander | 2 +- .../marker/source/SourceFileMarker.kt | 61 +++++-------------- .../marker/source/mark/api/ClassSourceMark.kt | 6 ++ .../source/mark/api/MethodSourceMark.kt | 6 ++ 4 files changed, 27 insertions(+), 48 deletions(-) diff --git a/commander b/commander index 5cb6f7c5c..9cd67992b 160000 --- a/commander +++ b/commander @@ -1 +1 @@ -Subproject commit 5cb6f7c5c0ffa7dcda671c31b653990672341b7a +Subproject commit 9cd67992b39e05330f72794fd9420a83797e6ca5 diff --git a/marker/src/main/kotlin/spp/jetbrains/marker/source/SourceFileMarker.kt b/marker/src/main/kotlin/spp/jetbrains/marker/source/SourceFileMarker.kt index f81eaf0be..4307fe594 100755 --- a/marker/src/main/kotlin/spp/jetbrains/marker/source/SourceFileMarker.kt +++ b/marker/src/main/kotlin/spp/jetbrains/marker/source/SourceFileMarker.kt @@ -143,26 +143,11 @@ open class SourceFileMarker(val psiFile: PsiFile) : SourceMarkProvider { sourceMark.triggerEvent(SourceMarkEvent(sourceMark, SourceMarkEventCode.MARK_BEFORE_ADDED)) if (sourceMarks.add(sourceMark)) { when (sourceMark) { - is ClassGutterMark -> sourceMark.getPsiElement().nameIdentifier!!.putUserData( - SourceKey.GutterMark, - sourceMark - ) - is ClassGuideMark -> sourceMark.getPsiElement().nameIdentifier!!.putUserData( - SourceKey.GuideMark, - sourceMark - ) - is MethodGutterMark -> sourceMark.getPsiElement().nameIdentifier!!.putUserData( - SourceKey.GutterMark, - sourceMark - ) - is MethodInlayMark -> sourceMark.getPsiElement().nameIdentifier!!.putUserData( - SourceKey.InlayMark, - sourceMark - ) - is MethodGuideMark -> sourceMark.getPsiElement().nameIdentifier!!.putUserData( - SourceKey.GuideMark, - sourceMark - ) + is ClassGutterMark -> sourceMark.getNameIdentifier().putUserData(SourceKey.GutterMark, sourceMark) + is ClassGuideMark -> sourceMark.getNameIdentifier().putUserData(SourceKey.GuideMark, sourceMark) + is MethodGutterMark -> sourceMark.getNameIdentifier().putUserData(SourceKey.GutterMark, sourceMark) + is MethodInlayMark -> sourceMark.getNameIdentifier().putUserData(SourceKey.InlayMark, sourceMark) + is MethodGuideMark -> sourceMark.getNameIdentifier().putUserData(SourceKey.GuideMark, sourceMark) is ExpressionGutterMark -> sourceMark.getPsiElement().putUserData(SourceKey.GutterMark, sourceMark) is ExpressionInlayMark -> sourceMark.getPsiElement().putUserData(SourceKey.InlayMark, sourceMark) is ExpressionGuideMark -> sourceMark.getPsiElement().putUserData(SourceKey.GuideMark, sourceMark) @@ -227,43 +212,25 @@ open class SourceFileMarker(val psiFile: PsiFile) : SourceMarkProvider { override fun createExpressionSourceMark(psiExpression: PsiElement, type: SourceMark.Type): ExpressionSourceMark { log.trace("Creating source mark. Expression: $psiExpression - Type: $type") return when (type) { - SourceMark.Type.GUTTER -> { - ExpressionGutterMark(this, psiExpression) - } - SourceMark.Type.INLAY -> { - ExpressionInlayMark(this, psiExpression) - } - SourceMark.Type.GUIDE -> { - ExpressionGuideMark(this, psiExpression) - } + SourceMark.Type.GUTTER -> ExpressionGutterMark(this, psiExpression) + SourceMark.Type.INLAY -> ExpressionInlayMark(this, psiExpression) + SourceMark.Type.GUIDE -> ExpressionGuideMark(this, psiExpression) } } override fun createMethodSourceMark(psiMethod: PsiNameIdentifierOwner, type: SourceMark.Type): MethodSourceMark { return when (type) { - SourceMark.Type.GUTTER -> { - MethodGutterMark(this, psiMethod) - } - SourceMark.Type.INLAY -> { - MethodInlayMark(this, psiMethod) - } - SourceMark.Type.GUIDE -> { - MethodGuideMark(this, psiMethod) - } + SourceMark.Type.GUTTER -> MethodGutterMark(this, psiMethod) + SourceMark.Type.INLAY -> MethodInlayMark(this, psiMethod) + SourceMark.Type.GUIDE -> MethodGuideMark(this, psiMethod) } } override fun createClassSourceMark(psiClass: PsiNameIdentifierOwner, type: SourceMark.Type): ClassSourceMark { return when (type) { - SourceMark.Type.GUTTER -> { - ClassGutterMark(this, psiClass) - } - SourceMark.Type.INLAY -> { - TODO("Not yet implemented") - } - SourceMark.Type.GUIDE -> { - ClassGuideMark(this, psiClass) - } + SourceMark.Type.GUTTER -> ClassGutterMark(this, psiClass) + SourceMark.Type.INLAY -> TODO("Not yet implemented") + SourceMark.Type.GUIDE -> ClassGuideMark(this, psiClass) } } diff --git a/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/ClassSourceMark.kt b/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/ClassSourceMark.kt index 46bd651db..c7a9e30b9 100644 --- a/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/ClassSourceMark.kt +++ b/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/ClassSourceMark.kt @@ -19,6 +19,7 @@ package spp.jetbrains.marker.source.mark.api import com.intellij.openapi.Disposable import com.intellij.openapi.editor.Editor import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.psi.PsiElement import com.intellij.psi.PsiInvalidElementAccessException import com.intellij.psi.PsiNameIdentifierOwner import spp.jetbrains.marker.impl.ArtifactNamingService @@ -133,6 +134,11 @@ abstract class ClassSourceMark( return psiClass } + fun getNameIdentifier(): PsiElement { + return psiClass.nameIdentifier + ?: throw PsiInvalidElementAccessException(psiClass, "No name identifier. Artifact: $artifactQualifiedName") + } + fun updatePsiClass(psiClass: PsiNameIdentifierOwner): Boolean { this.psiClass = psiClass val newArtifactQualifiedName = ArtifactNamingService.getFullyQualifiedName(psiClass) diff --git a/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/MethodSourceMark.kt b/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/MethodSourceMark.kt index c75012c53..07b84c683 100644 --- a/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/MethodSourceMark.kt +++ b/marker/src/main/kotlin/spp/jetbrains/marker/source/mark/api/MethodSourceMark.kt @@ -19,6 +19,7 @@ package spp.jetbrains.marker.source.mark.api import com.intellij.openapi.Disposable import com.intellij.openapi.editor.Editor import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.psi.PsiElement import com.intellij.psi.PsiInvalidElementAccessException import com.intellij.psi.PsiNameIdentifierOwner import spp.jetbrains.marker.impl.ArtifactNamingService @@ -138,6 +139,11 @@ abstract class MethodSourceMark( return psiMethod } + fun getNameIdentifier(): PsiElement { + return psiMethod.nameIdentifier + ?: throw PsiInvalidElementAccessException(psiMethod, "No name identifier. Artifact: $artifactQualifiedName") + } + fun updatePsiMethod(psiMethod: PsiNameIdentifierOwner): Boolean { this.psiMethod = psiMethod val newArtifactQualifiedName = ArtifactNamingService.getFullyQualifiedName(psiMethod)