Added new setting "pyright.disableLanguageServices" in VS Code extension for disabling language service features.

This commit is contained in:
Eric Traut 2019-08-16 22:51:50 -07:00
parent a08f9c6a24
commit 058d984755
5 changed files with 105 additions and 49 deletions

View File

@ -51,6 +51,7 @@ To update to the latest version:
* [Getting Started with Type Checking](/docs/getting-started.md) * [Getting Started with Type Checking](/docs/getting-started.md)
* [Command-line Options](/docs/command-line.md) * [Command-line Options](/docs/command-line.md)
* [Configuration](/docs/configuration.md) * [Configuration](/docs/configuration.md)
* [Settings](/docs/settings.md)
* [Comments](/docs/comments.md) * [Comments](/docs/comments.md)
* [Import Resolution](/docs/import-resolution.md) * [Import Resolution](/docs/import-resolution.md)
* [Building & Debugging](/docs/build-debug.md) * [Building & Debugging](/docs/build-debug.md)

View File

@ -42,7 +42,7 @@
], ],
"configuration": { "configuration": {
"type": "object", "type": "object",
"title": "Python Configuration", "title": "Pyright Configuration",
"properties": { "properties": {
"python.analysis.typeshedPaths": { "python.analysis.typeshedPaths": {
"type": "array", "type": "array",
@ -64,6 +64,12 @@
"default": "", "default": "",
"description": "Path to folder with a list of Virtual Environments.", "description": "Path to folder with a list of Virtual Environments.",
"scope": "resource" "scope": "resource"
},
"pyright.disableLanguageServices": {
"type": "boolean",
"default": false,
"description": "Disables type completion, definitions, and references.",
"scope": "resource"
} }
} }
}, },

View File

@ -34,7 +34,7 @@ export function activate(context: ExtensionContext) {
}], }],
synchronize: { synchronize: {
// Synchronize the setting section to the server. // Synchronize the setting section to the server.
configurationSection: 'python' configurationSection: ['python', 'pyright']
} }
} }

13
docs/settings.md Normal file
View File

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

View File

@ -27,11 +27,16 @@ interface PythonSettings {
}; };
} }
interface PyrightSettings {
disableLanguageServices?: boolean;
}
interface Settings { interface Settings {
// If more sections are added to this interface, // If more sections are added to this interface,
// make sure to update the sections array in the // make sure to update the sections array in the
// updateSettingsForAllWorkspaces method. // updateSettingsForAllWorkspaces method.
python?: PythonSettings; python?: PythonSettings;
pyright?: PyrightSettings;
} }
interface WorkspaceServiceInstance { interface WorkspaceServiceInstance {
@ -39,6 +44,7 @@ interface WorkspaceServiceInstance {
rootPath: string; rootPath: string;
rootUri: string; rootUri: string;
serviceInstance: AnalyzerService; serviceInstance: AnalyzerService;
disableLanguageServices: boolean;
} }
// Stash the base directory into a global variable. // Stash the base directory into a global variable.
@ -122,7 +128,8 @@ _connection.onInitialize((params): InitializeResult => {
workspaceName: folder.name, workspaceName: folder.name,
rootPath: path, rootPath: path,
rootUri: folder.uri, rootUri: folder.uri,
serviceInstance: _createAnalyzerService(folder.name) serviceInstance: _createAnalyzerService(folder.name),
disableLanguageServices: false
}); });
}); });
} else if (params.rootPath) { } else if (params.rootPath) {
@ -130,7 +137,8 @@ _connection.onInitialize((params): InitializeResult => {
workspaceName: '', workspaceName: '',
rootPath: params.rootPath, rootPath: params.rootPath,
rootUri: '', rootUri: '',
serviceInstance: _createAnalyzerService(params.rootPath) serviceInstance: _createAnalyzerService(params.rootPath),
disableLanguageServices: false
}); });
} }
@ -140,7 +148,8 @@ _connection.onInitialize((params): InitializeResult => {
workspaceName: '', workspaceName: '',
rootPath: '', rootPath: '',
rootUri: '', rootUri: '',
serviceInstance: _createAnalyzerService('<default>') serviceInstance: _createAnalyzerService('<default>'),
disableLanguageServices: false
}); });
_connection.console.log(`Fetching settings for workspace(s)`); _connection.console.log(`Fetching settings for workspace(s)`);
@ -205,8 +214,11 @@ _connection.onDefinition(params => {
column: params.position.character column: params.position.character
}; };
const service = _getWorkspaceForFile(filePath).serviceInstance; const workspace = _getWorkspaceForFile(filePath);
const locations = service.getDefinitionForPosition(filePath, position); if (workspace.disableLanguageServices) {
return;
}
const locations = workspace.serviceInstance.getDefinitionForPosition(filePath, position);
if (!locations) { if (!locations) {
return undefined; return undefined;
} }
@ -222,8 +234,11 @@ _connection.onReferences(params => {
column: params.position.character column: params.position.character
}; };
const service = _getWorkspaceForFile(filePath).serviceInstance; const workspace = _getWorkspaceForFile(filePath);
const locations = service.getReferencesForPosition(filePath, position, if (workspace.disableLanguageServices) {
return;
}
const locations = workspace.serviceInstance.getReferencesForPosition(filePath, position,
params.context.includeDeclaration); params.context.includeDeclaration);
if (!locations) { if (!locations) {
return undefined; return undefined;
@ -234,8 +249,13 @@ _connection.onReferences(params => {
_connection.onDocumentSymbol(params => { _connection.onDocumentSymbol(params => {
const filePath = _convertUriToPath(params.textDocument.uri); 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; return symbols;
}); });
@ -247,8 +267,8 @@ _connection.onHover(params => {
column: params.position.character column: params.position.character
}; };
const service = _getWorkspaceForFile(filePath).serviceInstance; const workspace = _getWorkspaceForFile(filePath);
const hoverResults = service.getHoverForPosition(filePath, position); const hoverResults = workspace.serviceInstance.getHoverForPosition(filePath, position);
if (!hoverResults) { if (!hoverResults) {
return undefined; return undefined;
} }
@ -277,8 +297,11 @@ _connection.onSignatureHelp(params => {
column: params.position.character column: params.position.character
}; };
const service = _getWorkspaceForFile(filePath).serviceInstance; const workspace = _getWorkspaceForFile(filePath);
const signatureHelpResults = service.getSignatureHelpForPosition( if (workspace.disableLanguageServices) {
return;
}
const signatureHelpResults = workspace.serviceInstance.getSignatureHelpForPosition(
filePath, position); filePath, position);
if (!signatureHelpResults) { if (!signatureHelpResults) {
return undefined; return undefined;
@ -311,8 +334,11 @@ _connection.onCompletion(params => {
column: params.position.character column: params.position.character
}; };
const service = _getWorkspaceForFile(filePath).serviceInstance; const workspace = _getWorkspaceForFile(filePath);
return service.getCompletionsForPosition(filePath, position); if (workspace.disableLanguageServices) {
return;
}
return workspace.serviceInstance.getCompletionsForPosition(filePath, position);
}); });
_connection.onDidOpenTextDocument(params => { _connection.onDidOpenTextDocument(params => {
@ -339,20 +365,31 @@ _connection.onDidCloseTextDocument(params => {
service.setFileClosed(filePath); 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() { function updateSettingsForAllWorkspaces() {
_workspaceMap.forEach(workspace => { _workspaceMap.forEach(workspace => {
let settingsPromise: Thenable<PythonSettings>; const pythonSettingsPromise = getConfiguration(workspace, 'python');
if (workspace.rootUri) { pythonSettingsPromise.then((settings: PythonSettings) => {
settingsPromise = _connection.workspace.getConfiguration({ updateOptionsAndRestartService(workspace, settings);
scopeUri: workspace.rootUri || undefined, }, () => {
section: 'python' // An error occurred trying to read the settings
}); // for this workspace, so ignore.
} else { });
settingsPromise = _connection.workspace.getConfiguration(
'python'); const pyrightSettingsPromise = getConfiguration(workspace, 'pyright');
} pyrightSettingsPromise.then((settings?: PyrightSettings) => {
settingsPromise.then(settings => { workspace.disableLanguageServices = settings !== undefined &&
updateOptionsAndRestartService(workspace, { python: settings }); !!settings.disableLanguageServices;
}, () => { }, () => {
// An error occurred trying to read the settings // An error occurred trying to read the settings
// for this workspace, so ignore. // for this workspace, so ignore.
@ -361,33 +398,31 @@ function updateSettingsForAllWorkspaces() {
} }
function updateOptionsAndRestartService(workspace: WorkspaceServiceInstance, function updateOptionsAndRestartService(workspace: WorkspaceServiceInstance,
settings: Settings) { settings: PythonSettings) {
const commandLineOptions = new CommandLineOptions(workspace.rootPath, true); const commandLineOptions = new CommandLineOptions(workspace.rootPath, true);
commandLineOptions.watch = true; commandLineOptions.watch = true;
commandLineOptions.verboseOutput = true; commandLineOptions.verboseOutput = true;
if (settings.python) { if (settings.venvPath) {
if (settings.python.venvPath) { commandLineOptions.venvPath = combinePaths(workspace.rootPath || _rootPath,
commandLineOptions.venvPath = combinePaths(workspace.rootPath || _rootPath, normalizePath(_expandPathVariables(settings.venvPath)));
normalizePath(_expandPathVariables(settings.python.venvPath))); }
}
if (settings.python.pythonPath) { if (settings.pythonPath) {
commandLineOptions.pythonPath = combinePaths(workspace.rootPath || _rootPath, commandLineOptions.pythonPath = combinePaths(workspace.rootPath || _rootPath,
normalizePath(_expandPathVariables(settings.python.pythonPath))); normalizePath(_expandPathVariables(settings.pythonPath)));
} }
if (settings.python.analysis && if (settings.analysis &&
settings.python.analysis.typeshedPaths && settings.analysis.typeshedPaths &&
settings.python.analysis.typeshedPaths.length > 0) { settings.analysis.typeshedPaths.length > 0) {
// Pyright supports only one typeshed path currently, whereas the // Pyright supports only one typeshed path currently, whereas the
// official VS Code Python extension supports multiple typeshed paths. // official VS Code Python extension supports multiple typeshed paths.
// We'll use the first one specified and ignore the rest. // We'll use the first one specified and ignore the rest.
commandLineOptions.typeshedPath = commandLineOptions.typeshedPath =
_expandPathVariables(settings.python.analysis.typeshedPaths[0]); _expandPathVariables(settings.analysis.typeshedPaths[0]);
}
} }
workspace.serviceInstance.setOptions(commandLineOptions); workspace.serviceInstance.setOptions(commandLineOptions);
@ -406,7 +441,8 @@ _connection.onInitialized(() => {
workspaceName: workspace.name, workspaceName: workspace.name,
rootPath: rootPath, rootPath: rootPath,
rootUri: workspace.uri, rootUri: workspace.uri,
serviceInstance: _createAnalyzerService(workspace.name) serviceInstance: _createAnalyzerService(workspace.name),
disableLanguageServices: false
}); });
}); });
}); });