Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Feature",
"description": "Inline completion for more json files, and all yaml files"
}
46 changes: 34 additions & 12 deletions packages/amazonq/test/unit/codewhisperer/util/commonUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
*/

import assert from 'assert'
import { checkLeftContextKeywordsForJsonAndYaml, getPrefixSuffixOverlap } from 'aws-core-vscode/codewhisperer'
import {
JsonConfigFileNamingConvention,
checkLeftContextKeywordsForJson,
getPrefixSuffixOverlap,
} from 'aws-core-vscode/codewhisperer'

describe('commonUtil', function () {
describe('getPrefixSuffixOverlap', function () {
Expand All @@ -31,29 +35,47 @@ describe('commonUtil', function () {
})
})

describe('checkLeftContextKeywordsForJsonAndYaml', function () {
describe('checkLeftContextKeywordsForJson', function () {
it('Should return true for valid left context keywords', async function () {
assert.strictEqual(
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer', 'json'),
true
)
assert.strictEqual(
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer', 'yaml'),
checkLeftContextKeywordsForJson('foo.json', 'Create an S3 Bucket named CodeWhisperer', 'json'),
true
)
})
it('Should return false for invalid left context keywords', async function () {
assert.strictEqual(
checkLeftContextKeywordsForJsonAndYaml('Create an S3 Bucket named CodeWhisperer in cfn', 'yaml'),
false
)
assert.strictEqual(
checkLeftContextKeywordsForJsonAndYaml(
checkLeftContextKeywordsForJson(
'foo.json',
'Create an S3 Bucket named CodeWhisperer in Cloudformation',
'json'
),
false
)
})

for (const jsonConfigFile of JsonConfigFileNamingConvention) {
it(`should evalute by filename ${jsonConfigFile}`, function () {
assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile, 'foo', 'json'), false)

assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile.toUpperCase(), 'bar', 'json'), false)

assert.strictEqual(checkLeftContextKeywordsForJson(jsonConfigFile.toUpperCase(), 'baz', 'json'), false)
})

const upperCaseFilename = jsonConfigFile.toUpperCase()
it(`should evalute by filename and case insensitive ${upperCaseFilename}`, function () {
assert.strictEqual(checkLeftContextKeywordsForJson(upperCaseFilename, 'foo', 'json'), false)

assert.strictEqual(
checkLeftContextKeywordsForJson(upperCaseFilename.toUpperCase(), 'bar', 'json'),
false
)

assert.strictEqual(
checkLeftContextKeywordsForJson(upperCaseFilename.toUpperCase(), 'baz', 'json'),
false
)
})
}
})
})
14 changes: 14 additions & 0 deletions packages/core/src/codewhisperer/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ export const AWSTemplateKeyWords = ['AWSTemplateFormatVersion', 'Resources', 'AW

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

export const JsonConfigFileNamingConvention = new Set([
'app.json',
'appsettings.json',
'bower.json',
'composer.json',
'db.json',
'manifest.json',
'package.json',
'schema.json',
'settings.json',
'tsconfig.json',
'vcpkg.json',
])

export const normalTextChangeRegex = /[A-Za-z0-9]/g

export const autoSuggestionConfig = {
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/codewhisperer/util/commonUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import * as vscode from 'vscode'
import * as semver from 'semver'
import { isCloud9 } from '../../shared/extensionUtilities'
import { getInlineSuggestEnabled } from '../../shared/utilities/editorUtilities'
import { AWSTemplateCaseInsensitiveKeyWords, AWSTemplateKeyWords } from '../models/constants'
import {
AWSTemplateCaseInsensitiveKeyWords,
AWSTemplateKeyWords,
JsonConfigFileNamingConvention,
} from '../models/constants'

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

export function checkLeftContextKeywordsForJsonAndYaml(leftFileContent: string, language: string): boolean {
export function checkLeftContextKeywordsForJson(fileName: string, leftFileContent: string, language: string): boolean {
if (
(language === 'json' || language === 'yaml') &&
language === 'json' &&
!AWSTemplateKeyWords.some((substring) => leftFileContent.includes(substring)) &&
!AWSTemplateCaseInsensitiveKeyWords.some((substring) => leftFileContent.toLowerCase().includes(substring))
!AWSTemplateCaseInsensitiveKeyWords.some((substring) => leftFileContent.toLowerCase().includes(substring)) &&
!JsonConfigFileNamingConvention.has(fileName.toLowerCase())
) {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/codewhisperer/util/editorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { fetchSupplementalContext } from './supplementalContext/supplementalCont
import { supplementalContextTimeoutInMs } from '../models/constants'
import { getSelectedCustomization } from './customizationUtil'
import { selectFrom } from '../../shared/utilities/tsUtils'
import { checkLeftContextKeywordsForJsonAndYaml } from './commonUtil'
import { checkLeftContextKeywordsForJson } from './commonUtil'
import { CodeWhispererSupplementalContext } from '../models/model'
import { getOptOutPreference } from '../../shared/telemetry/util'

Expand All @@ -39,7 +39,7 @@ export function extractContextForCodeWhisperer(editor: vscode.TextEditor): codew
)
)
let languageName = 'plaintext'
if (!checkLeftContextKeywordsForJsonAndYaml(caretLeftFileContext, editor.document.languageId)) {
if (!checkLeftContextKeywordsForJson(document.fileName, caretLeftFileContext, editor.document.languageId)) {
languageName =
runtimeLanguageContext.normalizeLanguage(editor.document.languageId) ?? editor.document.languageId
}
Expand Down
Loading