Skip to content

Commit 5a727bd

Browse files
authored
Disable sort imports in notebook cells (#12194)
For #12193
1 parent 55b6ec0 commit 5a727bd

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

news/2 Fixes/12193.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable `Sort Imports` command in `Notebook Cells`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@
700700
"command": "python.sortImports",
701701
"title": "Refactor: Sort Imports",
702702
"group": "Refactor",
703-
"when": "editorLangId == python"
703+
"when": "editorLangId == python && !notebookEditorFocused"
704704
},
705705
{
706706
"command": "python.execSelectionInTerminal",

src/client/providers/codeActionProvider/pythonCodeActionProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
'use strict';
55

66
import * as vscode from 'vscode';
7+
import { isNotebookCell } from '../../common/utils/misc';
78

89
export class PythonCodeActionProvider implements vscode.CodeActionProvider {
910
public provideCodeActions(
10-
_document: vscode.TextDocument,
11+
document: vscode.TextDocument,
1112
_range: vscode.Range,
1213
_context: vscode.CodeActionContext,
1314
_token: vscode.CancellationToken
1415
): vscode.ProviderResult<vscode.CodeAction[]> {
16+
if (isNotebookCell(document)) {
17+
return [];
18+
}
1519
const sortImports = new vscode.CodeAction('Sort imports', vscode.CodeActionKind.SourceOrganizeImports);
1620
sortImports.command = {
1721
title: 'Sort imports',

src/test/providers/codeActionProvider/pythonCodeActionsProvider.unit.test.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
'use strict';
55

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

1111
suite('Python CodeAction Provider', () => {
@@ -24,21 +24,36 @@ suite('Python CodeAction Provider', () => {
2424
});
2525

2626
test('Ensure it always returns a source.organizeImports CodeAction', async () => {
27+
document.setup((d) => d.uri).returns(() => Uri.file('hello.ipynb'));
2728
const codeActions = await codeActionsProvider.provideCodeActions(
2829
document.object,
2930
range.object,
3031
context.object,
3132
token.object
3233
);
3334

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

38-
const organizeImportsCodeAction = codeActions.filter(
37+
const organizeImportsCodeAction = (codeActions || []).filter(
3938
(codeAction) => codeAction.kind === CodeActionKind.SourceOrganizeImports
4039
);
4140
expect(organizeImportsCodeAction).to.have.length(1);
4241
expect(organizeImportsCodeAction[0].kind).to.eq(CodeActionKind.SourceOrganizeImports);
4342
});
43+
test('Ensure it does not returns a source.organizeImports CodeAction for Notebook Cells', async () => {
44+
document.setup((d) => d.uri).returns(() => Uri.file('hello.ipynb').with({ scheme: 'vscode-notebook-cell' }));
45+
const codeActions = await codeActionsProvider.provideCodeActions(
46+
document.object,
47+
range.object,
48+
context.object,
49+
token.object
50+
);
51+
52+
assert.isArray(codeActions, 'codeActionsProvider.provideCodeActions did not return an array');
53+
54+
const organizeImportsCodeAction = (codeActions || []).filter(
55+
(codeAction) => codeAction.kind === CodeActionKind.SourceOrganizeImports
56+
);
57+
expect(organizeImportsCodeAction).to.have.length(0);
58+
});
4459
});

0 commit comments

Comments
 (0)