Skip to content

Commit

Permalink
Enhance context gathering in Jupyter notebooks by including text from…
Browse files Browse the repository at this point in the history
… all cells

Signed-off-by: Tal Wertheimer <tal@Tals-MacBook-Pro-2.local>
  • Loading branch information
Tal Wertheimer authored and Tal Wertheimer committed Dec 13, 2023
1 parent 16c2111 commit b9f15eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/runCompletion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CancellationToken, Position, Range, TextDocument } from "vscode";
import * as vscode from "vscode";
import {
autocomplete,
AutocompleteParams,
Expand All @@ -14,6 +15,37 @@ import {
import languages from "./globals/languages";
import { getSDKPath } from "./languages";

// If the 'document' represents a cell within a Jupyter notebook, it is important to
// gather text from all cells to establish the full context. This approach ensures
// comprehensive understanding, not limited to the content of only the current cell.
function updateRequestForJupyterNoteboook(
document: TextDocument,
requestData: AutocompleteParams
): AutocompleteParams {
const notebookEditor = vscode.window.activeNotebookEditor;
if (notebookEditor) {
const cells = notebookEditor.notebook.getCells();

const index = cells.findIndex(
(cell) => cell.document.uri.toString() === document.uri.toString()
);
const before = cells
.slice(0, index)
.map((cell) => cell.document.getText())
.join("\n");
const after = cells
.slice(index + 1)
.map((cell) => cell.document.getText())
.join("\n");

requestData.before = [before, requestData.before].join("\n");

Check failure on line 41 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Assignment to property of function parameter 'requestData'
requestData.after = [requestData.after + after].join("\n");

Check failure on line 42 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Assignment to property of function parameter 'requestData'
return requestData;
} else {

Check failure on line 44 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Unnecessary 'else' after 'return'
return requestData;
}
}

export default async function runCompletion({
document,
position,
Expand All @@ -36,7 +68,8 @@ export default async function runCompletion({
const afterEndOffset = offset + CHAR_LIMIT;
const beforeStart = document.positionAt(beforeStartOffset);
const afterEnd = document.positionAt(afterEndOffset);
const requestData = {

let requestData = {

Check failure on line 72 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

'requestData' is never reassigned. Use 'const' instead
filename: getFileNameWithExtension(document),
before:
document.getText(new Range(beforeStart, position)) +
Expand All @@ -52,10 +85,15 @@ export default async function runCompletion({
sdk_path: getSDKPath(document.languageId),
};

const request: AutocompleteParams = updateRequestForJupyterNoteboook(
document,
requestData
);

const isEmptyLine = document.lineAt(position.line).text.trim().length === 0;

const result = await autocomplete(
requestData,
request,
isEmptyLine ? INLINE_REQUEST_TIMEOUT : timeout
);

Expand Down
10 changes: 10 additions & 0 deletions src/vscode.proposed.inlineCompletions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ declare module "vscode" {
* Be aware that this API will not ever be finalized.
*/
export namespace window {
export const activeNotebookEditor: NotebookEditor | undefined;

class NotebookEditor {
notebook: {
getCells: () => {
document: TextDocument;
}[];
};
}

export function getInlineCompletionItemController<
T extends InlineCompletionItem
>(provider: InlineCompletionItemProvider<T>): InlineCompletionController<T>;
Expand Down

0 comments on commit b9f15eb

Please sign in to comment.