Skip to content

Commit db49f5d

Browse files
committed
Complete function rename; refactored notebook logic out of main function.
1 parent 994380e commit db49f5d

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

packages/amazonq/test/unit/codewhisperer/util/editorContext.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ describe('editorContext', function () {
213213
})
214214
})
215215

216-
describe('extractCellsSliceContext', function () {
216+
describe('getNotebookCellsSliceContext', function () {
217217
it('Should extract content from cells in reverse order up to maxLength from prefix cells', function () {
218218
const mockCells = [
219219
createNotebookCell(createMockDocument('First cell content')),
220220
createNotebookCell(createMockDocument('Second cell content')),
221221
createNotebookCell(createMockDocument('Third cell content')),
222222
]
223223

224-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', false)
224+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', false)
225225
assert.strictEqual(result, 'First cell content\nSecond cell content\nThird cell content\n')
226226
})
227227

@@ -232,7 +232,7 @@ describe('editorContext', function () {
232232
createNotebookCell(createMockDocument('Third cell content')),
233233
]
234234

235-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', true)
235+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', true)
236236
assert.strictEqual(result, 'First cell content\nSecond cell content\nThird cell content\n')
237237
})
238238

@@ -244,7 +244,7 @@ describe('editorContext', function () {
244244
createNotebookCell(createMockDocument('Fourth')),
245245
]
246246
// Should only include part of second cell and the last two cells
247-
const result = EditorContext.extractCellsSliceContext(mockCells, 15, 'python', false)
247+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 15, 'python', false)
248248
assert.strictEqual(result, 'd\nThird\nFourth\n')
249249
})
250250

@@ -257,17 +257,17 @@ describe('editorContext', function () {
257257
]
258258

259259
// Should only include first cell and part of second cell
260-
const result = EditorContext.extractCellsSliceContext(mockCells, 15, 'python', true)
260+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 15, 'python', true)
261261
assert.strictEqual(result, 'First\nSecond\nTh')
262262
})
263263

264264
it('Should handle empty cells array from prefix cells', function () {
265-
const result = EditorContext.extractCellsSliceContext([], 100, 'python', false)
265+
const result = EditorContext.getNotebookCellsSliceContext([], 100, 'python', false)
266266
assert.strictEqual(result, '')
267267
})
268268

269269
it('Should handle empty cells array from suffix cells', function () {
270-
const result = EditorContext.extractCellsSliceContext([], 100, 'python', true)
270+
const result = EditorContext.getNotebookCellsSliceContext([], 100, 'python', true)
271271
assert.strictEqual(result, '')
272272
})
273273

@@ -276,7 +276,7 @@ describe('editorContext', function () {
276276
createNotebookCell(createMockDocument('# Heading\nThis is markdown'), vscode.NotebookCellKind.Markup),
277277
createNotebookCell(createMockDocument('def example():\n return "test"')),
278278
]
279-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', false)
279+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', false)
280280
assert.strictEqual(result, '# # Heading\n# This is markdown\ndef example():\n return "test"\n')
281281
})
282282

@@ -286,7 +286,7 @@ describe('editorContext', function () {
286286
createNotebookCell(createMockDocument('def example():\n return "test"')),
287287
]
288288

289-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', true)
289+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', true)
290290
assert.strictEqual(result, '# # Heading\n# This is markdown\ndef example():\n return "test"\n')
291291
})
292292

@@ -295,7 +295,7 @@ describe('editorContext', function () {
295295
createNotebookCell(createMockDocument('# Heading\nThis is markdown'), vscode.NotebookCellKind.Markup),
296296
createNotebookCell(createMockDocument('def example():\n return "test"')),
297297
]
298-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'java', false)
298+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'java', false)
299299
assert.strictEqual(result, '// # Heading\n// This is markdown\n// def example():\n// return "test"\n')
300300
})
301301

@@ -305,7 +305,7 @@ describe('editorContext', function () {
305305
createNotebookCell(createMockDocument('println(1 + 1);', 'somefile.ipynb', 'java')),
306306
]
307307

308-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'java', true)
308+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'java', true)
309309
assert.strictEqual(result, '// # Heading\n// This is markdown\nprintln(1 + 1);\n')
310310
})
311311

@@ -317,7 +317,7 @@ describe('editorContext', function () {
317317
),
318318
createNotebookCell(createMockDocument('def example():\n return "test"')),
319319
]
320-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', false)
320+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', false)
321321
assert.strictEqual(result, '# println(1 + 1);\ndef example():\n return "test"\n')
322322
})
323323

@@ -329,7 +329,7 @@ describe('editorContext', function () {
329329
),
330330
createNotebookCell(createMockDocument('def example():\n return "test"')),
331331
]
332-
const result = EditorContext.extractCellsSliceContext(mockCells, 100, 'python', true)
332+
const result = EditorContext.getNotebookCellsSliceContext(mockCells, 100, 'python', true)
333333
assert.strictEqual(result, '# println(1 + 1);\ndef example():\n return "test"\n')
334334
})
335335
})

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

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,52 @@ const languageCommentChars: Record<string, string> = {
2929
java: '// ',
3030
}
3131

32-
export function getNotebookCellContext(cell: vscode.NotebookCell, referenceLanguage?: string): string {
32+
function getEnclosingNotebook(editor: vscode.TextEditor): vscode.NotebookDocument | undefined {
33+
// For notebook cells, find the existing notebook with a cell that matches the current editor.
34+
return vscode.workspace.notebookDocuments.find(
35+
(nb) =>
36+
nb.notebookType === 'jupyter-notebook' && nb.getCells().some((cell) => cell.document === editor.document)
37+
)
38+
}
39+
40+
export function extractNotebookContext(
41+
notebook: vscode.NotebookDocument,
42+
editor: vscode.TextEditor,
43+
languageName: string,
44+
caretLeftFileContext: string,
45+
caretRightFileContext: string
46+
) {
47+
// Expand the context for a cell inside of a noteboo with whatever text fits from the preceding and subsequent cells
48+
const allCells = notebook.getCells()
49+
const cellIndex = allCells.findIndex((cell) => cell.document === editor.document)
50+
// Extract text from prior cells if there is enough room in left file context
51+
if (caretLeftFileContext.length < CodeWhispererConstants.charactersLimit - 1) {
52+
const leftCellsText = getNotebookCellsSliceContext(
53+
allCells.slice(0, cellIndex),
54+
CodeWhispererConstants.charactersLimit - (caretLeftFileContext.length + 1),
55+
languageName,
56+
true
57+
)
58+
if (leftCellsText.length > 0) {
59+
caretLeftFileContext = addNewlineIfMissing(leftCellsText) + caretLeftFileContext
60+
}
61+
}
62+
// Extract text from subsequent cells if there is enough room in right file context
63+
if (caretRightFileContext.length < CodeWhispererConstants.charactersLimit - 1) {
64+
const rightCellsText = getNotebookCellsSliceContext(
65+
allCells.slice(cellIndex + 1),
66+
CodeWhispererConstants.charactersLimit - (caretRightFileContext.length + 1),
67+
languageName,
68+
false
69+
)
70+
if (rightCellsText.length > 0) {
71+
caretRightFileContext = addNewlineIfMissing(caretRightFileContext) + rightCellsText
72+
}
73+
}
74+
return { caretLeftFileContext, caretRightFileContext }
75+
}
76+
77+
export function extractSingleCellContext(cell: vscode.NotebookCell, referenceLanguage?: string): string {
3378
// Extract the text verbatim if the cell is code and the cell has the same language.
3479
// Otherwise, add the correct comment string for the refeference language
3580
const cellText = cell.document.getText()
@@ -57,6 +102,8 @@ export function getNotebookCellsSliceContext(
57102
referenceLanguage: string,
58103
fromStart: boolean
59104
): string {
105+
// Extract context from array of notebook cells that fits inside `maxLength` characters,
106+
// from either the start or the end of the array.
60107
let output: string[] = []
61108
if (!fromStart) {
62109
cells = cells.reverse()
@@ -112,40 +159,15 @@ export function extractContextForCodeWhisperer(editor: vscode.TextEditor): codew
112159
runtimeLanguageContext.normalizeLanguage(editor.document.languageId) ?? editor.document.languageId
113160
}
114161
if (editor.document.uri.scheme === 'vscode-notebook-cell') {
115-
// For notebook cells, first find the existing notebook with a cell that matches the current editor.
116-
const notebook = vscode.workspace.notebookDocuments.find(
117-
(nb) =>
118-
nb.notebookType === 'jupyter-notebook' &&
119-
nb.getCells().some((cell) => cell.document === editor.document)
120-
)
162+
const notebook = getEnclosingNotebook(editor)
121163
if (notebook) {
122-
const allCells = notebook.getCells()
123-
const cellIndex = allCells.findIndex((cell) => cell.document === editor.document)
124-
125-
// Extract text from prior cells if there is enough room in left file context
126-
if (caretLeftFileContext.length < CodeWhispererConstants.charactersLimit - 1) {
127-
const leftCellsText = extractCellsSliceContext(
128-
allCells.slice(0, cellIndex),
129-
CodeWhispererConstants.charactersLimit - (caretLeftFileContext.length + 1),
130-
languageName,
131-
true
132-
)
133-
if (leftCellsText.length > 0) {
134-
caretLeftFileContext = addNewlineIfMissing(leftCellsText) + caretLeftFileContext
135-
}
136-
}
137-
// Extract text from subsequent cells if there is enough room in right file context
138-
if (caretRightFileContext.length < CodeWhispererConstants.charactersLimit - 1) {
139-
const rightCellsText = extractCellsSliceContext(
140-
allCells.slice(cellIndex + 1),
141-
CodeWhispererConstants.charactersLimit - (caretRightFileContext.length + 1),
142-
languageName,
143-
false
144-
)
145-
if (rightCellsText.length > 0) {
146-
caretRightFileContext = addNewlineIfMissing(caretRightFileContext) + rightCellsText
147-
}
148-
}
164+
;({ caretLeftFileContext, caretRightFileContext } = extractNotebookContext(
165+
notebook,
166+
editor,
167+
languageName,
168+
caretLeftFileContext,
169+
caretRightFileContext
170+
))
149171
}
150172
}
151173

0 commit comments

Comments
 (0)