Skip to content

Commit

Permalink
Implement the changes necessary to use the new custom editor (#12188)
Browse files Browse the repository at this point in the history
* Turn on custom editor again

* Fix copy problem

* Fix copy problem again

* Get undo/redo to work

* Some fixes for synching more than one editor

* Fix untitled. Fix timeouts

* fix command manager to not be used. Not necessary with temp path.
fix delete/insert commands to undo properly

* Fix functional test

* Add experiment

* Since package json is enabling proposed api, turn of notebooks with experiment

* Turn off proposed api

* Upgrade VS code to 1.45 so can use new api outside of insiders

* Update package.json dynamically

* Fix merge code to handle arrays

* Fix unit tests. Backup happens at the provider level now

* Fix hygiene

* Fix untitled to work for old provider too

* Some feedback and attempt to fix URI problem on linux/mac

* Review feedback
  • Loading branch information
rchiodo authored Jun 8, 2020
1 parent 34d0869 commit dc32568
Show file tree
Hide file tree
Showing 43 changed files with 657 additions and 354 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// Enable this to turn on redux logging during debugging
"XVSC_PYTHON_FORCE_LOGGING": "1",
// Enable this to try out new experiments locally
"XVSC_PYTHON_LOAD_EXPERIMENTS_FROM_FILE": "1",
"VSC_PYTHON_LOAD_EXPERIMENTS_FROM_FILE": "1",
// Enable this to log telemetry to the output during debugging
"XVSC_PYTHON_LOG_TELEMETRY": "1",
// Enable this to log debugger output. Directory must exist ahead of time
Expand Down
19 changes: 19 additions & 0 deletions customEditor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"activationEvents": [
"onCustomEditor:ms-python.python.notebook.ipynb"
],
"contributes": {
"customEditors": [
{
"viewType": "ms-python.python.notebook.ipynb",
"displayName": "Jupyter Notebook",
"selector": [
{
"filenamePattern": "*.ipynb"
}
],
"priority": "default"
}
]
}
}
12 changes: 12 additions & 0 deletions experiments.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@
"salt": "RunByLine",
"max": 0,
"min": 0
},
{
"name": "CustomEditorSupport - control",
"salt": "CustomEditorSupport",
"min": 0,
"max": 100
},
{
"name": "CustomEditorSupport - experiment",
"salt": "CustomEditorSupport",
"max": 0,
"min": 0
}

]
1 change: 1 addition & 0 deletions news/1 Enhancements/10744.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable the use of the custom editor for native notebooks.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"onCommand:python.datascience.selectJupyterInterpreter",
"onCommand:python.datascience.selectjupytercommandline",
"onCommand:python.enableSourceMapSupport",
"onCustomEditor:NativeEditorProvider.ipynb",

"onNotebookEditor:jupyter-notebook",
"workspaceContains:**/mspythonconfig.json"
],
Expand Down Expand Up @@ -3161,7 +3161,7 @@
"@types/tmp": "0.0.33",
"@types/untildify": "^3.0.0",
"@types/uuid": "^3.4.3",
"@types/vscode": "^1.43.0",
"@types/vscode": "^1.45.0",
"@types/webpack-bundle-analyzer": "^2.13.0",
"@types/winreg": "^1.2.30",
"@types/ws": "^6.0.1",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,5 +510,6 @@
"DataScience.continueRunByLine": "Stop",
"DataScience.couldNotInstallLibrary": "Could not install {0}. If pip is not available, please use the package manager of your choice to manually install this library into your Python environment.",
"DataScience.rawKernelSessionFailed": "Unable to start session for kernel {0}. Select another kernel to launch with.",
"DataScience.rawKernelConnectingSession": "Connecting to kernel."
"DataScience.rawKernelConnectingSession": "Connecting to kernel.",
"DataScience.reloadCustomEditor": "Please reload VS Code to use the custom editor API"
}
41 changes: 36 additions & 5 deletions src/client/common/application/customEditorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import * as vscode from 'vscode';
import { DataScience } from '../../common/utils/localize';

import { UseCustomEditorApi } from '../constants';
import { traceError } from '../../logging';
import { EXTENSION_ROOT_DIR, UseCustomEditorApi } from '../constants';
import { IFileSystem } from '../platform/types';
import { noop } from '../utils/misc';
import { CustomEditorProvider, ICommandManager, ICustomEditorService } from './types';
import { CustomEditorProvider, IApplicationEnvironment, ICommandManager, ICustomEditorService } from './types';

@injectable()
export class CustomEditorService implements ICustomEditorService {
constructor(
@inject(ICommandManager) private commandManager: ICommandManager,
@inject(UseCustomEditorApi) private readonly useCustomEditorApi: boolean
) {}
@inject(UseCustomEditorApi) private readonly useCustomEditorApi: boolean,
@inject(IApplicationEnvironment) private readonly appEnvironment: IApplicationEnvironment,
@inject(IFileSystem) private readonly fileSystem: IFileSystem
) {
// Double check the package json has the necessary entries for contributing a custom editor
if (this.useCustomEditorApi && !appEnvironment.packageJson.contributes?.customEditors) {
this.rewritePackageJson().catch((e) => traceError(`Error rewriting package json: `, e));
}
}

public registerCustomEditorProvider(
viewType: string,
provider: CustomEditorProvider,
options?: vscode.WebviewPanelOptions
options?: {
readonly webviewOptions?: vscode.WebviewPanelOptions;
readonly supportsMultipleEditorsPerDocument?: boolean;
}
): vscode.Disposable {
if (this.useCustomEditorApi) {
// tslint:disable-next-line: no-any
Expand All @@ -33,4 +47,21 @@ export class CustomEditorService implements ICustomEditorService {
await this.commandManager.executeCommand('vscode.openWith', file, viewType);
}
}

private async rewritePackageJson() {
// tslint:disable-next-line:no-require-imports no-var-requires
const _mergeWith = require('lodash/mergeWith') as typeof import('lodash/mergeWith');
const current = this.appEnvironment.packageJson;
const improvedContents = await this.fileSystem.readFile(path.join(EXTENSION_ROOT_DIR, 'customEditor.json'));
const improved = _mergeWith({ ...current }, JSON.parse(improvedContents), (l, r) => {
if (Array.isArray(l) && Array.isArray(r)) {
return [...l, ...r];
}
});
await this.fileSystem.writeFile(
path.join(EXTENSION_ROOT_DIR, 'package.json'),
JSON.stringify(improved, null, 4)
);
this.commandManager.executeCommand('python.reloadVSCode', DataScience.reloadCustomEditor());
}
}
8 changes: 5 additions & 3 deletions src/client/common/application/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import type {
NotebookOutputSelector
} from 'vscode-proposed';
import { UseProposedApi } from '../constants';
import { IDisposableRegistry } from '../types';
import { NativeNotebook } from '../experiments/groups';
import { IDisposableRegistry, IExperimentsManager } from '../types';
import {
IVSCodeNotebook,
NotebookCellLanguageChangeEvent,
Expand Down Expand Up @@ -62,9 +63,10 @@ export class VSCodeNotebook implements IVSCodeNotebook {
private readonly handledCellChanges = new WeakSet<VSCNotebookCellsChangeEvent>();
constructor(
@inject(UseProposedApi) private readonly useProposedApi: boolean,
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
@inject(IExperimentsManager) readonly experimentManager: IExperimentsManager
) {
if (this.useProposedApi) {
if (this.useProposedApi && experimentManager.inExperiment(NativeNotebook.experiment)) {
this.addEventHandlers();
}
}
Expand Down
Loading

0 comments on commit dc32568

Please sign in to comment.