Пример #1
0
        internal static string GetTooltip(string scriptFile, int caret, string hint, bool shortHintedTooltips)
        {
            // Simplified API for ST3
            Output.WriteLine("GetTooltip");
            //Console.WriteLine("hint: " + hint);

            string result = null;

            var script = new SourceInfo(scriptFile);

            if (script.Content.IsEmpty())
            {
                throw new Exception("The file containing code is empty");
            }

            void loockupDirective(string directive)
            {
                var css_directive = CssCompletionData.AllDirectives
                                    .FirstOrDefault(x => x.DisplayText == directive);

                if (css_directive != null)
                {
                    result = $"Directive: {css_directive.DisplayText}\n{css_directive.Description}";
                    result = result.NormalizeLineEnding().Replace("\r\n\r\n", "\r\n").TrimEnd();
                }
            };

            ParseAsCssDirective(script.Content, caret,
                                loockupDirective,
                                (directive, arg, extensions) =>
            {
                if (LookopDirectivePath(script, caret, directive, arg) is string file)
                {
                    result = $"File: {file}";
                }
                else
                {
                    loockupDirective(directive);
                }
            });

            if (result.HasText())
            {
                return(result);
            }

            if (!script.RawFile.EndsWith(".g.cs"))
            {
                CSScriptHelper.DecorateIfRequired(ref script.Content, ref caret);
            }

            Project project = CSScriptHelper.GenerateProjectFor(script);
            var     sources = project.Files
                              .Where(f => f != project.Script)
                              .Select(f => new Tuple <string, string>(File.ReadAllText(f), f))
                              .ToArray();

            int methodStartPosTemp;
            var items = Autocompleter.GetMemberInfo(script.Content, caret, out methodStartPosTemp, project.Refs, sources, includeOverloads: hint.HasAny());

            fullyLoaded = true;
            if (hint.HasAny())
            {
                if (shortHintedTooltips)
                {
                    items = items.Select(x => x.Split('\n').FirstOrDefault()).ToArray();
                }

                int count = hint.Split(',').Count();
                result = items.FirstOrDefault(x =>
                {
                    return(SyntaxMapper.GetArgumentCount(x) == count);
                })
                         ?? items.FirstOrDefault();

                bool hideOverloadsSummary = false;
                if (result != null && hideOverloadsSummary)
                {
                    var lines = result.Split('\n').Select(x => x.TrimEnd('\r')).ToArray();
                    //(+ 1 overloads)
                    if (lines[0].EndsWith(" overloads)"))
                    {
                        try
                        {
                            lines[0] = lines[0].Split(new[] { "(+" }, StringSplitOptions.None).First().Trim();
                        }
                        catch { }
                    }
                    result = lines.JoinBy("\n");
                }
            }
            else
            {
                result = items.FirstOrDefault();
            }

            if (result.HasText())
            {
                result = result.NormalizeLineEnding().Replace("\r\n\r\n", "\r\n").TrimEnd();
            }

            return(result);
        }