Skip to content

Commit 0e358b7

Browse files
committed
refactor(coroutines): try to follow best practices regarding coroutines
1 parent 645ed7e commit 0e358b7

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/GitlabLintRunner.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.intellij.ui.EditorNotifications
88
import kotlinx.coroutines.CoroutineScope
99
import kotlinx.coroutines.Dispatchers
1010
import kotlinx.coroutines.launch
11-
import kotlinx.coroutines.withContext
1211

1312

1413
fun lintGitlabYaml(file: PsiFile) {
@@ -21,13 +20,10 @@ fun lint(file: PsiFile) {
2120
val project = file.project
2221
val pipeline = project.service<Pipeline>()
2322

24-
CoroutineScope(Dispatchers.IO).launch {
23+
CoroutineScope(Dispatchers.Default).launch {
2524
withBackgroundProgress(file.project, GitlabLintBundle.message("inspection.title")) {
2625
pipeline.accept(file)
27-
28-
withContext(Dispatchers.Main) {
29-
EditorNotifications.getInstance(project).updateAllNotifications()
30-
}
26+
EditorNotifications.getInstance(project).updateAllNotifications()
3127
}
3228
}
3329
}

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/listeners/SaveActionListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ class SaveActionListener(val project: Project) : FileDocumentManagerListener {
2020
super.beforeDocumentSaving(document)
2121
}
2222
}
23-
}
23+
}

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/pipeline/middleware/LintContext.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import com.intellij.openapi.command.WriteCommandAction
1212
import com.intellij.openapi.components.Service
1313
import com.intellij.openapi.components.service
1414
import com.intellij.openapi.fileEditor.FileEditorManager
15+
import kotlinx.coroutines.Dispatchers
16+
import kotlinx.coroutines.withContext
1517

1618
@Service(Service.Level.PROJECT)
1719
class LintContext : Middleware {
@@ -78,7 +80,7 @@ class LintContext : Middleware {
7880
}
7981
}
8082

81-
private fun lintContent(
83+
private suspend fun lintContent(
8284
gitlab: Gitlab,
8385
gitlabUrl: String,
8486
gitlabToken: String,
@@ -93,13 +95,15 @@ class LintContext : Middleware {
9395
fileText = pass.file.text
9496
}
9597

96-
return gitlab.lintContent(
97-
gitlabUrl,
98-
gitlabToken,
99-
fileText,
100-
remoteId,
101-
branch,
102-
showGitlabTokenNotification
103-
).get()
98+
return withContext(Dispatchers.IO) {
99+
gitlab.lintContent(
100+
gitlabUrl,
101+
gitlabToken,
102+
fileText,
103+
remoteId,
104+
branch,
105+
showGitlabTokenNotification
106+
).get()
107+
}
104108
}
105109
}

src/main/kotlin/com/github/blarc/gitlab/template/lint/plugin/pipeline/middleware/ResolveContext.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import com.intellij.openapi.components.service
1717
import git4idea.repo.GitRemote
1818
import git4idea.repo.GitRepository
1919
import git4idea.repo.GitRepositoryManager
20+
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.withContext
2022

2123
@Service(Service.Level.PROJECT)
2224
class ResolveContext : Middleware {
@@ -75,8 +77,10 @@ class ResolveContext : Middleware {
7577
}
7678
}
7779

78-
private fun locateRepository(pass: Pass): GitRepository? {
79-
val repository = GitRepositoryManager.getInstance(pass.project).getRepositoryForFile(pass.file.virtualFile)
80+
private suspend fun locateRepository(pass: Pass): GitRepository? {
81+
val repository = withContext(Dispatchers.IO) {
82+
GitRepositoryManager.getInstance(pass.project).getRepositoryForFile(pass.file.virtualFile)
83+
}
8084

8185
showRepositoryNotification = if (repository == null) {
8286
sendNotification(Notification.repositoryNotFound(), pass.project)

0 commit comments

Comments
 (0)