Skip to content
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
3 changes: 0 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
<ExcludeFromSourceOnlyBuild>true</ExcludeFromSourceOnlyBuild>

<DefaultNetFxTargetFramework>net472</DefaultNetFxTargetFramework>

<!-- Uncomment this line to run formatting on runtime code-gen if FUSE is turned on -->
<!-- <DefineConstants>$(DefineConstants);FORMAT_FUSE</DefineConstants> -->
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalInlineCompleti
var formattingContext = FormattingContext.Create(
documentContext.Snapshot,
codeDocument,
options);
options,
// I don't think this makes a difference for inline completions, but it's better to be safe.
useNewFormattingEngine: false);
if (!SnippetFormatter.TryGetSnippetWithAdjustedIndentation(formattingContext, item.Text, hostDocumentIndex, out var newSnippetText))
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

#if !FORMAT_FUSE
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
#endif

namespace Microsoft.CodeAnalysis.Razor.Formatting;

internal sealed class FormattingContext
{
private IReadOnlyList<FormattingSpan>? _formattingSpans;
private IReadOnlyDictionary<int, IndentationContext>? _indentations;
private readonly bool _useNewFormattingEngine;

private FormattingContext(
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options,
bool automaticallyAddUsings,
int hostDocumentIndex,
char triggerCharacter)
char triggerCharacter,
bool useNewFormattingEngine)
{
OriginalSnapshot = originalSnapshot;
CodeDocument = codeDocument;
Options = options;
AutomaticallyAddUsings = automaticallyAddUsings;
HostDocumentIndex = hostDocumentIndex;
TriggerCharacter = triggerCharacter;
_useNewFormattingEngine = useNewFormattingEngine;
}

public static bool SkipValidateComponents { get; set; }
Expand Down Expand Up @@ -229,13 +229,9 @@ public async Task<FormattingContext> WithTextAsync(SourceText changedText, Cance
{
var changedSnapshot = OriginalSnapshot.WithText(changedText);

#if !FORMAT_FUSE
// Formatting always uses design time document
var generator = (IDesignTimeCodeGenerator)changedSnapshot;
var codeDocument = await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false);
#else
var codeDocument = await changedSnapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
#endif
var codeDocument = !_useNewFormattingEngine && changedSnapshot is IDesignTimeCodeGenerator generator
? await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false)
: await changedSnapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);

DEBUG_ValidateComponents(CodeDocument, codeDocument);

Expand All @@ -245,7 +241,8 @@ public async Task<FormattingContext> WithTextAsync(SourceText changedText, Cance
Options,
AutomaticallyAddUsings,
HostDocumentIndex,
TriggerCharacter);
TriggerCharacter,
_useNewFormattingEngine);

return newContext;
}
Expand Down Expand Up @@ -282,20 +279,23 @@ public static FormattingContext CreateForOnTypeFormatting(
options,
automaticallyAddUsings,
hostDocumentIndex,
triggerCharacter);
triggerCharacter,
useNewFormattingEngine: false);
}

public static FormattingContext Create(
IDocumentSnapshot originalSnapshot,
RazorCodeDocument codeDocument,
RazorFormattingOptions options)
RazorFormattingOptions options,
bool useNewFormattingEngine)
{
return new FormattingContext(
originalSnapshot,
codeDocument,
options,
automaticallyAddUsings: false,
hostDocumentIndex: 0,
triggerCharacter: '\0');
triggerCharacter: '\0',
useNewFormattingEngine: useNewFormattingEngine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal class RazorFormattingService : IRazorFormattingService
private readonly ImmutableArray<IFormattingPass> _validationPasses;
private readonly CSharpOnTypeFormattingPass _csharpOnTypeFormattingPass;
private readonly HtmlOnTypeFormattingPass _htmlOnTypeFormattingPass;
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions;

public RazorFormattingService(
IDocumentMappingService documentMappingService,
Expand All @@ -49,7 +50,8 @@ public RazorFormattingService(
new FormattingContentValidationPass(loggerFactory)
];

_documentFormattingPasses = languageServerFeatureOptions.UseNewFormattingEngine
_languageServerFeatureOptions = languageServerFeatureOptions;
_documentFormattingPasses = _languageServerFeatureOptions.UseNewFormattingEngine
? [
new New.HtmlFormattingPass(loggerFactory),
new RazorFormattingPass(languageServerFeatureOptions, loggerFactory),
Expand All @@ -71,13 +73,9 @@ public async Task<ImmutableArray<TextChange>> GetDocumentFormattingChangesAsync(
RazorFormattingOptions options,
CancellationToken cancellationToken)
{
#if !FORMAT_FUSE
// Formatting always uses design time document
var generator = (IDesignTimeCodeGenerator)documentContext.Snapshot;
var codeDocument = await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false);
#else
var codeDocument = await documentContext.Snapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
#endif
var codeDocument = !_languageServerFeatureOptions.UseNewFormattingEngine && documentContext.Snapshot is IDesignTimeCodeGenerator generator
? await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false)
: await documentContext.Snapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);

// Range formatting happens on every paste, and if there are Razor diagnostics in the file
// that can make some very bad results. eg, given:
Expand Down Expand Up @@ -108,7 +106,8 @@ public async Task<ImmutableArray<TextChange>> GetDocumentFormattingChangesAsync(
var context = FormattingContext.Create(
documentSnapshot,
codeDocument,
options);
options,
_languageServerFeatureOptions.UseNewFormattingEngine);
var originalText = context.SourceText;

var result = htmlChanges;
Expand All @@ -130,13 +129,9 @@ public async Task<ImmutableArray<TextChange>> GetCSharpOnTypeFormattingChangesAs
{
var documentSnapshot = documentContext.Snapshot;

#if !FORMAT_FUSE
// Formatting always uses design time document
var generator = (IDesignTimeCodeGenerator)documentSnapshot;
var codeDocument = await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false);
#else
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
#endif
var codeDocument = documentContext.Snapshot is IDesignTimeCodeGenerator generator
? await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false)
: await documentContext.Snapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);

return await ApplyFormattedChangesAsync(
documentSnapshot,
Expand All @@ -155,13 +150,8 @@ public async Task<ImmutableArray<TextChange>> GetHtmlOnTypeFormattingChangesAsyn
{
var documentSnapshot = documentContext.Snapshot;

#if !FORMAT_FUSE
// Formatting always uses design time document
var generator = (IDesignTimeCodeGenerator)documentSnapshot;
var codeDocument = await generator.GenerateDesignTimeOutputAsync(cancellationToken).ConfigureAwait(false);
#else
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
#endif
// Html formatting doesn't use the C# design time document
var codeDocument = await documentContext.Snapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);

return await ApplyFormattedChangesAsync(
documentSnapshot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

namespace Microsoft.CodeAnalysis.Razor.ProjectSystem;

internal sealed class DocumentSnapshot(ProjectSnapshot project, DocumentState state) : IDocumentSnapshot, ILegacyDocumentSnapshot
#if !FORMAT_FUSE
, IDesignTimeCodeGenerator
#endif
internal sealed class DocumentSnapshot(ProjectSnapshot project, DocumentState state) : IDocumentSnapshot, ILegacyDocumentSnapshot, IDesignTimeCodeGenerator
{
public ProjectSnapshot Project { get; } = project;

Expand Down Expand Up @@ -65,10 +62,8 @@ async Task<SyntaxTree> GetCSharpSyntaxTreeCoreAsync(CancellationToken cancellati
}
}

#if !FORMAT_FUSE
public Task<RazorCodeDocument> GenerateDesignTimeOutputAsync(CancellationToken cancellationToken)
=> CompilationHelpers.GenerateDesignTimeCodeDocumentAsync(this, Project.ProjectEngine, cancellationToken);
#endif

#region ILegacyDocumentSnapshot support

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

#if !FORMAT_FUSE
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
Expand All @@ -12,4 +11,3 @@ internal interface IDesignTimeCodeGenerator
{
Task<RazorCodeDocument> GenerateDesignTimeOutputAsync(CancellationToken cancellationToken);
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,18 @@ internal struct RemoteClientInitializationOptions
[JsonPropertyName("htmlVirtualDocumentSuffix")]
public required string HtmlVirtualDocumentSuffix { get; set; }

[JsonPropertyName("includeProjectKeyInGeneratedFilePath")]
public required bool IncludeProjectKeyInGeneratedFilePath { get; set; }

[JsonPropertyName("returnCodeActionAndRenamePathsWithPrefixedSlash")]
public required bool ReturnCodeActionAndRenamePathsWithPrefixedSlash { get; set; }

[JsonPropertyName("forceRuntimeCodeGeneration")]
public required bool ForceRuntimeCodeGeneration { get; set; }

[JsonPropertyName("supportsFileManipulation")]
public required bool SupportsFileManipulation { get; set; }

[JsonPropertyName("showAllCSharpCodeActions")]
public required bool ShowAllCSharpCodeActions { get; set; }

[JsonPropertyName("useNewFormattingEngine")]
public required bool UseNewFormattingEngine { get; set; }

[JsonPropertyName("supportsSoftSelectionInCompletion")]
public required bool SupportsSoftSelectionInCompletion { get; set; }

[JsonPropertyName("useVSCodeCompletionTriggerCharacters")]
public bool UseVsCodeCompletionTriggerCharacters { get; set; }
public required bool UseVsCodeCompletionTriggerCharacters { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public void SetOptions(RemoteClientInitializationOptions options)

public override bool ReturnCodeActionAndRenamePathsWithPrefixedSlash => _options.ReturnCodeActionAndRenamePathsWithPrefixedSlash;

public override bool IncludeProjectKeyInGeneratedFilePath => _options.IncludeProjectKeyInGeneratedFilePath;
public override bool IncludeProjectKeyInGeneratedFilePath => throw new InvalidOperationException("This option does not apply in cohosting.");

public override bool UseRazorCohostServer => _options.UseRazorCohostServer;

public override bool ForceRuntimeCodeGeneration => _options.ForceRuntimeCodeGeneration;
public override bool ForceRuntimeCodeGeneration => true;

public override bool UseNewFormattingEngine => _options.UseNewFormattingEngine;
public override bool UseNewFormattingEngine => true;

public override bool SupportsSoftSelectionInCompletion => _options.SupportsSoftSelectionInCompletion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected override IRemoteInlineCompletionService CreateService(in ServiceArgs a

var hostDocumentIndex = codeDocument.Source.Text.GetRequiredAbsoluteIndex(razorRange.End);

var formattingContext = FormattingContext.Create(context.Snapshot, codeDocument, options);
var formattingContext = FormattingContext.Create(context.Snapshot, codeDocument, options, useNewFormattingEngine: false);
if (!SnippetFormatter.TryGetSnippetWithAdjustedIndentation(formattingContext, text, hostDocumentIndex, out var newSnippetText))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;

internal sealed class RemoteDocumentSnapshot : IDocumentSnapshot
#if !FORMAT_FUSE
, IDesignTimeCodeGenerator
#endif
{
public TextDocument TextDocument { get; }
public RemoteProjectSnapshot ProjectSnapshot { get; }
Expand Down Expand Up @@ -80,17 +76,6 @@ public async ValueTask<RazorCodeDocument> GetGeneratedOutputAsync(CancellationTo
return InterlockedOperations.Initialize(ref _codeDocument, document);
}

#if !FORMAT_FUSE
public async Task<RazorCodeDocument> GenerateDesignTimeOutputAsync(CancellationToken cancellationToken)
{
var projectEngine = await ProjectSnapshot.GetProjectEngineAsync(cancellationToken).ConfigureAwait(false);

return await CompilationHelpers
.GenerateDesignTimeCodeDocumentAsync(this, projectEngine, cancellationToken)
.ConfigureAwait(false);
}
#endif

public IDocumentSnapshot WithText(SourceText text)
{
var id = TextDocument.Id;
Expand Down
Loading