Skip to content

Commit

Permalink
Improve jupyter kernel autocompletions (#8247)
Browse files Browse the repository at this point in the history
* Fix jupyter autocomplete to filter a bunch of stuff

* Add a bunch of tests

* Add news items and update tests

* Put mock document back

* Renaming and other review feedback

* Review feedback
  • Loading branch information
rchiodo authored Nov 11, 2021
1 parent 64bb04b commit 617384b
Show file tree
Hide file tree
Showing 19 changed files with 756 additions and 215 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
"--ui=tdd",
"--recursive",
"--colors",
//"--grep", "<suite name>",
//"--grep", "",
"--timeout=300000"
],
"outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"],
Expand Down
1 change: 1 addition & 0 deletions news/2 Fixes/7136.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix completions to skip file names when not inside of a string.
1 change: 1 addition & 0 deletions news/2 Fixes/7772.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminate duplicate content in jupyter completions.
1 change: 1 addition & 0 deletions news/2 Fixes/7880.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make jupyter kernel autocomplete less aggressive and customizable. The new 'jupyter.completionTriggerCharacters' can be used to determine what causes a kernel to return completions.
1 change: 1 addition & 0 deletions news/2 Fixes/8080.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Eliminate extra long labels in jupyter completions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,12 @@
"default": false,
"description": "Determines if pylance's experimental notebook support is used or not",
"scope": "machine"
},
"jupyter.pythonCompletionTriggerCharacters": {
"type": "string",
"default": ".%",
"description": "Characters which trigger auto completion on a python jupyter kernel (requires reload of VS code)",
"scope": "machine"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions src/client/common/application/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgu
];
['interactive.execute']: [string];
['outline.focus']: [];
['vscode.executeCompletionItemProvider']: [Uri, Position];
[DSCommands.NotebookEditorInterruptKernel]: [{ notebookEditor: { notebookUri: Uri } } | undefined | Uri];
[DSCommands.ExportFileAndOutputAsNotebook]: [Uri];
[DSCommands.RunAllCells]: [Uri];
Expand Down
1 change: 1 addition & 0 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class JupyterSettings implements IWatchableJupyterSettings {
public showVariableViewWhenDebugging: boolean = true;
public newCellOnRunLast: boolean = true;
public pylanceHandlesNotebooks: boolean = false;
public pythonCompletionTriggerCharacters: string | undefined;

public variableTooltipFields: IVariableTooltipFields = {
python: {
Expand Down
1 change: 1 addition & 0 deletions src/client/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export interface IJupyterSettings {
readonly showVariableViewWhenDebugging: boolean;
readonly newCellOnRunLast: boolean;
readonly pylanceHandlesNotebooks?: boolean;
readonly pythonCompletionTriggerCharacters?: string;
}

export interface IVariableTooltipFields {
Expand Down
28 changes: 19 additions & 9 deletions src/client/datascience/notebook/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { inject, injectable } from 'inversify';
import { languages } from 'vscode';
import { IExtensionSingleActivationService } from '../../activation/types';
import { NOTEBOOK_SELECTOR } from '../../common/constants';
import { IDisposableRegistry } from '../../common/types';
import { JupyterCompletionProvider } from './intellisense/jupyterCompletionProvider';
import { IConfigurationService, IDisposableRegistry } from '../../common/types';
import { PythonKernelCompletionProvider } from './intellisense/pythonKernelCompletionProvider';

// Default set of trigger characters for jupyter
const DefaultTriggerCharacters = ['.', '%'];

/**
* This class basically registers the necessary providers and the like with VSC.
Expand All @@ -16,20 +19,27 @@ import { JupyterCompletionProvider } from './intellisense/jupyterCompletionProvi
export class NotebookIntegration implements IExtensionSingleActivationService {
constructor(
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(JupyterCompletionProvider) private readonly completionProvider: JupyterCompletionProvider
@inject(IConfigurationService) private readonly config: IConfigurationService,
@inject(PythonKernelCompletionProvider) private readonly completionProvider: PythonKernelCompletionProvider
) {}
public async activate(): Promise<void> {
this.registerCompletionItemProvider();
let triggerChars =
this.config.getSettings().pythonCompletionTriggerCharacters?.split('') || DefaultTriggerCharacters;

// Special case. We know that the jupyter autocomplete works in strings, so if strings are available, trigger on / too so
// we can fill out paths.
if (triggerChars.includes('"') || triggerChars.includes("'")) {
triggerChars = [...triggerChars, '/'];
}
this.registerCompletionItemProvider(triggerChars);
}

private registerCompletionItemProvider() {
private registerCompletionItemProvider(triggerChars: string[]) {
// Register the jupyter kernel completions for PYTHON cells.
const disposable = languages.registerCompletionItemProvider(
NOTEBOOK_SELECTOR,
this.completionProvider,
'.',
'"',
"'",
','
...triggerChars
);
this.disposables.push(disposable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { PythonEnvironment } from '../../../pythonEnvironments/info';
import { getInterpreterId } from '../../../pythonEnvironments/info/interpreter';
import { IInteractiveWindowProvider } from '../../types';
import { findAssociatedNotebookDocument, isJupyterNotebook } from '../helpers/helpers';
import { INotebookControllerManager } from '../types';
import { INotebookControllerManager, INotebookLanguageClientProvider } from '../types';
import { VSCodeNotebookController } from '../vscodeNotebookController';
import { LanguageServer } from './languageServer';

/**
* This class sets up the concatenated intellisense for every notebook as it changes its kernel.
*/
@injectable()
export class IntellisenseProvider implements IExtensionSyncActivationService {
export class IntellisenseProvider implements INotebookLanguageClientProvider, IExtensionSyncActivationService {
private servers = new Map<string, Promise<LanguageServer | undefined>>();
private activeInterpreterCache = new Map<string, PythonEnvironment | undefined>();
private interpreterIdCache: Map<string, string> = new Map<string, string>();
Expand Down Expand Up @@ -58,6 +58,16 @@ export class IntellisenseProvider implements IExtensionSyncActivationService {
this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration, this, this.disposables);
}

public async getLanguageClient(notebook: NotebookDocument) {
const controller = this.notebookControllerManager.getSelectedNotebookController(notebook);
const interpreter = controller
? controller.connection.interpreter
: await this.interpreterService.getActiveInterpreter(notebook.uri);
const interpreterId = interpreter ? this.getInterpreterIdFromCache(interpreter) : undefined;
const server = interpreterId ? await this.servers.get(interpreterId) : undefined;
return server?.client;
}

private handleInterpreterChange() {
const folders = [...this.activeInterpreterCache.keys()];
this.activeInterpreterCache.clear();
Expand Down

This file was deleted.

Loading

0 comments on commit 617384b

Please sign in to comment.