Skip to content

Commit

Permalink
Added support for new Python extension callback so Pyright extension …
Browse files Browse the repository at this point in the history
…is notified when pythonPath is modified.
  • Loading branch information
msfterictraut committed Jul 20, 2020
1 parent 8118e87 commit c89b6a5
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import { ProgressReporting } from './progress';

let cancellationStrategy: FileBasedCancellationStrategy | undefined;

const pythonPathChangedListenerMap = new Map<string, string>();

export function activate(context: ExtensionContext) {
cancellationStrategy = new FileBasedCancellationStrategy();

Expand Down Expand Up @@ -104,7 +106,11 @@ export function activate(context: ExtensionContext) {
const pythonPathPromises: Promise<string | undefined>[] = params.items.map((item) => {
if (item.section === 'python') {
const uri = item.scopeUri ? Uri.parse(item.scopeUri) : undefined;
return getPythonPathFromPythonExtension(languageClient.outputChannel, uri);
return getPythonPathFromPythonExtension(languageClient.outputChannel, uri, () => {
// Posts a "workspace/didChangeConfiguration" message to the service
// so it re-queries the settings for all workspaces.
languageClient.sendRequest('workspace/executeCommand', {});
});
}
return Promise.resolve(undefined);
});
Expand Down Expand Up @@ -209,7 +215,8 @@ export function deactivate() {
// project and by user.
async function getPythonPathFromPythonExtension(
outputChannel: OutputChannel,
scopeUri?: Uri
scopeUri: Uri | undefined,
postConfigChanged: () => void
): Promise<string | undefined> {
try {
const extension = extensions.getExtension('ms-python.python');
Expand All @@ -223,7 +230,19 @@ async function getPythonPathFromPythonExtension(
outputChannel.appendLine('Python extension loaded');
}

const result = await extension.exports.settings.getExecutionCommand(scopeUri).join(' ');
const execDetails = await extension.exports.settings.getExecutionDetails(scopeUri);
let result: string | undefined;
if (execDetails.execCommand && execDetails.execCommand.length > 0) {
result = execDetails.execCommand[0];
}

if (extension.exports.settings.onDidChangeExecutionDetails) {
installPythonPathChangedListener(
extension.exports.settings.onDidChangeExecutionDetails,
scopeUri,
postConfigChanged
);
}

if (!result) {
outputChannel.appendLine(`No pythonPath provided by Python extension`);
Expand All @@ -246,3 +265,23 @@ async function getPythonPathFromPythonExtension(
function isThenable<T>(v: any): v is Thenable<T> {
return typeof v?.then === 'function';
}

function installPythonPathChangedListener(
onDidChangeExecutionDetails: (callback: () => void) => void,
scopeUri: Uri | undefined,
postConfigChanged: () => void
) {
const uriString = scopeUri ? scopeUri.toString() : '';

// No need to install another listener for this URI if
// it already exists.
if (pythonPathChangedListenerMap.has(uriString)) {
return;
}

onDidChangeExecutionDetails(() => {
postConfigChanged();
});

pythonPathChangedListenerMap.set(uriString, uriString);
}

0 comments on commit c89b6a5

Please sign in to comment.