Example #1
0
        /// <summary>
        /// Update an existing template.
        /// </summary>
        /// <param name="templateName">Original template name. The only id of a template.</param>
        /// <param name="newTemplateName">New template Name.</param>
        /// <param name="parameters">New params.</param>
        /// <param name="templateBody">New template body.</param>
        /// <returns>Updated LG file.</returns>
        public Templates UpdateTemplate(string templateName, string newTemplateName, List <string> parameters, string templateBody)
        {
            var template = this.FirstOrDefault(u => u.Name == templateName);

            if (template != null)
            {
                ClearDiagnostics();

                var templateNameLine = BuildTemplateNameLine(newTemplateName, parameters);
                var newTemplateBody  = ConvertTemplateBody(templateBody);
                var content          = $"{templateNameLine}{newLine}{newTemplateBody}";

                // update content
                this.Content = ReplaceRangeContent(
                    this.Content,
                    template.SourceRange.Range.Start.Line - 1,
                    template.SourceRange.Range.End.Line - 1,
                    content);

                var updatedTemplates = new Templates(content: string.Empty, id: Id, importResolver: ImportResolver, expressionParser: ExpressionParser);
                updatedTemplates = new TemplatesTransformer(updatedTemplates).Transform(AntlrParseTemplates(content, Id));

                var originStartLine = template.SourceRange.Range.Start.Line - 1;
                AppendDiagnosticsWithOffset(updatedTemplates.Diagnostics, originStartLine);

                var newTemplate = updatedTemplates.FirstOrDefault();
                if (newTemplate != null)
                {
                    AdjustRangeForUpdateTemplate(template, newTemplate);
                    new StaticChecker(this).Check().ForEach(u => this.Diagnostics.Add(u));
                }
            }

            return(this);
        }
Example #2
0
        /// <summary>
        /// Add a new template and return LG File.
        /// </summary>
        /// <param name="templateName">New template name.</param>
        /// <param name="parameters">New params.</param>
        /// <param name="templateBody">New  template body.</param>
        /// <returns>Updated LG file.</returns>
        public Templates AddTemplate(string templateName, List<string> parameters, string templateBody)
        {
            var template = this.FirstOrDefault(u => u.Name == templateName);
            if (template != null)
            {
                throw new Exception(TemplateErrors.TemplateExist(templateName));
            }

            ClearDiagnostics();

            var templateNameLine = BuildTemplateNameLine(templateName, parameters);
            var newTemplateBody = ConvertTemplateBody(templateBody);
            var content = $"{templateNameLine}{newLine}{newTemplateBody}";

            var originStartLine = GetLinesOfText(this.Content).Length;

            // update content
            this.Content = $"{Content}{newLine}{templateNameLine}{newLine}{newTemplateBody}";

            var newTemplates = new Templates(content: string.Empty, id: Id, importResolver: ImportResolver, expressionParser: ExpressionParser);
            newTemplates = new TemplatesTransformer(newTemplates).Transform(AntlrParseTemplates(content, Id));

            AppendDiagnosticsWithOffset(newTemplates.Diagnostics, originStartLine);

            var newTemplate = newTemplates.FirstOrDefault();
            if (newTemplate != null)
            {
                AdjustRangeForAddTemplate(newTemplate, originStartLine);
                this.Add(newTemplate);
                new StaticChecker(this).Check().ForEach(u => this.Diagnostics.Add(u));
            }

            return this;
        }