Skip to content

Commit a556a6c

Browse files
authored
CodeWhisperer: fix read operation is not wrapped in readAction (#3763)
1 parent 2eecb12 commit a556a6c

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "CodeWhisperer: Run read operation in the background thread without runReadAction"
4+
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileCrawler.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package software.aws.toolkits.jetbrains.services.codewhisperer.util
55

6+
import com.intellij.openapi.application.runReadAction
67
import com.intellij.openapi.fileEditor.FileEditorManager
78
import com.intellij.openapi.project.Project
89
import com.intellij.openapi.project.guessProjectDir
@@ -69,15 +70,17 @@ abstract class CodeWhispererFileCrawler : FileCrawler {
6970
val project = target.project
7071
val targetElements = keywordProducer(target)
7172

72-
return FileEditorManager.getInstance(project).openFiles
73-
.filter { openedFile ->
74-
openedFile.name != target.virtualFile.name && openedFile.extension == target.virtualFile.extension
75-
}
76-
.mapNotNull { openedFile -> PsiManager.getInstance(project).findFile(openedFile) }
77-
.maxByOrNull {
78-
val elementsToCheck = keywordProducer(it)
79-
countSubstringMatches(targetElements, elementsToCheck)
80-
}?.virtualFile
73+
return runReadAction {
74+
FileEditorManager.getInstance(project).openFiles
75+
.filter { openedFile ->
76+
openedFile.name != target.virtualFile.name && openedFile.extension == target.virtualFile.extension
77+
}
78+
.mapNotNull { openedFile -> PsiManager.getInstance(project).findFile(openedFile) }
79+
.maxByOrNull {
80+
val elementsToCheck = keywordProducer(it)
81+
countSubstringMatches(targetElements, elementsToCheck)
82+
}?.virtualFile
83+
}
8184
}
8285

8386
/**

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/JavaCodeWhispererFileCrawler.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ object JavaCodeWhispererFileCrawler : CodeWhispererFileCrawler() {
112112
* check files in editors and pick one which has most substring matches to the target
113113
*/
114114
private fun findRelevantFileFromEditors(psiFile: PsiFile): VirtualFile? = searchRelevantFileInEditors(psiFile) { myPsiFile ->
115-
myPsiFile as PsiClassOwner
116-
// (1)
117-
val classAndMethod = myPsiFile.classes.mapNotNull { clazz ->
118-
// class name itself + its method names
119-
listOfNotNull(clazz.name) +
120-
clazz.methods.mapNotNull { method -> method.name }
121-
}.flatten()
115+
if (myPsiFile !is PsiClassOwner) {
116+
return@searchRelevantFileInEditors emptyList()
117+
}
118+
119+
val classAndMethod = runReadAction {
120+
myPsiFile.classes.mapNotNull { clazz ->
121+
listOfNotNull(clazz.name) + clazz.methods.mapNotNull { method -> method.name }
122+
}.flatten()
123+
}
122124

123125
classAndMethod
124126
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/PythonCodeWhispererFileCrawler.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package software.aws.toolkits.jetbrains.services.codewhisperer.util
55

6+
import com.intellij.openapi.application.runReadAction
67
import com.intellij.openapi.module.ModuleUtilCore
78
import com.intellij.openapi.project.rootManager
89
import com.intellij.openapi.vfs.VfsUtil
@@ -51,16 +52,19 @@ object PythonCodeWhispererFileCrawler : CodeWhispererFileCrawler() {
5152
* check files in editors and pick one which has most substring matches to the target
5253
*/
5354
private fun findRelevantFileFromEditors(psiFile: PsiFile): VirtualFile? = searchRelevantFileInEditors(psiFile) { myPsiFile ->
54-
myPsiFile as PyFile
55-
// (1)
56-
val classAndMethod = myPsiFile.topLevelClasses.mapNotNull {
57-
// class name itself + its method names
58-
listOfNotNull(it.name) +
59-
it.methods.mapNotNull { method -> method.name }
60-
}.flatten()
55+
if (myPsiFile !is PyFile) {
56+
return@searchRelevantFileInEditors emptyList()
57+
}
58+
59+
val classAndMethod = runReadAction {
60+
myPsiFile.topLevelClasses.mapNotNull {
61+
listOfNotNull(it.name) + it.methods.mapNotNull { method -> method.name }
62+
}.flatten()
63+
}
6164

62-
// (2)
63-
val topLevelFunc = myPsiFile.topLevelFunctions.mapNotNull { it.name }
65+
val topLevelFunc = runReadAction {
66+
myPsiFile.topLevelFunctions.mapNotNull { it.name }
67+
}
6468

6569
classAndMethod + topLevelFunc
6670
}

0 commit comments

Comments
 (0)