-
Notifications
You must be signed in to change notification settings - Fork 273
fix(amazonq): change to use withExtensionFilter to hide unallowed files #5909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
58ab971
3b2cee2
3e0bb8e
c2fad64
5687629
cfec3f7
b6347fc
4578b57
c747a68
8daf725
d944608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -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(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.