Skip to content

Commit

Permalink
set language, provide deserialized editor for recovery process
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger committed Jun 16, 2023
1 parent 76c8654 commit d788479
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,40 +255,37 @@ workbenchContributionsRegistry.registerWorkbenchContribution(InteractiveDocument
workbenchContributionsRegistry.registerWorkbenchContribution(InteractiveInputContentProvider, LifecyclePhase.Ready);
workbenchContributionsRegistry.registerWorkbenchContribution(InteractiveWindowWorkingCopyEditorHandler, LifecyclePhase.Ready);

type interactiveEditorInputData = { resource: URI; inputResource: URI; name: string; language: string };

export class InteractiveEditorSerializer implements IEditorSerializer {
public static readonly ID = InteractiveEditorInput.ID;

constructor(@IConfigurationService private configurationService: IConfigurationService) {
}

canSerialize(): boolean {
return this.configurationService.getValue<boolean>(InteractiveWindowSetting.interactiveWindowRestore);
canSerialize(editor: EditorInput): boolean {
const interactiveEditorInput = editor as InteractiveEditorInput;
return URI.isUri(interactiveEditorInput?.primary?.resource) && URI.isUri(interactiveEditorInput?.inputResource);
}

serialize(input: EditorInput): string {
assertType(input instanceof InteractiveEditorInput);
return JSON.stringify({
resource: input.primary.resource,
inputResource: input.inputResource,
name: input.getName()
name: input.getName(),
language: input.language
});
}

deserialize(instantiationService: IInstantiationService, raw: string) {
if (!this.canSerialize()) {
return undefined;
}
type Data = { resource: URI; inputResource: URI; data: any };
const data = <Data>parse(raw);
const data = <interactiveEditorInputData>parse(raw);
if (!data) {
return undefined;
}
const { resource, inputResource } = data;
if (!data || !URI.isUri(resource) || !URI.isUri(inputResource)) {
const { resource, inputResource, name, language } = data;
if (!URI.isUri(resource) || !URI.isUri(inputResource)) {
return undefined;
}

const input = InteractiveEditorInput.create(instantiationService, resource, inputResource);
const input = InteractiveEditorInput.create(instantiationService, resource, inputResource, name, language);
return input;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ export class InteractiveEditor extends EditorPane {
}
}));

const editorModel = await input.resolveInput(this.#notebookWidget.value?.activeKernel?.supportedLanguages[0] ?? PLAINTEXT_LANGUAGE_ID);
const languageId = this.#notebookWidget.value?.activeKernel?.supportedLanguages[0] ?? input.language ?? PLAINTEXT_LANGUAGE_ID;
const editorModel = await input.resolveInput(languageId);
editorModel.setLanguage(languageId);
this.#codeEditorWidget.setModel(editorModel);
if (viewState?.input) {
this.#codeEditorWidget.restoreViewState(viewState.input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IReference } from 'vs/base/common/lifecycle';
import * as paths from 'vs/base/common/path';
import { isEqual, joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { PLAINTEXT_LANGUAGE_ID } from 'vs/editor/common/languages/modesRegistry';
import { IResolvedTextEditorModel, ITextModelService } from 'vs/editor/common/services/resolverService';
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand All @@ -20,8 +21,8 @@ import { ICompositeNotebookEditorInput, NotebookEditorInput } from 'vs/workbench
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';

export class InteractiveEditorInput extends EditorInput implements ICompositeNotebookEditorInput {
static create(instantiationService: IInstantiationService, resource: URI, inputResource: URI, title?: string) {
return instantiationService.createInstance(InteractiveEditorInput, resource, inputResource, title);
static create(instantiationService: IInstantiationService, resource: URI, inputResource: URI, title?: string, language?: string) {
return instantiationService.createInstance(InteractiveEditorInput, resource, inputResource, title, language);
}

static readonly ID: string = 'workbench.input.interactive';
Expand All @@ -36,6 +37,11 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot

private _initTitle?: string;

get language() {
return this._inputModelRef?.object.textEditorModel.getLanguageId() ?? this._initLanguage;
}
private _initLanguage?: string;

private _notebookEditorInput: NotebookEditorInput;
get notebookEditorInput() {
return this._notebookEditorInput;
Expand Down Expand Up @@ -73,6 +79,7 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot
resource: URI,
inputResource: URI,
title: string | undefined,
languageId: string | undefined,
@IInstantiationService instantiationService: IInstantiationService,
@ITextModelService textModelService: ITextModelService,
@IInteractiveDocumentService interactiveDocumentService: IInteractiveDocumentService,
Expand All @@ -85,6 +92,7 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot
this._notebookEditorInput = input;
this._register(this._notebookEditorInput);
this._initTitle = title;
this._initLanguage = languageId;
this._resource = resource;
this._inputResource = inputResource;
this._inputResolver = null;
Expand Down Expand Up @@ -141,12 +149,13 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot
return this._inputResolver;
}

async resolveInput(language: string) {
async resolveInput(language?: string) {
if (this._inputModelRef) {
return this._inputModelRef.object.textEditorModel;
}

this._interactiveDocumentService.willCreateInteractiveDocument(this.resource!, this.inputResource, language);
const resolvedLanguage = language ?? this._initLanguage ?? PLAINTEXT_LANGUAGE_ID;
this._interactiveDocumentService.willCreateInteractiveDocument(this.resource!, this.inputResource, resolvedLanguage);
this._inputModelRef = await this._textModelService.createModelReference(this.inputResource);

return this._inputModelRef.object.textEditorModel;
Expand Down

0 comments on commit d788479

Please sign in to comment.