Skip to content

Commit f9bf823

Browse files
Merge master into feature/LSP-gamma
2 parents 1e426c5 + 734bd98 commit f9bf823

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Avoid inline completion 'Improperly formed request' errors when file is too large"
4+
}

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export const lineBreakWin = '\r\n'
8787
export const supplementalContextTimeoutInMs = 100
8888

8989
export const supplementalContextMaxTotalLength = 20480
90+
91+
export const editorStateMaxLength = 40000
92+
9093
/**
9194
* Ux of recommendations
9295
*/

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ 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'
14-
import { supplementalContextTimeoutInMs } from '../models/constants'
15+
import { editorStateMaxLength, supplementalContextTimeoutInMs } from '../models/constants'
1516
import { getSelectedCustomization } from './customizationUtil'
1617
import { selectFrom } from '../../shared/utilities/tsUtils'
1718
import { checkLeftContextKeywordsForJson } from './commonUtil'
@@ -216,13 +217,29 @@ export function getTabSize(): number {
216217

217218
export function getEditorState(editor: vscode.TextEditor, fileContext: codewhispererClient.FileContext): any {
218219
try {
220+
const cursorPosition = editor.selection.active
221+
const cursorOffset = editor.document.offsetAt(cursorPosition)
222+
const documentText = editor.document.getText()
223+
224+
// Truncate if document content is too large (defined in constants.ts)
225+
let fileText = documentText
226+
if (documentText.length > editorStateMaxLength) {
227+
const halfLength = Math.floor(editorStateMaxLength / 2)
228+
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
234+
}
235+
219236
return {
220237
document: {
221238
programmingLanguage: {
222239
languageName: fileContext.programmingLanguage.languageName,
223240
},
224241
relativeFilePath: fileContext.filename,
225-
text: editor.document.getText(),
242+
text: fileText,
226243
},
227244
cursorState: {
228245
position: {

0 commit comments

Comments
 (0)