From 45b7b6e8c4916fd69277b33a86249b40dd1361fa Mon Sep 17 00:00:00 2001 From: detachhead Date: Tue, 29 Oct 2024 19:19:51 +1000 Subject: [PATCH] fix import suggestion code actions not working when no workspace is open --- .../src/languageService/codeActionProvider.ts | 112 +++++++++--------- 1 file changed, 59 insertions(+), 53 deletions(-) diff --git a/packages/pyright-internal/src/languageService/codeActionProvider.ts b/packages/pyright-internal/src/languageService/codeActionProvider.ts index 0ccef4c0b..8e9e5963c 100644 --- a/packages/pyright-internal/src/languageService/codeActionProvider.ts +++ b/packages/pyright-internal/src/languageService/codeActionProvider.ts @@ -44,7 +44,7 @@ export class CodeActionProvider { throwIfCancellationRequested(token); const codeActions: CodeAction[] = []; - if (!workspace.rootUri || workspace.disableLanguageServices) { + if (workspace.disableLanguageServices) { return codeActions; } @@ -54,59 +54,7 @@ export class CodeActionProvider { } const diags = await workspace.service.getDiagnosticsForRange(fileUri, range, token); - const typeStubDiag = diags.find((d) => { - const actions = d.getActions(); - return actions && actions.find((a) => a.action === Commands.createTypeStub); - }); - - if (typeStubDiag) { - const action = typeStubDiag - .getActions()! - .find((a) => a.action === Commands.createTypeStub) as CreateTypeStubFileAction; - if (action) { - const createTypeStubAction = CodeAction.create( - Localizer.CodeAction.createTypeStubFor().format({ moduleName: action.moduleName }), - createCommand( - Localizer.CodeAction.createTypeStub(), - Commands.createTypeStub, - workspace.rootUri.toString(), - action.moduleName, - fileUri.toString() - ), - CodeActionKind.QuickFix - ); - codeActions.push(createTypeStubAction); - } - } - const renameShadowed = diags.find((d) => { - const actions = d.getActions(); - return actions && actions.find((a) => a.action === ActionKind.RenameShadowedFileAction); - }); - if (renameShadowed) { - const action = renameShadowed - .getActions()! - .find((a) => a.action === ActionKind.RenameShadowedFileAction) as RenameShadowedFileAction; - if (action) { - const title = Localizer.CodeAction.renameShadowedFile().format({ - oldFile: action.oldUri.getShortenedFileName(), - newFile: action.newUri.getShortenedFileName(), - }); - const editActions: FileEditActions = { - edits: [], - fileOperations: [ - { - kind: 'rename', - oldFileUri: action.oldUri, - newFileUri: action.newUri, - }, - ], - }; - const workspaceEdit = convertToWorkspaceEdit(workspace.service.fs, editActions); - const renameAction = CodeAction.create(title, workspaceEdit, CodeActionKind.QuickFix); - codeActions.push(renameAction); - } - } if (diags.find((d) => d.getActions()?.some((action) => action.action === Commands.import))?.getActions()) { const parseResults = workspace.service.backgroundAnalysisProgram.program.getParseResults(fileUri)!; const lines = parseResults.tokenizerOutput.lines; @@ -177,6 +125,64 @@ export class CodeActionProvider { } } + if (!workspace.rootUri) { + return codeActions; + } + + const typeStubDiag = diags.find((d) => { + const actions = d.getActions(); + return actions && actions.find((a) => a.action === Commands.createTypeStub); + }); + + if (typeStubDiag) { + const action = typeStubDiag + .getActions()! + .find((a) => a.action === Commands.createTypeStub) as CreateTypeStubFileAction; + if (action) { + const createTypeStubAction = CodeAction.create( + Localizer.CodeAction.createTypeStubFor().format({ moduleName: action.moduleName }), + createCommand( + Localizer.CodeAction.createTypeStub(), + Commands.createTypeStub, + workspace.rootUri.toString(), + action.moduleName, + fileUri.toString() + ), + CodeActionKind.QuickFix + ); + codeActions.push(createTypeStubAction); + } + } + + const renameShadowed = diags.find((d) => { + const actions = d.getActions(); + return actions && actions.find((a) => a.action === ActionKind.RenameShadowedFileAction); + }); + if (renameShadowed) { + const action = renameShadowed + .getActions()! + .find((a) => a.action === ActionKind.RenameShadowedFileAction) as RenameShadowedFileAction; + if (action) { + const title = Localizer.CodeAction.renameShadowedFile().format({ + oldFile: action.oldUri.getShortenedFileName(), + newFile: action.newUri.getShortenedFileName(), + }); + const editActions: FileEditActions = { + edits: [], + fileOperations: [ + { + kind: 'rename', + oldFileUri: action.oldUri, + newFileUri: action.newUri, + }, + ], + }; + const workspaceEdit = convertToWorkspaceEdit(workspace.service.fs, editActions); + const renameAction = CodeAction.create(title, workspaceEdit, CodeActionKind.QuickFix); + codeActions.push(renameAction); + } + } + return codeActions; } }