Skip to content

Commit 16e8a77

Browse files
authored
fix(amazonq): cannot auto trigger Q inline suggestion on systemVerilog files #5990
## Problem within inline suggestion code path, `isLanguageSupported()` relies on `vscode.editor.document.langaugeId` to determine whether the given language is supported Q inline suggestion functionality. However, it's possible that some languages are not recognized by VSCode IDE itself without 3rd party extensions, for example: VSCode doesn't recognize `.sv`, `svh`, `vh` files which is bound to SystemVerilog language if users do not have the 3rd party extension installed and enabled, in this case, `vscode.editor.document.languageId` will simply return `plaintext`. This is the root cause why autotrigger doesn't work for systemverilog files, because at the beginning, we don't even register keyStrokeHandler for files with `.sv`, `svh`, `vh` files. ## Solution if language check return false, we should also check file extension
1 parent f98622b commit 16e8a77

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Fix broken inline suggestion auto-trigger on Systemverfilog files if users dont have systemverilog extension installed and enabled"
4+
}

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import assert from 'assert'
7-
import { resetCodeWhispererGlobalVariables } from 'aws-core-vscode/test'
7+
import { resetCodeWhispererGlobalVariables, toTextDocument } from 'aws-core-vscode/test'
88
import { runtimeLanguageContext, RuntimeLanguageContext, PlatformLanguageId } from 'aws-core-vscode/codewhisperer'
99
import * as codewhispererClient from 'aws-core-vscode/codewhisperer'
1010
import { CodewhispererLanguage } from 'aws-core-vscode/shared'
@@ -61,6 +61,61 @@ describe('runtimeLanguageContext', function () {
6161
assert.strictEqual(actual, expected)
6262
})
6363
})
64+
65+
describe('test isLanguageSupported with document as the argument', function () {
66+
const cases: [string, boolean][] = [
67+
['helloJava.java', true],
68+
['helloPython.py', true],
69+
['helloJavascript.js', true],
70+
['helloJsx.jsx', true],
71+
['helloTypescript.ts', true],
72+
['helloTsx.tsx', true],
73+
['helloCsharp.cs', true],
74+
['helloC.c', true],
75+
['helloC.h', true],
76+
['helloCpp.cpp', true],
77+
['helloCpp.cc', true],
78+
['helloGo.go', true],
79+
['helloKotlin.kt', true],
80+
['helloPhp.php', true],
81+
['helloRuby.rb', true],
82+
['helloRust.rs', true],
83+
['helloScala.scala', true],
84+
['helloShellscript.sh', true],
85+
['helloSql.sql', true],
86+
['helloSystemVerilog.svh', true],
87+
['helloSystemVerilog.sv', true],
88+
['helloSystemVerilog.vh', true],
89+
['helloDart.dart', true],
90+
['helloLua.lua', true],
91+
['helloLua.wlua', true],
92+
['helloSwift.swift', true],
93+
['helloVue.vue', true],
94+
['helloPowerShell.ps1', true],
95+
['helloPowerShell.psm1', true],
96+
['helloR.r', true],
97+
['helloJson.json', true],
98+
['helloYaml.yaml', true],
99+
['helloYaml.yml', true],
100+
['helloTf.tf', true],
101+
['helloPlaintext.txt', false],
102+
['helloHtml.html', false],
103+
['helloCss.css', false],
104+
['helloUnknown', false],
105+
['helloFoo.foo', false],
106+
]
107+
108+
cases.forEach((tuple) => {
109+
const fileName = tuple[0]
110+
const expected = tuple[1]
111+
112+
it(`pass document ${fileName} as argument should first try determine by languageId then file extensions`, async function () {
113+
const doc = await toTextDocument('', fileName)
114+
const actual = languageContext.isLanguageSupported(doc)
115+
assert.strictEqual(actual, expected)
116+
})
117+
})
118+
})
64119
})
65120

66121
describe('test getLanguageContext', function () {

packages/core/src/codewhisperer/activation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ export async function activate(context: ExtContext): Promise<void> {
478478
if (e.document !== editor.document) {
479479
return
480480
}
481-
if (!runtimeLanguageContext.isLanguageSupported(e.document.languageId)) {
481+
if (!runtimeLanguageContext.isLanguageSupported(e.document)) {
482482
return
483483
}
484484

@@ -549,7 +549,7 @@ export async function activate(context: ExtContext): Promise<void> {
549549
if (e.document !== editor.document) {
550550
return
551551
}
552-
if (!runtimeLanguageContext.isLanguageSupported(e.document.languageId)) {
552+
if (!runtimeLanguageContext.isLanguageSupported(e.document)) {
553553
return
554554
}
555555
/**

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import * as vscode from 'vscode'
67
import { getLogger } from '../../shared/logger/logger'
78
import { CodewhispererLanguage } from '../../shared/telemetry/telemetry.gen'
89
import { createConstantMap, ConstantMap } from '../../shared/utilities/tsUtils'
910
import * as codewhispererClient from '../client/codewhisperer'
1011
import * as CodeWhispererConstants from '../models/constants'
12+
import * as path from 'path'
1113

1214
type RuntimeLanguage = Exclude<CodewhispererLanguage, 'jsx' | 'tsx' | 'systemVerilog'> | 'systemverilog'
1315

@@ -106,7 +108,10 @@ export class RuntimeLanguageContext {
106108
})
107109
this.supportedLanguageExtensionMap = createConstantMap<string, CodewhispererLanguage>({
108110
c: 'c',
111+
h: 'c',
109112
cpp: 'cpp',
113+
cc: 'cpp',
114+
'c++': 'cpp',
110115
cs: 'csharp',
111116
go: 'go',
112117
hcl: 'tf',
@@ -251,24 +256,24 @@ export class RuntimeLanguageContext {
251256
}
252257
}
253258

254-
/**
255-
*
256-
* @param languageId: either vscodeLanguageId or CodewhispererLanguage
257-
* @returns true if the language is supported by CodeWhisperer otherwise false
258-
*/
259-
public isLanguageSupported(languageId: string): boolean {
260-
const lang = this.normalizeLanguage(languageId)
261-
switch (lang) {
262-
case undefined:
263-
return false
259+
public isLanguageSupported(languageId: string): boolean
260+
public isLanguageSupported(doc: vscode.TextDocument): boolean
261+
public isLanguageSupported(arg: string | vscode.TextDocument): boolean {
262+
if (typeof arg === 'string') {
263+
const normalizedLanguageId = this.normalizeLanguage(arg)
264+
const byLanguageId = !normalizedLanguageId || normalizedLanguageId === 'plaintext' ? false : true
264265

265-
case 'plaintext':
266-
return false
266+
return byLanguageId
267+
} else {
268+
const normalizedLanguageId = this.normalizeLanguage(arg.languageId)
269+
const byLanguageId = !normalizedLanguageId || normalizedLanguageId === 'plaintext' ? false : true
270+
const extension = path.extname(arg.uri.fsPath)
271+
const byFileExtension = this.isFileFormatSupported(extension.substring(1))
267272

268-
default:
269-
return true
273+
return byLanguageId || byFileExtension
270274
}
271275
}
276+
272277
/**
273278
*
274279
* @param fileFormat : vscode editor filecontext filename extension

packages/core/src/codewhisperer/views/lineAnnotationController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export class LineAnnotationController implements vscode.Disposable {
430430
}
431431

432432
// Disable Tips when language is not supported by Amazon Q.
433-
if (!runtimeLanguageContext.isLanguageSupported(editor.document.languageId)) {
433+
if (!runtimeLanguageContext.isLanguageSupported(editor.document)) {
434434
return
435435
}
436436

0 commit comments

Comments
 (0)