From 058d984755d5504df020b8ebcba381332eedca75 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Fri, 16 Aug 2019 22:51:50 -0700 Subject: [PATCH] Added new setting "pyright.disableLanguageServices" in VS Code extension for disabling language service features. --- README.md | 1 + client/package.json | 8 ++- client/src/extension.ts | 2 +- docs/settings.md | 13 ++++ server/src/server.ts | 130 +++++++++++++++++++++++++--------------- 5 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 docs/settings.md diff --git a/README.md b/README.md index 7d9c806be..932547615 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ To update to the latest version: * [Getting Started with Type Checking](/docs/getting-started.md) * [Command-line Options](/docs/command-line.md) * [Configuration](/docs/configuration.md) +* [Settings](/docs/settings.md) * [Comments](/docs/comments.md) * [Import Resolution](/docs/import-resolution.md) * [Building & Debugging](/docs/build-debug.md) diff --git a/client/package.json b/client/package.json index eb77d9d83..2fcb3bbe3 100644 --- a/client/package.json +++ b/client/package.json @@ -42,7 +42,7 @@ ], "configuration": { "type": "object", - "title": "Python Configuration", + "title": "Pyright Configuration", "properties": { "python.analysis.typeshedPaths": { "type": "array", @@ -64,6 +64,12 @@ "default": "", "description": "Path to folder with a list of Virtual Environments.", "scope": "resource" + }, + "pyright.disableLanguageServices": { + "type": "boolean", + "default": false, + "description": "Disables type completion, definitions, and references.", + "scope": "resource" } } }, diff --git a/client/src/extension.ts b/client/src/extension.ts index a5b528f4e..d8337d3d0 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -34,7 +34,7 @@ export function activate(context: ExtensionContext) { }], synchronize: { // Synchronize the setting section to the server. - configurationSection: 'python' + configurationSection: ['python', 'pyright'] } } diff --git a/docs/settings.md b/docs/settings.md new file mode 100644 index 000000000..fb22e53fd --- /dev/null +++ b/docs/settings.md @@ -0,0 +1,13 @@ +# Pyright Settings + +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. + +**python.analysis.typeshedPaths** [array of paths]: Paths to look for typeshed modules. Pyright currently honors only the first path in the array. + +**python.pythonPath** [path]: Path to Python interpreter. + +**python.venvPath** [path]: Path to folder with subdirectories that contain virtual environments. + + diff --git a/server/src/server.ts b/server/src/server.ts index 684d5b8ba..6266285fe 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -27,11 +27,16 @@ interface PythonSettings { }; } +interface PyrightSettings { + disableLanguageServices?: boolean; +} + interface Settings { // If more sections are added to this interface, // make sure to update the sections array in the // updateSettingsForAllWorkspaces method. python?: PythonSettings; + pyright?: PyrightSettings; } interface WorkspaceServiceInstance { @@ -39,6 +44,7 @@ interface WorkspaceServiceInstance { rootPath: string; rootUri: string; serviceInstance: AnalyzerService; + disableLanguageServices: boolean; } // Stash the base directory into a global variable. @@ -122,7 +128,8 @@ _connection.onInitialize((params): InitializeResult => { workspaceName: folder.name, rootPath: path, rootUri: folder.uri, - serviceInstance: _createAnalyzerService(folder.name) + serviceInstance: _createAnalyzerService(folder.name), + disableLanguageServices: false }); }); } else if (params.rootPath) { @@ -130,7 +137,8 @@ _connection.onInitialize((params): InitializeResult => { workspaceName: '', rootPath: params.rootPath, rootUri: '', - serviceInstance: _createAnalyzerService(params.rootPath) + serviceInstance: _createAnalyzerService(params.rootPath), + disableLanguageServices: false }); } @@ -140,7 +148,8 @@ _connection.onInitialize((params): InitializeResult => { workspaceName: '', rootPath: '', rootUri: '', - serviceInstance: _createAnalyzerService('') + serviceInstance: _createAnalyzerService(''), + disableLanguageServices: false }); _connection.console.log(`Fetching settings for workspace(s)`); @@ -205,8 +214,11 @@ _connection.onDefinition(params => { column: params.position.character }; - const service = _getWorkspaceForFile(filePath).serviceInstance; - const locations = service.getDefinitionForPosition(filePath, position); + const workspace = _getWorkspaceForFile(filePath); + if (workspace.disableLanguageServices) { + return; + } + const locations = workspace.serviceInstance.getDefinitionForPosition(filePath, position); if (!locations) { return undefined; } @@ -222,8 +234,11 @@ _connection.onReferences(params => { column: params.position.character }; - const service = _getWorkspaceForFile(filePath).serviceInstance; - const locations = service.getReferencesForPosition(filePath, position, + const workspace = _getWorkspaceForFile(filePath); + if (workspace.disableLanguageServices) { + return; + } + const locations = workspace.serviceInstance.getReferencesForPosition(filePath, position, params.context.includeDeclaration); if (!locations) { return undefined; @@ -234,8 +249,13 @@ _connection.onReferences(params => { _connection.onDocumentSymbol(params => { const filePath = _convertUriToPath(params.textDocument.uri); - const service = _getWorkspaceForFile(filePath).serviceInstance; - const symbols = service.getSymbolsForDocument(filePath); + + const worksspace = _getWorkspaceForFile(filePath); + if (worksspace.disableLanguageServices) { + return; + } + + const symbols = worksspace.serviceInstance.getSymbolsForDocument(filePath); return symbols; }); @@ -247,8 +267,8 @@ _connection.onHover(params => { column: params.position.character }; - const service = _getWorkspaceForFile(filePath).serviceInstance; - const hoverResults = service.getHoverForPosition(filePath, position); + const workspace = _getWorkspaceForFile(filePath); + const hoverResults = workspace.serviceInstance.getHoverForPosition(filePath, position); if (!hoverResults) { return undefined; } @@ -277,8 +297,11 @@ _connection.onSignatureHelp(params => { column: params.position.character }; - const service = _getWorkspaceForFile(filePath).serviceInstance; - const signatureHelpResults = service.getSignatureHelpForPosition( + const workspace = _getWorkspaceForFile(filePath); + if (workspace.disableLanguageServices) { + return; + } + const signatureHelpResults = workspace.serviceInstance.getSignatureHelpForPosition( filePath, position); if (!signatureHelpResults) { return undefined; @@ -311,8 +334,11 @@ _connection.onCompletion(params => { column: params.position.character }; - const service = _getWorkspaceForFile(filePath).serviceInstance; - return service.getCompletionsForPosition(filePath, position); + const workspace = _getWorkspaceForFile(filePath); + if (workspace.disableLanguageServices) { + return; + } + return workspace.serviceInstance.getCompletionsForPosition(filePath, position); }); _connection.onDidOpenTextDocument(params => { @@ -339,20 +365,31 @@ _connection.onDidCloseTextDocument(params => { service.setFileClosed(filePath); }); +function getConfiguration(workspace: WorkspaceServiceInstance, section: string) { + if (workspace.rootUri) { + return _connection.workspace.getConfiguration({ + scopeUri: workspace.rootUri || undefined, + section + }); + } else { + return _connection.workspace.getConfiguration(section); + } +} + function updateSettingsForAllWorkspaces() { _workspaceMap.forEach(workspace => { - let settingsPromise: Thenable; - if (workspace.rootUri) { - settingsPromise = _connection.workspace.getConfiguration({ - scopeUri: workspace.rootUri || undefined, - section: 'python' - }); - } else { - settingsPromise = _connection.workspace.getConfiguration( - 'python'); - } - settingsPromise.then(settings => { - updateOptionsAndRestartService(workspace, { python: settings }); + const pythonSettingsPromise = getConfiguration(workspace, 'python'); + pythonSettingsPromise.then((settings: PythonSettings) => { + updateOptionsAndRestartService(workspace, settings); + }, () => { + // An error occurred trying to read the settings + // for this workspace, so ignore. + }); + + const pyrightSettingsPromise = getConfiguration(workspace, 'pyright'); + pyrightSettingsPromise.then((settings?: PyrightSettings) => { + workspace.disableLanguageServices = settings !== undefined && + !!settings.disableLanguageServices; }, () => { // An error occurred trying to read the settings // for this workspace, so ignore. @@ -361,33 +398,31 @@ function updateSettingsForAllWorkspaces() { } function updateOptionsAndRestartService(workspace: WorkspaceServiceInstance, - settings: Settings) { + settings: PythonSettings) { const commandLineOptions = new CommandLineOptions(workspace.rootPath, true); commandLineOptions.watch = true; commandLineOptions.verboseOutput = true; - if (settings.python) { - if (settings.python.venvPath) { - commandLineOptions.venvPath = combinePaths(workspace.rootPath || _rootPath, - normalizePath(_expandPathVariables(settings.python.venvPath))); - } + if (settings.venvPath) { + commandLineOptions.venvPath = combinePaths(workspace.rootPath || _rootPath, + normalizePath(_expandPathVariables(settings.venvPath))); + } - if (settings.python.pythonPath) { - commandLineOptions.pythonPath = combinePaths(workspace.rootPath || _rootPath, - normalizePath(_expandPathVariables(settings.python.pythonPath))); - } + if (settings.pythonPath) { + commandLineOptions.pythonPath = combinePaths(workspace.rootPath || _rootPath, + normalizePath(_expandPathVariables(settings.pythonPath))); + } - if (settings.python.analysis && - settings.python.analysis.typeshedPaths && - settings.python.analysis.typeshedPaths.length > 0) { + if (settings.analysis && + settings.analysis.typeshedPaths && + settings.analysis.typeshedPaths.length > 0) { - // Pyright supports only one typeshed path currently, whereas the - // official VS Code Python extension supports multiple typeshed paths. - // We'll use the first one specified and ignore the rest. - commandLineOptions.typeshedPath = - _expandPathVariables(settings.python.analysis.typeshedPaths[0]); - } + // Pyright supports only one typeshed path currently, whereas the + // official VS Code Python extension supports multiple typeshed paths. + // We'll use the first one specified and ignore the rest. + commandLineOptions.typeshedPath = + _expandPathVariables(settings.analysis.typeshedPaths[0]); } workspace.serviceInstance.setOptions(commandLineOptions); @@ -406,7 +441,8 @@ _connection.onInitialized(() => { workspaceName: workspace.name, rootPath: rootPath, rootUri: workspace.uri, - serviceInstance: _createAnalyzerService(workspace.name) + serviceInstance: _createAnalyzerService(workspace.name), + disableLanguageServices: false }); }); });