Skip to content

Commit ce68ffe

Browse files
committed
fix: kt property needs to be member to be field
1 parent 855f106 commit ce68ffe

File tree

8 files changed

+121
-1
lines changed

8 files changed

+121
-1
lines changed

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/JVMArtifactScopeService.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class JVMArtifactScopeService : IArtifactScopeService {
5959

6060
override fun getFields(element: PsiElement): List<PsiElement> {
6161
return when {
62-
ArtifactTypeService.isKotlin(element) -> element.descendantsOfType<KtProperty>().toList()
62+
ArtifactTypeService.isKotlin(element) -> {
63+
element.descendantsOfType<KtProperty>().filter { it.isMember }.toList()
64+
}
65+
6366
else -> element.descendantsOfType<PsiField>().toList()
6467
}
6568
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Source++, the continuous feedback platform for developers.
3+
* Copyright (C) 2022-2024 CodeBrig, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package spp.jetbrains.marker.jvm.service
18+
19+
import com.google.common.base.CaseFormat
20+
import com.intellij.openapi.application.ApplicationManager
21+
import com.intellij.openapi.util.Computable
22+
import com.intellij.psi.PsiClass
23+
import com.intellij.psi.PsiElement
24+
import com.intellij.psi.PsiJavaFile
25+
import com.intellij.psi.PsiNamedElement
26+
import com.intellij.testFramework.TestDataPath
27+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
28+
import kotlinx.coroutines.runBlocking
29+
import org.jetbrains.kotlin.psi.KtClass
30+
import org.jetbrains.kotlin.psi.KtFile
31+
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
32+
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
33+
import spp.jetbrains.artifact.service.getFields
34+
import spp.jetbrains.marker.SourceMarker
35+
import spp.jetbrains.marker.jvm.JVMLanguageProvider
36+
import spp.jetbrains.marker.source.SourceFileMarker
37+
38+
@TestDataPath("\$CONTENT_ROOT/testData/field/")
39+
class JVMFieldTest : BasePlatformTestCase() {
40+
41+
override fun setUp() {
42+
super.setUp()
43+
ApplicationManager.getApplication().runReadAction(Computable {
44+
runBlocking {
45+
SourceMarker.getInstance(myFixture.project).clearAvailableSourceFileMarkers()
46+
}
47+
})
48+
49+
JVMLanguageProvider().setup(project)
50+
SourceFileMarker.SUPPORTED_FILE_TYPES.add(PsiJavaFile::class.java)
51+
SourceFileMarker.SUPPORTED_FILE_TYPES.add(KtFile::class.java)
52+
SourceFileMarker.SUPPORTED_FILE_TYPES.add(GroovyFile::class.java)
53+
}
54+
55+
override fun getTestDataPath(): String {
56+
return "src/test/testData/field/"
57+
}
58+
59+
fun testSingleField() {
60+
doTest<PsiClass>("java")
61+
doTest<KtClass>("kt")
62+
doTest<PsiClass>("groovy")
63+
}
64+
65+
fun testFieldAndFunction() {
66+
doTest<PsiClass>("java")
67+
doTest<KtClass>("kt")
68+
doTest<PsiClass>("groovy")
69+
}
70+
71+
private inline fun <reified T : PsiElement> doTest(extension: String) {
72+
val className = getTestName(false)
73+
val testDir = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN).convert(className)
74+
val psiFile = myFixture.configureByFile("$testDir/$className.$extension")
75+
val clazz = psiFile.findDescendantOfType<T> { true }
76+
assertNotNull(clazz)
77+
78+
val fields = clazz!!.getFields()
79+
assertEquals(1, fields.size)
80+
assertEquals("foo", (fields[0] as PsiNamedElement).name)
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class FieldAndFunction {
2+
def foo = 42
3+
4+
def add(int x, int y) {
5+
def result = x + y
6+
println "Adding $x and $y gives $result"
7+
return result
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class FieldAndFunction {
2+
int foo = 42;
3+
4+
public int add(int x, int y) {
5+
int result = x + y;
6+
System.out.println("Adding " + x + " and " + y + " gives " + result);
7+
return result;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class FieldAndFunction {
2+
val foo = 42
3+
fun add(x: Int, y: Int): Int {
4+
val result = x + y
5+
println("Adding $x and $y gives $result")
6+
return result
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SingleField {
2+
public def foo = 42
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public class SingleField {
2+
public int foo = 42;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SingleField {
2+
val foo = 42
3+
}

0 commit comments

Comments
 (0)