Esempio n. 1
0
        protected override async Task <SourceText> FormatFileAsync(
            Document document,
            OptionSet options,
            ICodingConventionsSnapshot codingConventions,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            var formattedDocument = await Formatter.FormatAsync(document, options, cancellationToken).ConfigureAwait(false);

            return(await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false));
        }
Esempio n. 2
0
        private static bool TryGetCharset(ICodingConventionsSnapshot codingConventions, out Encoding encoding)
        {
            if (codingConventions.TryGetConventionValue("charset", out string charsetOption))
            {
                encoding = GetCharset(charsetOption);
                return(true);
            }

            encoding = null;
            return(false);
        }
Esempio n. 3
0
        public static bool TryGetEndOfLine(ICodingConventionsSnapshot codingConventions, [NotNullWhen(true)] out string?endOfLine)
        {
            if (codingConventions.TryGetConventionValue("end_of_line", out string endOfLineOption))
            {
                endOfLine = GetEndOfLine(endOfLineOption);
                return(true);
            }

            endOfLine = null;
            return(false);
        }
Esempio n. 4
0
 protected override async Task <SourceText> FormatFileAsync(
     Document document,
     OptionSet options,
     ICodingConventionsSnapshot codingConventions,
     FormatOptions formatOptions,
     ILogger logger,
     CancellationToken cancellationToken)
 {
     if (formatOptions.SaveFormattedFiles)
     {
         return(await GetFormattedDocument(document, options, cancellationToken));
     }
     else
     {
         return(await GetFormattedDocumentWithDetailedChanges(document, options, cancellationToken));
     }
 }
Esempio n. 5
0
        protected override async Task <SourceText> FormatFileAsync(
            Document document,
            OptionSet options,
            ICodingConventionsSnapshot codingConventions,
            FormatOptions formatOptions,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            if (!codingConventions.TryGetConventionValue("insert_final_newline", out bool insertFinalNewline))
            {
                return(await document.GetTextAsync(cancellationToken));
            }

            var endOfLine = codingConventions.TryGetConventionValue("end_of_line", out string endOfLineOption)
                ? GetEndOfLine(endOfLineOption)
                : Environment.NewLine;

            var sourceText = await document.GetTextAsync(cancellationToken);

            var lastLine = sourceText.Lines.Last();

            var hasFinalNewline = lastLine.Span.IsEmpty;

            if (insertFinalNewline && !hasFinalNewline)
            {
                var finalNewlineSpan = new TextSpan(lastLine.End, 0);
                var addNewlineChange = new TextChange(finalNewlineSpan, endOfLine);
                sourceText = sourceText.WithChanges(addNewlineChange);
            }
            else if (!insertFinalNewline && hasFinalNewline)
            {
                // In the case of empty files where there is a single empty line, there is nothing to remove.
                while (sourceText.Lines.Count > 1 && hasFinalNewline)
                {
                    var lineBeforeLast      = sourceText.Lines[sourceText.Lines.Count - 2];
                    var finalNewlineSpan    = new TextSpan(lineBeforeLast.End, lineBeforeLast.EndIncludingLineBreak - lineBeforeLast.End);
                    var removeNewlineChange = new TextChange(finalNewlineSpan, string.Empty);
                    sourceText = sourceText.WithChanges(removeNewlineChange);

                    lastLine        = sourceText.Lines.Last();
                    hasFinalNewline = lastLine.Span.IsEmpty;
                }
            }

            return(sourceText);
        }
Esempio n. 6
0
        protected override Task <SourceText> FormatFileAsync(
            Document document,
            SourceText sourceText,
            OptionSet options,
            ICodingConventionsSnapshot codingConventions,
            FormatOptions formatOptions,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                if (!TryGetEndOfLine(codingConventions, out var endOfLine))
                {
                    return sourceText;
                }

                var newSourceText = sourceText;
                for (var lineIndex = 0; lineIndex < newSourceText.Lines.Count; lineIndex++)
                {
                    var line = newSourceText.Lines[lineIndex];
                    var lineEndingSpan = new TextSpan(line.End, line.EndIncludingLineBreak - line.End);

                    // Check for end of file
                    if (lineEndingSpan.Length == 0)
                    {
                        break;
                    }

                    var lineEnding = newSourceText.ToString(lineEndingSpan);

                    if (lineEnding == endOfLine)
                    {
                        continue;
                    }

                    var newLineChange = new TextChange(lineEndingSpan, endOfLine);
                    newSourceText = newSourceText.WithChanges(newLineChange);
                }

                return newSourceText;
            }));
        }
Esempio n. 7
0
        protected override Task <SourceText> FormatFileAsync(
            Document document,
            SourceText sourceText,
            OptionSet options,
            ICodingConventionsSnapshot codingConventions,
            FormatOptions formatOptions,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            return(Task.Run(() =>
            {
                if (!TryGetCharset(codingConventions, out var encoding) ||
                    sourceText.Encoding.Equals(encoding))
                {
                    return sourceText;
                }

                return SourceText.From(sourceText.ToString(), encoding, sourceText.ChecksumAlgorithm);
            }));
        }
Esempio n. 8
0
 public DocumentOptions(OptionSet optionSet, ICodingConventionsSnapshot codingConventionsSnapshot)
 {
     this.optionSet = optionSet;
     this.codingConventionsSnapshot = codingConventionsSnapshot;
 }
 public DocumentOptions(ICodingConventionsSnapshot codingConventionSnapshot, IErrorLoggerService errorLogger)
 {
     _codingConventionSnapshot = codingConventionSnapshot;
     _errorLogger = errorLogger;
 }
Esempio n. 10
0
 public CodingConventionsAnalyzerConfigOptions(ICodingConventionsSnapshot codingConventionsSnapshot, OptionSet fallbackOptions)
 {
     _codingConventionsSnapshot = codingConventionsSnapshot;
     _fallbackOptions           = fallbackOptions;
 }
Esempio n. 11
0
 public OptionSet ApplyConventions(OptionSet optionSet, ICodingConventionsSnapshot codingConventions)
 {
     return(new CodingConventionsAnalyzerConfigOptions(codingConventions, optionSet));
 }
 public DocumentOptions(ICodingConventionsSnapshot codingConventionSnapshot)
 {
     _codingConventionSnapshot = codingConventionSnapshot;
 }
Esempio n. 13
0
 public DocumentOptions(PolicyBag policyBag, ICodingConventionsSnapshot codingConventionsSnapshot)
 {
     this.policyBag = policyBag;
     this.codingConventionsSnapshot = codingConventionsSnapshot;
 }
 public DocumentOptions(ICodingConventionsSnapshot codingConventionSnapshot)
 {
     _codingConventionSnapshot = codingConventionSnapshot;
 }