Skip to content

Commit 5687629

Browse files
committed
fix(amazonq): comments
1 parent c2fad64 commit 5687629

File tree

5 files changed

+85
-42
lines changed

5 files changed

+85
-42
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"type" : "bugfix",
3-
"description" : "change to use withExtensionFilter to hide unallowed files when selecting image"
3+
"description" : "Fix unsupported files being shown in file picker when selecting images for adding image context in Windows"
44
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.util
5+
6+
import com.intellij.openapi.fileChooser.FileChooserDescriptor
7+
8+
/**
9+
* FileChooserCompat implementation for IntelliJ Platform versions before 2024.3 (baseline < 243).
10+
* Uses withFileFilter method which provides filtering functionality but doesn't visually filter
11+
* files in the chooser dialog.
12+
*/
13+
internal class FileChooserCompatImpl : FileChooserCompat {
14+
override fun applyExtensionFilter(
15+
descriptor: FileChooserDescriptor,
16+
filterName: String,
17+
allowedExtensions: Set<String>
18+
): FileChooserDescriptor {
19+
return descriptor.withFileFilter { virtualFile ->
20+
if (virtualFile.isDirectory) {
21+
true // Always allow directories for navigation
22+
} else {
23+
val extension = virtualFile.extension?.lowercase()
24+
extension != null && allowedExtensions.contains(extension)
25+
}
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.util
5+
6+
import com.intellij.openapi.fileChooser.FileChooserDescriptor
7+
8+
/**
9+
* FileChooserCompat implementation for IntelliJ Platform versions 2024.3+ (baseline >= 243).
10+
* Uses withExtensionFilter method which provides both filtering functionality and visual
11+
* filtering in the chooser dialog.
12+
*/
13+
internal class FileChooserCompatImpl : FileChooserCompat {
14+
override fun applyExtensionFilter(
15+
descriptor: FileChooserDescriptor,
16+
filterName: String,
17+
allowedExtensions: Set<String>
18+
): FileChooserDescriptor {
19+
return descriptor.withExtensionFilter(filterName, *allowedExtensions.toTypedArray())
20+
}
21+
}

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.intellij.diff.requests.SimpleDiffRequest
99
import com.intellij.ide.BrowserUtil
1010
import com.intellij.notification.NotificationAction
1111
import com.intellij.notification.NotificationType
12-
import com.intellij.openapi.application.ApplicationInfo
1312
import com.intellij.openapi.application.ApplicationManager
1413
import com.intellij.openapi.fileChooser.FileChooser
1514
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory
@@ -65,6 +64,7 @@ import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ShowO
6564
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ShowSaveFileDialogParams
6665
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.chat.ShowSaveFileDialogResult
6766
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.ConnectionMetadata
67+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.FileChooserCompat
6868
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.LspEditorUtil
6969
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.TelemetryParsingUtil
7070
import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
@@ -312,32 +312,8 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
312312
if (params.filters.isNotEmpty() && !params.canSelectFolders) {
313313
// Create a combined list of all allowed extensions
314314
val allowedExtensions = params.filters.values.flatten().toSet()
315-
if (isExtensionFilterSupported()) {
316-
// Use reflection to call withExtensionFilter which is only available in 2024.3+
317-
try {
318-
val method = this.javaClass.getMethod("withExtensionFilter", String::class.java, Array<String>::class.java)
319-
method.invoke(this, "Image", allowedExtensions.toTypedArray())
320-
} catch (e: Exception) {
321-
// Fallback to withFileFilter if reflection fails
322-
withFileFilter { virtualFile ->
323-
if (virtualFile.isDirectory) {
324-
true // Always allow directories for navigation
325-
} else {
326-
val extension = virtualFile.extension?.lowercase()
327-
extension != null && allowedExtensions.contains(extension)
328-
}
329-
}
330-
}
331-
} else {
332-
withFileFilter { virtualFile ->
333-
if (virtualFile.isDirectory) {
334-
true // Always allow directories for navigation
335-
} else {
336-
val extension = virtualFile.extension?.lowercase()
337-
extension != null && allowedExtensions.contains(extension)
338-
}
339-
}
340-
}
315+
val fileChooserCompat = FileChooserCompat.getInstance()
316+
fileChooserCompat.applyExtensionFilter(this, "Images", allowedExtensions)
341317
}
342318
}
343319

@@ -614,20 +590,6 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
614590
}
615591
}
616592

617-
/**
618-
* Checks if the current JetBrains IDE version supports the withExtensionFilter API.
619-
*
620-
* The withExtensionFilter method was introduced in IntelliJ Platform 2024.3 (baseline version 243).
621-
* For older versions, we need to fall back to withFileFilter which provides similar functionality
622-
* but with different UI behavior (files are not visually filtered in the file chooser dialog).
623-
*
624-
* @return true if the IDE version supports withExtensionFilter (2024.3+), false otherwise
625-
*/
626-
private fun isExtensionFilterSupported(): Boolean {
627-
val baselineVersion = ApplicationInfo.getInstance().build.baselineVersion
628-
return baselineVersion >= 243 // 2024.3 or later
629-
}
630-
631593
companion object {
632594
val localHistoryPath = Paths.get(
633595
UserHomeDirectoryUtils.userHomeDirectory(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.util
5+
6+
import com.intellij.openapi.fileChooser.FileChooserDescriptor
7+
8+
/**
9+
* Compatibility interface for handling file chooser extension filtering across different IntelliJ Platform versions.
10+
*
11+
* The withExtensionFilter method was introduced in IntelliJ Platform 2024.3 (baseline version 243).
12+
* For older versions, fall back to withFileFilter which provides similar functionality
13+
*/
14+
interface FileChooserCompat {
15+
/**
16+
* Applies file extension filtering to the given FileChooserDescriptor.
17+
*
18+
* @param descriptor The FileChooserDescriptor to apply filtering to
19+
* @param filterName The display name for the filter (e.g., "Images")
20+
* @param allowedExtensions Set of allowed file extensions (e.g., "jpg", "png")
21+
* @return The modified FileChooserDescriptor
22+
*/
23+
fun applyExtensionFilter(
24+
descriptor: FileChooserDescriptor,
25+
filterName: String,
26+
allowedExtensions: Set<String>
27+
): FileChooserDescriptor
28+
29+
companion object {
30+
fun getInstance(): FileChooserCompat = FileChooserCompatImpl()
31+
}
32+
}

0 commit comments

Comments
 (0)