Skip to content

Commit

Permalink
Restore cell import sorting (#305)
Browse files Browse the repository at this point in the history
* Restore cell import sorting (#304)

* Restore cell import sorting

* Add tests

* Update version and packages for recovery release
  • Loading branch information
karthiknadig committed Jul 12, 2023
1 parent b7512f8 commit f392edd
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 217 deletions.
399 changes: 201 additions & 198 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "isort",
"displayName": "isort",
"description": "%extension.description%",
"version": "2023.10.0",
"version": "2023.10.1",
"preview": true,
"serverInfo": {
"name": "isort",
Expand Down Expand Up @@ -202,15 +202,15 @@
"@types/node": "16.x",
"@types/sinon": "^10.0.15",
"@types/vscode": "^1.74.0",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vscode/test-electron": "^2.3.3",
"@vscode/vsce": "^2.19.0",
"chai": "^4.3.7",
"chai-arrays": "^2.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^8.44.0",
"glob": "^10.3.1",
"glob": "^10.3.3",
"mocha": "^10.2.0",
"prettier": "^3.0.0",
"sinon": "^15.2.0",
Expand Down
3 changes: 1 addition & 2 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { traceError, traceInfo, traceVerbose } from './logging';
import { getDebuggerPath } from './python';
import { getExtensionSettings, getGlobalSettings, ISettings } from './settings';
import { updateStatus } from './status';
import { getLSClientTraceLevel } from './utilities';
import { getDocumentSelector } from './vscodeapi';
import { getDocumentSelector, getLSClientTraceLevel } from './utilities';

export type IInitOptions = { settings: ISettings[]; globalSettings: ISettings };

Expand Down
10 changes: 8 additions & 2 deletions src/common/sortImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {
Uri,
workspace,
} from 'vscode';
import { traceWarn } from './logging';
import { diagnosticRunner, textEditRunner } from './runner';
import { getDocumentSelector } from './vscodeapi';
import { getDocumentSelector } from './utilities';

export const notebookCellScheme = 'vscode-notebook-cell';
export const interactiveInputScheme = 'vscode-interactive-input';
Expand Down Expand Up @@ -56,7 +57,12 @@ class SortImportsCodeActionProvider implements CodeActionProvider<CodeAction> {
_token: CancellationToken,
): Promise<(CodeAction | Command)[]> {
const codeActions: (CodeAction | Command)[] = [];
if (document.uri.fsPath.includes('site-packages') || isNotebookCell(document.uri)) {
if (document.uri.fsPath.includes('site-packages')) {
traceWarn('Skipping site-packages file: ', document.uri.fsPath);
return codeActions;
}
if (isNotebookCell(document.uri)) {
traceWarn('Skipping notebook cell (not supported in server-less mode: ', document.uri.fsPath);
return codeActions;
}

Expand Down
3 changes: 2 additions & 1 deletion src/common/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import { Disposable, LanguageStatusItem, LanguageStatusSeverity, l10n } from 'vscode';
import { Command } from 'vscode-languageclient';
import { createLanguageStatusItem, getDocumentSelector } from './vscodeapi';
import { getDocumentSelector } from './utilities';
import { createLanguageStatusItem } from './vscodeapi';

let _status: LanguageStatusItem | undefined;
export function registerLanguageStatusItem(id: string, name: string, command: string): Disposable {
Expand Down
15 changes: 14 additions & 1 deletion src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import { LogLevel, Uri, WorkspaceFolder } from 'vscode';
import { Trace } from 'vscode-jsonrpc/node';
import { getWorkspaceFolders } from './vscodeapi';
import { DocumentSelector } from 'vscode-languageclient';
import { getWorkspaceFolders, isVirtualWorkspace } from './vscodeapi';

function logLevelToTrace(logLevel: LogLevel): Trace {
switch (logLevel) {
Expand Down Expand Up @@ -65,3 +66,15 @@ export async function getProjectRoot(): Promise<WorkspaceFolder> {
return rootWorkspace;
}
}

export function getDocumentSelector(): DocumentSelector {
// virtual workspaces are not supported yet
return isVirtualWorkspace()
? [{ language: 'python' }]
: [
{ scheme: 'file', language: 'python' },
{ scheme: 'untitled', language: 'python' },
{ scheme: 'vscode-notebook', language: 'python' },
{ scheme: 'vscode-notebook-cell', language: 'python' },
];
}
9 changes: 0 additions & 9 deletions src/common/vscodeapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,3 @@ export function createLanguageStatusItem(id: string, selector: DocumentSelector)
export function getActiveTextEditor(): TextEditor | undefined {
return window.activeTextEditor;
}

export function getDocumentSelector(): DocumentSelector {
return isVirtualWorkspace()
? [{ language: 'python' }]
: [
{ scheme: 'file', language: 'python' },
{ scheme: 'untitled', language: 'python' },
];
}
33 changes: 33 additions & 0 deletions src/test/ts_tests/tests/common/vscodeapi.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { assert } from 'chai';
import * as sinon from 'sinon';
import { getDocumentSelector } from '../../../../common/utilities';
import * as vscodeapi from '../../../../common/vscodeapi';

suite('Document Selector Tests', () => {
let isVirtualWorkspaceStub: sinon.SinonStub;
setup(() => {
isVirtualWorkspaceStub = sinon.stub(vscodeapi, 'isVirtualWorkspace');
isVirtualWorkspaceStub.returns(false);
});
teardown(() => {
sinon.restore();
});

test('Document selector default', () => {
const selector = getDocumentSelector();
assert.deepStrictEqual(selector, [
{ scheme: 'file', language: 'python' },
{ scheme: 'untitled', language: 'python' },
{ scheme: 'vscode-notebook', language: 'python' },
{ scheme: 'vscode-notebook-cell', language: 'python' },
]);
});
test('Document selector virtual workspace', () => {
isVirtualWorkspaceStub.returns(true);
const selector = getDocumentSelector();
assert.deepStrictEqual(selector, [{ language: 'python' }]);
});
});

0 comments on commit f392edd

Please sign in to comment.