From a0b48358f5a419a80ead883a81f11177dd319f9f Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sun, 12 Apr 2020 02:10:20 -0700 Subject: [PATCH] Implemented new setting "pyright.disableOrganizeImports". --- client/package.json | 6 ++++++ docs/settings.md | 2 ++ server/src/commands/createTypeStub.ts | 1 + server/src/commands/quickActionCommand.ts | 8 +++++++- server/src/languageServerBase.ts | 7 +++++++ server/src/languageService/codeActionProvider.ts | 6 +++++- server/src/server.ts | 2 ++ server/src/tests/harness/fourslash/testState.ts | 1 + server/src/workspaceMap.ts | 1 + 9 files changed, 32 insertions(+), 2 deletions(-) diff --git a/client/package.json b/client/package.json index ef2b0419d..c8a3845c1 100644 --- a/client/package.json +++ b/client/package.json @@ -93,6 +93,12 @@ "description": "Disables type completion, definitions, and references.", "scope": "resource" }, + "pyright.disableOrganizeImports": { + "type": "boolean", + "default": false, + "description": "Disables the “Organize Imports” command.", + "scope": "resource" + }, "pyright.openFilesOnly": { "type": "boolean", "default": true, diff --git a/docs/settings.md b/docs/settings.md index 079790a24..c6e286112 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -4,6 +4,8 @@ The Pyright VS Code extension honors the following settings. **pyright.disableLanguageServices** [boolean]: Disables all language services except for “hover”. This includes type completion, signature completion, find definition, find references, and find symbols in file. This option is useful if you want to use pyright only as a type checker but want to run another Python language server for langue service features. +**pyright.disableOrganizeImports** [boolean]: Disables the “Organize Imports” command. This is useful if you are using another extension that provides similar functionality and you don’t want the two extensions to fight each other. + **pyright.openFilesOnly** [boolean]: Determines whether pyright analyzes (and reports errors for) all files in the workspace, as indicated by the config file. If this option is set to true, pyright analyzes only open files. **pyright.typeCheckingMode** ["off", "basic", "strict"]: Determines the default type-checking level used by pyright. This can be overridden in the configuration file. diff --git a/server/src/commands/createTypeStub.ts b/server/src/commands/createTypeStub.ts index 5f1a4b122..a33ee95e9 100644 --- a/server/src/commands/createTypeStub.ts +++ b/server/src/commands/createTypeStub.ts @@ -31,6 +31,7 @@ export class CreateTypeStubCommand implements ServerCommand { rootUri: convertPathToUri(workspaceRoot), serviceInstance: service, disableLanguageServices: true, + disableOrganizeImports: true, }; service.setCompletionCallback((results) => { diff --git a/server/src/commands/quickActionCommand.ts b/server/src/commands/quickActionCommand.ts index eca66eb86..74d418c0a 100644 --- a/server/src/commands/quickActionCommand.ts +++ b/server/src/commands/quickActionCommand.ts @@ -1,5 +1,5 @@ /* - * createTypeStub.ts + * quickActionCommand.ts * * Implements command that maps to a quick action. */ @@ -9,6 +9,7 @@ import { CancellationToken, ExecuteCommandParams, TextEdit } from 'vscode-langua import { convertUriToPath } from '../common/pathUtils'; import { LanguageServerInterface } from '../languageServerBase'; import { ServerCommand } from './commandController'; +import { Commands } from './commands'; export class QuickActionCommand implements ServerCommand { constructor(private _ls: LanguageServerInterface) {} @@ -19,6 +20,11 @@ export class QuickActionCommand implements ServerCommand { const otherArgs = params.arguments.slice(1); const filePath = convertUriToPath(docUri); const workspace = this._ls.getWorkspaceForFile(filePath); + + if (params.command === Commands.orderImports && workspace.disableOrganizeImports) { + return []; + } + const editActions = workspace.serviceInstance.performQuickAction( filePath, params.command, diff --git a/server/src/languageServerBase.ts b/server/src/languageServerBase.ts index c960dd331..551647240 100644 --- a/server/src/languageServerBase.ts +++ b/server/src/languageServerBase.ts @@ -70,6 +70,7 @@ export interface ServerSettings { typeCheckingMode?: string; useLibraryCodeForTypes?: boolean; disableLanguageServices?: boolean; + disableOrganizeImports?: boolean; autoSearchPaths?: boolean; watchForLibraryChanges?: boolean; } @@ -80,6 +81,7 @@ export interface WorkspaceServiceInstance { rootUri: string; serviceInstance: AnalyzerService; disableLanguageServices: boolean; + disableOrganizeImports: boolean; } export interface WindowInterface { @@ -152,6 +154,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface { params: CodeActionParams, token: CancellationToken ): Promise<(Command | CodeAction)[] | undefined | null>; + abstract async getSettings(workspace: WorkspaceServiceInstance): Promise; protected getConfiguration(workspace: WorkspaceServiceInstance, section: string) { @@ -296,6 +299,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface { rootUri: folder.uri, serviceInstance: this.createAnalyzerService(folder.name), disableLanguageServices: false, + disableOrganizeImports: false, }); }); } else if (params.rootPath) { @@ -305,6 +309,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface { rootUri: '', serviceInstance: this.createAnalyzerService(params.rootPath), disableLanguageServices: false, + disableOrganizeImports: false, }); } @@ -604,6 +609,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface { rootUri: workspace.uri, serviceInstance: this.createAnalyzerService(workspace.name), disableLanguageServices: false, + disableOrganizeImports: false, }; this._workspaceMap.set(rootPath, newWorkspace); await this.updateSettingsForWorkspace(newWorkspace); @@ -675,6 +681,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface { const serverSettings = await this.getSettings(workspace); this.updateOptionsAndRestartService(workspace, serverSettings); workspace.disableLanguageServices = !!serverSettings.disableLanguageServices; + workspace.disableOrganizeImports = !!serverSettings.disableOrganizeImports; } updateOptionsAndRestartService( diff --git a/server/src/languageService/codeActionProvider.ts b/server/src/languageService/codeActionProvider.ts index 93200bc9c..46e04831c 100644 --- a/server/src/languageService/codeActionProvider.ts +++ b/server/src/languageService/codeActionProvider.ts @@ -26,7 +26,11 @@ export class CodeActionProvider { Command.create('Organize Imports', Commands.orderImports), CodeActionKind.SourceOrganizeImports ); - const codeActions: CodeAction[] = [sortImportsCodeAction]; + const codeActions: CodeAction[] = []; + + if (!workspace.disableOrganizeImports) { + codeActions.push(sortImportsCodeAction); + } if (!workspace.disableLanguageServices) { const diags = workspace.serviceInstance.getDiagnosticsForRange(filePath, range); diff --git a/server/src/server.ts b/server/src/server.ts index 0f9992e5f..eb957a28f 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -46,11 +46,13 @@ class PyrightServer extends LanguageServerBase { serverSettings.openFilesOnly = !!pyrightSection.openFilesOnly; serverSettings.useLibraryCodeForTypes = !!pyrightSection.useLibraryCodeForTypes; serverSettings.disableLanguageServices = !!pyrightSection.disableLanguageServices; + serverSettings.disableOrganizeImports = !!pyrightSection.disableOrganizeImports; serverSettings.typeCheckingMode = pyrightSection.typeCheckingMode; } else { serverSettings.openFilesOnly = true; serverSettings.useLibraryCodeForTypes = false; serverSettings.disableLanguageServices = false; + serverSettings.disableOrganizeImports = false; serverSettings.typeCheckingMode = undefined; } } catch (error) { diff --git a/server/src/tests/harness/fourslash/testState.ts b/server/src/tests/harness/fourslash/testState.ts index 72b377935..5ce257de7 100644 --- a/server/src/tests/harness/fourslash/testState.ts +++ b/server/src/tests/harness/fourslash/testState.ts @@ -139,6 +139,7 @@ export class TestState { rootUri: convertPathToUri(this.fs.getModulePath()), serviceInstance: service, disableLanguageServices: false, + disableOrganizeImports: false, }; if (this._files.length > 0) { diff --git a/server/src/workspaceMap.ts b/server/src/workspaceMap.ts index 7b7635537..8cc71a758 100644 --- a/server/src/workspaceMap.ts +++ b/server/src/workspaceMap.ts @@ -63,6 +63,7 @@ export class WorkspaceMap extends Map { rootUri: '', serviceInstance: this._ls.createAnalyzerService(this._defaultWorkspacePath), disableLanguageServices: false, + disableOrganizeImports: false, }; this.set(this._defaultWorkspacePath, defaultWorkspace); this._ls.updateSettingsForWorkspace(defaultWorkspace).ignoreErrors();