public void BasicNodeInformationIsCorrect()
        {
            AbstractFileNode resultNode  = new BaseFileNode();
            const string     content     = "this is a##test";
            const string     rawContent  = @"    " + content + " #comment";
            const int        lineNumber  = 0;
            var expectedContentRange     = RangeExtensions.From(lineNumber, 4, 19);
            var expectedCommentRange     = RangeExtensions.From(lineNumber, 19, 28);
            var expectedIndentationRange = RangeExtensions.From(lineNumber, 0, 4);

            var file = new SkriptFile(new Uri("memory://tests"));

            NodeContentHelper.ApplyBasicNodeInfoToNode(rawContent, lineNumber, file, ref resultNode);

            Assert.Equal(content, resultNode.NodeContent);
            Assert.Equal(expectedContentRange, resultNode.ContentRange);
            Assert.Equal(expectedCommentRange, resultNode.CommentRange);
            Assert.Equal(expectedIndentationRange, resultNode.IndentationRange);
            Assert.Equal(new[] { new NodeIndentation(IndentType.Space, 4) }, resultNode.Indentations);
            Assert.Single(resultNode.Indentations);
        }
        private static void ExtractBasicNodeInformationFrom(string content, int line,
                                                            out NodeIndentation[] indentations, out Range indentRange, out Range contentRange,
                                                            out Range nodeRange, out Range commentRange, out string commentContent, out string nodeContent)
        {
            var length           = content.Length;
            var indentCharsCount = content.TakeWhile(char.IsWhiteSpace).Count();

            indentations = content.GetNodeIndentations();
            nodeContent  = content;

            nodeRange = RangeExtensions.From(line, 0, content.Length);

            indentRange = RangeExtensions.From(line, 0, indentCharsCount);

            commentRange = RangeExtensions.From(line, length, length);

            commentContent = string.Empty;

            var matchResult = LineRegex.Match(content);

            if (matchResult.Success)
            {
                var contentGroup = matchResult.Groups[1];
                var commentGroup = matchResult.Groups[2];

                var endingSpaces = Math.Clamp(contentGroup.Length - contentGroup.Value.TrimEnd().Length, 0,
                                              int.MaxValue);

                nodeContent    = contentGroup.Value;
                commentContent = commentGroup.Value;
                length         = contentGroup.Value.TrimEnd().Length;
                commentRange.Start.Character = commentGroup.Index - endingSpaces;
                commentRange.End.Character   = (commentGroup.Index - endingSpaces) + (commentGroup.Length + endingSpaces);
            }

            nodeContent  = nodeContent.Trim();
            contentRange = RangeExtensions.From(line, indentCharsCount, length);
        }
Esempio n. 3
0
        public void KnowsHowToParseSimpleSendEffect()
        {
            var url      = new Uri("memory://file1");
            var contents = "on chat\n    send \"hi\"".SplitOnNewLines();
            var file     = new SkriptFile(url)
            {
                RawContents = contents
            };

            WorkspaceManager.Instance.HandleOpenedFile(file);
            file.PrepareNodes();

            file.HandleChange(new TextDocumentContentChangeEvent
            {
                Range       = RangeExtensions.From(0, 7, 7),
                RangeLength = 1,
                Text        = ":"
            });

            Assert.Equal(2, file.Nodes.Count);

            //Basic node match just to be sure
            for (var i = 0; i < file.Nodes.Count; i++)
            {
                Assert.Equal(contents[i], file.Nodes[i].ToString());
            }


            var results = new[] { "org.bukkit.event.player.AsyncPlayerChatEvent", "ch.njol.skript.effects.EffMessage" };

            for (var i = 0; i < results.Length; i++)
            {
                Assert.True(file.Nodes[i].IsMatchOfType(results[i]),
                            $"{i}: {file.Nodes[i].NodeContent} supposed to be {results[i]}");
            }
        }
Esempio n. 4
0
        public void KnowsHowToHandleParentsAndChildren()
        {
            var url      = new Uri("memory://file1");
            var contents = (
                "on chat\n" +
                "    if message contains \"h\"\n" +
                "        if message contains \"i\"\n" +
                "            send \"hi\""
                ).SplitOnNewLines();
            var file = new SkriptFile(url)
            {
                RawContents = contents
            };

            WorkspaceManager.Instance.HandleOpenedFile(file);
            file.PrepareNodes();

            file.HandleChange(new TextDocumentContentChangeEvent
            {
                Range       = RangeExtensions.From(2, 31, 31),
                RangeLength = 1,
                Text        = ":"
            });

            file.HandleChange(new TextDocumentContentChangeEvent
            {
                Range       = RangeExtensions.From(1, 27, 27),
                RangeLength = 1,
                Text        = ":"
            });

            file.HandleChange(new TextDocumentContentChangeEvent
            {
                Range       = RangeExtensions.From(0, 7, 7),
                RangeLength = 1,
                Text        = ":"
            });

            Assert.Equal(4, file.Nodes.Count);

            //Basic node match just to be sure
            for (var i = 0; i < file.Nodes.Count; i++)
            {
                Assert.Equal(contents[i], file.Nodes[i].ToString());
            }


            var results = new[]
            { "org.bukkit.event.player.AsyncPlayerChatEvent", "", "", "ch.njol.skript.effects.EffMessage" };

            for (var i = 0; i < results.Length; i++)
            {
                if (!string.IsNullOrEmpty(results[i]))
                {
                    Assert.True(file.Nodes[i].IsMatchOfType(results[i]),
                                $"{i}: {file.Nodes[i].NodeContent} supposed to be {results[i]}");
                }
            }


            Assert.Equal(file.Nodes[0], file.Nodes[1].Parent);
            Assert.Equal(file.Nodes[1], file.Nodes[2].Parent);
            Assert.Equal(file.Nodes[2], file.Nodes[3].Parent);
        }