Skip to content

Commit 3e5936a

Browse files
committed
add settings to disable each type of inlay hint
1 parent 11e77cf commit 3e5936a

File tree

11 files changed

+85
-8
lines changed

11 files changed

+85
-8
lines changed
1.72 KB
Loading
2.24 KB
Loading
1.44 KB
Loading

docs/configuration/language-server-settings.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ The basedpyright language server honors the following settings.
3030
!!! note "a note about `python.venvPath`"
3131
if your venv path is the same for all users working on your project (which should be the case if you're using [uv](https://docs.astral.sh/uv/pip/compatibility/#virtual-environments-by-default) or [pdm](https://pdm-project.org/en/latest/usage/venv/#virtualenv-auto-creation)), we recommend configuring `venvPath` in [a config file](./config-files.md) instead. see [discouraged settings](#discouraged-settings) below for more information.
3232

33+
## based settings
34+
35+
the following settings are exclusive to basedpyright
36+
37+
**basedpyright.inlayHints.variableTypes** [boolean]: Whether to show inlay hints on assignments to variables. Defaults to `true`:
38+
39+
![](inlayHints.variableTypes.png)
40+
41+
**basedpyright.inlayHints.callArgumentNames** [boolean]: Whether to show inlay hints on function arguments. Defaults to `true`:
42+
43+
![](inlayHints.callArgumentNames.png)
44+
45+
**basedpyright.inlayHints.functionReturnTypes** [boolean]: Whether to show inlay hints on function return types. Defaults to `true`:
46+
47+
![](inlayHints.functionReturnTypes.png)
48+
3349
## discouraged settings
3450

3551
these options can also be configured [using a config file](./config-files.md). it's recommended to use either a `pyproject.toml` or `pyrightconfig.json` file instead of the language server to configure type checking for the following reasons:

packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { TextRange } from '../common/textRange';
1818
import { convertRangeToTextRange } from '../common/positionUtils';
1919
import { Uri } from '../common/uri/uri';
2020
import { ParseFileResults } from '../parser/parser';
21+
import { InlayHintSettings } from '../common/languageServerInterface';
2122

2223
export type TypeInlayHintsItemType = {
2324
inlayHintType: 'variable' | 'functionReturn' | 'parameter';
@@ -84,7 +85,12 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
8485
parseResults?: ParseFileResults;
8586
private _range: TextRange | undefined;
8687

87-
constructor(private readonly _program: ProgramView, fileUri: Uri, range?: Range) {
88+
constructor(
89+
private readonly _program: ProgramView,
90+
private _settings: InlayHintSettings,
91+
fileUri: Uri,
92+
range?: Range
93+
) {
8894
super();
8995
this.parseResults = this._program.getParseResults(fileUri);
9096
if (this.parseResults) {
@@ -97,6 +103,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
97103

98104
override visitName(node: NameNode): boolean {
99105
if (
106+
this._settings.variableTypes &&
100107
this._checkInRange(node) &&
101108
isLeftSideOfAssignment(node) &&
102109
!isDunderName(node.d.value) &&
@@ -129,14 +136,14 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
129136
}
130137

131138
override visitCall(node: CallNode): boolean {
132-
if (this._checkInRange(node)) {
139+
if (this._settings.callArgumentNames && this._checkInRange(node)) {
133140
this._generateHintsForCallNode(node);
134141
}
135142
return super.visitCall(node);
136143
}
137144

138145
override visitFunction(node: FunctionNode): boolean {
139-
if (this._checkInRange(node)) {
146+
if (this._settings.functionReturnTypes && this._checkInRange(node)) {
140147
const evaluator = this._program.evaluator;
141148
const functionType = evaluator?.getTypeOfFunction(node)?.functionType;
142149
if (functionType !== undefined && !functionType.shared.declaredReturnType) {

packages/pyright-internal/src/common/languageServerInterface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ import { ServiceProvider } from './serviceProvider';
2121
import { Uri } from './uri/uri';
2222
import { FileDiagnostics } from './diagnosticSink';
2323

24+
// if adding a new inlay hint type here, make sure you update onInlayHints where it checks if all of them are false
25+
export interface InlayHintSettings {
26+
/**
27+
* pylance's version of this option supports 3 settings: `"all" | "partial" | "off"`. `"all"` shows inlay hints
28+
* for positional only arguments which i think is dumb so we don't support it
29+
*/
30+
callArgumentNames: boolean;
31+
functionReturnTypes: boolean;
32+
variableTypes: boolean;
33+
}
34+
2435
export interface ServerSettings {
2536
venvPath?: Uri | undefined;
2637
pythonPath?: Uri | undefined;
@@ -49,6 +60,7 @@ export interface ServerSettings {
4960
ignoreFileSpecs?: string[];
5061
taskListTokens?: TaskListToken[];
5162
functionSignatureDisplay?: SignatureDisplayType | undefined;
63+
inlayHints?: InlayHintSettings;
5264
}
5365

5466
export interface MessageAction {

packages/pyright-internal/src/languageServerBase.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,23 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
998998
const uri = Uri.parse(params.textDocument.uri, this.serviceProvider);
999999
const workspace = await this.getWorkspaceForFile(uri);
10001000

1001-
if (workspace.disableLanguageServices) {
1001+
const inlayHintSettings = (await this.getSettings(workspace)).inlayHints;
1002+
if (
1003+
workspace.disableLanguageServices ||
1004+
// don't bother creating the inlay hint provider if all the inlay hint settings are off
1005+
(inlayHintSettings &&
1006+
!inlayHintSettings.callArgumentNames &&
1007+
!inlayHintSettings.functionReturnTypes &&
1008+
!inlayHintSettings.variableTypes)
1009+
) {
10021010
return null;
10031011
}
10041012
return workspace.service.run((program) => {
1005-
return new InlayHintsProvider(program, uri, params.range).onInlayHints();
1013+
return new InlayHintsProvider(program, uri, params.range, {
1014+
callArgumentNames: inlayHintSettings?.callArgumentNames ?? true,
1015+
functionReturnTypes: inlayHintSettings?.functionReturnTypes ?? true,
1016+
variableTypes: inlayHintSettings?.variableTypes ?? true,
1017+
}).onInlayHints();
10061018
}, token);
10071019
}
10081020

packages/pyright-internal/src/languageService/inlayHintsProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { convertOffsetToPosition } from '../common/positionUtils';
55
import { TypeInlayHintsWalker } from '../analyzer/typeInlayHintsWalker';
66
import { Uri } from '../common/uri/uri';
77
import { Range } from 'vscode-languageserver-types';
8+
import { InlayHintSettings } from '../common/languageServerInterface';
89

910
export class InlayHintsProvider {
1011
private readonly _walker: TypeInlayHintsWalker;
1112

12-
constructor(private _program: ProgramView, fileUri: Uri, range: Range) {
13-
this._walker = new TypeInlayHintsWalker(this._program, fileUri, range);
13+
constructor(private _program: ProgramView, fileUri: Uri, range: Range, inlayHintSettings: InlayHintSettings) {
14+
this._walker = new TypeInlayHintsWalker(this._program, inlayHintSettings, fileUri, range);
1415
}
1516

1617
async onInlayHints(): Promise<InlayHint[] | null> {

packages/pyright-internal/src/realLanguageServer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export abstract class RealLanguageServer extends LanguageServerBase {
102102
logLevel: LogLevel.Info,
103103
autoImportCompletions: true,
104104
functionSignatureDisplay: SignatureDisplayType.formatted,
105+
inlayHints: { callArgumentNames: true, functionReturnTypes: true, variableTypes: true },
105106
};
106107

107108
try {
@@ -193,6 +194,11 @@ export abstract class RealLanguageServer extends LanguageServerBase {
193194
serverSettings.autoSearchPaths = true;
194195
}
195196

197+
const inlayHintSection = await this.getConfiguration(workspace.rootUri, 'basedpyright.inlayHints');
198+
if (inlayHintSection) {
199+
serverSettings.inlayHints = { ...serverSettings.inlayHints, ...inlayHintSection };
200+
}
201+
196202
const pyrightSection = await this.getConfiguration(workspace.rootUri, 'basedpyright');
197203
if (pyrightSection) {
198204
if (pyrightSection.openFilesOnly !== undefined) {

packages/pyright-internal/src/tests/testUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ export const inlayHintSampleFile = (fileName: string, range?: Range): TypeInlayH
151151
const program = createProgram();
152152
const fileUri = UriEx.file(resolveSampleFilePath(path.join('inlay_hints', fileName)));
153153
program.setTrackedFiles([fileUri]);
154-
const walker = new TypeInlayHintsWalker(program, fileUri, range);
154+
const walker = new TypeInlayHintsWalker(
155+
program,
156+
{ callArgumentNames: true, functionReturnTypes: true, variableTypes: true },
157+
fileUri,
158+
range
159+
);
155160
walker.walk(program.getParseResults(fileUri)!.parserOutput.parseTree);
156161
program.dispose();
157162
return walker.featureItems;

0 commit comments

Comments
 (0)