Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "change to use withExtensionFilter to hide unallowed files when selecting image"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"description" : "change to use withExtensionFilter to hide unallowed files when selecting image"
"description" : "Fix unsupported files being shown in file picker when selecting images for adding image context in Windows"

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.ide.BrowserUtil
import com.intellij.notification.NotificationAction
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileChooser.FileChooser
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
Expand Down Expand Up @@ -311,13 +312,30 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
if (params.filters.isNotEmpty() && !params.canSelectFolders) {
// Create a combined list of all allowed extensions
val allowedExtensions = params.filters.values.flatten().toSet()

withFileFilter { virtualFile ->
if (virtualFile.isDirectory) {
true // Always allow directories for navigation
} else {
val extension = virtualFile.extension?.lowercase()
extension != null && allowedExtensions.contains(extension)
if (isExtensionFilterSupported()) {
// Use reflection to call withExtensionFilter which is only available in 2024.3+
try {
val method = this.javaClass.getMethod("withExtensionFilter", String::class.java, Array<String>::class.java)
Copy link
Contributor

Choose a reason for hiding this comment

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

we have different source modules that support code compatibility based on versions. Instead of using reflection, we can use those? This code can be added to src-243+. Will this still be an issue on 242?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

method.invoke(this, "Image", allowedExtensions.toTypedArray())
} catch (e: Exception) {
// Fallback to withFileFilter if reflection fails
withFileFilter { virtualFile ->
if (virtualFile.isDirectory) {
true // Always allow directories for navigation
} else {
val extension = virtualFile.extension?.lowercase()
extension != null && allowedExtensions.contains(extension)
}
}
}
} else {
withFileFilter { virtualFile ->
if (virtualFile.isDirectory) {
true // Always allow directories for navigation
} else {
val extension = virtualFile.extension?.lowercase()
extension != null && allowedExtensions.contains(extension)
}
}
}
}
Expand Down Expand Up @@ -596,6 +614,20 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
}
}

/**
* Checks if the current JetBrains IDE version supports the withExtensionFilter API.
*
* The withExtensionFilter method was introduced in IntelliJ Platform 2024.3 (baseline version 243).
* For older versions, we need to fall back to withFileFilter which provides similar functionality
* but with different UI behavior (files are not visually filtered in the file chooser dialog).
*
* @return true if the IDE version supports withExtensionFilter (2024.3+), false otherwise
*/
private fun isExtensionFilterSupported(): Boolean {
val baselineVersion = ApplicationInfo.getInstance().build.baselineVersion
return baselineVersion >= 243 // 2024.3 or later
}

companion object {
val localHistoryPath = Paths.get(
UserHomeDirectoryUtils.userHomeDirectory(),
Expand Down
Loading