|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | +using Microsoft.CodeAnalysis.Formatting; |
| 9 | +using Microsoft.CodeAnalysis.ErrorReporting; |
| 10 | +using Microsoft.CodeAnalysis.Options; |
| 11 | +using Roslyn.Utilities; |
| 12 | +using Microsoft.CodeAnalysis.ImplementType; |
| 13 | + |
| 14 | +namespace Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Options; |
| 15 | + |
| 16 | +using Workspace = CodeAnalysis.Workspace; |
| 17 | + |
| 18 | +internal static class OmniSharpSolutionAnalyzerConfigOptionsUpdater |
| 19 | +{ |
| 20 | + internal static void UpdateOptions(Workspace workspace, OmniSharpEditorConfigOptions editorConfigOptions) |
| 21 | + { |
| 22 | + try |
| 23 | + { |
| 24 | + workspace.SetCurrentSolution(UpdateOptions, changeKind: WorkspaceChangeKind.SolutionChanged); |
| 25 | + |
| 26 | + Solution UpdateOptions(Solution oldSolution) |
| 27 | + { |
| 28 | + var oldFallbackOptions = oldSolution.FallbackAnalyzerOptions; |
| 29 | + oldFallbackOptions.TryGetValue(LanguageNames.CSharp, out var csharpFallbackOptions); |
| 30 | + |
| 31 | + var changedOptions = DetermineChangedOptions(csharpFallbackOptions, editorConfigOptions); |
| 32 | + if (changedOptions.IsEmpty) |
| 33 | + { |
| 34 | + return oldSolution; |
| 35 | + } |
| 36 | + |
| 37 | + var builder = ImmutableDictionary.CreateBuilder<string, string>(AnalyzerConfigOptions.KeyComparer); |
| 38 | + if (csharpFallbackOptions is not null) |
| 39 | + { |
| 40 | + // copy existing option values: |
| 41 | + foreach (var oldKey in csharpFallbackOptions.Keys) |
| 42 | + { |
| 43 | + if (csharpFallbackOptions.TryGetValue(oldKey, out var oldValue)) |
| 44 | + { |
| 45 | + builder.Add(oldKey, oldValue); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // update changed values: |
| 51 | + foreach (var (key, value) in changedOptions) |
| 52 | + { |
| 53 | + builder[key] = value; |
| 54 | + } |
| 55 | + |
| 56 | + var newFallbackOptions = oldFallbackOptions.SetItem( |
| 57 | + LanguageNames.CSharp, |
| 58 | + StructuredAnalyzerConfigOptions.Create(new DictionaryAnalyzerConfigOptions(builder.ToImmutable()))); |
| 59 | + |
| 60 | + return oldSolution.WithFallbackAnalyzerOptions(newFallbackOptions); |
| 61 | + } |
| 62 | + } |
| 63 | + catch (Exception e) when (FatalError.ReportAndPropagate(e, ErrorSeverity.Diagnostic)) |
| 64 | + { |
| 65 | + throw ExceptionUtilities.Unreachable(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static ImmutableDictionary<string, string> DetermineChangedOptions( |
| 70 | + StructuredAnalyzerConfigOptions? csharpFallbackOptions, |
| 71 | + OmniSharpEditorConfigOptions editorConfigOptions) |
| 72 | + { |
| 73 | + var builder = ImmutableDictionary.CreateBuilder<string, string>(); |
| 74 | + |
| 75 | + AddOptionIfChanged(FormattingOptions2.UseTabs, csharpFallbackOptions, editorConfigOptions.LineFormattingOptions.UseTabs, builder); |
| 76 | + AddOptionIfChanged(FormattingOptions2.UseTabs, csharpFallbackOptions, editorConfigOptions.LineFormattingOptions.UseTabs, builder); |
| 77 | + AddOptionIfChanged(FormattingOptions2.TabSize, csharpFallbackOptions, editorConfigOptions.LineFormattingOptions.TabSize, builder); |
| 78 | + AddOptionIfChanged(FormattingOptions2.IndentationSize, csharpFallbackOptions, editorConfigOptions.LineFormattingOptions.IndentationSize, builder); |
| 79 | + AddOptionIfChanged(FormattingOptions2.NewLine, csharpFallbackOptions, editorConfigOptions.LineFormattingOptions.NewLine, builder); |
| 80 | + |
| 81 | + AddOptionIfChanged(ImplementTypeOptionsStorage.InsertionBehavior, csharpFallbackOptions, (ImplementTypeInsertionBehavior)editorConfigOptions.ImplementTypeOptions.InsertionBehavior, builder); |
| 82 | + AddOptionIfChanged(ImplementTypeOptionsStorage.PropertyGenerationBehavior, csharpFallbackOptions, (ImplementTypePropertyGenerationBehavior)editorConfigOptions.ImplementTypeOptions.PropertyGenerationBehavior, builder); |
| 83 | + |
| 84 | + return builder.ToImmutable(); |
| 85 | + |
| 86 | + static void AddOptionIfChanged<T>( |
| 87 | + PerLanguageOption2<T> option, |
| 88 | + StructuredAnalyzerConfigOptions? analyzerConfigOptions, |
| 89 | + T value, |
| 90 | + ImmutableDictionary<string, string>.Builder builder) |
| 91 | + { |
| 92 | + var configName = option.Definition.ConfigName; |
| 93 | + var configValue = option.Definition.Serializer.Serialize(value); |
| 94 | + |
| 95 | + if (analyzerConfigOptions?.TryGetValue(configName, out var existingValue) == true && |
| 96 | + existingValue == configValue) |
| 97 | + { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + builder.Add(configName, configValue); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments