Skip to content

Commit 340bd34

Browse files
authored
fix(amazonq): Automatic start workspace indexing for new project open (#4858)
* start lsp early
1 parent c0bd5f6 commit 340bd34

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
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" : "Automatically start workspace indexing when new project is opened"
4+
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/startup/AmazonQStartupActivity.kt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ package software.aws.toolkits.jetbrains.services.amazonq.startup
55

66
import com.intellij.openapi.application.runInEdt
77
import com.intellij.openapi.project.Project
8+
import com.intellij.openapi.project.waitForSmartMode
89
import com.intellij.openapi.startup.ProjectActivity
910
import com.intellij.openapi.wm.ToolWindowManager
11+
import kotlinx.coroutines.TimeoutCancellationException
12+
import kotlinx.coroutines.delay
13+
import kotlinx.coroutines.time.withTimeout
14+
import software.aws.toolkits.core.utils.getLogger
15+
import software.aws.toolkits.core.utils.warn
1016
import software.aws.toolkits.jetbrains.core.gettingstarted.emitUserState
1117
import software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AmazonQToolWindow
1218
import software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AmazonQToolWindowFactory
1319
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
20+
import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhispererSettings
21+
import software.aws.toolkits.jetbrains.services.cwc.editor.context.project.ProjectContextController
22+
import java.lang.management.ManagementFactory
23+
import java.time.Duration
1424
import java.util.concurrent.atomic.AtomicBoolean
1525

1626
class AmazonQStartupActivity : ProjectActivity {
@@ -27,9 +37,40 @@ class AmazonQStartupActivity : ProjectActivity {
2737
CodeWhispererExplorerActionManager.getInstance().setIsFirstRestartAfterQInstall(false)
2838
}
2939
}
30-
40+
startLsp(project)
3141
if (runOnce.get()) return
3242
emitUserState(project)
3343
runOnce.set(true)
3444
}
45+
46+
private suspend fun startLsp(project: Project) {
47+
// Automatically start the project context LSP after some delay when average CPU load is below 30%.
48+
// The CPU load requirement is to avoid competing with native JetBrains indexing and other CPU expensive OS processes
49+
// In the future we will decouple LSP start and indexing start to let LSP perform other tasks.
50+
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
51+
val startLspIndexingDuration = Duration.ofMinutes(30)
52+
project.waitForSmartMode()
53+
try {
54+
withTimeout(startLspIndexingDuration) {
55+
while (true) {
56+
val cpuUsage = ManagementFactory.getOperatingSystemMXBean().systemLoadAverage
57+
if (cpuUsage > 0 && cpuUsage < 30) {
58+
ProjectContextController.getInstance(project = project)
59+
break
60+
} else {
61+
delay(60_000) // Wait for 60 seconds
62+
}
63+
}
64+
}
65+
} catch (e: TimeoutCancellationException) {
66+
LOG.warn { "Failed to start LSP server due to time out" }
67+
} catch (e: Exception) {
68+
LOG.warn { "Failed to start LSP server" }
69+
}
70+
}
71+
}
72+
73+
companion object {
74+
private val LOG = getLogger<AmazonQStartupActivity>()
75+
}
3576
}

0 commit comments

Comments
 (0)