Skip to content

Commit dbfe86b

Browse files
committed
Move language-to-comment mapping to runtimeLanguageContext
1 parent 98a36dc commit dbfe86b

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,40 @@ describe('runtimeLanguageContext', function () {
333333
}
334334
})
335335

336+
describe('getSingleLineCommentPrefix', function () {
337+
it('should return the correct comment prefix for supported languages', function () {
338+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('java'), '// ')
339+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('javascript'), '// ')
340+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('jsonc'), '// ')
341+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('kotlin'), '// ')
342+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('lua'), '-- ')
343+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('python'), '# ')
344+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('ruby'), '# ')
345+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('sql'), '-- ')
346+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('tf'), '# ')
347+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('typescript'), '// ')
348+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('vue'), '')
349+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('yaml'), '# ')
350+
})
351+
352+
it('should normalize language ID before getting comment prefix', function () {
353+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('hcl'), '# ')
354+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('javascriptreact'), '// ')
355+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('shellscript'), '# ')
356+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('typescriptreact'), '// ')
357+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('yml'), '# ')
358+
})
359+
360+
it('should return empty string for unsupported languages', function () {
361+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('nonexistent'), '')
362+
assert.strictEqual(languageContext.getSingleLineCommentPrefix(undefined), '')
363+
})
364+
365+
it('should return empty string for plaintext', function () {
366+
assert.strictEqual(languageContext.getSingleLineCommentPrefix('plaintext'), '')
367+
})
368+
})
369+
336370
// for now we will only jsx mapped to javascript, tsx mapped to typescript, all other language should remain the same
337371
describe('test covertCwsprRequest', function () {
338372
const leftFileContent = 'left'

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ import { predictionTracker } from '../nextEditPrediction/activation'
2525

2626
let tabSize: number = getTabSizeSetting()
2727

28-
const languageCommentChars: Record<string, string> = {
29-
python: '# ',
30-
java: '// ',
31-
}
32-
3328
function getEnclosingNotebook(editor: vscode.TextEditor): vscode.NotebookDocument | undefined {
3429
// For notebook cells, find the existing notebook with a cell that matches the current editor.
3530
return vscode.workspace.notebookDocuments.find(
@@ -77,14 +72,14 @@ export function getNotebookContext(
7772

7873
export function getNotebookCellContext(cell: vscode.NotebookCell, referenceLanguage?: string): string {
7974
// Extract the text verbatim if the cell is code and the cell has the same language.
80-
// Otherwise, add the correct comment string for the refeference language
75+
// Otherwise, add the correct comment string for the reference language
8176
const cellText = cell.document.getText()
8277
if (
8378
cell.kind === vscode.NotebookCellKind.Markup ||
8479
(runtimeLanguageContext.normalizeLanguage(cell.document.languageId) ?? cell.document.languageId) !==
8580
referenceLanguage
8681
) {
87-
const commentPrefix = (referenceLanguage && languageCommentChars[referenceLanguage]) ?? ''
82+
const commentPrefix = runtimeLanguageContext.getSingleLineCommentPrefix(referenceLanguage)
8883
if (commentPrefix === '') {
8984
return cellText
9085
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ export class RuntimeLanguageContext {
5858
*/
5959
private supportedLanguageExtensionMap: ConstantMap<string, CodewhispererLanguage>
6060

61+
/**
62+
* A map storing single-line comment prefixes for different languages
63+
* Key: CodewhispererLanguage
64+
* Value: Comment prefix string
65+
*/
66+
private languageSingleLineCommentPrefixMap: ConstantMap<CodewhispererLanguage, string>
67+
6168
constructor() {
6269
this.supportedLanguageMap = createConstantMap<
6370
CodeWhispererConstants.PlatformLanguageId | CodewhispererLanguage,
@@ -146,6 +153,39 @@ export class RuntimeLanguageContext {
146153
psm1: 'powershell',
147154
r: 'r',
148155
})
156+
this.languageSingleLineCommentPrefixMap = createConstantMap<CodewhispererLanguage, string>({
157+
c: '// ',
158+
cpp: '// ',
159+
csharp: '// ',
160+
dart: '// ',
161+
go: '// ',
162+
hcl: '# ',
163+
java: '// ',
164+
javascript: '// ',
165+
json: '// ',
166+
jsonc: '// ',
167+
jsx: '// ',
168+
kotlin: '// ',
169+
lua: '-- ',
170+
php: '// ',
171+
plaintext: '',
172+
powershell: '# ',
173+
python: '# ',
174+
r: '# ',
175+
ruby: '# ',
176+
rust: '// ',
177+
scala: '// ',
178+
shell: '# ',
179+
sql: '-- ',
180+
swift: '// ',
181+
systemVerilog: '// ',
182+
tf: '# ',
183+
tsx: '// ',
184+
typescript: '// ',
185+
vue: '', // vue lacks a single-line comment prefix
186+
yaml: '# ',
187+
yml: '# ',
188+
})
149189
}
150190

151191
/**
@@ -159,6 +199,16 @@ export class RuntimeLanguageContext {
159199
return this.supportedLanguageMap.get(languageId)
160200
}
161201

202+
/**
203+
* Get the comment prefix for a given language
204+
* @param language The language to get comment prefix for
205+
* @returns The comment prefix string, or empty string if not found
206+
*/
207+
public getSingleLineCommentPrefix(language?: string): string {
208+
const normalizedLanguage = this.normalizeLanguage(language)
209+
return normalizedLanguage ? (this.languageSingleLineCommentPrefixMap.get(normalizedLanguage) ?? '') : ''
210+
}
211+
162212
/**
163213
* Normalize client side language id to service aware language id (service is not aware of jsx/tsx)
164214
* Only used when invoking CodeWhisperer service API, for client usage please use normalizeLanguage

0 commit comments

Comments
 (0)