|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import {TextDocument, Position, CancellationToken, SignatureHelp, ExtensionContext} from "vscode"; |
| 5 | +import * as proxy from "./jediProxy"; |
| 6 | +import * as telemetryContracts from "../common/telemetryContracts"; |
| 7 | + |
| 8 | +const DOCSTRING_PARAM_PATTERNS = [ |
| 9 | + "\\s*:type\\s*PARAMNAME:\\s*([^\\n, ]+)", // Sphinx |
| 10 | + "\\s*:param\\s*(\\w?)\\s*PARAMNAME:[^\\n]+", // Sphinx param with type |
| 11 | + "\\s*@type\\s*PARAMNAME:\\s*([^\\n, ]+)" // Epydoc |
| 12 | +]; |
| 13 | + |
| 14 | +/** |
| 15 | + * Extrct the documentation for parameters from a given docstring |
| 16 | + * |
| 17 | + * @param {string} paramName Name of the parameter |
| 18 | + * @param {string} docString The docstring for the function |
| 19 | + * @returns {string} Docstring for the parameter |
| 20 | + */ |
| 21 | +function extractParamDocString(paramName: string, docString: string): string { |
| 22 | + let paramDocString = ""; |
| 23 | + // In docstring the '*' is escaped with a backslash |
| 24 | + paramName = paramName.replace(new RegExp("\\*", "g"), "\\\\\\*"); |
| 25 | + |
| 26 | + DOCSTRING_PARAM_PATTERNS.forEach(pattern => { |
| 27 | + if (paramDocString.length > 0) { |
| 28 | + return; |
| 29 | + } |
| 30 | + pattern = pattern.replace("PARAMNAME", paramName); |
| 31 | + let regExp = new RegExp(pattern); |
| 32 | + let matches = regExp.exec(docString); |
| 33 | + if (matches && matches.length > 0) { |
| 34 | + paramDocString = matches[0]; |
| 35 | + if (paramDocString.indexOf(":") >= 0) { |
| 36 | + paramDocString = paramDocString.substring(paramDocString.indexOf(":") + 1); |
| 37 | + } |
| 38 | + if (paramDocString.indexOf(":") >= 0) { |
| 39 | + paramDocString = paramDocString.substring(paramDocString.indexOf(":") + 1); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + return paramDocString.trim(); |
| 45 | +} |
| 46 | +export class PythonSignatureProvider implements vscode.SignatureHelpProvider { |
| 47 | + private jediProxyHandler: proxy.JediProxyHandler<proxy.IArgumentsResult, vscode.SignatureHelp>; |
| 48 | + |
| 49 | + public constructor(context: vscode.ExtensionContext, jediProxy: proxy.JediProxy = null) { |
| 50 | + this.jediProxyHandler = new proxy.JediProxyHandler(context, null, PythonSignatureProvider.parseData, jediProxy); |
| 51 | + } |
| 52 | + private static parseData(data: proxy.IArgumentsResult): vscode.SignatureHelp { |
| 53 | + if (data && Array.isArray(data.definitions) && data.definitions.length > 0) { |
| 54 | + let signature = new SignatureHelp(); |
| 55 | + signature.activeSignature = 0; |
| 56 | + |
| 57 | + data.definitions.forEach(def => { |
| 58 | + signature.activeParameter = def.paramindex; |
| 59 | + // Don't display the documentation, as vs code doesn't format the docmentation |
| 60 | + // i.e. line feeds are not respected, long content is stripped |
| 61 | + let sig = <vscode.SignatureInformation>{ |
| 62 | + // documentation: def.docstring, |
| 63 | + label: def.description, |
| 64 | + parameters: [] |
| 65 | + }; |
| 66 | + sig.parameters = def.params.map(arg => { |
| 67 | + if (arg.docstring.length === 0) { |
| 68 | + arg.docstring = extractParamDocString(arg.name, def.docstring); |
| 69 | + } |
| 70 | + return <vscode.ParameterInformation>{ |
| 71 | + documentation: arg.docstring.length > 0 ? arg.docstring : arg.description, |
| 72 | + label: arg.description.length > 0 ? arg.description : arg.name |
| 73 | + }; |
| 74 | + }); |
| 75 | + signature.signatures.push(sig); |
| 76 | + }); |
| 77 | + return signature; |
| 78 | + } |
| 79 | + |
| 80 | + return new SignatureHelp(); |
| 81 | + } |
| 82 | + provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Thenable<SignatureHelp> { |
| 83 | + return new Promise<SignatureHelp>((resolve, reject) => { |
| 84 | + let cmd: proxy.ICommand<proxy.IArgumentsResult> = { |
| 85 | + telemetryEvent: telemetryContracts.IDE.Symbol, |
| 86 | + command: proxy.CommandType.Arguments, |
| 87 | + fileName: document.fileName, |
| 88 | + columnIndex: position.character, |
| 89 | + lineIndex: position.line, |
| 90 | + source: document.getText() |
| 91 | + }; |
| 92 | + this.jediProxyHandler.sendCommand(cmd, resolve, token); |
| 93 | + }); |
| 94 | + } |
| 95 | +} |
0 commit comments