Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV2-564 Enhance context gathering in Jupyter notebooks by including text from… #1409

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/runCompletion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CancellationToken, Position, Range, TextDocument } from "vscode";
import {
CancellationToken,
Position,
Range,
TextDocument,
window,
Uri,
} from "vscode";
import {
autocomplete,
AutocompleteParams,
Expand All @@ -14,6 +21,29 @@
import languages from "./globals/languages";
import { getSDKPath } from "./languages";

function calculateContextForJupyterNotebook(
notebookEditor: window.NotebookEditor,
documentUri: Uri
): { before: string; after: string } {
const cells = notebookEditor.notebook.getCells();

const index = cells.findIndex(
(cell) => cell.document.uri.toString() === documentUri.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");
return {
before: before + "\n",

Check failure on line 42 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Unexpected string concatenation
after: "\n" + after,

Check failure on line 43 in src/runCompletion.ts

View workflow job for this annotation

GitHub Actions / Build & Lint

Unexpected string concatenation
};
}

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

const notebookEditor = window.activeNotebookEditor;

const { before, after } = notebookEditor
? calculateContextForJupyterNotebook(notebookEditor, document.uri)
: { before: "", after: "" };

const requestData = {
filename: getFileNameWithExtension(document),
before:
before +
document.getText(new Range(beforeStart, position)) +
currentSuggestionText,
after: document.getText(new Range(position, afterEnd)),
after: document.getText(new Range(position, afterEnd)) + after,
region_includes_beginning: beforeStartOffset === 0,
region_includes_end: document.offsetAt(afterEnd) !== afterEndOffset,
max_num_results: getMaxResults(),
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
Loading