Skip to content

Commit 6b828b0

Browse files
committed
add line marker for package-names.base/location
1 parent 1c2d2d3 commit 6b828b0

4 files changed

Lines changed: 62 additions & 18 deletions

File tree

src/main/kotlin/io/openapiprocessor/intellij/TypeMappingPackageLineMarker.kt

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import javax.swing.Icon as JIcon
3535
class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
3636
private val log: Logger = LoggerFactory.getLogger(this.javaClass.name)
3737

38-
class Renderer() : PsiTargetPresentationRenderer<PsiElement>() {
38+
class Renderer(private val userDataKey: Key<String>) : PsiTargetPresentationRenderer<PsiElement>() {
3939

4040
override fun getContainerText(element: PsiElement): String? {
4141
return getSymbolContainerText(element)
@@ -55,7 +55,7 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
5555
}
5656

5757
val moduleLocation = getModuleLocation(element)
58-
val packageLocation = element.getUserData(PACKAGE_LOCATION_USER_KEY)
58+
val packageLocation = element.getUserData(userDataKey)
5959
val locationText = if (packageLocation == null) {
6060
moduleLocation?.text
6161
} else {
@@ -74,13 +74,15 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
7474
}
7575
}
7676

77-
class GotoPackage(element: PsiDirectory): GotoRelatedItem(element, Goto.I18n.GROUP) {
77+
class GotoPackage(element: PsiDirectory, private val userDataKey: Key<String>):
78+
GotoRelatedItem(element, Goto.I18n.GROUP) {
79+
7880
override fun getCustomName(): String {
7981
return "${getSymbolPresentableText(this.element!!)}"
8082
}
8183

8284
override fun getCustomContainerName(): String {
83-
val loc = element!!.getUserData(PACKAGE_LOCATION_USER_KEY)!!
85+
val loc = element!!.getUserData(userDataKey)!!
8486
val pkg = getSymbolContainerText(element!!)!!
8587
return "$pkg - $loc"
8688
}
@@ -94,16 +96,31 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
9496
if (!isMapping)
9597
return
9698

97-
if (element.text != PACKAGE_KEY)
98-
return
99+
if (element.text == PACKAGE_NAME_KEY) {
100+
val info = createLineMarkerInfo(element, PACKAGE_NAME_USER_KEY)
101+
if (info != null) {
102+
result.add(info)
103+
}
104+
}
99105

100-
val info = createLineMarkerInfo(element)
101-
?: return
106+
if (element.text == PACKAGE_BASE_KEY) {
107+
// todo check package-names parent key
108+
val info = createLineMarkerInfo(element, PACKAGE_NAMES_BASE_USER_KEY)
109+
if (info != null) {
110+
result.add(info)
111+
}
112+
}
102113

103-
result.add(info)
114+
if (element.text == PACKAGE_LOCATION_KEY) {
115+
// todo check package-names parent key
116+
val info = createLineMarkerInfo(element, PACKAGE_NAMES_LOCATION_USER_KEY)
117+
if (info != null) {
118+
result.add(info)
119+
}
120+
}
104121
}
105122

106-
private fun createLineMarkerInfo(packageKey: PsiElement): RelatedItemLineMarkerInfo<*>? {
123+
private fun createLineMarkerInfo(packageKey: PsiElement, userDataKey: Key<String>): RelatedItemLineMarkerInfo<*>? {
107124
val packageKeyValue = packageKey.parent
108125
if (packageKeyValue !is YAMLKeyValue)
109126
return null
@@ -115,7 +132,7 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
115132
val pkgDirs = service<TargetPackageService>()
116133
.findPackageDirs(pkgName, module)
117134

118-
val targets = addLocations(pkgDirs)
135+
val targets = addLocations(pkgDirs, userDataKey)
119136
if (targets.isEmpty()) {
120137
log.warn("found no targets!")
121138
return null
@@ -125,16 +142,16 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
125142
.create<PsiDirectory>(
126143
Icon.`package`,
127144
{ listOf(it) },
128-
{ listOf(GotoPackage(it)) })
145+
{ listOf(GotoPackage(it, userDataKey)) })
129146
.setTooltipText(I18n.TOOLTIP_TEXT)
130147
.setPopupTitle(I18n.POPUP_TITLE)
131148
.setTargets(targets)
132-
.setTargetRenderer { Renderer() }
149+
.setTargetRenderer { Renderer(userDataKey) }
133150

134151
return builder.createLineMarkerInfo(packageKey)
135152
}
136153

137-
private fun addLocations(pkgDirs: List<PsiDirectory>): List<PsiDirectory> {
154+
private fun addLocations(pkgDirs: List<PsiDirectory>, userDataKey: Key<String>): List<PsiDirectory> {
138155
if (pkgDirs.size < 2) {
139156
return pkgDirs
140157
}
@@ -158,7 +175,7 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
158175
return pkgDirs
159176
.map {
160177
it.putUserData(
161-
PACKAGE_LOCATION_USER_KEY,
178+
userDataKey,
162179
it.virtualFile.path.drop(commonPrefix.length).dropLast(commonSuffix.length)
163180
)
164181
it
@@ -188,8 +205,13 @@ class TypeMappingPackageLineMarker : RelatedItemLineMarkerProvider() {
188205
}
189206

190207
companion object {
191-
const val PACKAGE_KEY = "package-name"
208+
const val PACKAGE_NAME_KEY = "package-name"
209+
const val PACKAGE_NAMES_KEY = "package-names"
210+
const val PACKAGE_BASE_KEY = "base"
211+
const val PACKAGE_LOCATION_KEY = "location"
192212
}
193213
}
194214

195-
private val PACKAGE_LOCATION_USER_KEY: Key<String> = Key.create("openapiprocessor.package-location")
215+
private val PACKAGE_NAME_USER_KEY: Key<String> = Key.create("openapiprocessor.package-name")
216+
private val PACKAGE_NAMES_BASE_USER_KEY: Key<String> = Key.create("openapiprocessor.package-names.base")
217+
private val PACKAGE_NAMES_LOCATION_USER_KEY: Key<String> = Key.create("openapiprocessor.package-names.location")

src/test/kotlin/io/openapiprocessor/intellij/TypeMappingPackageLineMarkerSpec.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TypeMappingPackageLineMarkerSpec: StringSpec({
3030

3131
"adds navigation gutter at package-name" {
3232
withContext(Dispatchers.EDT) {
33-
val gutters = test.findAllGutters("api/mapping.yaml")
33+
val gutters = test.findAllGutters("api/mapping-package-name.yaml")
3434
val gutter = gutters.first()
3535

3636
gutter.icon shouldBe AllIcons.Modules.GeneratedFolder
@@ -39,4 +39,21 @@ class TypeMappingPackageLineMarkerSpec: StringSpec({
3939
getTargets(gutter).first() shouldEndWith expectedPkgDir
4040
}
4141
}
42+
43+
"adds navigation gutter at package-names.base & package-names.location" {
44+
withContext(Dispatchers.EDT) {
45+
val gutters = test.findAllGutters("api/mapping-package-names.yaml")
46+
gutters.size shouldBe 2
47+
48+
val gutter0 = gutters[0]
49+
gutter0.icon shouldBe AllIcons.Modules.GeneratedFolder
50+
gutter0.tooltipText shouldBe TypeMappingPackageLineMarker.I18n.TOOLTIP_TEXT
51+
getTargets(gutter0).first() shouldEndWith expectedPkgDir
52+
53+
val gutter1 = gutters[1]
54+
gutter1.icon shouldBe AllIcons.Modules.GeneratedFolder
55+
gutter1.tooltipText shouldBe TypeMappingPackageLineMarker.I18n.TOOLTIP_TEXT
56+
getTargets(gutter1).first() shouldEndWith expectedPkgDir
57+
}
58+
}
4259
})

src/test/testdata/package-name/api/mapping.yaml renamed to src/test/testdata/package-name/api/mapping-package-name.yaml

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
openapi-processor-mapping: v13
2+
options:
3+
package-names:
4+
base: io.openapiprocessor
5+
location: io.openapiprocessor

0 commit comments

Comments
 (0)