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 1 commit
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
35 changes: 35 additions & 0 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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a named import from vscode one line above, just add the window and the Uri to it

import {
autocomplete,
AutocompleteParams,
Expand All @@ -14,6 +15,29 @@ import {
import languages from "./globals/languages";
import { getSDKPath } from "./languages";

function calculateContextForJupyterNotebook(
notebookEditor: vscode.window.NotebookEditor,
documentUri: vscode.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,
after,
};
}

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

const requestData = {
filename: getFileNameWithExtension(document),
before:
Expand All @@ -51,6 +76,16 @@ export default async function runCompletion({
indentation_size: getTabSize(),
sdk_path: getSDKPath(document.languageId),
};
const notebookEditor = vscode.window.activeNotebookEditor;

if (notebookEditor) {
const { before, after } = calculateContextForJupyterNotebook(
notebookEditor,
document.uri
);
requestData.before = [before, requestData.before].join("\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer an approach that's more "immutable". how about introducing a getContextParts function that returns something like {before: string, after: string}? this function could calculate the 'before' and 'after' based on whether it's a notebook or a regular document, instead of directly mutating the requestData. WDYT?

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

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

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