Skip to content

Commit

Permalink
RazorLineFormattingOptions (#61054)
Browse files Browse the repository at this point in the history
* RazorLineFormattingOptions

* Fix
  • Loading branch information
tmat authored May 9, 2022
1 parent 3311ae2 commit 321b4d6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Api
{
internal static class Extensions
{
private const string RazorCSharp = "RazorCSharp";

public static bool IsRazorDocument(this Document document)
{
var documentPropertiesService = document.Services.GetService<DocumentPropertiesService>();
if (documentPropertiesService != null && documentPropertiesService.DiagnosticsLspClientName == RazorCSharp)
{
return true;
}

return false;
}
=> Host.Extensions.IsRazorDocument(document);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private async Task AnalyzeDocumentForKindAsync(TextDocument document, AnalysisKi

var isActiveDocument = _documentTrackingService.TryGetActiveDocument() == document.Id;
var isOpenDocument = document.IsOpen();
var isGeneratedRazorDocument = document.Services.GetService<DocumentPropertiesService>()?.DiagnosticsLspClientName != null;
var isGeneratedRazorDocument = document.IsRazorDocument();

// Only analyze open/active documents, unless it is a generated Razor document.
if (!isActiveDocument && !isOpenDocument && !isGeneratedRazorDocument)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis.Options;

namespace Microsoft.CodeAnalysis.Formatting;

/// <summary>
/// Formatting options for Razor design-time documents.
/// </summary>
internal class RazorLineFormattingOptionsStorage
{
internal static readonly Option2<bool> UseTabs = new(
"RazorDesignTimeDocumentFormattingOptions", "UseTabs", LineFormattingOptions.Default.UseTabs);

internal static readonly Option2<int> TabSize = new(
"RazorDesignTimeDocumentFormattingOptions", "TabSize", LineFormattingOptions.Default.TabSize);
}
12 changes: 12 additions & 0 deletions src/Tools/ExternalAccess/Razor/RazorGlobalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public RazorGlobalOptions(IGlobalOptionService globalOptions)
public RazorAutoFormattingOptions GetAutoFormattingOptions()
=> new(_globalOptions.GetAutoFormattingOptions(LanguageNames.CSharp));

public bool UseTabs
{
get => _globalOptions.GetOption(RazorLineFormattingOptionsStorage.UseTabs);
set => _globalOptions.SetGlobalOption(new OptionKey(RazorLineFormattingOptionsStorage.UseTabs), value);
}

public int TabSize
{
get => _globalOptions.GetOption(RazorLineFormattingOptionsStorage.TabSize);
set => _globalOptions.SetGlobalOption(new OptionKey(RazorLineFormattingOptionsStorage.TabSize), value);
}

#pragma warning disable IDE0060 // Remove unused parameter
/// <summary>
/// For testing purposes only. Razor does not use MEF composition for host services so we need to return a mock.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.CodeAnalysis.Host
{
internal static class Extensions
{
private const string RazorCSharp = "RazorCSharp";

public static bool CanApplyChange([NotNullWhen(returnValue: true)] this TextDocument? document)
=> document?.State.CanApplyChange() ?? false;

Expand All @@ -19,5 +21,8 @@ public static bool SupportsDiagnostics([NotNullWhen(returnValue: true)] this Tex

public static bool SupportsDiagnostics([NotNullWhen(returnValue: true)] this TextDocumentState? document)
=> document?.Services.GetService<IDocumentOperationService>()?.SupportDiagnostics ?? false;

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

0 comments on commit 321b4d6

Please sign in to comment.