@@ -12,6 +12,7 @@ import com.intellij.openapi.application.ApplicationManager
1212import com.intellij.openapi.fileEditor.FileDocumentManager
1313import com.intellij.openapi.fileEditor.FileEditorManager
1414import com.intellij.openapi.project.Project
15+ import com.intellij.openapi.project.guessProjectDir
1516import com.intellij.openapi.vfs.LocalFileSystem
1617import com.intellij.openapi.vfs.VirtualFile
1718import com.intellij.openapi.vfs.VirtualFileManager
@@ -22,6 +23,7 @@ import kotlinx.coroutines.future.await
2223import kotlinx.coroutines.launch
2324import kotlinx.coroutines.withContext
2425import migration.software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
26+ import software.amazon.awssdk.services.codewhispererruntime.model.IdeCategory
2527import software.amazon.awssdk.services.codewhispererruntime.model.Position
2628import software.amazon.awssdk.services.codewhispererruntime.model.Range
2729import software.amazon.awssdk.services.codewhispererruntime.model.Reference
@@ -87,6 +89,7 @@ import software.aws.toolkits.resources.message
8789import software.aws.toolkits.telemetry.AmazonqTelemetry
8890import software.aws.toolkits.telemetry.MetricResult
8991import software.aws.toolkits.telemetry.UiTelemetry
92+ import java.io.File
9093import java.nio.file.Files
9194import java.nio.file.Path
9295import java.nio.file.Paths
@@ -184,7 +187,7 @@ class CodeTestChatController(
184187 message.tabId,
185188 false
186189 )
187- if (isLanguageSupported(fileInfo.fileLanguage.languageId)) {
190+ if (fileInfo.fileInWorkspace && isLanguageSupported(fileInfo.fileLanguage.languageId)) {
188191 // Send Capability card to chat
189192 codeTestChatHelper.addNewMessage(
190193 CodeTestChatMessageContent (informationCard = true , message = null , type = ChatMessageType .Answer , canBeVoted = false ),
@@ -230,9 +233,15 @@ class CodeTestChatController(
230233 }
231234 .build()
232235
233- val messageContent = " <span style=\" color: #EE9D28;\" >⚠<b> ${fileInfo.fileLanguage.languageId} is not a " +
234- " language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
235- " I support now are Python and Java. I can still provide examples, instructions and code suggestions."
236+ val messageContent = if (fileInfo.fileInWorkspace) {
237+ " <span style=\" color: #EE9D28;\" >⚠<b> ${fileInfo.fileLanguage.languageId} is not a " +
238+ " language I support specialized unit test generation for at the moment.</b><br></span>The languages " +
239+ " I support now are Python and Java. I can still provide examples, instructions and code suggestions."
240+ } else {
241+ " <span style=\" color: #EE9D28;\" >⚠<b> I can't generate tests for ${fileInfo.fileName} " +
242+ " because it's outside the project directory.</b><br></span> " +
243+ " I can still provide examples, instructions and code suggestions."
244+ }
236245
237246 codeTestChatHelper.addNewMessage(
238247 CodeTestChatMessageContent (
@@ -287,7 +296,8 @@ class CodeTestChatController(
287296 AmazonqTelemetry .utgGenerateTests(
288297 cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
289298 hasUserPromptSupplied = session.hasUserPromptSupplied,
290- isSupportedLanguage = false ,
299+ isFileInWorkspace = fileInfo.fileInWorkspace,
300+ isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId),
291301 credentialStartUrl = getStartUrl(project),
292302 result = MetricResult .Succeeded ,
293303 perfClientLatency = (Instant .now().toEpochMilli() - session.startTimeOfTestGeneration),
@@ -571,6 +581,7 @@ class CodeTestChatController(
571581 session.testGenerationJob,
572582 session.testGenerationJobGroupName,
573583 session.programmingLanguage,
584+ IdeCategory .JETBRAINS ,
574585 session.numberOfUnitTestCasesGenerated,
575586 session.numberOfUnitTestCasesGenerated,
576587 session.linesOfCodeGenerated,
@@ -588,6 +599,7 @@ class CodeTestChatController(
588599 AmazonqTelemetry .utgGenerateTests(
589600 cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
590601 hasUserPromptSupplied = session.hasUserPromptSupplied,
602+ isFileInWorkspace = true ,
591603 isSupportedLanguage = true ,
592604 credentialStartUrl = getStartUrl(project = context.project),
593605 jobGroup = session.testGenerationJobGroupName,
@@ -766,6 +778,7 @@ class CodeTestChatController(
766778 session.testGenerationJob,
767779 session.testGenerationJobGroupName,
768780 session.programmingLanguage,
781+ IdeCategory .JETBRAINS ,
769782 session.numberOfUnitTestCasesGenerated,
770783 0 ,
771784 session.linesOfCodeGenerated,
@@ -782,6 +795,7 @@ class CodeTestChatController(
782795 AmazonqTelemetry .utgGenerateTests(
783796 cwsprChatProgrammingLanguage = session.programmingLanguage.languageId,
784797 hasUserPromptSupplied = session.hasUserPromptSupplied,
798+ isFileInWorkspace = true ,
785799 isSupportedLanguage = true ,
786800 credentialStartUrl = getStartUrl(project = context.project),
787801 jobGroup = session.testGenerationJobGroupName,
@@ -1111,6 +1125,7 @@ class CodeTestChatController(
11111125 val filePath : String ,
11121126 val fileName : String ,
11131127 val fileLanguage : CodeWhispererProgrammingLanguage ,
1128+ val fileInWorkspace : Boolean = true ,
11141129 )
11151130
11161131 private suspend fun updateUIState () {
@@ -1142,6 +1157,9 @@ class CodeTestChatController(
11421157 val fileEditorManager = FileEditorManager .getInstance(project)
11431158 val activeEditor = fileEditorManager.selectedEditor
11441159 val activeFile = fileEditorManager.selectedFiles.firstOrNull()
1160+ val projectRoot = project.basePath?.let { Path .of(it) }?.toFile()?.toVirtualFile() ? : run {
1161+ project.guessProjectDir() ? : error(" Cannot guess base directory for project ${project.name} " )
1162+ }
11451163
11461164 if (activeEditor == null || activeFile == null ) {
11471165 handleInvalidFileState(message.tabId)
@@ -1156,6 +1174,7 @@ class CodeTestChatController(
11561174 filePath = activeFile.path,
11571175 fileName = activeFile.name,
11581176 fileLanguage = programmingLanguage,
1177+ fileInWorkspace = activeFile.path.startsWith(projectRoot.path)
11591178 )
11601179 } catch (e: Exception ) {
11611180 LOG .debug { " Error checking active file: $e " }
@@ -1169,6 +1188,8 @@ class CodeTestChatController(
11691188 }
11701189 }
11711190
1191+ private fun File.toVirtualFile () = LocalFileSystem .getInstance().findFileByIoFile(this )
1192+
11721193 /* UTG Tab Chat input use cases:
11731194 * 1. If User exits the flow and want to start a new generate unit test cycle.
11741195 * 2. If User clicks on Modify build command option and can enter the build command from chat input
0 commit comments