forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle both API versions #15932
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
Merged
IanMatthewHuff
merged 3 commits into
microsoft:main
from
IanMatthewHuff:dev/ianhu/handleAPIs
Apr 13, 2021
Merged
Handle both API versions #15932
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
import { NotebookCell, NotebookCellRange, NotebookDocumentMetadata, Uri } from 'vscode'; | ||
import { NotebookDocument } from 'vscode-proposed'; | ||
|
||
export interface ISafeNotebookDocument extends NotebookDocument {} | ||
|
||
// The old API for NotebookDocument for vscode engine version < 1.56 | ||
interface IOldNotebookDocument { | ||
readonly cells: ReadonlyArray<NotebookCell>; | ||
} | ||
|
||
// In the Python extension we often need to support different changing | ||
// VS Code api versions. This class adds a layer of indirection so that we | ||
// can handle changes to the NotebookDocument class in the API | ||
// When python extension ships with engine version 1.56 for stable and insiders we can remove | ||
// this class and just use NotebookDocument directly | ||
export class SafeNotebookDocument implements ISafeNotebookDocument { | ||
constructor(private notebook: NotebookDocument) {} | ||
|
||
// Functions changed to handle multiple APIs | ||
public getCells(range?: NotebookCellRange): ReadonlyArray<NotebookCell> { | ||
if ('getCells' in this.notebook) { | ||
return this.notebook.getCells(range); | ||
} | ||
// Old API with .cells | ||
return (this.notebook as IOldNotebookDocument).cells; | ||
} | ||
|
||
public cellAt(index: number): NotebookCell { | ||
if ('cellAt' in this.notebook) { | ||
return this.notebook.cellAt(index); | ||
} | ||
|
||
// Old API with .cells | ||
return (this.notebook as IOldNotebookDocument).cells[index]; | ||
} | ||
|
||
public get cellCount(): number { | ||
if ('cellCount' in this.notebook) { | ||
return this.notebook.cellCount; | ||
} | ||
|
||
// Old API with .cells | ||
return (this.notebook as IOldNotebookDocument).cells.length; | ||
} | ||
|
||
// Functions directly implemented by NotebookDocument | ||
public get uri(): Uri { | ||
return this.notebook.uri; | ||
} | ||
|
||
public get version(): number { | ||
return this.notebook.version; | ||
} | ||
|
||
public get fileName(): string { | ||
return this.notebook.fileName; | ||
} | ||
|
||
public get isDirty(): boolean { | ||
return this.notebook.isDirty; | ||
} | ||
|
||
public get isUntitled(): boolean { | ||
return this.notebook.isUntitled; | ||
} | ||
|
||
public get isClosed(): boolean { | ||
return this.notebook.isClosed; | ||
} | ||
|
||
public get metadata(): NotebookDocumentMetadata { | ||
return this.notebook.metadata; | ||
} | ||
|
||
public get viewType(): string { | ||
return this.notebook.viewType; | ||
} | ||
|
||
public save(): Thenable<boolean> { | ||
return this.notebook.save(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion only: we can create a GitHub issue for deprecating this wrapper and reference it in the comment here so we don't forget to remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, if we move forward with this PR I'll add the issue. Just don't want to add it if we don't go with the wrapper class.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using a new class you can use the ES6 Proxy object.
This way u dont need to implement each property or method.
Just override getCells, cellAt and cellCount
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But thats an optionally improvement, hence a non blocking susggestion.
Else LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proxy totally looks like what I was searching for earlier today. I'm going to go ahead with this as the goal here is to just remove this code in a few releases. I filed this issue #5503 to remove it once 1.56 has been in stable for a release or two.