Esempio n. 1
0
        private void TemplateServiceProviderOnTemplateCreated(object sender, GeneratedTemplateInfos e)
        {
            BeginThreadSaveAction(() =>
            {
                TextMarkerService.RemoveAll(f => true);
                foreach (var morestachioError in e.Errors ?? new IMorestachioError[0])
                {
                    //var column = Math.Max(
                    //	morestachioError.Location.Snipped.Snipped.Length - morestachioError.Location.Character - 1,
                    //	morestachioError.Location.Character - 1);
                    var charOffset = Template.GetOffset(morestachioError.Location.Line,
                                                        morestachioError.Location.Character - 1);
                    var textMarker         = TextMarkerService.Create(charOffset, 1);
                    textMarker.MarkerColor = Colors.Red;
                    textMarker.MarkerTypes = TextMarkerTypes.SquigglyUnderline;
                }

                if (e.InferredTemplateModel != null)
                {
                    MorestachioFoldingStrategy.UpdateFolding(FoldingManager, Template, e.InferredTemplateModel);
                }
            });
        }
        public async Task <GeneratedTemplateInfos> GenerateTemplateExecute(string template, IDataSourceProvider dataSourceProvider)
        {
            if (template == null)
            {
                return(null);
            }

            var parsingOptions = new ParserOptions(template,
                                                   () => new MemoryStream(),
                                                   Encoding.Default,
                                                   _templateServiceProvider.ParserOptions.MaxSize,
                                                   _templateServiceProvider.ParserOptions.DisableContentEscaping);

            parsingOptions.Timeout = _templateServiceProvider.ParserOptions.Timeout;
            parsingOptions.Null    = _templateServiceProvider.ParserOptions.NullSubstitute;
            parsingOptions.StackOverflowBehavior = _templateServiceProvider.ParserOptions.PartialStackOverflowBehavior;
            parsingOptions.PartialStackSize      = _templateServiceProvider.ParserOptions.PartialStackSize;
            parsingOptions.ValueResolver         = new JObjectResolver();

            //if (_templateServiceProvider.ParserOptions.EnableExtendedMissingPathOutput)
            //{
            //	parsingOptions.UnresolvedPath += ParsingOptionsOnUnresolvedPath;
            //}

            //var formatterService = new MorestachioFormatterService();
            //var formatters = _templateServiceProvider.ObtainFormatters();

            //foreach (var formatter in formatters)
            //{
            //	var morestachioFormatterModel = formatter.CreateFormatter();
            //	formatterService.Add(morestachioFormatterModel.Function, morestachioFormatterModel);
            //}


            MorestachioDocumentInfo extendedParseInformation;

            try
            {
                extendedParseInformation = Parser.ParseWithOptions(parsingOptions);
            }
            catch (Exception e)
            {
                return(new GeneratedTemplateInfos()
                {
                    Errors = new IMorestachioError[]
                    {
                        new MorestachioSyntaxError(new CharacterLocationExtended(), "Error ", "", "", e.Message),
                    }
                });
            }

            if (extendedParseInformation.Errors.Any())
            {
                var generatedTemplateInfos = new GeneratedTemplateInfos()
                {
                    InferredTemplateModel = extendedParseInformation.Document,
                    Errors = extendedParseInformation.Errors.ToArray()
                };
                _templateServiceProvider.OnTemplateCreated(generatedTemplateInfos);
                return(generatedTemplateInfos);
            }

            var result = await dataSourceProvider.Fetch();

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

            var generateTemplateExecute = new GeneratedTemplateInfos()
            {
                Result = await extendedParseInformation.CreateAndStringifyAsync(result),
                InferredTemplateModel = extendedParseInformation.Document
            };

            _templateServiceProvider.OnTemplateCreated(generateTemplateExecute);
            return(generateTemplateExecute);
        }