|
| 1 | +// Copyright(c) Microsoft Corporation |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the License); you may not use |
| 5 | +// this file except in compliance with the License. You may obtain a copy of the |
| 6 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS |
| 9 | +// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY |
| 10 | +// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 11 | +// MERCHANTABILITY OR NON-INFRINGEMENT. |
| 12 | +// |
| 13 | +// See the Apache Version 2.0 License for specific language governing |
| 14 | +// permissions and limitations under the License. |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using Microsoft.Python.Analysis; |
| 19 | +using Microsoft.Python.Analysis.Types; |
| 20 | +using Microsoft.Python.Core.Text; |
| 21 | + |
| 22 | +namespace Microsoft.Python.LanguageServer.Sources { |
| 23 | + internal abstract class DocumentationSource { |
| 24 | + public string GetSignatureString(IPythonFunctionType ft, IPythonType self, out IndexSpan[] parameterSpans, int overloadIndex = 0, string name = null) { |
| 25 | + var o = ft.Overloads[overloadIndex]; |
| 26 | + |
| 27 | + var parameterStrings = GetFunctionParameters(ft, out var parameterNameLengths); |
| 28 | + var returnDoc = o.GetReturnDocumentation(self); |
| 29 | + var annString = string.IsNullOrEmpty(returnDoc) ? string.Empty : $" -> {returnDoc}"; |
| 30 | + |
| 31 | + // Calculate parameter spans |
| 32 | + parameterSpans = new IndexSpan[parameterStrings.Length]; |
| 33 | + name = name ?? ft.Name; |
| 34 | + var offset = name.Length + 1; |
| 35 | + for (var i = 0; i < parameterStrings.Length; i++) { |
| 36 | + parameterSpans[i] = IndexSpan.FromBounds(offset, offset + parameterNameLengths[i]); |
| 37 | + offset += parameterStrings[i].Length + 2; // name,<space> |
| 38 | + } |
| 39 | + |
| 40 | + var combinedParameterString = string.Join(", ", parameterStrings); |
| 41 | + return $"{name}({combinedParameterString}){annString}"; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Constructs parameter strings that include parameter name, type and default value |
| 46 | + /// like 'x: int' or 'a = None'. Returns collection of parameter strings and |
| 47 | + /// the respective lengths of names for rendering in bold (as current parameter). |
| 48 | + /// </summary> |
| 49 | + private string[] GetFunctionParameters(IPythonFunctionType ft, out int[] parameterNameLengths, int overloadIndex = 0) { |
| 50 | + var o = ft.Overloads[overloadIndex]; // TODO: display all? |
| 51 | + var skip = ft.IsStatic || ft.IsUnbound ? 0 : 1; |
| 52 | + |
| 53 | + var parameters = new string[o.Parameters.Count - skip]; |
| 54 | + parameterNameLengths = new int[o.Parameters.Count - skip]; |
| 55 | + for (var i = skip; i < o.Parameters.Count; i++) { |
| 56 | + string paramString; |
| 57 | + var p = o.Parameters[i]; |
| 58 | + if (!string.IsNullOrEmpty(p.DefaultValueString)) { |
| 59 | + paramString = $"{p.Name}={p.DefaultValueString}"; |
| 60 | + } else { |
| 61 | + paramString = p.Type.IsUnknown() ? p.Name : $"{p.Name}: {p.Type.Name}"; |
| 62 | + } |
| 63 | + parameters[i - skip] = paramString; |
| 64 | + parameterNameLengths[i - skip] = p.Name.Length; |
| 65 | + } |
| 66 | + return parameters; |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments