public bool TryGetDocumentOption(Document document, OptionKey option, out object value) { var editorConfigPersistence = option.Option.StorageLocations.OfType <EditorConfigStorageLocation>().SingleOrDefault(); if (editorConfigPersistence == null) { value = null; return(false); } if (_codingConventionSnapshot.TryGetConventionValue(editorConfigPersistence.KeyName, out value)) { try { value = editorConfigPersistence.ParseValue(value.ToString(), option.Option.Type); return(true); } catch (Exception) { // TODO: report this somewhere? return(false); } } else { return(false); } }
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); }
public override bool TryGetValue(string key, out string value) { if (_codingConventionsSnapshot.TryGetConventionValue(key, out value)) { return(true); } return(_fallbackOptions.TryGetValue(key, out value)); }
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); }
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); }