Skip to content

Commit a6625ea

Browse files
committed
Move language-to-comment mapping to runtimeLanguageContext
1 parent 671314c commit a6625ea

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
@@ -329,6 +329,40 @@ describe('runtimeLanguageContext', function () {
329329
}
330330
})
331331

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

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

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

2525
let tabSize: number = getTabSizeSetting()
2626

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

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

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)