Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,32 @@
return runnable(lsp)
}

suspend fun<T> executeIfRunning(runnable: suspend AmazonQLspService.(AmazonQLanguageServer) -> T): T? {
val lsp = try {
withTimeout(1.seconds) {
val holder = mutex.withLock { instance }.await()
holder.initializeResult.join()

holder.languageServer
}
} catch (_: Exception) {
LOG.debug { "LSP not running" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should this be an info with exception message?

null
}

return lsp?.let { runnable(it) }
}

fun<T> executeSync(runnable: suspend AmazonQLspService.(AmazonQLanguageServer) -> T): T =
runBlocking(cs.coroutineContext) {
execute(runnable)
}

fun<T> syncExecuteIfRunning(runnable: suspend AmazonQLspService.(AmazonQLanguageServer) -> T): T? =

Check warning

Code scanning / QDJVMC

Unused symbol Warning

Function "syncExecuteIfRunning" is never used
runBlocking(cs.coroutineContext) {
executeIfRunning(runnable)
}

companion object {
private val LOG = getLogger<AmazonQLspService>()
private const val MAX_RESTARTS = 5
Expand All @@ -295,10 +316,10 @@

@Deprecated("Easy to accidentally freeze EDT")
fun <T> executeIfRunning(project: Project, runnable: AmazonQLspService.(AmazonQLanguageServer) -> T): T? =
project.serviceIfCreated<AmazonQLspService>()?.executeSync(runnable)
project.serviceIfCreated<AmazonQLspService>()?.syncExecuteIfRunning(runnable)

suspend fun <T> asyncExecuteIfRunning(project: Project, runnable: suspend AmazonQLspService.(AmazonQLanguageServer) -> T): T? =
project.serviceIfCreated<AmazonQLspService>()?.execute(runnable)
suspend fun <T> executeAsyncIfRunning(project: Project, runnable: suspend AmazonQLspService.(AmazonQLanguageServer) -> T): T? =

Check warning on line 321 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLspService.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Function "executeAsyncIfRunning" is never used
project.serviceIfCreated<AmazonQLspService>()?.executeIfRunning(runnable)

fun didChangeConfiguration(project: Project) {
executeIfRunning(project) {
Expand Down Expand Up @@ -496,13 +517,13 @@
DefaultAuthCredentialsService(project, encryptionManager).also {
Disposer.register(this, it)
}
TextDocumentServiceHandler(project).also {
TextDocumentServiceHandler(project, cs).also {
Disposer.register(this, it)
}
WorkspaceServiceHandler(project, lspInitResult).also {
WorkspaceServiceHandler(project, cs, lspInitResult).also {
Disposer.register(this, it)
}
DefaultModuleDependenciesService(project).also {
DefaultModuleDependenciesService(project, cs).also {
Disposer.register(this, it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
import software.aws.toolkits.jetbrains.services.amazonq.lsp.dependencies.ModuleDependencyProvider.Companion.EP_NAME
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.dependencies.DidChangeDependencyPathsParams
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread

class DefaultModuleDependenciesService(
private val project: Project,
private val cs: CoroutineScope,
) : ModuleDependenciesService,
ModuleRootListener,
Disposable {
Expand All @@ -35,7 +37,7 @@ class DefaultModuleDependenciesService(

override fun didChangeDependencyPaths(params: DidChangeDependencyPathsParams) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
pluginAwareExecuteOnPooledThread {
cs.launch {
languageServer.didChangeDependencyPaths(params)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.eclipse.lsp4j.DidChangeTextDocumentParams
import org.eclipse.lsp4j.DidCloseTextDocumentParams
import org.eclipse.lsp4j.DidOpenTextDocumentParams
Expand All @@ -29,10 +31,10 @@ import org.eclipse.lsp4j.TextDocumentItem
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.LspEditorUtil.toUriString
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread

class TextDocumentServiceHandler(
private val project: Project,
private val cs: CoroutineScope,
) : FileDocumentManagerListener,
FileEditorManagerListener,
BulkFileListener,
Expand Down Expand Up @@ -78,7 +80,7 @@ class TextDocumentServiceHandler(
}
AmazonQLspService.executeIfRunning(project) { languageServer ->
toUriString(file)?.let { uri ->
pluginAwareExecuteOnPooledThread {
cs.launch {
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
Expand All @@ -98,7 +100,7 @@ class TextDocumentServiceHandler(
AmazonQLspService.executeIfRunning(project) { languageServer ->
val file = FileDocumentManager.getInstance().getFile(document) ?: return@executeIfRunning
toUriString(file)?.let { uri ->
pluginAwareExecuteOnPooledThread {
cs.launch {
languageServer.textDocumentService.didSave(
DidSaveTextDocumentParams().apply {
textDocument = TextDocumentIdentifier().apply {
Expand All @@ -114,7 +116,7 @@ class TextDocumentServiceHandler(

override fun after(events: MutableList<out VFileEvent>) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
pluginAwareExecuteOnPooledThread {
cs.launch {
events.filterIsInstance<VFileContentChangeEvent>().forEach { event ->
val document = FileDocumentManager.getInstance().getCachedDocument(event.file) ?: return@forEach
toUriString(event.file)?.let { uri ->
Expand Down Expand Up @@ -168,8 +170,8 @@ class TextDocumentServiceHandler(

private fun realTimeEdit(event: DocumentEvent) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
pluginAwareExecuteOnPooledThread {
val vFile = FileDocumentManager.getInstance().getFile(event.document) ?: return@pluginAwareExecuteOnPooledThread
cs.launch {
val vFile = FileDocumentManager.getInstance().getFile(event.document) ?: return@launch
toUriString(vFile)?.let { uri ->
languageServer.textDocumentService.didChange(
DidChangeTextDocumentParams().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent
import com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.eclipse.lsp4j.CreateFilesParams
import org.eclipse.lsp4j.DeleteFilesParams
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
Expand All @@ -37,13 +39,13 @@ import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent
import software.aws.toolkits.jetbrains.services.amazonq.lsp.AmazonQLspService
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.LspEditorUtil.toUriString
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.WorkspaceFolderUtil.createWorkspaceFolders
import software.aws.toolkits.jetbrains.utils.pluginAwareExecuteOnPooledThread
import java.nio.file.FileSystems
import java.nio.file.PathMatcher
import java.nio.file.Paths

class WorkspaceServiceHandler(
private val project: Project,
private val cs: CoroutineScope,
initializeResult: InitializeResult,
) : BulkFileListener,
ModuleRootListener,
Expand Down Expand Up @@ -281,7 +283,7 @@ class WorkspaceServiceHandler(

override fun after(events: List<VFileEvent>) {
// since we are using synchronous FileListener
pluginAwareExecuteOnPooledThread {
cs.launch {
didCreateFiles(events.filter { it is VFileCreateEvent || it is VFileMoveEvent || it is VFileCopyEvent })
didDeleteFiles(events.filter { it is VFileMoveEvent || it is VFileDeleteEvent })
didRenameFiles(events.filterIsInstance<VFilePropertyChangeEvent>())
Expand Down
Loading