-
Notifications
You must be signed in to change notification settings - Fork 171
Added final newline formatter #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoeRobich
merged 2 commits into
dotnet:master
from
JoeRobich:add-final-newline-formatter
May 24, 2019
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis.Options; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.VisualStudio.CodingConventions; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Tools.Formatters | ||
| { | ||
| internal sealed class EOFNewLineFormatter : DocumentFormatter | ||
| { | ||
| protected override async Task<SourceText> FormatFileAsync( | ||
| Document document, | ||
| OptionSet options, | ||
| ICodingConventionsSnapshot codingConventions, | ||
| 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; | ||
| } | ||
|
|
||
| private string GetEndOfLine(string endOfLineOption) | ||
| { | ||
| switch (endOfLineOption) | ||
| { | ||
| case "lf": | ||
| return "\n"; | ||
| case "cr": | ||
| return "\r"; | ||
| case "crlf": | ||
| return "\r\n"; | ||
| default: | ||
| return Environment.NewLine; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Composition; | ||
| using System.Composition.Hosting.Core; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using Microsoft.VisualStudio.Composition; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Tools.Tests | ||
| { | ||
| internal static class ExportProviderExtensions | ||
| { | ||
| public static CompositionContext AsCompositionContext(this ExportProvider exportProvider) | ||
| { | ||
| return new CompositionContextShim(exportProvider); | ||
| } | ||
|
|
||
| private class CompositionContextShim : CompositionContext | ||
| { | ||
| private readonly ExportProvider _exportProvider; | ||
|
|
||
| public CompositionContextShim(ExportProvider exportProvider) | ||
| { | ||
| _exportProvider = exportProvider; | ||
| } | ||
|
|
||
| public override bool TryGetExport(CompositionContract contract, out object export) | ||
| { | ||
| var importMany = contract.MetadataConstraints.Contains(new KeyValuePair<string, object>("IsImportMany", true)); | ||
| var (contractType, metadataType) = GetContractType(contract.ContractType, importMany); | ||
|
|
||
| if (metadataType != null) | ||
| { | ||
| var methodInfo = (from method in _exportProvider.GetType().GetTypeInfo().GetMethods() | ||
| where method.Name == nameof(ExportProvider.GetExports) | ||
| where method.IsGenericMethod && method.GetGenericArguments().Length == 2 | ||
| where method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(string) | ||
| select method).Single(); | ||
| var parameterizedMethod = methodInfo.MakeGenericMethod(contractType, metadataType); | ||
| export = parameterizedMethod.Invoke(_exportProvider, new[] { contract.ContractName }); | ||
| } | ||
| else | ||
| { | ||
| var methodInfo = (from method in _exportProvider.GetType().GetTypeInfo().GetMethods() | ||
| where method.Name == nameof(ExportProvider.GetExports) | ||
| where method.IsGenericMethod && method.GetGenericArguments().Length == 1 | ||
| where method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType == typeof(string) | ||
| select method).Single(); | ||
| var parameterizedMethod = methodInfo.MakeGenericMethod(contractType); | ||
| export = parameterizedMethod.Invoke(_exportProvider, new[] { contract.ContractName }); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private (Type exportType, Type metadataType) GetContractType(Type contractType, bool importMany) | ||
| { | ||
| if (importMany && contractType.IsConstructedGenericType) | ||
| { | ||
| if (contractType.GetGenericTypeDefinition() == typeof(IList<>) | ||
| || contractType.GetGenericTypeDefinition() == typeof(ICollection<>) | ||
| || contractType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) | ||
| { | ||
| contractType = contractType.GenericTypeArguments[0]; | ||
| } | ||
| } | ||
|
|
||
| if (contractType.IsConstructedGenericType) | ||
| { | ||
| if (contractType.GetGenericTypeDefinition() == typeof(Lazy<>)) | ||
| { | ||
| return (contractType.GenericTypeArguments[0], null); | ||
| } | ||
| else if (contractType.GetGenericTypeDefinition() == typeof(Lazy<,>)) | ||
| { | ||
| return (contractType.GenericTypeArguments[0], contractType.GenericTypeArguments[1]); | ||
| } | ||
| else | ||
| { | ||
| throw new NotSupportedException(); | ||
| } | ||
| } | ||
|
|
||
| throw new NotSupportedException(); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.