Skip to content

Commit

Permalink
Moving the extractCodeBlockLanguage function to shared/markdown.ts an…
Browse files Browse the repository at this point in the history
…d added test cases for this function
  • Loading branch information
laileni-aws committed Oct 15, 2024
1 parent b375876 commit 483591b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { CodeScanIssue } from '../../../../codewhisperer/models/model'
import { marked } from 'marked'
import { JSDOM } from 'jsdom'
import { LspController } from '../../../../amazonq/lsp/lspController'
import { extractCodeBlockLanguage } from '../../../../shared/markdown'

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

Expand Down Expand Up @@ -185,7 +186,7 @@ export class Messenger {
) {
message += chatEvent.assistantResponseEvent.content
if (codeBlockLanguage === 'plaintext') {
codeBlockLanguage = this.extractCodeBlockLanguage(message)
codeBlockLanguage = extractCodeBlockLanguage(message)
}
this.dispatcher.sendChatMessage(
new ChatMessage(
Expand Down Expand Up @@ -342,24 +343,6 @@ export class Messenger {
})
}

private extractCodeBlockLanguage(message: string): string {
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
const codeBlockStart = message.indexOf('```')
if (codeBlockStart === -1) {
return 'plaintext'
}

const languageStart = codeBlockStart + 3
const languageEnd = message.indexOf('\n', languageStart)

if (languageEnd === -1) {
return 'plaintext'
}

const language = message.substring(languageStart, languageEnd).trim()
return language !== '' ? language : 'plaintext'
}

public sendErrorMessage(errorMessage: string | undefined, tabID: string, requestID: string | undefined) {
this.showChatExceptionMessage(
{
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/shared/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

export const extractCodeBlockLanguage = (message: string) => {
// This fulfills both the cases of unit test generation(java, python) and general use case(Non java and Non python) languages.
const codeBlockStart = message.indexOf('```')
if (codeBlockStart === -1) {
return 'plaintext'
}

const languageStart = codeBlockStart + 3
const languageEnd = message.indexOf('\n', languageStart)

if (languageEnd === -1) {
return 'plaintext'
}

const language = message.substring(languageStart, languageEnd).trim()
return language !== '' ? language : 'plaintext'
}
38 changes: 38 additions & 0 deletions packages/core/src/test/shared/markdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import assert from 'assert'
import { extractCodeBlockLanguage } from '../../shared/markdown'

describe('extractCodeBlockLanguage', () => {
it('should return "plaintext" when no code block is present', () => {
const message = 'This is a message without a code block'
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
})

it('should return the language when a code block with language is present', () => {
const message = 'Here is some code:\n```javascript\nconsole.log("Hello");\n```'
assert.strictEqual(extractCodeBlockLanguage(message), 'javascript')
})

it('should return "plaintext" when a code block is present but no language is specified', () => {
const message = 'Here is some code:\n```\nconsole.log("Hello");\n```'
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
})

it('should handle whitespace before the language specification', () => {
const message = 'Code:\n``` typescript\nconst x: number = 5;\n```'
assert.strictEqual(extractCodeBlockLanguage(message), 'typescript')
})

it('should return "plaintext" when the code block is not closed', () => {
const message = 'Incomplete code block:\n```javascript\nconsole.log("Hello");'
assert.strictEqual(extractCodeBlockLanguage(message), 'plaintext')
})

it('should handle empty messages', () => {
assert.strictEqual(extractCodeBlockLanguage(''), 'plaintext')
})
})

0 comments on commit 483591b

Please sign in to comment.