Esempio n. 1
0
 public FormattedTextLinePart(string Text, TextSpan LineSpan, TextFormat TextFormat)
 {
     this.Text       = Text;
     this.LineSpan   = LineSpan;
     this.TextFormat = TextFormat;
 }
        internal async Task <FormattedText> InternalGetFormattedCodeAsync(string scriptCode, ScriptingOptions Options, FormatColorScheme ColorScheme)
        {
            var document = GetOrCreateDocument(Array.Empty <IParameter>(), scriptCode, Options);

            document = Formatter.FormatAsync(document).Result;
            SourceText documentText = await document.GetTextAsync();

            IEnumerable <ClassifiedSpan> classifiedSpans = await Classifier.GetClassifiedSpansAsync(document, Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(0, documentText.Length));

            var ranges = classifiedSpans.Select(classifiedSpan =>
                                                new Range(classifiedSpan, documentText.GetSubText(classifiedSpan.TextSpan).ToString()));

            var LineSpans = documentText.Lines.Select(x => x.SpanIncludingLineBreak);
            var result    = new FormattedText();

            var originalText = documentText.ToString();
            var codeBuilder  = new StringBuilder();

            var segmentedSpans = DivideSpans(LineSpans, ranges.Select(x => x.TextSpan)).ToArray();

            FormattedTextLine line = result.AppendLine();

            foreach (var span in segmentedSpans)
            {
                var info = ranges.SingleOrDefault(x => x.TextSpan.CompareTo(span) == 0);

                TextFormat format;

                if (info != null)
                {
                    var color = ColorScheme.GetColorForKeyword(info.ClassificationType);
                    format = new TextFormat(color);
                }
                else
                {
                    var color = ColorScheme.Unknown;
                    format = new TextFormat(color);
                }

                var text = originalText.Substring(span.Start, span.Length);

                if (text.EndsWith(Environment.NewLine))
                {
                    text = text.Substring(0, text.Length - Environment.NewLine.Length);
                    line.AppendText(text, format);
                    line = result.AppendLine();
                }
                else
                {
                    line.AppendText(text, format);
                }


                //{
                //text = text.Substring(0, text.Length - Environment.NewLine.Length);

                // line = result.AppendLine();

                /*  } else
                 * {
                 *    line.AppendText(text, format);
                 * }*/
            }

            return(result);
        }