示例#1
0
 /// <summary>
 /// Creates a new <see cref="CodeScript"/> with the <see cref="CodeServiceFactory"/> changed.
 /// </summary>
 public CodeScript WithFactory(CodeServiceFactory factory)
 {
     if (factory != this.Factory)
     {
         return(CreateScript(this.Text, factory, null));
     }
     else
     {
         return(this);
     }
 }
示例#2
0
 private CodeScript(
     string text,
     List <int> lineStarts,
     List <CodeBlock> blocks,
     CodeServiceFactory factory)
 {
     this.Text       = text;
     this.lineStarts = lineStarts;
     this.blocks     = blocks;
     this.Factory    = factory;
 }
 /// <summary>
 /// Combine an additional factory with this factory.
 /// </summary>
 public virtual CodeServiceFactory WithFactory(CodeServiceFactory factory)
 {
     if (factory is AggregateCodeServiceFactory af)
     {
         return(af.WithFactory(this));
     }
     else if (factory.GetType() == this.GetType())
     {
         // specified factory is a replacement for this factory
         return(factory);
     }
     else
     {
         return(new AggregateCodeServiceFactory(new[] { this }).WithFactory(factory));
     }
 }
示例#4
0
        /// <summary>
        /// Creates a <see cref="CodeScript"/> from the text
        /// </summary>
        private static CodeScript CreateScript(
            string text,
            CodeServiceFactory factory,
            IEnumerable <CodeBlock> existingBlocks = null)
        {
            var lineStarts  = new List <int>();
            var blockStarts = new List <int>();

            GetStarts(text, lineStarts, blockStarts);

            var existingBlockMap = existingBlocks != null
                ? existingBlocks.ToTextKeyedDictionary(b => b.Text, b => b)
                : null;

            var blocks = new List <CodeBlock>();

            for (int i = 0; i < blockStarts.Count; i++)
            {
                var start  = blockStarts[i];
                var length = i + 1 < blockStarts.Count ? blockStarts[i + 1] - start : text.Length - start;

                if (existingBlockMap != null && existingBlockMap.TryGetValue(text, start, length, out var block))
                {
                    block = block.WithStart(start);
                }
                else
                {
                    var blockText = text.Substring(start, length);

                    if (!factory.TryGetCodeService(blockText, out var service))
                    {
                        service = new UnknownCodeService(blockText);
                    }

                    block = new CodeBlock(start, service);
                }

                blocks.Add(block);
            }

            return(new CodeScript(text, lineStarts, blocks, factory));
        }
            public override CodeServiceFactory WithFactory(CodeServiceFactory factory)
            {
                // if not changed, then return same aggregate
                if (_factories.Contains(factory))
                {
                    return(this);
                }

                var list = new List <CodeServiceFactory>(_factories);

                var index = list.FindIndex(f => f.GetType() == factory.GetType());

                if (index >= 0)
                {
                    list[index] = factory;
                }
                else
                {
                    list.Add(factory);
                }

                return(new AggregateCodeServiceFactory(list.ToReadOnly()));
            }
示例#6
0
 /// <summary>
 /// Creates a new <see cref="CodeScript"/> with the text and globals changed.
 /// </summary>
 public CodeScript WithTextAndFactory(string newText, CodeServiceFactory factory)
 {
     return(WithText(newText).WithFactory(factory));
 }
示例#7
0
 /// <summary>
 /// Create a new <see cref="CodeScript"/> from the specified text and a <see cref="CodeServiceFactory"/>
 /// </summary>
 public static CodeScript From(string text, CodeServiceFactory factory)
 {
     return(CreateScript(text ?? "", factory));
 }