Skip to content

Commit 483591b

Browse files
committed
Moving the extractCodeBlockLanguage function to shared/markdown.ts and added test cases for this function
1 parent b375876 commit 483591b

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

packages/core/src/codewhispererChat/controllers/chat/messenger/messenger.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { CodeScanIssue } from '../../../../codewhisperer/models/model'
3333
import { marked } from 'marked'
3434
import { JSDOM } from 'jsdom'
3535
import { LspController } from '../../../../amazonq/lsp/lspController'
36+
import { extractCodeBlockLanguage } from '../../../../shared/markdown'
3637

3738
export type StaticTextResponseType = 'quick-action-help' | 'onboarding-help' | 'transform' | 'help'
3839

@@ -185,7 +186,7 @@ export class Messenger {
185186
) {
186187
message += chatEvent.assistantResponseEvent.content
187188
if (codeBlockLanguage === 'plaintext') {
188-
codeBlockLanguage = this.extractCodeBlockLanguage(message)
189+
codeBlockLanguage = extractCodeBlockLanguage(message)
189190
}
190191
this.dispatcher.sendChatMessage(
191192
new ChatMessage(
@@ -342,24 +343,6 @@ export class Messenger {
342343
})
343344
}
344345

345-
private extractCodeBlockLanguage(message: string): string {
346-
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
347-
const codeBlockStart = message.indexOf('```')
348-
if (codeBlockStart === -1) {
349-
return 'plaintext'
350-
}
351-
352-
const languageStart = codeBlockStart + 3
353-
const languageEnd = message.indexOf('\n', languageStart)
354-
355-
if (languageEnd === -1) {
356-
return 'plaintext'
357-
}
358-
359-
const language = message.substring(languageStart, languageEnd).trim()
360-
return language !== '' ? language : 'plaintext'
361-
}
362-
363346
public sendErrorMessage(errorMessage: string | undefined, tabID: string, requestID: string | undefined) {
364347
this.showChatExceptionMessage(
365348
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
export const extractCodeBlockLanguage = (message: string) => {
7+
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
8+
const codeBlockStart = message.indexOf('```')
9+
if (codeBlockStart === -1) {
10+
return 'plaintext'
11+
}
12+
13+
const languageStart = codeBlockStart + 3
14+
const languageEnd = message.indexOf('\n', languageStart)
15+
16+
if (languageEnd === -1) {
17+
return 'plaintext'
18+
}
19+
20+
const language = message.substring(languageStart, languageEnd).trim()
21+
return language !== '' ? language : 'plaintext'
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from 'assert'
7+
import { extractCodeBlockLanguage } from '../../shared/markdown'
8+
9+
describe('extractCodeBlockLanguage', () => {
10+
it('should return "plaintext" when no code block is present', () => {
11+
const message = 'This is a message without a code block'
12+
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
13+
})
14+
15+
it('should return the language when a code block with language is present', () => {
16+
const message = 'Here is some code:\n```javascript\nconsole.log("Hello");\n```'
17+
assert.strictEqual(extractCodeBlockLanguage(message), 'javascript')
18+
})
19+
20+
it('should return "plaintext" when a code block is present but no language is specified', () => {
21+
const message = 'Here is some code:\n```\nconsole.log("Hello");\n```'
22+
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
23+
})
24+
25+
it('should handle whitespace before the language specification', () => {
26+
const message = 'Code:\n``` typescript\nconst x: number = 5;\n```'
27+
assert.strictEqual(extractCodeBlockLanguage(message), 'typescript')
28+
})
29+
30+
it('should return "plaintext" when the code block is not closed', () => {
31+
const message = 'Incomplete code block:\n```javascript\nconsole.log("Hello");'
32+
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
33+
})
34+
35+
it('should handle empty messages', () => {
36+
assert.strictEqual(extractCodeBlockLanguage(''), 'plaintext')
37+
})
38+
})

0 commit comments

Comments
 (0)