Skip to content

Commit e7962f3

Browse files
authored
feat(amzonq): inline suggestion more json/yaml files aws#5733
YAML files - Provide completions for all YAML files. - Remove any current CloudFormation-only restrictions for YAML JSON files - Provide completions for a JSON file if the filename matches one of the below names or the file content matches the CloudFormation regexp. ``` app.json appsettings.json bower.json composer.json db.json manifest.json package.json schema.json settings.json tsconfig.json vcpkg.json ```
1 parent 4cecf55 commit e7962f3

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
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": "Inline completion for more json files, and all yaml files"
4+
}

packages/amazonq/test/unit/codewhisperer/util/commonUtil.test.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55

66
import assert from 'assert'
7-
import { checkLeftContextKeywordsForJsonAndYaml, getPrefixSuffixOverlap } from 'aws-core-vscode/codewhisperer'
7+
import {
8+
JsonConfigFileNamingConvention,
9+
checkLeftContextKeywordsForJson,
10+
getPrefixSuffixOverlap,
11+
} from 'aws-core-vscode/codewhisperer'
812

913
describe('commonUtil', function () {
1014
describe('getPrefixSuffixOverlap', function () {
@@ -31,29 +35,47 @@ describe('commonUtil', function () {
3135
})
3236
})
3337

34-
describe('checkLeftContextKeywordsForJsonAndYaml', function () {
38+
describe('checkLeftContextKeywordsForJson', function () {
3539
it('Should return true for valid left context keywords', async function () {
3640
assert.strictEqual(
37-
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer', 'json'),
38-
true
39-
)
40-
assert.strictEqual(
41-
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer', 'yaml'),
41+
checkLeftContextKeywordsForJson('foo.json', 'Create an S3 Bucket named CodeWhisperer', 'json'),
4242
true
4343
)
4444
})
4545
it('Should return false for invalid left context keywords', async function () {
4646
assert.strictEqual(
47-
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer in cfn', 'yaml'),
48-
false
49-
)
50-
assert.strictEqual(
51-
checkLeftContextKeywordsForJsonAndYaml(
47+
checkLeftContextKeywordsForJson(
48+
'foo.json',
5249
'Create an S3 Bucket named CodeWhisperer in Cloudformation',
5350
'json'
5451
),
5552
false
5653
)
5754
})
55+
56+
for (const jsonConfigFile of JsonConfigFileNamingConvention) {
57+
it(`should evalute by filename ${jsonConfigFile}`, function () {
58+
assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile, 'foo', 'json'), false)
59+
60+
assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile.toUpperCase(), 'bar', 'json'), false)
61+
62+
assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile.toUpperCase(), 'baz', 'json'), false)
63+
})
64+
65+
const upperCaseFilename = jsonConfigFile.toUpperCase()
66+
it(`should evalute by filename and case insensitive ${upperCaseFilename}`, function () {
67+
assert.strictEqual(checkLeftContextKeywordsForJson(upperCaseFilename, 'foo', 'json'), false)
68+
69+
assert.strictEqual(
70+
checkLeftContextKeywordsForJson(upperCaseFilename.toUpperCase(), 'bar', 'json'),
71+
false
72+
)
73+
74+
assert.strictEqual(
75+
checkLeftContextKeywordsForJson(upperCaseFilename.toUpperCase(), 'baz', 'json'),
76+
false
77+
)
78+
})
79+
}
5880
})
5981
})

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ export const AWSTemplateKeyWords = ['AWSTemplateFormatVersion', 'Resources', 'AW
2222

2323
export const AWSTemplateCaseInsensitiveKeyWords = ['cloudformation', 'cfn', 'template', 'description']
2424

25+
export const JsonConfigFileNamingConvention = new Set([
26+
'app.json',
27+
'appsettings.json',
28+
'bower.json',
29+
'composer.json',
30+
'db.json',
31+
'manifest.json',
32+
'package.json',
33+
'schema.json',
34+
'settings.json',
35+
'tsconfig.json',
36+
'vcpkg.json',
37+
])
38+
2539
export const normalTextChangeRegex = /[A-Za-z0-9]/g
2640

2741
export const autoSuggestionConfig = {

packages/core/src/codewhisperer/util/commonUtil.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import * as vscode from 'vscode'
77
import * as semver from 'semver'
88
import { isCloud9 } from '../../shared/extensionUtilities'
99
import { getInlineSuggestEnabled } from '../../shared/utilities/editorUtilities'
10-
import { AWSTemplateCaseInsensitiveKeyWords, AWSTemplateKeyWords } from '../models/constants'
10+
import {
11+
AWSTemplateCaseInsensitiveKeyWords,
12+
AWSTemplateKeyWords,
13+
JsonConfigFileNamingConvention,
14+
} from '../models/constants'
1115

1216
export function getLocalDatetime() {
1317
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
@@ -61,11 +65,12 @@ export function getPrefixSuffixOverlap(firstString: string, secondString: string
6165
return secondString.slice(0, i)
6266
}
6367

64-
export function checkLeftContextKeywordsForJsonAndYaml(leftFileContent: string, language: string): boolean {
68+
export function checkLeftContextKeywordsForJson(fileName: string, leftFileContent: string, language: string): boolean {
6569
if (
66-
(language === 'json' || language === 'yaml') &&
70+
language === 'json' &&
6771
!AWSTemplateKeyWords.some((substring) => leftFileContent.includes(substring)) &&
68-
!AWSTemplateCaseInsensitiveKeyWords.some((substring) => leftFileContent.toLowerCase().includes(substring))
72+
!AWSTemplateCaseInsensitiveKeyWords.some((substring) => leftFileContent.toLowerCase().includes(substring)) &&
73+
!JsonConfigFileNamingConvention.has(fileName.toLowerCase())
6974
) {
7075
return true
7176
}

packages/core/src/codewhisperer/util/editorContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { fetchSupplementalContext } from './supplementalContext/supplementalCont
1414
import { supplementalContextTimeoutInMs } from '../models/constants'
1515
import { getSelectedCustomization } from './customizationUtil'
1616
import { selectFrom } from '../../shared/utilities/tsUtils'
17-
import { checkLeftContextKeywordsForJsonAndYaml } from './commonUtil'
17+
import { checkLeftContextKeywordsForJson } from './commonUtil'
1818
import { CodeWhispererSupplementalContext } from '../models/model'
1919
import { getOptOutPreference } from '../../shared/telemetry/util'
2020

@@ -39,7 +39,7 @@ export function extractContextForCodeWhisperer(editor: vscode.TextEditor): codew
3939
)
4040
)
4141
let languageName = 'plaintext'
42-
if (!checkLeftContextKeywordsForJsonAndYaml(caretLeftFileContext, editor.document.languageId)) {
42+
if (!checkLeftContextKeywordsForJson(document.fileName, caretLeftFileContext, editor.document.languageId)) {
4343
languageName =
4444
runtimeLanguageContext.normalizeLanguage(editor.document.languageId) ?? editor.document.languageId
4545
}

0 commit comments

Comments
 (0)