コード例 #1
0
        public SignatureHelp GetSignatureHelp(TextDocument document, Position position)
        {
            string text  = document.Text;
            int    index = document.GetPosition(position);

            string[] lines = text.Split('\n');
            string   line  = lines[position.Line];

            int startIndex = line.Substring(0, position.Character).LastIndexOf("(", StringComparison.Ordinal);

            if (startIndex != -1)
            {
                string functionName = line.Substring(0, startIndex).Trim().Split(" ").Last();

                // Remove the current line because it might contain errors.
                lines[position.Line] = "";

                DustContext currentContext = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines)).GlobalContext);

                Function function = currentContext.GetFunction(functionName);

                StringBuilder labelBuilder = new StringBuilder($"{function.Name}(");

                if (function.Parameters.Length > 0)
                {
                    for (int i = 0; i < function.Parameters.Length; i++)
                    {
                        labelBuilder.Append($"{function.Parameters[i].Identifier.Name}: any");

                        if (i != function.Parameters.Length - 1)
                        {
                            labelBuilder.Append(", ");
                        }
                    }
                }

                labelBuilder.Append("): any");

                return(new SignatureHelp
                {
                    ActiveParameter = line.Substring(startIndex, line.IndexOf(")", startIndex, StringComparison.Ordinal) - startIndex).Count(character => character == ','),
                    ActiveSignature = 0,
                    Signatures = new[]
                    {
                        new SignatureInformation
                        {
                            Label = labelBuilder.ToString(),
                            Parameters = function.Parameters.Select(parameter => new ParameterInformation
                            {
                                Label = parameter.Identifier.Name + ": any"
                            }).ToArray()
                        }
                    }
                });
            }

            return(new SignatureHelp());
        }
コード例 #2
0
        public Hover GetHover(TextDocument document, Position position)
        {
            string word = document.GetWordAtPosition(position);

            DustContext context = document.GetContextAtPosition(position, Project.CompileFile(document.Text).GlobalContext);

            string content = "";

            if (context.ContainsPropety(word))
            {
                content = context.GetProperty(word).GetDetail();
            }

            if (context.ContainsFunction(word))
            {
                content = context.GetFunction(word).GetDetail();
            }

            if (string.IsNullOrEmpty(content) == false)
            {
                return(new Hover
                {
                    Contents = (StringOrObject <MarkedString>) $@"```dust
{content}
```"
                });
            }

            return(new Hover());

/*
 *
 *    string name = document.GetWordAtPosition(position);
 *    string[] lines = document.Text.Split('\n');
 *
 *    lines[position.Line] = "";
 *
 *    DustContext context = document.GetContextAtPosition(position, Project.CompileFile(string.Join('\n', lines)));
 *
 *    Function function = context.GetFunction(name);
 *
 *    if (function != null)
 *    {
 *      return new Hover
 *      {
 *        Contents = new StringOrObject<MarkedString>(new MarkedString
 *        {
 *          Value = function.GetDetail(),
 *          Language = "dust"
 *        })
 *      };
 *    }
 *
 *    return new Hover();
 */
        }