Skip to content

Commit 3668c49

Browse files
authored
Merge branch 'main' into manodnyb/addSourceToMetrics
2 parents 5097cb9 + 90fda64 commit 3668c49

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "feature",
3+
"description" : "Loosen inline completion support limitations for YAML/JSON"
4+
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ 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.languages.CodeWhispererJson
16-
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererYaml
1715
import software.aws.toolkits.jetbrains.services.codewhisperer.language.programmingLanguage
1816
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretContext
1917
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretPosition
2018
import software.aws.toolkits.jetbrains.services.codewhisperer.model.FileContextInfo
2119
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
20+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.AWSTemplateCaseInsensitiveKeyWordsRegex
21+
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants.AWSTemplateKeyWordsRegex
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
2425
import java.util.Locale
@@ -106,16 +107,12 @@ object CodeWhispererEditorUtil {
106107
}
107108

108109
/**
109-
* Checks if the language is json or yaml and checks if left context contains keywords
110+
* Check if left context contains keywords or file name follow config json file naming pattern
110111
*/
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-
)
112+
fun isSupportedJsonFormat(fileName: String, leftContext: String): Boolean =
113+
JsonConfigFileNamingConvention.contains(fileName.lowercase()) ||
114+
AWSTemplateKeyWordsRegex.containsMatchIn(leftContext) ||
115+
AWSTemplateCaseInsensitiveKeyWordsRegex.containsMatchIn(leftContext.lowercase(Locale.getDefault()))
119116

120117
/**
121118
* 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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ 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
5958
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.getCaretPosition
59+
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil.isSupportedJsonFormat
6060
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.CodeWhispererExplorerActionManager
6161
import software.aws.toolkits.jetbrains.services.codewhisperer.explorer.isCodeWhispererEnabled
62+
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererJson
6263
import software.aws.toolkits.jetbrains.services.codewhisperer.model.CaretPosition
6364
import software.aws.toolkits.jetbrains.services.codewhisperer.model.DetailContext
6465
import software.aws.toolkits.jetbrains.services.codewhisperer.model.FileContextInfo
@@ -171,7 +172,13 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
171172

172173
val language = requestContext.fileContextInfo.programmingLanguage
173174
val leftContext = requestContext.fileContextInfo.caretContext.leftFileContext
174-
if (!language.isCodeCompletionSupported() || (checkLeftContextKeywordsForJsonAndYaml(leftContext, language.languageId))) {
175+
if (!language.isCodeCompletionSupported() || (
176+
language is CodeWhispererJson && !isSupportedJsonFormat(
177+
requestContext.fileContextInfo.filename,
178+
leftContext
179+
)
180+
)
181+
) {
175182
LOG.debug { "Programming language $language is not supported by CodeWhisperer" }
176183
if (triggerTypeInfo.triggerType == CodewhispererTriggerType.OnDemand) {
177184
showCodeWhispererInfoHint(

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ object CodeWhispererConstants {
3636
val AWSTemplateKeyWordsRegex = Regex("(AWSTemplateFormatVersion|Resources|AWS::|Description)")
3737
val AWSTemplateCaseInsensitiveKeyWordsRegex = Regex("(cloudformation|cfn|template|description)")
3838

39+
val JsonConfigFileNamingConvention = setOf(
40+
"app.json",
41+
"appsettings.json",
42+
"bower.json",
43+
"composer.json",
44+
"db.json",
45+
"manifest.json",
46+
"package.json",
47+
"schema.json",
48+
"settings.json",
49+
"tsconfig.json",
50+
"vcpkg.json"
51+
)
52+
3953
// TODO: this is currently set to 2050 to account for the server side 0.5 TPS and and extra 50 ms buffer to
4054
// avoid ThrottlingException as much as possible.
4155
const val INVOCATION_INTERVAL: Long = 2050
@@ -131,6 +145,7 @@ object CodeWhispererConstants {
131145
}
132146
}
133147
}
148+
134149
object CrossFile {
135150
const val CHUNK_SIZE = 60
136151
const val NUMBER_OF_LINE_IN_CHUNK = 50
@@ -145,7 +160,7 @@ object CodeWhispererConstants {
145160
object TryExampleFileContent {
146161

147162
private const val AUTO_TRIGGER_CONTENT_JAVA =
148-
"""import java.util.ArrayList;
163+
"""import java.util.ArrayList;
149164
import java.util.HashMap;
150165
import java.util.List;
151166
import java.util.Map;
@@ -167,7 +182,7 @@ public class Main {
167182
}"""
168183

169184
private const val MANUAL_TRIGGER_CONTENT_JAVA =
170-
"""// TODO: Press either Option + C on MacOS or Alt + C on Windows on a new line.
185+
"""// TODO: Press either Option + C on MacOS or Alt + C on Windows on a new line.
171186
172187
public class S3Uploader {
173188
@@ -178,7 +193,7 @@ public class S3Uploader {
178193
}"""
179194

180195
private const val UNIT_TEST_CONTENT_JAVA =
181-
"""// TODO: Ask Amazon Q to write unit tests.
196+
"""// TODO: Ask Amazon Q to write unit tests.
182197
183198
// Write a test case for the sum function.
184199

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import org.assertj.core.api.Assertions.assertThat
1111
import org.junit.Before
1212
import org.junit.Rule
1313
import org.junit.Test
14+
import org.junit.jupiter.params.ParameterizedTest
15+
import org.junit.jupiter.params.provider.ValueSource
1416
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.leftContext_success_Iac
1517
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonFileName
1618
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.pythonTestLeftContext
17-
import software.aws.toolkits.jetbrains.services.codewhisperer.CodeWhispererTestUtil.yaml_langauge
1819
import software.aws.toolkits.jetbrains.services.codewhisperer.editor.CodeWhispererEditorUtil
1920
import software.aws.toolkits.jetbrains.services.codewhisperer.language.languages.CodeWhispererPython
2021
import software.aws.toolkits.jetbrains.utils.rules.PythonCodeInsightTestFixtureRule
@@ -76,12 +77,36 @@ class CodeWhispererEditorUtilTest {
7677
assertThat(caretContext.leftContextOnCurrentLine).isEqualTo(pythonTestLeftContext)
7778
}
7879

79-
/**
80-
* Test for keyword checks for json and yaml
81-
*/
8280
@Test
83-
fun `test for keyword check for json and yaml`() {
84-
val result = CodeWhispererEditorUtil.checkLeftContextKeywordsForJsonAndYaml(leftContext_success_Iac, yaml_langauge)
81+
fun `test for keyword check for json`() {
82+
val result = CodeWhispererEditorUtil.isSupportedJsonFormat("foo.json", leftContext_success_Iac)
83+
assertThat(result).isEqualTo(true)
84+
}
85+
86+
@ParameterizedTest
87+
@ValueSource(
88+
strings = [
89+
"app.json",
90+
"appsettings.json",
91+
"bower.json",
92+
"composer.json",
93+
"db.json",
94+
"manifest.json",
95+
"package.json",
96+
"schema.json",
97+
"settings.json",
98+
"tsconfig.json",
99+
"vcpkg.json"
100+
]
101+
)
102+
fun `isSupportedJsonFormat should return true by file name`(fileName: String) {
103+
val result = CodeWhispererEditorUtil.isSupportedJsonFormat(fileName, "")
104+
assertThat(result).isEqualTo(true)
105+
}
106+
107+
@Test
108+
fun `isSupportedJsonFormat should return false due to no match`() {
109+
val result = CodeWhispererEditorUtil.isSupportedJsonFormat("foo.json", "")
85110
assertThat(result).isEqualTo(false)
86111
}
87112
}

0 commit comments

Comments
 (0)