Skip to content

Commit 253dffb

Browse files
committed
patch
1 parent a01a618 commit 253dffb

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEditorUtil.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretContext
1919
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretPosition
2020
import software.aws.toolkits.jetbrains.services.codewhisperer.model.FileContextInfo
2121
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
22+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.AWSTemplateCaseInsensitiveKeyWordsRegex
23+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.AWSTemplateKeyWordsRegex
2224
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.JsonConfigFileNamingConvention
2325
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.LEFT_CONTEXT_ON_CURRENT_LINE
2426
import java.awt.Point
27+
import java.util.Locale
2528
import kotlin.math.max
2629
import kotlin.math.min
2730

@@ -108,8 +111,13 @@ object CodeWhispererEditorUtil {
108111
/**
109112
* Checks if the language is json and checks if left context contains keywords
110113
*/
111-
fun isConfigFileIfJsonFile(fileName: String, language: CodeWhispererProgrammingLanguage): Boolean =
112-
(language is CodeWhispererJson) && JsonConfigFileNamingConvention.contains(fileName)
114+
fun isConfigFileIfJsonFile(fileName: String, leftContext: String, language: CodeWhispererProgrammingLanguage): Boolean =
115+
(language is CodeWhispererJson) &&
116+
(
117+
JsonConfigFileNamingConvention.contains(fileName) ||
118+
AWSTemplateKeyWordsRegex.containsMatchIn(leftContext) ||
119+
AWSTemplateCaseInsensitiveKeyWordsRegex.containsMatchIn(leftContext.lowercase(Locale.getDefault()))
120+
)
113121

114122
/**
115123
* Checks if the [otherRange] overlaps this TextRange. Note that the comparison is `<` because the endOffset of TextRange is exclusive.

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
170170
}
171171

172172
val language = requestContext.fileContextInfo.programmingLanguage
173-
if (!language.isCodeCompletionSupported() || !(isConfigFileIfJsonFile(requestContext.fileContextInfo.filename, language))) {
173+
val leftContext = requestContext.fileContextInfo.caretContext.leftFileContext
174+
if (!language.isCodeCompletionSupported() || !isConfigFileIfJsonFile(requestContext.fileContextInfo.filename, leftContext, language)) {
174175
LOG.debug { "Programming language $language is not supported by CodeWhisperer" }
175176
if (triggerTypeInfo.triggerType == CodewhispererTriggerType.OnDemand) {
176177
showCodeWhispererInfoHint(

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererConstants.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ object CodeWhispererConstants {
3333
const val SUPPLEMENTAL_CONTEXT_TIMEOUT = 50L
3434
const val FEATURE_EVALUATION_PRODUCT_NAME = "CodeWhisperer"
3535

36+
val AWSTemplateKeyWordsRegex = Regex("(AWSTemplateFormatVersion|Resources|AWS::|Description)")
37+
val AWSTemplateCaseInsensitiveKeyWordsRegex = Regex("(cloudformation|cfn|template|description)")
38+
3639
val JsonConfigFileNamingConvention = setOf(
3740
"app.json",
3841
"appsettings.json",

plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererEditorUtilTest.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.junit.Rule
1313
import org.junit.Test
1414
import org.junit.jupiter.params.ParameterizedTest
1515
import org.junit.jupiter.params.provider.ValueSource
16+
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.leftContext_success_Iac
1617
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonFileName
1718
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonTestLeftContext
1819
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil
@@ -78,6 +79,12 @@ class CodeWhispererEditorUtilTest {
7879
assertThat(caretContext.leftContextOnCurrentLine).isEqualTo(pythonTestLeftContext)
7980
}
8081

82+
@Test
83+
fun `test for keyword check for json and yaml`() {
84+
val result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("foo.json", leftContext_success_Iac, CodeWhispererJson.INSTANCE)
85+
assertThat(result).isEqualTo(true)
86+
}
87+
8188
@ParameterizedTest
8289
@ValueSource(
8390
strings = ["app.json",
@@ -93,17 +100,17 @@ class CodeWhispererEditorUtilTest {
93100
"vcpkg.json"
94101
]
95102
)
96-
fun `isConfigFileIfJsonFile should return true`(fileName: String) {
97-
val result = CodeWhispererEditorUtil.isConfigFileIfJsonFile(fileName, CodeWhispererJson.INSTANCE)
103+
fun `isConfigFileIfJsonFile should return true by file name`(fileName: String) {
104+
val result = CodeWhispererEditorUtil.isConfigFileIfJsonFile(fileName, "", CodeWhispererJson.INSTANCE)
98105
assertThat(result).isEqualTo(true)
99106
}
100107

101108
@Test
102-
fun `isConfigFileIfJsonFile should retrun false`() {
103-
var result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("foo.json", CodeWhispererJson.INSTANCE)
109+
fun `isConfigFileIfJsonFile should retrun false due to no match`() {
110+
var result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("foo.json", "",CodeWhispererJson.INSTANCE)
104111
assertThat(result).isEqualTo(false)
105112

106-
result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("package.json", CodeWhispererYaml.INSTANCE)
113+
result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("package.json", "", CodeWhispererYaml.INSTANCE)
107114
assertThat(result).isEqualTo(false)
108115
}
109116
}

0 commit comments

Comments
 (0)