Skip to content

Commit 8a7d0bb

Browse files
committed
Changed tests to use VsLspFactory utiltiies for multi point selection
1 parent 8dbd672 commit 8a7d0bb

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/ExtractToNewComponentCodeActionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static (MarkupElementSyntax? Start, MarkupElementSyntax? End) GetStartAn
9797
return (null, null);
9898
}
9999

100-
var endElementNode = GetEndElementNode(context, syntaxTree, logger);
100+
var endElementNode = GetEndElementNode(context, syntaxTree);
101101
return (startElementNode, endElementNode);
102102
}
103103

@@ -113,7 +113,7 @@ private static bool IsInsideProperHtmlContent(RazorCodeActionContext context, Ma
113113
context.Location.AbsoluteIndex < startElementNode.EndTag.SpanStart;
114114
}
115115

116-
private static MarkupElementSyntax? GetEndElementNode(RazorCodeActionContext context, RazorSyntaxTree syntaxTree, ILogger logger)
116+
private static MarkupElementSyntax? GetEndElementNode(RazorCodeActionContext context, RazorSyntaxTree syntaxTree)
117117
{
118118
var selectionStart = context.Request.Range.Start;
119119
var selectionEnd = context.Request.Range.End;

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ExtractToNewComponentCodeActionProviderTest.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
using Microsoft.AspNetCore.Razor.Test.Common.LanguageServer;
1414
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1515
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
16-
using Microsoft.CodeAnalysis.Razor.Workspaces;
1716
using Microsoft.CodeAnalysis.Testing;
1817
using Microsoft.CodeAnalysis.Text;
1918
using Microsoft.VisualStudio.LanguageServer.Protocol;
2019
using Moq;
2120
using Xunit;
2221
using Xunit.Abstractions;
22+
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;
2323

2424
namespace Microsoft.AspNetCore.Razor.LanguageServer.Test.CodeActions.Razor;
2525

@@ -55,7 +55,7 @@ public async Task Handle_InvalidFileKind()
5555
var request = new VSCodeActionParams()
5656
{
5757
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
58-
Range = new Range(),
58+
Range = VsLspFactory.DefaultRange,
5959
Context = new VSInternalCodeActionContext()
6060
};
6161

@@ -76,7 +76,7 @@ public async Task Handle_InvalidFileKind()
7676
public async Task Handle_SinglePointSelection_ReturnsNotEmpty()
7777
{
7878
// Arrange
79-
var documentPath = "c:/Test.cs";
79+
var documentPath = "c:/Test.razor";
8080
var contents = """
8181
@page "/"
8282
@@ -102,12 +102,12 @@ public async Task Handle_SinglePointSelection_ReturnsNotEmpty()
102102
var request = new VSCodeActionParams()
103103
{
104104
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
105-
Range = new Range(),
105+
Range = VsLspFactory.DefaultRange,
106106
Context = new VSInternalCodeActionContext()
107107
};
108108

109109
var location = new SourceLocation(cursorPosition, -1, -1);
110-
var context = CreateRazorCodeActionContext(request, location, documentPath, contents);
110+
var context = CreateRazorCodeActionContext(request, location, documentPath, contents, supportsFileCreation: true);
111111

112112
var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory);
113113

@@ -122,7 +122,7 @@ public async Task Handle_SinglePointSelection_ReturnsNotEmpty()
122122
public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
123123
{
124124
// Arrange
125-
var documentPath = "c:/Test.cs";
125+
var documentPath = "c:/Test.razor";
126126
var contents = """
127127
@page "/"
128128
@@ -148,14 +148,15 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
148148
var request = new VSCodeActionParams()
149149
{
150150
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
151-
Range = new Range(),
151+
Range = VsLspFactory.DefaultRange,
152152
Context = new VSInternalCodeActionContext()
153153
};
154154

155155
var location = new SourceLocation(cursorPosition, -1, -1);
156156
var context = CreateRazorCodeActionContext(request, location, documentPath, contents);
157157

158-
AddMultiPointSelectionToContext(context, selectionSpan);
158+
var lineSpan = context.SourceText.GetLinePositionSpan(selectionSpan);
159+
request.Range = VsLspFactory.CreateRange(lineSpan);
159160

160161
var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory);
161162

@@ -175,7 +176,7 @@ public async Task Handle_MultiPointSelection_ReturnsNotEmpty()
175176
public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentElement()
176177
{
177178
// Arrange
178-
var documentPath = "c:/Test.cs";
179+
var documentPath = "c:/Test.razor";
179180
var contents = """
180181
@page "/"
181182
@@ -201,14 +202,15 @@ public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentEl
201202
var request = new VSCodeActionParams()
202203
{
203204
TextDocument = new VSTextDocumentIdentifier { Uri = new Uri(documentPath) },
204-
Range = new Range(),
205+
Range = VsLspFactory.DefaultRange,
205206
Context = new VSInternalCodeActionContext()
206207
};
207208

208209
var location = new SourceLocation(cursorPosition, -1, -1);
209210
var context = CreateRazorCodeActionContext(request, location, documentPath, contents);
210211

211-
AddMultiPointSelectionToContext(context, selectionSpan);
212+
var lineSpan = context.SourceText.GetLinePositionSpan(selectionSpan);
213+
request.Range = VsLspFactory.CreateRange(lineSpan);
212214

213215
var provider = new ExtractToNewComponentCodeActionProvider(LoggerFactory);
214216

@@ -222,6 +224,8 @@ public async Task Handle_MultiPointSelectionWithEndAfterElement_ReturnsCurrentEl
222224
Assert.NotNull(razorCodeActionResolutionParams);
223225
var actionParams = ((JsonElement)razorCodeActionResolutionParams.Data).Deserialize<ExtractToNewComponentCodeActionParams>();
224226
Assert.NotNull(actionParams);
227+
Assert.Equal(selectionSpan.Start, actionParams.ExtractStart);
228+
Assert.Equal(selectionSpan.End, actionParams.ExtractEnd);
225229
}
226230

227231
private static RazorCodeActionContext CreateRazorCodeActionContext(VSCodeActionParams request, SourceLocation location, string filePath, string text, bool supportsFileCreation = true)
@@ -241,34 +245,18 @@ private static RazorCodeActionContext CreateRazorCodeActionContext(VSCodeActionP
241245
codeDocument.SetFileKind(FileKinds.Component);
242246
codeDocument.SetCodeGenerationOptions(RazorCodeGenerationOptions.Create(o =>
243247
{
244-
o.RootNamespace = "ExtractToCodeBehindTest";
248+
o.RootNamespace = "ExtractToComponentTest";
245249
}));
246250
codeDocument.SetSyntaxTree(syntaxTree);
247251

248252
var documentSnapshot = Mock.Of<IDocumentSnapshot>(document =>
249253
document.GetGeneratedOutputAsync() == Task.FromResult(codeDocument) &&
250-
document.GetTextAsync() == Task.FromResult(codeDocument.GetSourceText()), MockBehavior.Strict);
254+
document.GetTextAsync() == Task.FromResult(codeDocument.Source.Text), MockBehavior.Strict);
251255

252256
var sourceText = SourceText.From(text);
253257

254258
var context = new RazorCodeActionContext(request, documentSnapshot, codeDocument, location, sourceText, supportsFileCreation, SupportsCodeActionResolve: true);
255259

256260
return context;
257261
}
258-
259-
private static void AddMultiPointSelectionToContext(RazorCodeActionContext context, TextSpan selectionSpan)
260-
{
261-
var sourceText = context.CodeDocument.Source.Text;
262-
var startLinePosition = sourceText.Lines.GetLinePosition(selectionSpan.Start);
263-
var startPosition = new Position(startLinePosition.Line, startLinePosition.Character);
264-
265-
var endLinePosition = sourceText.Lines.GetLinePosition(selectionSpan.End);
266-
var endPosition = new Position(endLinePosition.Line, endLinePosition.Character);
267-
268-
context.Request.Range = new Range
269-
{
270-
Start = startPosition,
271-
End = endPosition
272-
};
273-
}
274262
}

0 commit comments

Comments
 (0)