This commit is contained in:
David Wengier 2024-11-28 11:46:55 +11:00
Родитель a5a55ea9e9
Коммит 0f2d8eeac3
3 изменённых файлов: 23 добавлений и 21 удалений

Просмотреть файл

@ -7,11 +7,11 @@ namespace Microsoft.CodeAnalysis.Razor.CodeActions.Models;
internal sealed class PromoteToUsingCodeActionParams
{
[JsonPropertyName("importsFileName")]
public required string ImportsFileName { get; init; }
[JsonPropertyName("usingStart")]
public required int UsingStart { get; init; }
[JsonPropertyName("usingDirective")]
public required string UsingDirective { get; init; }
[JsonPropertyName("usingEnd")]
public required int UsingEnd { get; init; }
[JsonPropertyName("removeStart")]
public required int RemoveStart { get; init; }

Просмотреть файл

@ -37,21 +37,19 @@ internal class PromoteUsingCodeActionProvider : IRazorCodeActionProvider
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
}
if (owner.FirstAncestorOrSelf<RazorDirectiveSyntax>() is not { } directive ||
!directive.IsUsingDirective(out _))
var directive = owner.FirstAncestorOrSelf<RazorDirectiveSyntax>();
if (directive is null || !directive.IsUsingDirective(out _))
{
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
}
var importFileName = FileKinds.IsLegacy(context.DocumentSnapshot.FileKind)
? MvcImportProjectFeature.ImportsFileName
: ComponentMetadata.ImportsFileName;
var importFileName = GetImportsFileName(context.DocumentSnapshot.FileKind);
var line = context.CodeDocument.Source.Text.Lines.GetLineFromPosition(context.StartAbsoluteIndex);
var data = new PromoteToUsingCodeActionParams
{
ImportsFileName = importFileName,
UsingDirective = directive.GetContent(),
UsingStart = directive.SpanStart,
UsingEnd = directive.Span.End,
RemoveStart = line.Start,
RemoveEnd = line.EndIncludingLineBreak
};
@ -69,4 +67,11 @@ internal class PromoteUsingCodeActionProvider : IRazorCodeActionProvider
return Task.FromResult<ImmutableArray<RazorVSInternalCodeAction>>([action]);
}
public static string GetImportsFileName(string fileKind)
{
return FileKinds.IsLegacy(fileKind)
? MvcImportProjectFeature.ImportsFileName
: ComponentMetadata.ImportsFileName;
}
}

Просмотреть файл

@ -7,7 +7,6 @@ using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Utilities;
using Microsoft.CodeAnalysis.Razor.CodeActions.Models;
@ -34,15 +33,13 @@ internal class PromoteUsingCodeActionResolver(IFileSystem fileSystem) : IRazorCo
return null;
}
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
if (codeDocument.IsUnsupported())
{
return null;
}
var sourceText = await documentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
var importsFileName = PromoteUsingCodeActionProvider.GetImportsFileName(documentContext.FileKind);
var file = FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath());
var folder = Path.GetDirectoryName(file).AssumeNotNull();
var importsFile = Path.GetFullPath(Path.Combine(folder, "..", actionParams.ImportsFileName));
var importsFile = Path.GetFullPath(Path.Combine(folder, "..", importsFileName));
var importFileUri = new UriBuilder
{
Scheme = Uri.UriSchemeFile,
@ -52,7 +49,7 @@ internal class PromoteUsingCodeActionResolver(IFileSystem fileSystem) : IRazorCo
using var edits = new PooledArrayBuilder<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>();
var textToInsert = actionParams.UsingDirective;
var textToInsert = sourceText.GetSubTextString(TextSpan.FromBounds(actionParams.UsingStart, actionParams.UsingEnd));
var insertLocation = new LinePosition(0, 0);
if (!_fileSystem.FileExists(importsFile))
{
@ -61,7 +58,7 @@ internal class PromoteUsingCodeActionResolver(IFileSystem fileSystem) : IRazorCo
else
{
var st = SourceText.From(_fileSystem.ReadFile(importsFile));
var lastLine = st.Lines[st.Lines.Count - 1];
var lastLine = st.Lines[^1];
insertLocation = new LinePosition(lastLine.LineNumber, 0);
if (lastLine.GetFirstNonWhitespaceOffset() is { } nonWhiteSpaceOffset)
{
@ -77,7 +74,7 @@ internal class PromoteUsingCodeActionResolver(IFileSystem fileSystem) : IRazorCo
Edits = [VsLspFactory.CreateTextEdit(insertLocation, textToInsert)]
});
var removeRange = codeDocument.Source.Text.GetRange(actionParams.RemoveStart, actionParams.RemoveEnd);
var removeRange = sourceText.GetRange(actionParams.RemoveStart, actionParams.RemoveEnd);
edits.Add(new TextDocumentEdit
{