File tree Expand file tree Collapse file tree 4 files changed +38
-25
lines changed
jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/util Expand file tree Collapse file tree 4 files changed +38
-25
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ "type" : " bugfix" ,
3
+ "description" : " CodeWhisperer: Run read operation in the background thread without runReadAction"
4
+ }
Original file line number Diff line number Diff line change 3
3
4
4
package software.aws.toolkits.jetbrains.services.codewhisperer.util
5
5
6
+ import com.intellij.openapi.application.runReadAction
6
7
import com.intellij.openapi.fileEditor.FileEditorManager
7
8
import com.intellij.openapi.project.Project
8
9
import com.intellij.openapi.project.guessProjectDir
@@ -69,15 +70,17 @@ abstract class CodeWhispererFileCrawler : FileCrawler {
69
70
val project = target.project
70
71
val targetElements = keywordProducer(target)
71
72
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
+ }
81
84
}
82
85
83
86
/* *
Original file line number Diff line number Diff line change @@ -112,13 +112,15 @@ object JavaCodeWhispererFileCrawler : CodeWhispererFileCrawler() {
112
112
* check files in editors and pick one which has most substring matches to the target
113
113
*/
114
114
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
+ }
122
124
123
125
classAndMethod
124
126
}
Original file line number Diff line number Diff line change 3
3
4
4
package software.aws.toolkits.jetbrains.services.codewhisperer.util
5
5
6
+ import com.intellij.openapi.application.runReadAction
6
7
import com.intellij.openapi.module.ModuleUtilCore
7
8
import com.intellij.openapi.project.rootManager
8
9
import com.intellij.openapi.vfs.VfsUtil
@@ -51,16 +52,19 @@ object PythonCodeWhispererFileCrawler : CodeWhispererFileCrawler() {
51
52
* check files in editors and pick one which has most substring matches to the target
52
53
*/
53
54
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
+ }
61
64
62
- // (2)
63
- val topLevelFunc = myPsiFile.topLevelFunctions.mapNotNull { it.name }
65
+ val topLevelFunc = runReadAction {
66
+ myPsiFile.topLevelFunctions.mapNotNull { it.name }
67
+ }
64
68
65
69
classAndMethod + topLevelFunc
66
70
}
You can’t perform that action at this time.
0 commit comments