Пример #1
0
        // https://microsoft.github.io/language-server-protocol/specification#textDocument_hover
        string GetHover(string json)
        {
            PosData posData = GetPosData(json);

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

            Hover hover = null;

            if (/*parserData.Success &&*/ posData.SelectedNode != null && posData.SelectedNode.Length > 0)
            {
                switch (posData.SelectedNode[0])
                {
                case MethodNode methodNode:

                    IMethod method = parserData.GetMethod(methodNode.Name);

                    if (method != null)
                    {
                        hover = new Hover(new MarkupContent(MarkupContent.Markdown, method.GetLabel(true)))
                        {
                            range = methodNode.Location.range
                        }
                    }
                    ;
                    break;

                case ImportNode importNode:

                    string path = Extras.CombinePathWithDotNotation(posData.File, importNode.File);

                    if (path != null)
                    {
                        hover = new Hover(new MarkupContent(MarkupContent.Markdown, path))
                        {
                            range = importNode.Location.range
                        }
                    }
                    ;

                    break;

                default:
                    hover = null;
                    break;
                }
            }

            return(JsonConvert.SerializeObject(hover));
        }
Пример #2
0
        string GetDefinition(string json)
        {
            PosData posData = GetPosData(json);

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

            /*
             * LocationLink location = null;
             *
             * if (posData.SelectedNode != null && posData.SelectedNode.Length > 0)
             *  switch(posData.SelectedNode[0])
             *  {
             *      case ImportNode importNode:
             *
             *          string path = null;
             *          try
             *          {
             *              path = Extras.CombinePathWithDotNotation(posData.File, importNode.File);
             *          }
             *          catch (ArgumentException) {}
             *
             *          if (path != null)
             *              //location = new Location(new Uri(path).AbsoluteUri, Range.Zero);
             *              location = new LocationLink(importNode.Location.range, new Uri(path).AbsoluteUri, Range.Zero, Range.Zero);
             *
             *          break;
             *  }
             *
             * if (location == null) return null;
             * return JsonConvert.SerializeObject(new LocationLink[] { location });
             */

            List <LocationLink> locations = new List <LocationLink>();

            if (documents.ContainsKey(posData.File) && documents[posData.File].Ruleset != null)
            {
                foreach (ImportNode node in documents[posData.File].Ruleset.Imports)
                {
                    string path = Extras.CombinePathWithDotNotation(posData.File, node.File);

                    if (path != null)
                    {
                        locations.Add(new LocationLink(node.Location.range, new Uri(path).AbsoluteUri, Range.Zero, Range.Zero));
                    }
                }
            }
            return(JsonConvert.SerializeObject(locations.ToArray()));
        }
Пример #3
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));
        }
Пример #4
0
        string GetAutocomplete(string json)
        {
            /*
             *  Format:
             *      textDocument
             *      caret
             *          caret.line
             *          caret.character
             */
            PosData posData = GetPosData(json);

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

            List <CompletionItem> completion = new List <CompletionItem>();

            if (posData.SelectedNode != null)
            {
                switch (posData.SelectedNode.FirstOrDefault())
                {
                // Ruleset
                case RulesetNode rulesetNode:

                    completion.AddRange(new CompletionItem[]
                    {
                        new CompletionItem("rule")
                        {
                            kind = CompletionItem.Keyword, textEdit = TextEdit.Insert(posData.Caret, Extras.Lines(
                                                                                          "rule: \"My Rule\"",
                                                                                          "Event.OngoingGlobal",
                                                                                          "{",
                                                                                          "}"
                                                                                          ))
                        },
                        new CompletionItem("define")
                        {
                            kind = CompletionItem.Keyword
                        },
                        new CompletionItem("method")
                        {
                            kind = CompletionItem.Keyword, textEdit = TextEdit.Insert(posData.Caret, Extras.Lines(
                                                                                          "method myMethod()",
                                                                                          "{",
                                                                                          "}"
                                                                                          ))
                        }
                    });
                    break;

                // Rule node
                case RuleNode ruleNode:

                    // Event type
                    if (ruleNode.IsEventOptionSelected(posData.Caret))
                    {
                        completion.AddRange(EnumData.GetEnum <RuleEvent>().GetCompletion());
                    }

                    // Player
                    else if (ruleNode.IsPlayerOptionSelected(posData.Caret))
                    {
                        completion.AddRange(EnumData.GetEnum <PlayerSelector>().GetCompletion());
                    }

                    // Team
                    else if (ruleNode.IsTeamOptionSelected(posData.Caret))
                    {
                        completion.AddRange(EnumData.GetEnum <Team>().GetCompletion());
                    }

                    else if (ruleNode.IsIfSelected(posData.Caret))
                    {
                        completion.AddRange(Element.GetCompletion(true, false));
                    }

                    else
                    {
                        completion.AddRange(new CompletionItem[]
                        {
                            new CompletionItem("Event")
                            {
                                kind = CompletionItem.Enum
                            },
                            new CompletionItem("Team")
                            {
                                kind = CompletionItem.Enum
                            },
                            new CompletionItem("Player")
                            {
                                kind = CompletionItem.Enum
                            },
                        });
                    }
                    break;

                // Actions
                case BlockNode blockNode:

                    // Get all action methods
                    completion.AddRange(Element.GetCompletion(true, true));
                    completion.AddRange(CustomMethodData.GetCompletion());

                    if (parserData.Success)
                    {
                        // Get all variables
                        if (blockNode.RelatedScopeGroup != null)
                        {
                            completion.AddRange(blockNode.RelatedScopeGroup.GetCompletionItems(posData.Caret));
                        }
                        // Get custom methods
                        if (parserData.UserMethods != null)
                        {
                            completion.AddRange(UserMethod.CollectionCompletion(parserData.UserMethods.ToArray()));
                        }
                        // Get structs
                        if (parserData.DefinedTypes != null)
                        {
                            completion.AddRange(DefinedType.CollectionCompletion(parserData.DefinedTypes.ToArray()));
                        }
                    }

                    break;

                // Values
                case MethodNode methodNode:

                    completion.AddRange(Element.GetCompletion(true, false));
                    completion.AddRange(EnumData.GetAllEnumCompletion());
                    completion.AddRange(CustomMethodData.GetCompletion());

                    if (parserData.Success)
                    {
                        // Get all variables
                        if (methodNode.RelatedScopeGroup != null)
                        {
                            completion.AddRange(methodNode.RelatedScopeGroup?.GetCompletionItems(posData.Caret));
                        }
                        // Get custom methods
                        if (parserData.UserMethods != null)
                        {
                            completion.AddRange(UserMethod.CollectionCompletion(parserData.UserMethods.ToArray()));
                        }
                    }

                    break;

                // If the selected node is a string node, show all strings.
                case StringNode stringNode:

                    completion.AddRange(Constants.Strings.Select(str =>
                                                                 new CompletionItem(str)
                    {
                        kind = CompletionItem.Text
                    }
                                                                 ));

                    break;

                case EnumNode enumNode:
                    var add = EnumData.GetEnum(enumNode.Type)?.GetCompletion();

                    if (add != null)
                    {
                        completion.AddRange(add);
                    }

                    break;

                case ImportNode importNode:

                    string currentPath = importNode.File;

                    string path = Extras.CombinePathWithDotNotation(posData.File, importNode.File);

                    if (path != null)
                    {
                        completion.Add(new CompletionItem("../")
                        {
                            kind = CompletionItem.Folder
                        });

                        // GetDirectoryName can return null even if path isn't null.
                        path = Path.GetDirectoryName(path);

                        if (path != null)
                        {
                            foreach (string fullDirectoryPath in Directory.GetDirectories(path))
                            {
                                string directory = new DirectoryInfo(fullDirectoryPath).Name;
                                completion.Add(new CompletionItem(directory)
                                {
                                    kind = CompletionItem.Folder
                                });
                            }
                            foreach (string fullFilePath in Directory.GetFiles(path))
                            {
                                string file = Path.GetFileName(fullFilePath);
                                completion.Add(new CompletionItem(file)
                                {
                                    kind = CompletionItem.File
                                });
                            }
                        }
                    }

                    break;
                }
            }

            return(JsonConvert.SerializeObject(completion.ToArray()));
        }