-
Notifications
You must be signed in to change notification settings - Fork 5
fix: Ensure that Code blocks are Code blocks #244
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
Merged
Changes from all commits
Commits
Show all changes
4 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { inject, injectable } from 'inversify'; | ||
| import { languages, NotebookCellKind, NotebookDocumentChangeEvent, workspace } from 'vscode'; | ||
|
|
||
| import { DEEPNOTE_NOTEBOOK_TYPE } from '../../kernels/deepnote/types'; | ||
| import { IExtensionSyncActivationService } from '../../platform/activation/types'; | ||
| import { PYTHON_LANGUAGE } from '../../platform/common/constants'; | ||
| import { IDisposableRegistry } from '../../platform/common/types'; | ||
| import { noop } from '../../platform/common/utils/misc'; | ||
|
|
||
| /** | ||
| * Ensures newly added code cells in Deepnote notebooks default to Python language. | ||
| * VS Code copies the language from adjacent cells when inserting, which causes | ||
| * new cells after SQL blocks to be SQL. This service corrects that by resetting | ||
| * unintentional language inheritance to Python. | ||
| */ | ||
| @injectable() | ||
| export class DeepnoteNewCellLanguageService implements IExtensionSyncActivationService { | ||
| constructor(@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry) {} | ||
|
|
||
| public activate(): void { | ||
| this.disposables.push(workspace.onDidChangeNotebookDocument(this.onDidChangeNotebookDocument, this)); | ||
| } | ||
|
|
||
| private async onDidChangeNotebookDocument(e: NotebookDocumentChangeEvent): Promise<void> { | ||
| if (e.notebook.notebookType !== DEEPNOTE_NOTEBOOK_TYPE) { | ||
| return; | ||
| } | ||
|
|
||
| for (const change of e.contentChanges) { | ||
| for (const cell of change.addedCells) { | ||
| if (cell.kind !== NotebookCellKind.Code) { | ||
| continue; | ||
| } | ||
|
|
||
| if (cell.document.getText().trim().length > 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const pocketType = cell.metadata?.__deepnotePocket?.type; | ||
|
|
||
| if (pocketType) { | ||
| continue; | ||
| } | ||
|
|
||
| // TODO: This will have to be revisited if we add support for other languages in Deepnote. | ||
| if (cell.document.languageId !== PYTHON_LANGUAGE) { | ||
| languages.setTextDocumentLanguage(cell.document, PYTHON_LANGUAGE).then(noop, noop); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
275 changes: 275 additions & 0 deletions
275
src/notebooks/deepnote/deepnoteNewCellLanguageService.unit.test.ts
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,275 @@ | ||
| import { expect } from 'chai'; | ||
| import { anything, verify, when } from 'ts-mockito'; | ||
| import { Disposable, NotebookCell, NotebookCellKind, NotebookDocument, TextDocument, Uri } from 'vscode'; | ||
|
|
||
| import { IDisposableRegistry } from '../../platform/common/types'; | ||
| import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../test/vscode-mock'; | ||
| import { DeepnoteNewCellLanguageService } from './deepnoteNewCellLanguageService'; | ||
|
|
||
| suite('DeepnoteNewCellLanguageService', () => { | ||
| let service: DeepnoteNewCellLanguageService; | ||
| let disposables: Disposable[]; | ||
| let notebookChangeHandler: ((e: any) => void) | undefined; | ||
|
|
||
| function createMockNotebook(notebookType: string): NotebookDocument { | ||
| return { | ||
| uri: Uri.file('/test/notebook.deepnote'), | ||
| notebookType | ||
| } as NotebookDocument; | ||
| } | ||
|
|
||
| function createMockCell(options: { | ||
| kind?: NotebookCellKind; | ||
| languageId?: string; | ||
| content?: string; | ||
| metadata?: Record<string, unknown>; | ||
| }): NotebookCell { | ||
| const { kind = NotebookCellKind.Code, languageId = 'python', content = '', metadata = {} } = options; | ||
|
|
||
| return { | ||
| index: 0, | ||
| notebook: createMockNotebook('deepnote'), | ||
| kind, | ||
| document: { | ||
| uri: Uri.file('/test/notebook.deepnote#cell0'), | ||
| languageId, | ||
| getText: () => content | ||
| } as TextDocument, | ||
| metadata, | ||
| outputs: [], | ||
| executionSummary: undefined | ||
| } as unknown as NotebookCell; | ||
| } | ||
|
|
||
| setup(() => { | ||
| resetVSCodeMocks(); | ||
| disposables = []; | ||
|
|
||
| when(mockedVSCodeNamespaces.workspace.onDidChangeNotebookDocument(anything(), anything())).thenCall( | ||
| (handler, thisArg) => { | ||
| notebookChangeHandler = (e: any) => handler.call(thisArg, e); | ||
|
|
||
| return { dispose: () => undefined }; | ||
| } | ||
| ); | ||
|
|
||
| when(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).thenReturn( | ||
| Promise.resolve({} as TextDocument) | ||
| ); | ||
|
|
||
| service = new DeepnoteNewCellLanguageService(disposables as unknown as IDisposableRegistry); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| notebookChangeHandler = undefined; | ||
| disposables.forEach((d) => d.dispose()); | ||
| }); | ||
|
|
||
| test('activate registers onDidChangeNotebookDocument listener', () => { | ||
| service.activate(); | ||
|
|
||
| verify(mockedVSCodeNamespaces.workspace.onDidChangeNotebookDocument(anything(), anything())).once(); | ||
| }); | ||
|
|
||
| test('activate adds disposable to registry', () => { | ||
| service.activate(); | ||
|
|
||
| expect(disposables.length).to.be.greaterThan(0); | ||
| }); | ||
|
|
||
| test('ignores non-deepnote notebooks', async () => { | ||
| service.activate(); | ||
| const jupyterNotebook = createMockNotebook('jupyter-notebook'); | ||
| const cell = createMockCell({ languageId: 'sql' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook: jupyterNotebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores markdown cells', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ kind: NotebookCellKind.Markup, languageId: 'markdown' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores cells with content', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ languageId: 'sql', content: 'SELECT * FROM table' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores cells that already have Python language', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ languageId: 'python' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores intentional SQL blocks (with __deepnotePocket.type)', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ | ||
| languageId: 'sql', | ||
| metadata: { __deepnotePocket: { type: 'sql' } } | ||
| }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores intentional chart blocks (with __deepnotePocket.type)', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ | ||
| languageId: 'json', | ||
| metadata: { __deepnotePocket: { type: 'chart-vega' } } | ||
| }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('ignores intentional input blocks (with __deepnotePocket.type)', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ | ||
| languageId: 'plaintext', | ||
| metadata: { __deepnotePocket: { type: 'input-text' } } | ||
| }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('changes SQL cell to Python when no __deepnotePocket metadata', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ languageId: 'sql' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(cell.document, 'python')).once(); | ||
| }); | ||
|
|
||
| test('changes JSON cell to Python when no __deepnotePocket metadata', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ languageId: 'json' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(cell.document, 'python')).once(); | ||
| }); | ||
|
|
||
| test('handles multiple added cells', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const sqlCell = createMockCell({ languageId: 'sql' }); | ||
| const pythonCell = createMockCell({ languageId: 'python' }); | ||
| const jsonCell = createMockCell({ languageId: 'json' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [sqlCell, pythonCell, jsonCell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(sqlCell.document, 'python')).once(); | ||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(pythonCell.document, anything())).never(); | ||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(jsonCell.document, 'python')).once(); | ||
| }); | ||
|
|
||
| test('handles multiple content changes', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell1 = createMockCell({ languageId: 'sql' }); | ||
| const cell2 = createMockCell({ languageId: 'javascript' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell1] }, { addedCells: [cell2] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(cell1.document, 'python')).once(); | ||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(cell2.document, 'python')).once(); | ||
| }); | ||
|
|
||
| test('ignores content changes with no added cells', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(anything(), anything())).never(); | ||
| }); | ||
|
|
||
| test('changes cells with whitespace-only content to Python', async () => { | ||
| service.activate(); | ||
| const notebook = createMockNotebook('deepnote'); | ||
| const cell = createMockCell({ languageId: 'sql', content: ' \n\t ' }); | ||
|
|
||
| notebookChangeHandler!({ | ||
| notebook, | ||
| contentChanges: [{ addedCells: [cell] }] | ||
| }); | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| verify(mockedVSCodeNamespaces.languages.setTextDocumentLanguage(cell.document, 'python')).once(); | ||
| }); | ||
| }); | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.