Implemented new setting "pyright.disableOrganizeImports".

This commit is contained in:
Eric Traut 2020-04-12 02:10:20 -07:00
parent b9f291afc3
commit a0b48358f5
9 changed files with 32 additions and 2 deletions

View File

@ -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,

View File

@ -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 dont 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.

View File

@ -31,6 +31,7 @@ export class CreateTypeStubCommand implements ServerCommand {
rootUri: convertPathToUri(workspaceRoot),
serviceInstance: service,
disableLanguageServices: true,
disableOrganizeImports: true,
};
service.setCompletionCallback((results) => {

View File

@ -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,

View File

@ -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<ServerSettings>;
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(

View File

@ -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);

View File

@ -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) {

View File

@ -139,6 +139,7 @@ export class TestState {
rootUri: convertPathToUri(this.fs.getModulePath()),
serviceInstance: service,
disableLanguageServices: false,
disableOrganizeImports: false,
};
if (this._files.length > 0) {

View File

@ -63,6 +63,7 @@ export class WorkspaceMap extends Map<string, WorkspaceServiceInstance> {
rootUri: '',
serviceInstance: this._ls.createAnalyzerService(this._defaultWorkspacePath),
disableLanguageServices: false,
disableOrganizeImports: false,
};
this.set(this._defaultWorkspacePath, defaultWorkspace);
this._ls.updateSettingsForWorkspace(defaultWorkspace).ignoreErrors();