Skip to content

Commit

Permalink
Merge pull request #191138 from microsoft/rebornix/brainy-badger
Browse files Browse the repository at this point in the history
Re #183449. Roaming kernel history.
  • Loading branch information
rebornix authored Aug 24, 2023
2 parents 9e2cd07 + 7a860fb commit 83421ae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Disposable } from 'vs/base/common/lifecycle';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { LinkedMap, Touch } from 'vs/base/common/map';
import { localize } from 'vs/nls';
import { Categories } from 'vs/platform/action/common/actionCommonCategories';
Expand Down Expand Up @@ -36,6 +36,9 @@ export class NotebookKernelHistoryService extends Disposable implements INoteboo

this._loadState();
this._register(this._storageService.onWillSaveState(() => this._saveState()));
this._register(this._storageService.onDidChangeValue(StorageScope.WORKSPACE, NotebookKernelHistoryService.STORAGE_KEY, this._register(new DisposableStore()))(() => {
this._restoreState();
}));
}

getKernels(notebook: INotebookTextModelLike): { selected: INotebookKernel | undefined; all: INotebookKernel[] } {
Expand Down Expand Up @@ -79,12 +82,30 @@ export class NotebookKernelHistoryService extends Disposable implements INoteboo

if (notEmpty) {
const serialized = this._serialize();
this._storageService.store(NotebookKernelHistoryService.STORAGE_KEY, JSON.stringify(serialized), StorageScope.WORKSPACE, StorageTarget.MACHINE);
this._storageService.store(NotebookKernelHistoryService.STORAGE_KEY, JSON.stringify(serialized), StorageScope.WORKSPACE, StorageTarget.USER);
} else {
this._storageService.remove(NotebookKernelHistoryService.STORAGE_KEY, StorageScope.WORKSPACE);
}
}

private _restoreState(): void {
const serialized = this._storageService.get(NotebookKernelHistoryService.STORAGE_KEY, StorageScope.WORKSPACE);
if (serialized) {
try {
for (const [viewType, kernels] of JSON.parse(serialized)) {
const linkedMap = this._mostRecentKernelsMap[viewType] ?? new LinkedMap<string, string>();
for (const entry of kernels.entries) {
linkedMap.set(entry, entry, Touch.AsOld);
}

this._mostRecentKernelsMap[viewType] = linkedMap;
}
} catch (e) {
console.error('Deserialize notebook kernel history failed', e);
}
}
}

private _loadState(): void {
const serialized = this._storageService.get(NotebookKernelHistoryService.STORAGE_KEY, StorageScope.WORKSPACE);
if (serialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/no
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { IMenu, IMenuService } from 'vs/platform/actions/common/actions';
import { NotebookKernelHistoryService } from 'vs/workbench/contrib/notebook/browser/services/notebookKernelHistoryServiceImpl';
import { IStorageService, IWillSaveStateEvent, StorageScope } from 'vs/platform/storage/common/storage';
import { IApplicationStorageValueChangeEvent, IProfileStorageValueChangeEvent, IStorageService, IStorageValueChangeEvent, IWillSaveStateEvent, IWorkspaceStorageValueChangeEvent, StorageScope } from 'vs/platform/storage/common/storage';
import { INotebookLoggingService } from 'vs/workbench/contrib/notebook/common/notebookLoggingService';

suite('NotebookKernelHistoryService', () => {
Expand Down Expand Up @@ -70,6 +70,12 @@ suite('NotebookKernelHistoryService', () => {

instantiationService.stub(IStorageService, new class extends mock<IStorageService>() {
override onWillSaveState: Event<IWillSaveStateEvent> = Event.None;
override onDidChangeValue(scope: StorageScope.WORKSPACE, key: string | undefined, disposable: DisposableStore): Event<IWorkspaceStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope.PROFILE, key: string | undefined, disposable: DisposableStore): Event<IProfileStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope.APPLICATION, key: string | undefined, disposable: DisposableStore): Event<IApplicationStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope, key: string | undefined, disposable: DisposableStore): Event<IStorageValueChangeEvent> {
return Event.None;
}
override get(key: string, scope: StorageScope, fallbackValue: string): string;
override get(key: string, scope: StorageScope, fallbackValue?: string | undefined): string | undefined;
override get(key: unknown, scope: unknown, fallbackValue?: unknown): string | undefined {
Expand Down Expand Up @@ -119,6 +125,12 @@ suite('NotebookKernelHistoryService', () => {

instantiationService.stub(IStorageService, new class extends mock<IStorageService>() {
override onWillSaveState: Event<IWillSaveStateEvent> = Event.None;
override onDidChangeValue(scope: StorageScope.WORKSPACE, key: string | undefined, disposable: DisposableStore): Event<IWorkspaceStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope.PROFILE, key: string | undefined, disposable: DisposableStore): Event<IProfileStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope.APPLICATION, key: string | undefined, disposable: DisposableStore): Event<IApplicationStorageValueChangeEvent>;
override onDidChangeValue(scope: StorageScope, key: string | undefined, disposable: DisposableStore): Event<IStorageValueChangeEvent> {
return Event.None;
}
override get(key: string, scope: StorageScope, fallbackValue: string): string;
override get(key: string, scope: StorageScope, fallbackValue?: string | undefined): string | undefined;
override get(key: unknown, scope: unknown, fallbackValue?: unknown): string | undefined {
Expand Down

0 comments on commit 83421ae

Please sign in to comment.