Beispiel #1
0
        private async Task DoInitializeAsync(InitializeParams @params)
        {
            _analyzer = await CreateAnalyzer(@params.initializationOptions.interpreter);

            _clientCaps = @params.capabilities;
            _settings.SetCompletionTimeout(_clientCaps?.python?.completionsTimeout);
            _traceLogging = _clientCaps?.python?.traceLogging ?? false;
            _analyzer.EnableDiagnostics = _clientCaps?.python?.liveLinting ?? false;

            _reloadModulesQueueItem = new ReloadModulesQueueItem(_analyzer);

            if (@params.initializationOptions.displayOptions != null)
            {
                DisplayOptions = @params.initializationOptions.displayOptions;
            }
            _displayTextBuilder = DocumentationBuilder.Create(DisplayOptions);

            if (string.IsNullOrEmpty(_analyzer.InterpreterFactory?.Configuration?.InterpreterPath))
            {
                LogMessage(MessageType.Log, "Initializing for generic interpreter");
            }
            else
            {
                LogMessage(MessageType.Log, $"Initializing for {_analyzer.InterpreterFactory.Configuration.InterpreterPath}");
            }

            if (@params.rootUri != null)
            {
                _rootDir = @params.rootUri.ToAbsolutePath();
            }
            else if (!string.IsNullOrEmpty(@params.rootPath))
            {
                _rootDir = PathUtils.NormalizePath(@params.rootPath);
            }

            SetSearchPaths(@params.initializationOptions.searchPaths);
            SetTypeStubSearchPaths(@params.initializationOptions.typeStubSearchPaths);

            if (_rootDir != null && !(_clientCaps?.python?.manualFileLoad ?? false))
            {
                LogMessage(MessageType.Log, $"Loading files from {_rootDir}");
                _loadingFromDirectory = LoadFromDirectoryAsync(_rootDir);
            }
        }
Beispiel #2
0
 protected DocumentationBuilder(InformationDisplayOptions displayOptions)
 {
     DisplayOptions = displayOptions;
 }
Beispiel #3
0
 public PlainTextDocumentationBuilder(InformationDisplayOptions displayOptions) : base(displayOptions)
 {
 }
Beispiel #4
0
 public MarkdownDocumentationBuilder(InformationDisplayOptions displayOptions) : base(displayOptions)
 {
 }
Beispiel #5
0
        public string MakeHoverText(IEnumerable <AnalysisValue> values, string originalExpression, InformationDisplayOptions displayOptions)
        {
            var result         = new StringBuilder();
            var documentations = new HashSet <string>();
            var descriptions   = new HashSet <string>();

            foreach (var v in values)
            {
                var d = v.Description;
                if (v.MemberType == PythonMemberType.Instance || v.MemberType == PythonMemberType.Constant)
                {
                    if (!string.IsNullOrEmpty(d))
                    {
                        descriptions.Add(d);
                    }
                    continue;
                }

                var doc = v.Documentation;

                if ((d?.Length ?? 0) < (doc?.Length ?? 0))
                {
                    if (displayOptions.trimDocumentationLines)
                    {
                        doc = LimitLines(doc);
                    }
                    if (!string.IsNullOrEmpty(doc))
                    {
                        documentations.Add(doc);
                    }
                    continue;
                }

                if (!string.IsNullOrEmpty(d))
                {
                    descriptions.Add(d);
                }
            }

            foreach (var d in descriptions.Ordered())
            {
                if (result.Length > 0)
                {
                    result.Append(", ");
                }
                result.Append(d);
            }

            foreach (var d in documentations.Ordered())
            {
                if (result.Length > 0)
                {
                    result.AppendLine();
                }

                result.AppendLine(d);
            }

            if (displayOptions.trimDocumentationText && result.Length > displayOptions.maxDocumentationTextLength)
            {
                result.Length = Math.Max(0, displayOptions.maxDocumentationTextLength - 3);
                result.Append("...");
            }
            else if (displayOptions.trimDocumentationLines)
            {
                using (var sr = new StringReader(result.ToString())) {
                    result.Clear();
                    int lines = displayOptions.maxDocumentationLineLength;
                    for (var line = sr.ReadLine(); line != null; line = sr.ReadLine())
                    {
                        if (--lines < 0)
                        {
                            result.Append("...");
                            break;
                        }
                        result.AppendLine(line);
                    }
                }
            }

            while (result.Length > 0 && char.IsWhiteSpace(result[result.Length - 1]))
            {
                result.Length -= 1;
            }

            if (!string.IsNullOrEmpty(originalExpression))
            {
                if (displayOptions.trimDocumentationText && originalExpression.Length > displayOptions.maxDocumentationTextLength)
                {
                    originalExpression = originalExpression.Substring(0,
                                                                      Math.Max(3, displayOptions.maxDocumentationTextLength) - 3) + "...";
                }
                if (result.ToString().IndexOf('\n') >= 0)
                {
                    result.Insert(0, $"{originalExpression}:{Environment.NewLine}");
                }
                else if (result.Length > 0)
                {
                    result.Insert(0, $"{originalExpression}: ");
                }
                else
                {
                    result.Append($"{originalExpression}: <unknown type>");
                }
            }

            return(result.ToString().TrimEnd());
        }