Skip to content

Commit a01a618

Browse files
committed
loose inline suggestion support for json/yaml files
1 parent b88f7d8 commit a01a618

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import com.intellij.openapi.util.TextRange
1212
import com.intellij.openapi.vfs.VfsUtilCore
1313
import com.intellij.psi.PsiFile
1414
import com.intellij.ui.popup.AbstractPopup
15+
import software.aws.toolkits.jetbrains.services.codewhisperer.language.CodeWhispererProgrammingLanguage
1516
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererJson
16-
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererYaml
1717
import software.aws.toolkits.jetbrains.services.codewhisperer.language.programmingLanguage
1818
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.JsonConfigFileNamingConvention
2223
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.LEFT_CONTEXT_ON_CURRENT_LINE
2324
import java.awt.Point
24-
import java.util.Locale
2525
import kotlin.math.max
2626
import kotlin.math.min
2727

@@ -106,16 +106,10 @@ object CodeWhispererEditorUtil {
106106
}
107107

108108
/**
109-
* Checks if the language is json or yaml and checks if left context contains keywords
109+
* Checks if the language is json and checks if left context contains keywords
110110
*/
111-
fun checkLeftContextKeywordsForJsonAndYaml(leftContext: String, language: String): Boolean = (
112-
(language == CodeWhispererJson.INSTANCE.languageId) ||
113-
(language == CodeWhispererYaml.INSTANCE.languageId)
114-
) &&
115-
(
116-
(!CodeWhispererConstants.AWSTemplateKeyWordsRegex.containsMatchIn(leftContext)) &&
117-
(!CodeWhispererConstants.AWSTemplateCaseInsensitiveKeyWordsRegex.containsMatchIn(leftContext.lowercase(Locale.getDefault())))
118-
)
111+
fun isConfigFileIfJsonFile(fileName: String, language: CodeWhispererProgrammingLanguage): Boolean =
112+
(language is CodeWhispererJson) && JsonConfigFileNamingConvention.contains(fileName)
119113

120114
/**
121115
* 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import software.aws.toolkits.jetbrains.core.credentials.pinning.CodeWhispererCon
5555
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
5656
import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
5757
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorManager
58-
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.checkLeftContextKeywordsForJsonAndYaml
58+
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.isConfigFileIfJsonFile
5959
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.getCaretPosition
6060
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
6161
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.isCodeWhispererEnabled
@@ -170,8 +170,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
170170
}
171171

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

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@ 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)")
36+
val JsonConfigFileNamingConvention = setOf(
37+
"app.json",
38+
"appsettings.json",
39+
"bower.json",
40+
"composer.json",
41+
"db.json",
42+
"manifest.json",
43+
"package.json",
44+
"schema.json",
45+
"settings.json",
46+
"tsconfig.json",
47+
"vcpkg.json"
48+
)
3849

3950
// TODO: this is currently set to 2050 to account for the server side 0.5 TPS and and extra 50 ms buffer to
4051
// avoid ThrottlingException as much as possible.
@@ -131,6 +142,7 @@ object CodeWhispererConstants {
131142
}
132143
}
133144
}
145+
134146
object CrossFile {
135147
const val CHUNK_SIZE = 60
136148
const val NUMBER_OF_LINE_IN_CHUNK = 10
@@ -145,7 +157,7 @@ object CodeWhispererConstants {
145157
object TryExampleFileContent {
146158

147159
private const val AUTO_TRIGGER_CONTENT_JAVA =
148-
"""import java.util.ArrayList;
160+
"""import java.util.ArrayList;
149161
import java.util.HashMap;
150162
import java.util.List;
151163
import java.util.Map;
@@ -167,7 +179,7 @@ public class Main {
167179
}"""
168180

169181
private const val MANUAL_TRIGGER_CONTENT_JAVA =
170-
"""// TODO: Press either Option + C on MacOS or Alt + C on Windows on a new line.
182+
"""// TODO: Press either Option + C on MacOS or Alt + C on Windows on a new line.
171183
172184
public class S3Uploader {
173185
@@ -178,7 +190,7 @@ public class S3Uploader {
178190
}"""
179191

180192
private const val UNIT_TEST_CONTENT_JAVA =
181-
"""// TODO: Ask Amazon Q to write unit tests.
193+
"""// TODO: Ask Amazon Q to write unit tests.
182194
183195
// Write a test case for the sum function.
184196

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import org.assertj.core.api.Assertions.assertThat
1111
import org.junit.Before
1212
import org.junit.Rule
1313
import org.junit.Test
14-
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.leftContext_success_Iac
14+
import org.junit.jupiter.params.ParameterizedTest
15+
import org.junit.jupiter.params.provider.ValueSource
1516
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonFileName
1617
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonTestLeftContext
17-
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.yaml_langauge
1818
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil
19+
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererJson
1920
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererPython
21+
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererYaml
2022
import software.aws.toolkits.jetbrains.utils.rules.PythonCodeInsightTestFixtureRule
2123

2224
class CodeWhispererEditorUtilTest {
@@ -76,12 +78,32 @@ class CodeWhispererEditorUtilTest {
7678
assertThat(caretContext.leftContextOnCurrentLine).isEqualTo(pythonTestLeftContext)
7779
}
7880

79-
/**
80-
* Test for keyword checks for json and yaml
81-
*/
81+
@ParameterizedTest
82+
@ValueSource(
83+
strings = ["app.json",
84+
"appsettings.json",
85+
"bower.json",
86+
"composer.json",
87+
"db.json",
88+
"manifest.json",
89+
"package.json",
90+
"schema.json",
91+
"settings.json",
92+
"tsconfig.json",
93+
"vcpkg.json"
94+
]
95+
)
96+
fun `isConfigFileIfJsonFile should return true`(fileName: String) {
97+
val result = CodeWhispererEditorUtil.isConfigFileIfJsonFile(fileName, CodeWhispererJson.INSTANCE)
98+
assertThat(result).isEqualTo(true)
99+
}
100+
82101
@Test
83-
fun `test for keyword check for json and yaml`() {
84-
val result = CodeWhispererEditorUtil.checkLeftContextKeywordsForJsonAndYaml(leftContext_success_Iac, yaml_langauge)
102+
fun `isConfigFileIfJsonFile should retrun false`() {
103+
var result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("foo.json", CodeWhispererJson.INSTANCE)
104+
assertThat(result).isEqualTo(false)
105+
106+
result = CodeWhispererEditorUtil.isConfigFileIfJsonFile("package.json", CodeWhispererYaml.INSTANCE)
85107
assertThat(result).isEqualTo(false)
86108
}
87109
}

0 commit comments

Comments
 (0)