Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import suggestion code actions not working when no workspace is open #824

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 59 additions & 53 deletions packages/pyright-internal/src/languageService/codeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class CodeActionProvider {
throwIfCancellationRequested(token);

const codeActions: CodeAction[] = [];
if (!workspace.rootUri || workspace.disableLanguageServices) {
if (workspace.disableLanguageServices) {
return codeActions;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
Loading