// TODO comment this
        static string GetSignatures(string json)
        {
            dynamic inputJson = JsonConvert.DeserializeObject(json);
            string  document  = inputJson.textDocument;

            int line      = inputJson.caret.line;
            int character = inputJson.caret.character;
            Pos caret     = new Pos(line, character);

            var parser = ParserData.GetParser(document, new Parse.Pos(caret.line, caret.character));

            int methodIndex    = 0;
            int parameterIndex = 0;

            MethodNode methodNode = null;

            SignatureHelp signatures = null;

            if (parser.Success)
            {
                if (parser.Bav.SelectedNode.ElementAtOrDefault(0) is MethodNode)
                {
                    methodNode     = (MethodNode)parser.Bav.SelectedNode[0];
                    parameterIndex = methodNode.Parameters.Length;
                }
                else if (parser.Bav.SelectedNode.ElementAtOrDefault(0) is IExpressionNode &&
                         parser.Bav.SelectedNode.ElementAtOrDefault(1) is MethodNode)
                {
                    methodNode     = (MethodNode)parser.Bav.SelectedNode[1];
                    parameterIndex = Array.IndexOf(methodNode.Parameters, parser.Bav.SelectedNode[0]);
                }

                SignatureInformation information = null;
                if (methodNode != null)
                {
                    Type methodType = Element.GetMethod(methodNode.Name);

                    if (methodType != null)
                    {
                        Element element = (Element)Activator.CreateInstance(methodType);

                        information = new SignatureInformation(
                            element.ToString(),
                            "",
                            element.ParameterData.Select(p =>
                                                         new ParameterInformation(p.Name, "")
                                                         ).ToArray());
                    }
                }

                signatures = new SignatureHelp
                             (
                    new SignatureInformation[] { information },
                    methodIndex,
                    parameterIndex
                             );
            }

            return(JsonConvert.SerializeObject(signatures));
        }
Ejemplo n.º 2
0
        string GetSignatures(string json)
        {
            PosData posData = GetPosData(json);

            if (posData == null)
            {
                return(null);
            }

            MethodNode methodNode = null;

            SignatureHelp signatures = null;

            int methodIndex = 0;
            int parameterIndex;

            if (posData.SelectedNode != null)
            {
                // Get the signature for the method the cursor is on.
                // Check if the selected node is a method node.
                if (posData.SelectedNode.ElementAtOrDefault(0) is MethodNode)
                {
                    methodNode = (MethodNode)posData.SelectedNode[0];

                    // If the parameters of the method node is not selected and the parent is a method node,
                    // select the parent method node.
                    if (!methodNode.IsParametersSelected(posData.Caret) && posData.SelectedNode.ElementAtOrDefault(1) is MethodNode)
                    {
                        methodNode = (MethodNode)posData.SelectedNode[1];
                        // Get the index of the selected node.
                        parameterIndex = Array.IndexOf(methodNode.Parameters, posData.SelectedNode[0]);
                    }
                    else
                    {
                        if (methodNode.IsNameSelected(posData.Caret))
                        {
                            // If the name is selected, -1 will not highlight any parameters.
                            parameterIndex = -1;
                        }
                        else
                        {
                            // The last parameter is selected.
                            parameterIndex = methodNode.Parameters.Length;
                        }
                    }
                }
                else if (/*parser.Bav.SelectedNode.ElementAtOrDefault(0) is IExpressionNode
                          * &&*/posData.SelectedNode.ElementAtOrDefault(1) is MethodNode)
                {
                    methodNode = (MethodNode)posData.SelectedNode[1];
                    // Get the index of the selected node.
                    parameterIndex = Array.IndexOf(methodNode.Parameters, posData.SelectedNode[0]);
                }
                else
                {
                    parameterIndex = 0;
                }

                SignatureInformation information = null;
                if (methodNode != null)
                {
                    IMethod method = parserData.GetMethod(methodNode.Name);

                    if (method != null)
                    {
                        ParameterInformation[] parameterInfo = new ParameterInformation[method.Parameters.Length];
                        for (int i = 0; i < parameterInfo.Length; i++)
                        {
                            parameterInfo[i] = new ParameterInformation(
                                method.Parameters[i].GetLabel(false),
                                // Every value in the tree can potentially be null.
                                method.Wiki?.Parameters?.ElementAtOrDefault(i)?.Description
                                );
                        }

                        information = new SignatureInformation(
                            method.GetLabel(false),
                            // Get the method's documentation
                            method.Wiki?.Description,
                            // Get the parameter data
                            parameterInfo
                            //method.Wiki?.Parameters?.Select(v => v.ToParameterInformation())
                            //    .ToArray()
                            );
                    }
                }

                signatures = new SignatureHelp
                             (
                    new SignatureInformation[] { information },
                    methodIndex,
                    parameterIndex
                             );
            }

            return(JsonConvert.SerializeObject(signatures));
        }