Skip to content

Commit 9ef56c9

Browse files
committed
clean up service setup
1 parent aa397f2 commit 9ef56c9

12 files changed

Lines changed: 98 additions & 48 deletions

File tree

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package io.openapiprocessor.intellij
88
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo
99
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider
1010
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder
11-
import com.intellij.openapi.module.Module
11+
import com.intellij.openapi.components.service
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.util.IconLoader
1414
import com.intellij.psi.PsiAnnotation
@@ -44,14 +44,15 @@ class MappingAnnotationLineMarker: RelatedItemLineMarkerProvider() {
4444
return null
4545
}
4646

47-
val apiPath = match.path(element)
48-
val modules = findModules(element)
47+
val moduleService = service<ModuleService>()
48+
val modules = moduleService.findModules(element)
4949

5050
var searchScope = GlobalSearchScope.EMPTY_SCOPE
5151
for (module in modules) {
5252
searchScope = searchScope.uniteWith(GlobalSearchScope.moduleScope(module))
5353
}
5454

55+
val apiPath = match.path(element)
5556
val targets = findPsiElementsOfPath(apiPath!!, searchScope, element.project)
5657
if (targets.isEmpty()) {
5758
log.warn("found no targets!")
@@ -69,22 +70,6 @@ class MappingAnnotationLineMarker: RelatedItemLineMarkerProvider() {
6970
return builder.createLineMarkerInfo(id)
7071
}
7172

72-
private fun findModules(element: PsiElement): List<Module> {
73-
val finder = ModuleFinder(element.project)
74-
val modules = finder.findModules(element.containingFile.virtualFile.presentableUrl)
75-
76-
if (modules.isNotEmpty()) {
77-
modules.forEach {
78-
log.debug("related modules of file '{}'", element.containingFile.name)
79-
log.debug("found module '{}'", it.name)
80-
}
81-
} else {
82-
log.debug("could not find module of file '{}'", element.containingFile.name)
83-
}
84-
85-
return modules
86-
}
87-
8873
private fun findPsiElementsOfPath(path: String, searchScope: GlobalSearchScope, project: Project): List<PsiElement> {
8974
val targets = mutableListOf<PsiElement>()
9075

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-intellij
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.intellij
7+
8+
import com.intellij.openapi.module.Module
9+
import com.intellij.psi.PsiElement
10+
11+
interface ModuleService {
12+
fun findModules(element: PsiElement): List<Module>
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-intellij
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.intellij
7+
8+
import com.intellij.openapi.module.Module
9+
import com.intellij.psi.PsiElement
10+
import org.slf4j.Logger
11+
import org.slf4j.LoggerFactory
12+
13+
internal class ModuleServiceImpl: ModuleService {
14+
private val log: Logger = LoggerFactory.getLogger(javaClass.name)
15+
16+
override fun findModules(element: PsiElement): List<Module> {
17+
val finder = ModuleFinder(element.project)
18+
val modules = finder.findModules(element.containingFile.virtualFile.presentableUrl)
19+
20+
if (modules.isNotEmpty()) {
21+
modules.forEach {
22+
log.debug("related modules of file '{}'", element.containingFile.name)
23+
log.debug("found module '{}'", it.name)
24+
}
25+
} else {
26+
log.debug("could not find module of file '{}'", element.containingFile.name)
27+
}
28+
29+
return modules
30+
}
31+
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
package io.openapiprocessor.intellij
77

8-
import com.intellij.openapi.components.Service
8+
import com.intellij.openapi.module.Module
9+
import com.intellij.psi.PsiDirectory
910

10-
@Service
11-
class TargetPackageService(finder: TargetPackageFinder = TargetPackageFinderImpl()) :
12-
TargetPackageFinder by finder
11+
interface TargetPackageService {
12+
fun findPackageDirs(pkgName: String, mappingModule: Module): List<PsiDirectory>
13+
}

src/main/kotlin/io/openapiprocessor/intellij/TargetPackageFinderImpl.kt renamed to src/main/kotlin/io/openapiprocessor/intellij/TargetPackageServiceImpl.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.openapiprocessor.intellij
77

8-
import com.intellij.openapi.components.Service
98
import com.intellij.openapi.module.Module
109
import com.intellij.psi.JavaPsiFacade
1110
import com.intellij.psi.PsiDirectory
@@ -15,8 +14,7 @@ import org.slf4j.LoggerFactory
1514
/**
1615
* find the package directories of the `package-name` configuration in the `mapping.yaml`.
1716
*/
18-
@Service
19-
class TargetPackageFinderImpl : TargetPackageFinder {
17+
class TargetPackageServiceImpl : TargetPackageService {
2018
private val log: Logger = LoggerFactory.getLogger(this.javaClass.name)
2119

2220
override fun findPackageDirs(pkgName: String, mappingModule: Module): List<PsiDirectory> {

src/main/resources/META-INF/plugin.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
implementationClass="io.openapiprocessor.intellij.MappingAnnotationLineMarker"/>
3535

3636
<iconMapper mappingFile="IconMappings.json"/>
37+
38+
<applicationService
39+
serviceInterface="io.openapiprocessor.intellij.TargetPackageService"
40+
serviceImplementation="io.openapiprocessor.intellij.TargetPackageServiceImpl"/>
41+
42+
<applicationService
43+
serviceInterface="io.openapiprocessor.intellij.ModuleService"
44+
serviceImplementation="io.openapiprocessor.intellij.ModuleServiceImpl"/>
45+
3746
</extensions>
3847

3948
<extensions defaultExtensionNs="JavaScript.JsonSchema">

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import com.intellij.openapi.application.EDT
99
import io.kotest.core.spec.style.StringSpec
1010
import io.kotest.matchers.shouldBe
1111
import io.openapiprocessor.intellij.listener.LightCodeInsightListener
12+
import io.openapiprocessor.intellij.support.ModuleServiceStub
1213
import io.openapiprocessor.intellij.support.psiTargets
1314
import kotlinx.coroutines.Dispatchers
1415
import kotlinx.coroutines.withContext
1516

1617
class MappingAnnotationLineMarkerSpec : StringSpec({
1718
val fixture = register(LightCodeInsightListener("src/test/testdata/interface-to-openapi/paths"))
1819

20+
beforeTest {
21+
fixture.replaceService(ModuleService::class.java, ModuleServiceStub())
22+
}
23+
1924
"adds navigation gutter icon to mapping annotation" {
2025
withContext(Dispatchers.EDT) {
2126
fixture.copyDirectoryToProject("", "")

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import io.kotest.core.spec.style.StringSpec
1212
import io.kotest.matchers.shouldBe
1313
import io.kotest.matchers.string.shouldEndWith
1414
import io.openapiprocessor.intellij.listener.LightCodeInsightListener
15-
import io.openapiprocessor.intellij.support.TargetPackageFinderStub
15+
import io.openapiprocessor.intellij.support.TargetPackageServiceStub
1616
import io.openapiprocessor.intellij.support.getTargets
1717
import kotlinx.coroutines.Dispatchers
1818
import kotlinx.coroutines.withContext
@@ -24,10 +24,8 @@ class TypeMappingPackageLineMarkerSpec: StringSpec({
2424
beforeTest {
2525
val pkgDir = test.createDir(expectedPkgDir)
2626
val psiDir = readAction { test.findPsiDir(pkgDir) }
27-
28-
test.replaceService(
29-
TargetPackageService::class.java,
30-
TargetPackageService(TargetPackageFinderStub(psiDir)))
27+
val stub = TargetPackageServiceStub(psiDir)
28+
test.replaceService(TargetPackageService::class.java, stub)
3129
}
3230

3331
"adds navigation gutter at package-name" {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-intellij
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.intellij.support
7+
8+
import com.intellij.openapi.components.Service
9+
import com.intellij.openapi.module.Module
10+
import com.intellij.openapi.roots.ProjectRootManager
11+
import com.intellij.psi.PsiElement
12+
import io.openapiprocessor.intellij.ModuleService
13+
14+
@Service
15+
class ModuleServiceStub: ModuleService {
16+
17+
override fun findModules(element: PsiElement): List<Module> {
18+
val module = ProjectRootManager.getInstance(element.project)
19+
.fileIndex
20+
.getModuleForFile(element.containingFile.virtualFile)
21+
22+
return listOfNotNull(module)
23+
}
24+
}

0 commit comments

Comments
 (0)