-
Notifications
You must be signed in to change notification settings - Fork 193
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
|
@@ -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, | ||
|
@@ -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: | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
requestData.after = [requestData.after, after].join("\n"); | ||
} | ||
|
||
const isEmptyLine = document.lineAt(position.line).text.trim().length === 0; | ||
|
||
|
There was a problem hiding this comment.
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 theUri
to it