Skip to content

Add isort CodeAction (sort imports on save) #1926

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 4 commits into from
Jun 11, 2018
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/1 Enhancements/156.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for the `editor.codeActionsOnSave.source.organizeImports` setting (thanks [Nathan Gaberel](https://github.com/n6g7)).
3 changes: 3 additions & 0 deletions src/client/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { IServiceContainer, IServiceManager } from './ioc/types';
import { LinterCommands } from './linters/linterCommands';
import { registerTypes as lintersRegisterTypes } from './linters/serviceRegistry';
import { ILintingEngine } from './linters/types';
import { PythonCodeActionProvider } from './providers/codeActionsProvider';
import { PythonFormattingEditProvider } from './providers/formatProvider';
import { LinterProvider } from './providers/linterProvider';
import { PythonRenameProvider } from './providers/renameProvider';
Expand Down Expand Up @@ -145,6 +146,8 @@ export async function activate(context: ExtensionContext) {
context.subscriptions.push(new TerminalProvider(serviceContainer));
context.subscriptions.push(new WorkspaceSymbols(serviceContainer));

context.subscriptions.push(languages.registerCodeActionsProvider(PYTHON, new PythonCodeActionProvider()));

type ConfigurationProvider = BaseConfigurationProvider<LaunchRequestArguments, AttachRequestArguments>;
serviceContainer.getAll<ConfigurationProvider>(IDebugConfigurationProvider).forEach(debugConfig => {
context.subscriptions.push(debug.registerDebugConfigurationProvider(debugConfig.debugType, debugConfig));
Expand Down
21 changes: 21 additions & 0 deletions src/client/providers/codeActionsProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import * as vscode from 'vscode';

export class PythonCodeActionProvider implements vscode.CodeActionProvider {
public provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeAction[]> {
const sortImports = new vscode.CodeAction(
'Sort imports on save',
vscode.CodeActionKind.SourceOrganizeImports
);
sortImports.command = {
title: 'Sort imports',
command: 'python.sortImports'
};

return [sortImports];
}
}
44 changes: 44 additions & 0 deletions src/test/providers/codeActionsProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

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

suite('CodeAction Provider', () => {
let codeActionsProvider: PythonCodeActionProvider;
let document: TypeMoq.IMock<TextDocument>;
let range: TypeMoq.IMock<Range>;
let context: TypeMoq.IMock<CodeActionContext>;
let token: TypeMoq.IMock<CancellationToken>;

setup(() => {
codeActionsProvider = new PythonCodeActionProvider();
document = TypeMoq.Mock.ofType<TextDocument>();
range = TypeMoq.Mock.ofType<Range>();
context = TypeMoq.Mock.ofType<CodeActionContext>();
token = TypeMoq.Mock.ofType<CancellationToken>();
});

test('Ensure it always returns a source.organizeImports CodeAction', async () => {
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})`);
}

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