Skip to content

Commit bcc7251

Browse files
committed
fix: Live log variables not working in Kotlin
ref: sourceplusplus/sourceplusplus#786
1 parent 84b592b commit bcc7251

File tree

5 files changed

+116
-14
lines changed

5 files changed

+116
-14
lines changed

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,11 @@ import com.intellij.psi.scope.processor.VariablesProcessor
3131
import com.intellij.psi.scope.util.PsiScopesUtil
3232
import com.intellij.psi.search.GlobalSearchScope
3333
import com.intellij.psi.search.searches.ReferencesSearch
34-
import com.intellij.psi.util.descendantsOfType
35-
import com.intellij.psi.util.findParentOfType
36-
import com.intellij.psi.util.parentOfType
37-
import com.intellij.psi.util.parentOfTypes
34+
import com.intellij.psi.util.*
3835
import com.siyeh.ig.psiutils.ControlFlowUtils
3936
import org.jetbrains.kotlin.backend.jvm.ir.psiElement
4037
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
41-
import org.jetbrains.kotlin.psi.KtCallExpression
42-
import org.jetbrains.kotlin.psi.KtIfExpression
43-
import org.jetbrains.kotlin.psi.KtNamedFunction
38+
import org.jetbrains.kotlin.psi.*
4439
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall
4540
import org.joor.Reflect
4641
import spp.jetbrains.artifact.service.ArtifactTypeService
@@ -138,19 +133,41 @@ class JVMArtifactScopeService : IArtifactScopeService {
138133
}
139134

140135
override fun getScopeVariables(file: PsiFile, lineNumber: Int): List<String> {
141-
//determine available vars
136+
//skip blank lines (if present) till valid minimum scope
142137
var checkLine = lineNumber
143-
val scopeVars = mutableListOf<String>()
144138
var minScope: PsiElement? = null
145139
while (minScope == null) {
146140
minScope = SourceMarkerUtils.getElementAtLine(file, --checkLine)
147141
}
148-
val variablesProcessor: VariablesProcessor = object : VariablesProcessor(false) {
149-
override fun check(`var`: PsiVariable, state: ResolveState): Boolean = true
142+
143+
return if (file is KtFile) {
144+
getScopeVariables(minScope)
145+
} else {
146+
val scopeVars = mutableListOf<String>()
147+
val variablesProcessor: VariablesProcessor = object : VariablesProcessor(false) {
148+
override fun check(`var`: PsiVariable, state: ResolveState): Boolean = true
149+
}
150+
PsiScopesUtil.treeWalkUp(variablesProcessor, minScope, null)
151+
for (i in 0 until variablesProcessor.size()) {
152+
scopeVars.add(variablesProcessor.getResult(i).name!!)
153+
}
154+
scopeVars
150155
}
151-
PsiScopesUtil.treeWalkUp(variablesProcessor, minScope, null)
152-
for (i in 0 until variablesProcessor.size()) {
153-
scopeVars.add(variablesProcessor.getResult(i).name!!)
156+
}
157+
158+
private fun getScopeVariables(minScope: PsiElement): List<String> {
159+
val scopeVars = mutableListOf<String>()
160+
minScope.parents(true).filterIsInstance<KtBlockExpression>().forEach {
161+
it.statements.filterIsInstance<KtProperty>().forEach {
162+
if (it.textRange.startOffset <= minScope.textRange.startOffset) {
163+
it.name?.let { scopeVars.add(it) }
164+
}
165+
}
166+
}
167+
minScope.parents(true).filterIsInstance<KtCatchClause>().forEach {
168+
it.parameterList?.parameters?.forEach {
169+
it.name?.let { scopeVars.add(it) }
170+
}
154171
}
155172
return scopeVars
156173
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Source++, the continuous feedback platform for developers.
3+
* Copyright (C) 2022-2023 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.intellij.openapi.application.ApplicationManager
20+
import com.intellij.openapi.util.Computable
21+
import com.intellij.testFramework.TestDataPath
22+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
23+
import kotlinx.coroutines.runBlocking
24+
import spp.jetbrains.artifact.service.ArtifactScopeService
25+
import spp.jetbrains.marker.SourceMarker
26+
import spp.jetbrains.marker.jvm.JVMLanguageProvider
27+
28+
@TestDataPath("\$CONTENT_ROOT/testData/scope/")
29+
class JVMVariableScopeTest : BasePlatformTestCase() {
30+
31+
override fun setUp() {
32+
super.setUp()
33+
ApplicationManager.getApplication().runReadAction(Computable {
34+
runBlocking {
35+
SourceMarker.getInstance(myFixture.project).clearAvailableSourceFileMarkers()
36+
}
37+
})
38+
39+
JVMLanguageProvider().setup(project)
40+
}
41+
42+
override fun getTestDataPath(): String {
43+
return "src/test/testData/scope/"
44+
}
45+
46+
fun testVariableScope() {
47+
doVariableScope("groovy")
48+
doVariableScope("java")
49+
doVariableScope("kt")
50+
}
51+
52+
private fun doVariableScope(extension: String) {
53+
val psiFile = myFixture.configureByFile(getTestName(false) + ".$extension")
54+
val scopeVariables = ArtifactScopeService.getScopeVariables(psiFile, 7)
55+
assertEquals(4, scopeVariables.size)
56+
assertTrue(scopeVariables.any { it == "i" })
57+
assertTrue(scopeVariables.any { it == "c" })
58+
assertTrue(scopeVariables.any { it == "s" })
59+
assertTrue(scopeVariables.any { it == "f" })
60+
}
61+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class VariableScope {
2+
private void variableScope() {
3+
def i = 1
4+
def c = 'h'
5+
def s = "hi"
6+
def f = 1.0f
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
private class VariableScope {
2+
private void variableScope() {
3+
int i = 1;
4+
char c = 'h';
5+
String s = "hi";
6+
float f = 1.0f;
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
private class VariableScope {
2+
private fun variableScope() {
3+
val i = 1
4+
val c = 'h'
5+
val s = "hi"
6+
val f = 1.0f
7+
}
8+
}

0 commit comments

Comments
 (0)