Skip to content
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

RazorLineFormattingOptions #61054

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}