Skip to content

Disable sort imports in notebook cells #12194

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
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
1 change: 1 addition & 0 deletions news/2 Fixes/12193.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disable `Sort Imports` command in `Notebook Cells`.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@
"command": "python.sortImports",
"title": "Refactor: Sort Imports",
"group": "Refactor",
"when": "editorLangId == python"
"when": "editorLangId == python && !notebookEditorFocused"
},
{
"command": "python.execSelectionInTerminal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
'use strict';

import * as vscode from 'vscode';
import { isNotebookCell } from '../../common/utils/misc';

export class PythonCodeActionProvider implements vscode.CodeActionProvider {
public provideCodeActions(
_document: vscode.TextDocument,
document: vscode.TextDocument,
_range: vscode.Range,
_context: vscode.CodeActionContext,
_token: vscode.CancellationToken
): vscode.ProviderResult<vscode.CodeAction[]> {
if (isNotebookCell(document)) {
return [];
}
const sortImports = new vscode.CodeAction('Sort imports', vscode.CodeActionKind.SourceOrganizeImports);
sortImports.command = {
title: 'Sort imports',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

'use strict';

import { expect } from 'chai';
import { assert, expect } from 'chai';
import * as TypeMoq from 'typemoq';
import { CancellationToken, CodeActionContext, CodeActionKind, Range, TextDocument } from 'vscode';
import { CancellationToken, CodeActionContext, CodeActionKind, Range, TextDocument, Uri } from 'vscode';
import { PythonCodeActionProvider } from '../../../client/providers/codeActionProvider/pythonCodeActionProvider';

suite('Python CodeAction Provider', () => {
Expand All @@ -24,21 +24,36 @@ suite('Python CodeAction Provider', () => {
});

test('Ensure it always returns a source.organizeImports CodeAction', async () => {
document.setup((d) => d.uri).returns(() => Uri.file('hello.ipynb'));
const codeActions = await codeActionsProvider.provideCodeActions(
document.object,
range.object,
context.object,
token.object
);

if (!codeActions) {
throw Error(`codeActionsProvider.provideCodeActions did not return an array (it returned ${codeActions})`);
}
assert.isArray(codeActions, 'codeActionsProvider.provideCodeActions did not return an array');

const organizeImportsCodeAction = codeActions.filter(
const organizeImportsCodeAction = (codeActions || []).filter(
(codeAction) => codeAction.kind === CodeActionKind.SourceOrganizeImports
);
expect(organizeImportsCodeAction).to.have.length(1);
expect(organizeImportsCodeAction[0].kind).to.eq(CodeActionKind.SourceOrganizeImports);
});
test('Ensure it does not returns a source.organizeImports CodeAction for Notebook Cells', async () => {
document.setup((d) => d.uri).returns(() => Uri.file('hello.ipynb').with({ scheme: 'vscode-notebook-cell' }));
const codeActions = await codeActionsProvider.provideCodeActions(
document.object,
range.object,
context.object,
token.object
);

assert.isArray(codeActions, 'codeActionsProvider.provideCodeActions did not return an array');

const organizeImportsCodeAction = (codeActions || []).filter(
(codeAction) => codeAction.kind === CodeActionKind.SourceOrganizeImports
);
expect(organizeImportsCodeAction).to.have.length(0);
});
});