diff --git a/packages/pyright-internal/src/analyzer/binder.ts b/packages/pyright-internal/src/analyzer/binder.ts index c1585aacd98d..58a04999da69 100644 --- a/packages/pyright-internal/src/analyzer/binder.ts +++ b/packages/pyright-internal/src/analyzer/binder.ts @@ -22,7 +22,6 @@ import { DiagnosticLevel } from '../common/configOptions'; import { assert, assertNever, fail } from '../common/debug'; import { CreateTypeStubFileAction, Diagnostic } from '../common/diagnostic'; import { DiagnosticRule } from '../common/diagnosticRules'; -import { DocStringService } from '../common/docStringService'; import { stripFileExtension } from '../common/pathUtils'; import { convertTextRangeToRange } from '../common/positionUtils'; import { TextRange, getEmptyRange } from '../common/textRange'; @@ -256,11 +255,7 @@ export class Binder extends ParseTreeWalker { // the current function. private _codeFlowComplexity = 0; - constructor( - fileInfo: AnalyzerFileInfo, - private _docStringService: DocStringService, - private _moduleSymbolOnly = false - ) { + constructor(fileInfo: AnalyzerFileInfo, private _moduleSymbolOnly = false) { super(); this._fileInfo = fileInfo; @@ -542,15 +537,6 @@ export class Binder extends ParseTreeWalker { if (paramNode.d.name) { const symbol = this._bindNameToScope(this._currentScope, paramNode.d.name); - // Extract the parameter docString from the function docString - let docString = ParseTreeUtils.getDocString(node?.d.suite?.d.statements ?? []); - if (docString !== undefined) { - docString = this._docStringService.extractParameterDocumentation( - docString, - paramNode.d.name.d.value - ); - } - if (symbol) { const paramDeclaration: ParamDeclaration = { type: DeclarationType.Param, @@ -559,7 +545,6 @@ export class Binder extends ParseTreeWalker { range: convertTextRangeToRange(paramNode, this._fileInfo.lines), moduleName: this._fileInfo.moduleName, isInExceptSuite: this._isInExceptSuite, - docString: docString, }; symbol.addDeclaration(paramDeclaration); diff --git a/packages/pyright-internal/src/analyzer/declaration.ts b/packages/pyright-internal/src/analyzer/declaration.ts index dca2d6456e03..9b1220eb5cfb 100644 --- a/packages/pyright-internal/src/analyzer/declaration.ts +++ b/packages/pyright-internal/src/analyzer/declaration.ts @@ -109,10 +109,6 @@ export interface ParamDeclaration extends DeclarationBase { type: DeclarationType.Param; node: ParameterNode; - // Documentation specified in the function's docstring (if any) can be - // associated with the parameter - docString?: string; - // Inferred parameters can be inferred from pieces of an actual NameNode, so this // value represents the actual 'name' as the user thinks of it. inferredName?: string; diff --git a/packages/pyright-internal/src/analyzer/sourceFile.ts b/packages/pyright-internal/src/analyzer/sourceFile.ts index f28f34029c99..78d9c0c096b9 100644 --- a/packages/pyright-internal/src/analyzer/sourceFile.ts +++ b/packages/pyright-internal/src/analyzer/sourceFile.ts @@ -823,11 +823,7 @@ export class SourceFile { ); AnalyzerNodeInfo.setFileInfo(this._writableData.parserOutput!.parseTree, fileInfo); - const binder = new Binder( - fileInfo, - this.serviceProvider.docStringService(), - configOptions.indexGenerationMode - ); + const binder = new Binder(fileInfo, configOptions.indexGenerationMode); this._writableData.isBindingInProgress = true; binder.bindModule(this._writableData.parserOutput!.parseTree); diff --git a/packages/pyright-internal/src/languageService/hoverProvider.ts b/packages/pyright-internal/src/languageService/hoverProvider.ts index 3a04d75c9eb9..d429afa06e56 100644 --- a/packages/pyright-internal/src/languageService/hoverProvider.ts +++ b/packages/pyright-internal/src/languageService/hoverProvider.ts @@ -94,6 +94,35 @@ export function convertHoverResults(hoverResults: HoverResults | null, format: M }; } +export function addParameterResultsPart( + serviceProvider: ServiceProvider, + paramNameNode: NameNode, + resolvedDecl: Declaration | undefined, + format: MarkupKind, + parts: HoverTextPart[] +) { + // See if we have a docstring for the parent function. + let docString: string | undefined = undefined; + const funcNode = ParseTreeUtils.getEnclosingFunction(resolvedDecl?.node || paramNameNode); + if (funcNode) { + docString = ParseTreeUtils.getDocString(funcNode?.d.suite?.d.statements ?? []); + if (docString) { + // Compute the docstring now. + docString = serviceProvider + .docStringService() + .extractParameterDocumentation(docString, paramNameNode.d.value, format); + } + } + if (!docString) { + return; + } + + parts.push({ + python: false, + text: docString, + }); +} + export function addDocumentationResultsPart( serviceProvider: ServiceProvider, docString: string | undefined, @@ -376,10 +405,7 @@ export class HoverProvider { case DeclarationType.Param: { this._addResultsPart(parts, '(parameter) ' + node.d.value + this._getTypeText(node), /* python */ true); - - if (resolvedDecl.docString) { - this._addResultsPart(parts, resolvedDecl.docString); - } + addParameterResultsPart(this._program.serviceProvider, node, resolvedDecl, this._format, parts); this._addDocumentationPart(parts, node, resolvedDecl); break; }