コード例 #1
0
        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);
        }
コード例 #2
0
        public override void DoWork(SkriptFile file, int lineNumber, string rawContent, FileParseContext context)
        {
            AbstractFileNode resultNode = new BaseFileNode();

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

            if (resultNode.NodeContent.IsEmpty() && !resultNode.RawComment.IsEmpty())
            {
                AbstractFileNode commentNode = new CommentLineNode();
                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref commentNode);
                resultNode = commentNode;
            }
            else if (resultNode.NodeContent.IsEmpty() && resultNode.RawComment.IsEmpty())
            {
                AbstractFileNode emptyLineNode = new EmptyLineNode();
                emptyLineNode.MatchedSyntax =
                    new SyntaxMatch(SignatureElements.EmptyLine, ParseResult.Success(context));
                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref emptyLineNode);
                resultNode = emptyLineNode;
            }
            else
            {
                if (file.IsNodeVisible(resultNode))
                {
                    var ctx = ParseContext.FromCode(rawContent);

                    var signatureMatches = new List <(bool isSectionMismatch, AbstractFileNode node)>();
                    //Try to match to one of our known signatures
                    foreach (var(signatureNodeType, signatureDelegate) in NodeSignaturesManager.Instance.SignatureTypes
                             )
                    {
                        ctx.Matches.Clear();
                        ctx.CurrentPosition = context.IndentationChars;

                        var isSectionTypeMismatch = resultNode.IsSectionNode !=
                                                    (signatureNodeType.GetCustomAttribute <SectionNodeAttribute>() !=
                                                     null);


                        var tryParseResult = signatureDelegate.DynamicInvoke(ctx);

                        // We matched one signature
                        if (tryParseResult != null && ctx.HasFinishedLine)
                        {
                            var instance = signatureNodeType.NewInstance(tryParseResult);

                            if (instance is AbstractFileNode fileNode)
                            {
                                NodeContentHelper.ApplyBasicNodeInfoToOtherNode(resultNode, ref fileNode);
                                signatureMatches.Add((isSectionTypeMismatch, fileNode));
                            }

                            if (!isSectionTypeMismatch)
                            {
                                break;
                            }
                        }
                    }

                    var resultingNode = signatureMatches.FirstOrDefault(x => !x.isSectionMismatch).node;
                    if (resultingNode != null)
                    {
                        resultNode = resultingNode;
                    }
                }
            }

            file.Nodes[lineNumber] = resultNode;
        }