Skip to content

Commit 4665309

Browse files
committed
use truncate util
1 parent b1055c1 commit 4665309

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as codewhispererClient from '../client/codewhisperer'
88
import * as path from 'path'
99
import * as CodeWhispererConstants from '../models/constants'
1010
import { getTabSizeSetting } from '../../shared/utilities/editorUtilities'
11+
import { truncate } from '../../shared/utilities/textUtilities'
1112
import { getLogger } from '../../shared/logger/logger'
1213
import { runtimeLanguageContext } from './runtimeLanguageContext'
1314
import { fetchSupplementalContext } from './supplementalContext/supplementalContextUtil'
@@ -223,10 +224,13 @@ export function getEditorState(editor: vscode.TextEditor, fileContext: codewhisp
223224
// Truncate if document content is too large (defined in constants.ts)
224225
let fileText = documentText
225226
if (documentText.length > editorStateMaxLength) {
226-
const startOffset = Math.max(0, cursorOffset - Math.floor(editorStateMaxLength / 2))
227-
const endOffset = Math.min(documentText.length, cursorOffset + Math.floor(editorStateMaxLength / 2))
227+
const halfLength = Math.floor(editorStateMaxLength / 2)
228228

229-
fileText = documentText.substring(startOffset, endOffset)
229+
// Use truncate function to get the text around the cursor position
230+
const leftPart = truncate(documentText.substring(0, cursorOffset), -halfLength, '')
231+
const rightPart = truncate(documentText.substring(cursorOffset), halfLength, '')
232+
233+
fileText = leftPart + rightPart
230234
}
231235

232236
return {
@@ -291,8 +295,8 @@ function logSupplementalContext(supplementalContext: CodeWhispererSupplementalCo
291295
logString += indent(`\nChunk ${index}:\n`, 4, true)
292296
logString += indent(
293297
`Path: ${context.filePath}
294-
Length: ${context.content.length}
295-
Score: ${context.score}`,
298+
Length: ${context.content.length}
299+
Score: ${context.score}`,
296300
8,
297301
true
298302
)

0 commit comments

Comments
 (0)