Skip to content

Commit

Permalink
refactor(coroutines): try to follow best practices regarding coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
Blarc committed May 20, 2024
1 parent 645ed7e commit 0e358b7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.intellij.ui.EditorNotifications
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


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

CoroutineScope(Dispatchers.IO).launch {
CoroutineScope(Dispatchers.Default).launch {
withBackgroundProgress(file.project, GitlabLintBundle.message("inspection.title")) {
pipeline.accept(file)

withContext(Dispatchers.Main) {
EditorNotifications.getInstance(project).updateAllNotifications()
}
EditorNotifications.getInstance(project).updateAllNotifications()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class SaveActionListener(val project: Project) : FileDocumentManagerListener {
super.beforeDocumentSaving(document)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.FileEditorManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Service(Service.Level.PROJECT)
class LintContext : Middleware {
Expand Down Expand Up @@ -78,7 +80,7 @@ class LintContext : Middleware {
}
}

private fun lintContent(
private suspend fun lintContent(
gitlab: Gitlab,
gitlabUrl: String,
gitlabToken: String,
Expand All @@ -93,13 +95,15 @@ class LintContext : Middleware {
fileText = pass.file.text
}

return gitlab.lintContent(
gitlabUrl,
gitlabToken,
fileText,
remoteId,
branch,
showGitlabTokenNotification
).get()
return withContext(Dispatchers.IO) {
gitlab.lintContent(
gitlabUrl,
gitlabToken,
fileText,
remoteId,
branch,
showGitlabTokenNotification
).get()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.intellij.openapi.components.service
import git4idea.repo.GitRemote
import git4idea.repo.GitRepository
import git4idea.repo.GitRepositoryManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Service(Service.Level.PROJECT)
class ResolveContext : Middleware {
Expand Down Expand Up @@ -75,8 +77,10 @@ class ResolveContext : Middleware {
}
}

private fun locateRepository(pass: Pass): GitRepository? {
val repository = GitRepositoryManager.getInstance(pass.project).getRepositoryForFile(pass.file.virtualFile)
private suspend fun locateRepository(pass: Pass): GitRepository? {
val repository = withContext(Dispatchers.IO) {
GitRepositoryManager.getInstance(pass.project).getRepositoryForFile(pass.file.virtualFile)
}

showRepositoryNotification = if (repository == null) {
sendNotification(Notification.repositoryNotFound(), pass.project)
Expand Down

0 comments on commit 0e358b7

Please sign in to comment.