Skip to content
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

update vscode api #3496

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/polyglot-notebooks-vscode-common/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export async function registerAcquisitionCommands(context: vscode.ExtensionConte
DotNetPathManager.setDotNetPath(installArgs.dotnetPath);

if (cachedInstallArgs) {

// todo: ask Brett
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About what? Does this need to be resolved?

if (installArgs.dotnetPath !== cachedInstallArgs.dotnetPath ||
installArgs.toolVersion !== cachedInstallArgs.toolVersion) {
// if specified install args are different than what we previously computed, invalidate the acquisition
Expand Down
6 changes: 6 additions & 0 deletions src/polyglot-notebooks-vscode-common/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export const DotNetPathManager = new CachedDotNetPathManager();
const disposables: (() => void)[] = [];

export async function activate(context: vscode.ExtensionContext) {
// Wait for ipynb extension to be ready, temporary.
const ipynbExtension = vscode.extensions.getExtension('vscode.ipynb');
if (ipynbExtension && !ipynbExtension.isActive) {
await ipynbExtension.activate();
}
metadataUtilities.setUseLegacyMetadata(ipynbExtension && !ipynbExtension.exports.dropCustomMetadata ? true : false);

const dotnetConfig = vscode.workspace.getConfiguration(constants.DotnetConfigurationSectionName);
const polyglotConfig = vscode.workspace.getConfiguration(constants.PolyglotConfigurationSectionName);
Expand Down
118 changes: 85 additions & 33 deletions src/polyglot-notebooks-vscode-common/src/metadataUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function isDotNetNotebook(notebook: vscodeLike.NotebookDocument): boolean
return true;
}

const kernelspecMetadata = getKernelspecMetadataFromIpynbNotebookDocument(notebook);
const kernelspecMetadata = getKernelspecMetadataFromIpynbNotebookDocument(notebook); //?
if (kernelspecMetadata.name.startsWith('.net-')) {
return true;
}
Expand Down Expand Up @@ -63,10 +63,12 @@ export function getNotebookCellMetadataFromInteractiveDocumentElement(interactiv
return cellMetadata;
}



export function getNotebookCellMetadataFromNotebookCellElement(notebookCell: vscodeLike.NotebookCell): NotebookCellMetadata {
const cellMetadata = createDefaultNotebookCellMetadata();

const metadata = notebookCell.metadata?.custom?.metadata;
const metadata = getCellMetadata(notebookCell);

if (typeof metadata === 'object') {
// first try to get the old `dotnet_interactive` value...
Expand Down Expand Up @@ -111,13 +113,37 @@ export function getNotebookDocumentMetadataFromInteractiveDocument(interactiveDo
return notebookMetadata;
}

let _useLegacyMetadata = true;
export function useLegacyMetadata() {
return _useLegacyMetadata;//?
}

export function setUseLegacyMetadata(value: boolean) {
_useLegacyMetadata = value;
}

export function getCellMetadata(cell: vscodeLike.NotebookCell) {
return (useLegacyMetadata() ? cell.metadata?.custom?.metadata : cell.metadata?.metadata) || {};
}

export function getDocumentMetadata(document: vscodeLike.NotebookDocument) {
const ipynbMetadata = (useLegacyMetadata() ? document.metadata?.custom?.metadata?.polyglot_notebook : document.metadata?.metadata?.polyglot_notebook) ?? {};
const metadata = document.metadata.polyglot_notebook ?? {};

const merged = { ...ipynbMetadata, ...metadata };

return merged;
}

export function getNotebookDocumentMetadataFromNotebookDocument(document: vscodeLike.NotebookDocument): NotebookDocumentMetadata {
const notebookMetadata = createDefaultNotebookDocumentMetadata();
let setDefaultKernel = false;
let setItems = false;


// .dib files will have their metadata at the root; .ipynb files will have their metadata a little deeper
const polyglot_notebook = document.metadata.polyglot_notebook ?? document.metadata?.custom?.metadata?.polyglot_notebook;
const polyglot_notebook = getDocumentMetadata(document);

if (typeof polyglot_notebook === 'object') {
const kernelInfo = polyglot_notebook.kernelInfo;
if (typeof kernelInfo === 'object') {
Expand Down Expand Up @@ -187,26 +213,25 @@ export function getKernelspecMetadataFromIpynbNotebookDocument(notebook: vscodeL
name: ''
};

const custom = notebook.metadata.custom;
if (typeof custom === 'object') {
const metadata = custom.metadata;
if (typeof metadata === 'object') {
const kernelspec = metadata.kernelspec;
if (typeof kernelspec === 'object') {
const display_name = kernelspec.display_name;
if (typeof display_name === 'string') {
kernelspecMetadata.display_name = display_name;
}
const metadata = useLegacyMetadata() ? notebook.metadata.custom?.metadata : notebook.metadata.metadata; //?

const language = kernelspec.language;
if (typeof language === 'string') {
kernelspecMetadata.language = language;
}

const name = kernelspec.name;
if (typeof name === 'string') {
kernelspecMetadata.name = name;
}
if (typeof metadata === 'object') {
const kernelspec = metadata.kernelspec;
if (typeof kernelspec === 'object') {
const display_name = kernelspec.display_name;
if (typeof display_name === 'string') {
kernelspecMetadata.display_name = display_name;
}

const language = kernelspec.language;
if (typeof language === 'string') {
kernelspecMetadata.language = language;
}

const name = kernelspec.name;
if (typeof name === 'string') {
kernelspecMetadata.name = name;
}
}
}
Expand Down Expand Up @@ -268,10 +293,16 @@ export function createNewIpynbMetadataWithNotebookDocumentMetadata(existingMetad

// kernelspec
const kernelspec = getKernelspecMetadataFromNotebookDocumentMetadata(notebookDocumentMetadata);
resultMetadata.custom = resultMetadata.custom ?? {};
resultMetadata.custom.metadata = resultMetadata.custom.metadata ?? {};
resultMetadata.custom.metadata.kernelspec = kernelspec;
resultMetadata.custom.metadata.polyglot_notebook = notebookDocumentMetadata;
if (useLegacyMetadata()) {
resultMetadata.custom = resultMetadata.custom ?? {};
resultMetadata.custom.metadata = resultMetadata.custom.metadata ?? {};
resultMetadata.custom.metadata.kernelspec = kernelspec;
resultMetadata.custom.metadata.polyglot_notebook = notebookDocumentMetadata;
} else {
resultMetadata.metadata = resultMetadata.metadata ?? {};
resultMetadata.metadata.kernelspec = kernelspec;
resultMetadata.metadata.polyglot_notebook = notebookDocumentMetadata;
}
return resultMetadata;
}

Expand All @@ -280,8 +311,21 @@ export function getRawInteractiveDocumentElementMetadataFromNotebookCellMetadata
}

export function getRawNotebookCellMetadataFromNotebookCellMetadata(notebookCellMetadata: NotebookCellMetadata): { [key: string]: any } {
return {
custom: {
if (useLegacyMetadata()) {
return {
custom: {
metadata: {
// this is the canonical metadata
polyglot_notebook: notebookCellMetadata,
// this is to maintain backwards compatibility for a while
dotnet_interactive: {
language: notebookCellMetadata.kernelName
}
}
}
};
} else {
return {
metadata: {
// this is the canonical metadata
polyglot_notebook: notebookCellMetadata,
Expand All @@ -290,8 +334,8 @@ export function getRawNotebookCellMetadataFromNotebookCellMetadata(notebookCellM
language: notebookCellMetadata.kernelName
}
}
}
};
};
}
}

export function getRawInteractiveDocumentMetadataFromNotebookDocumentMetadata(notebookDocumentMetadata: NotebookDocumentMetadata): { [key: string]: any } {
Expand All @@ -303,12 +347,20 @@ export function getMergedRawNotebookDocumentMetadataFromNotebookDocumentMetadata

if (createForIpynb) {
const kernelspec = getKernelspecMetadataFromNotebookDocumentMetadata(notebookDocumentMetadata);
rawMetadata.custom = {
metadata: {

if (useLegacyMetadata()) {
rawMetadata.custom = {
metadata: {
kernelspec,
polyglot_notebook: notebookDocumentMetadata
},
};
} else {
rawMetadata.metadata = {
kernelspec,
polyglot_notebook: notebookDocumentMetadata
},
};
};
}
} else {
rawMetadata.polyglot_notebook = notebookDocumentMetadata;
}
Expand Down
27 changes: 21 additions & 6 deletions src/polyglot-notebooks-vscode-common/src/notebookControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,20 @@ function stopTrackingNotebook(notebook: vscode.NotebookDocument) {
}

async function ensureCellKernelMetadata(cell: vscode.NotebookCell, options: { preferPreviousCellMetadata: boolean }): Promise<void> {

// markdown cells should not have metadata, so if we find it, remove it.
if (cell.document.languageId === 'markdown') {
const existingCellMetadata = cell.metadata?.custom?.metadata;
const existingCellMetadata = metadataUtilities.getCellMetadata(cell);
if (existingCellMetadata?.polyglot_notebook || existingCellMetadata?.dotnet_interactive) {
const updatedCellMetadata = { ...cell.metadata };
delete updatedCellMetadata.custom.metadata.dotnet_interactive;
delete updatedCellMetadata.custom.metadata.polyglot_notebook;
if (metadataUtilities.useLegacyMetadata()) {
delete updatedCellMetadata.custom.metadata.dotnet_interactive;
delete updatedCellMetadata.custom.metadata.polyglot_notebook;
}
else {
delete updatedCellMetadata.dotnet_interactive;
delete updatedCellMetadata.polyglot_notebook;
}
await vscodeNotebookManagement.replaceNotebookCellMetadata(cell.notebook.uri, cell.index, updatedCellMetadata);
}
return;
Expand Down Expand Up @@ -410,10 +417,18 @@ async function updateKernelInfoMetadata(client: InteractiveClient, document: vsc
const rawNotebookDocumentMetadata = metadataUtilities.getMergedRawNotebookDocumentMetadataFromNotebookDocumentMetadata(mergedMetadata, document.metadata, isIpynb);

if (isIpynb) {
if (!rawNotebookDocumentMetadata.custom.metadata.language_info) {
rawNotebookDocumentMetadata.custom.metadata.language_info = { name: "polyglot-notebook" };
if (metadataUtilities.useLegacyMetadata()) {
if (!rawNotebookDocumentMetadata.custom.metadata.language_info) {
rawNotebookDocumentMetadata.custom.metadata.language_info = { name: "polyglot-notebook" };
} else {
rawNotebookDocumentMetadata.custom.metadata.language_info.name = "polyglot-notebook";
}
} else {
rawNotebookDocumentMetadata.custom.metadata.language_info.name = "polyglot-notebook";
if (!rawNotebookDocumentMetadata.language_info) {
rawNotebookDocumentMetadata.language_info = { name: "polyglot-notebook" };
} else {
rawNotebookDocumentMetadata.language_info.name = "polyglot-notebook";
}
}
}
await vscodeNotebookManagement.replaceNotebookMetadata(document.uri, rawNotebookDocumentMetadata);
Expand Down
Loading
Loading