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 being disabled when basedpyright.analysis.autoImportCompletions is disabled #983

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions docs/configuration/language-server-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ The basedpyright language server honors the following settings.

**basedpyright.analysis.autoImportCompletions** [boolean]: Determines whether pyright offers auto-import completions.

!!! note
this setting is only for import suggestion [*completions*](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion), which are displayed as you type. it does not affect [import suggestion *code actions*](../benefits-over-pyright/pylance-features.md#import-suggestion-code-actions), which are tied to the [`reportUndefinedVariable`](./config-files.md#reportUndefinedVariable) diagnostic rule. to disable those, you must disable the diagnostic rule itself.

**basedpyright.analysis.autoSearchPaths** [boolean]: Determines whether pyright automatically adds common search paths like "src" if there are no execution environments defined in the config file.

**basedpyright.analysis.diagnosticMode** ["openFilesOnly", "workspace"]: Determines whether pyright analyzes (and reports errors for) all files in the workspace, as indicated by the config file. If this option is set to "openFilesOnly", pyright analyzes only open files. Defaults to "openFilesOnly"
Expand Down
6 changes: 4 additions & 2 deletions packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,8 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
lazyEdit: false,
triggerCharacter: params?.context?.triggerCharacter,
},
token
token,
false
).getCompletions();

this.setCompletionIncomplete(params, completions);
Expand Down Expand Up @@ -953,7 +954,8 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
snippet: this.client.completionSupportsSnippet,
lazyEdit: false,
},
token
token,
false
).resolveCompletionItem(params);
}, token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export class CodeActionProvider {
lazyEdit: false,
snippet: false,
},
token
token,
true
);

const word = node.nodeType === ParseNodeType.Name ? node.d.value : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ export class CompletionProvider {
protected readonly fileUri: Uri,
protected readonly position: Position,
protected readonly options: CompletionOptions,
protected readonly cancellationToken: CancellationToken
protected readonly cancellationToken: CancellationToken,
private readonly _codeActions: boolean
) {
this.execEnv = this.configOptions.findExecEnvironment(this.fileUri);

Expand Down Expand Up @@ -824,7 +825,7 @@ export class CompletionProvider {
completionMap: CompletionMap,
parensDisabled?: boolean
) {
if (!this.configOptions.autoImportCompletions) {
if (!this._codeActions && !this.configOptions.autoImportCompletions) {
// If auto import on the server is turned off or this particular invocation
// is turned off (ex, notebook), don't do any thing.
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ test('check chained files', () => {
lazyEdit: false,
snippet: false,
},
CancellationToken.None
CancellationToken.None,
false
).getCompletions();

assert(result?.items.some((i) => i.label === 'foo1'));
Expand Down Expand Up @@ -105,7 +106,8 @@ test('modify chained files', () => {
lazyEdit: false,
snippet: false,
},
CancellationToken.None
CancellationToken.None,
false
).getCompletions();

assert(result);
Expand Down
9 changes: 6 additions & 3 deletions packages/pyright-internal/src/tests/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,8 @@ test('completion quote trigger', async () => {
uri,
position,
options,
CancellationToken.None
CancellationToken.None,
false
).getCompletions();

assert(result);
Expand Down Expand Up @@ -852,7 +853,8 @@ test('completion quote trigger - middle', async () => {
uri,
position,
options,
CancellationToken.None
CancellationToken.None,
false
).getCompletions();

assert.strictEqual(result?.items.length, 0);
Expand Down Expand Up @@ -897,7 +899,8 @@ test('auto import sort text', async () => {
uri,
position,
options,
CancellationToken.None
CancellationToken.None,
false
).getCompletions();

const items = result?.items.filter((i) => i.label === 'os');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// <reference path="typings/fourslash.d.ts" />

// @filename: pyrightconfig.json
//// {
//// "autoImportCompletions": false
//// }

// @filename: test.py
//// [|/*import*/|]foo[|/*noImportMarker*/|]: [|/*startMarker*/|][|TracebackType/*range*/|][|/*endMarker*/|]
{
const importRange = helper.getPositionRange('import');
const symbolRange = helper.getPositionRange('range');

const codeActions = {
codeActions: [
{
title: `from types import TracebackType`,
edit: {
changes: {
'file:///test.py': [
// this is a useless TextEdit that replaces the text with the same thing, probably because
// it needs to change it to prepend a module name if the whole module is already imported
{ range: symbolRange, newText: 'TracebackType' },
{ range: importRange, newText: 'from types import TracebackType\n\n\n' },
],
},
},
kind: 'quickfix',
},
],
};

//@ts-expect-error https://github.com/DetachHead/basedpyright/issues/86
await helper.verifyCodeActions('included', {
noImportMarker: { codeActions: [] },
startMarker: codeActions,
endMarker: codeActions,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
edit: {
changes: {
'file:///test.py': [
// this is a useless TextEdit that replaces the text with the same thing for some reason.
// keeping it in case there's ever code actions that rename it for whatever reason
// this is a useless TextEdit that replaces the text with the same thing, probably because
// it needs to change it to prepend a module name if the whole module is already imported
{ range: symbolRange, newText: 'TracebackType' },
{ range: importRange, newText: 'from types import TracebackType\n\n\n' },
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,8 @@ export class TestState {
Uri.file(filePath, this.serviceProvider),
completionPosition,
options,
CancellationToken.None
CancellationToken.None,
false
);

return {
Expand Down
Loading