Skip to content

Commit

Permalink
Emulate editorconfig options for Razor design-time document, remove R…
Browse files Browse the repository at this point in the history
…azorDocumentOptionsProvider
  • Loading branch information
tmat committed May 2, 2022
1 parent c379c72 commit 6a047ed
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Composition;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;

namespace Microsoft.CodeAnalysis.Options
Expand All @@ -22,5 +23,11 @@ public LegacyGlobalOptionsWorkspaceService(IGlobalOptionService globalOptions)
{
GlobalOptions = globalOptions;
}

public bool RazorUseTabs
=> GlobalOptions.GetOption(RazorLineFormattingOptionsStorage.UseTabs);

public int RazorTabSize
=> GlobalOptions.GetOption(RazorLineFormattingOptionsStorage.TabSize);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ namespace Microsoft.CodeAnalysis.Options
internal interface ILegacyGlobalOptionsWorkspaceService : IWorkspaceService
{
public IGlobalOptionService GlobalOptions { get; }

public bool RazorUseTabs { get; }
public int RazorTabSize { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static bool SupportsDiagnostics([NotNullWhen(returnValue: true)] this Tex
=> document?.Services.GetService<IDocumentOperationService>()?.SupportDiagnostics ?? false;

public static bool IsRazorDocument(this TextDocument document)
=> document.Services.GetService<DocumentPropertiesService>()?.DiagnosticsLspClientName == RazorCSharp;
=> IsRazorDocument(document.State);

public static bool IsRazorDocument(this TextDocumentState documentState)
=> documentState.Services.GetService<DocumentPropertiesService>()?.DiagnosticsLspClientName == RazorCSharp;
}
}
52 changes: 51 additions & 1 deletion src/Workspaces/Core/Portable/Workspace/Solution/ProjectState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Serialization;
using Roslyn.Utilities;

Expand Down Expand Up @@ -292,6 +293,7 @@ public async Task<ImmutableDictionary<string, string>> GetAnalyzerOptionsForPath
internal sealed class ProjectAnalyzerConfigOptionsProvider : AnalyzerConfigOptionsProvider
{
private readonly ProjectState _projectState;
private RazorDesignTimeAnalyzerConfigOptions? _lazyRazorDesignTimeOptions;

public ProjectAnalyzerConfigOptionsProvider(ProjectState projectState)
=> _projectState = projectState;
Expand All @@ -300,7 +302,17 @@ public override AnalyzerConfigOptions GlobalOptions
=> GetOptionsForSourcePath(string.Empty);

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
=> GetOptionsForSourcePath(tree.FilePath);
{
var documentId = DocumentState.GetDocumentIdForTree(tree);
if (documentId != null &&
_projectState.DocumentStates.TryGetState(documentId, out var documentState) &&
documentState.IsRazorDocument())
{
_lazyRazorDesignTimeOptions ??= new RazorDesignTimeAnalyzerConfigOptions(_projectState.LanguageServices.WorkspaceServices);
}

return GetOptionsForSourcePath(tree.FilePath);
}

public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
{
Expand All @@ -312,6 +324,44 @@ public AnalyzerConfigOptions GetOptionsForSourcePath(string path)
=> new DictionaryAnalyzerConfigOptions(_projectState._lazyAnalyzerConfigOptions.GetValue(CancellationToken.None).GetOptionsForSourcePath(path).AnalyzerOptions);
}

/// <summary>
/// Provides editorconfig options for Razor design-time documents.
/// Razor does not support editorconfig options but has custom settings for a few formatting options whose values
/// are only available in-proc and the same for all Razor design-time documents.
/// This type emulates these options as analyzer config options.
/// </summary>
private sealed class RazorDesignTimeAnalyzerConfigOptions : AnalyzerConfigOptions
{
private readonly ILegacyGlobalOptionsWorkspaceService? _globalOptions;

public RazorDesignTimeAnalyzerConfigOptions(HostWorkspaceServices services)
{
// not available OOP:
_globalOptions = services.GetService<ILegacyGlobalOptionsWorkspaceService>());
}

public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value)
{
if (_globalOptions != null)
{
if (key == "indent_style")
{
value = _globalOptions.RazorUseTabs ? "tab" : "space";
return true;
}

if (key == "tab_width" || key == "indent_size")
{
value = _globalOptions.RazorTabSize.ToString();
return true;
}
}

value = null;
return false;
}
}

private sealed class ProjectSyntaxTreeOptionsProvider : SyntaxTreeOptionsProvider
{
private readonly ValueSource<AnalyzerConfigOptionsCache> _lazyAnalyzerConfigSet;
Expand Down

0 comments on commit 6a047ed

Please sign in to comment.