Skip to content

Commit

Permalink
Merge branch 'master' into feature/update-net-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorkstromm authored Oct 14, 2022
2 parents 041a67d + d598ebe commit 547015b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public static QuickFix GetQuickFix(this Location location, OmniSharpWorkspace wo
if (!location.IsInSource)
throw new Exception("Location is not in the source tree");

var lineSpan = Path.GetExtension(location.SourceTree.FilePath).Equals(".cake", StringComparison.OrdinalIgnoreCase)
var lineSpan = Path.GetExtension(location.SourceTree.FilePath).Equals(".cake", StringComparison.OrdinalIgnoreCase) ||
location.SourceTree.FilePath.EndsWith("razor__virtual.cs") ||
location.SourceTree.FilePath.EndsWith("cshtml__virtual.cs")
? location.GetLineSpan()
: location.GetMappedLineSpan();

Expand Down
92 changes: 92 additions & 0 deletions tests/OmniSharp.Lsp.Tests/ReferencesHandlerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,98 @@ public FooConsumer()
Assert.Equal(0, mappedResult.Range.End.Character);
}

[Theory]
[InlineData(9)]
[InlineData(100)]
public async Task FindReferencesWithLineMappingReturnsRegularPosition_Razor(int mappingLine)
{
var code = @"
public class Foo
{
public void b$$ar() { }
}
public class FooConsumer
{
public FooConsumer()
{
#line " + mappingLine + @"
new Foo().bar();
#line default
}
}";

var usages = await FindUsagesAsync(new[] { new TestFile("dummy.razor__virtual.cs", code) }, excludeDefinition: false);
Assert.Equal(2, usages.Count());

var quickFixes = usages.OrderBy(x => x.Range.Start.Line);
var regularResult = quickFixes.ElementAt(0);
var mappedResult = quickFixes.ElementAt(1);

Assert.Equal("dummy.razor__virtual.cs", regularResult.Uri);
Assert.Equal("dummy.razor__virtual.cs", mappedResult.Uri);

Assert.Equal(3, regularResult.Range.Start.Line);
Assert.Equal(10, mappedResult.Range.Start.Line);

// regular + mapped results have regular postition
Assert.Equal(32, regularResult.Range.Start.Character);
Assert.Equal(35, regularResult.Range.End.Character);

Assert.Equal(34, mappedResult.Range.Start.Character);
Assert.Equal(37, mappedResult.Range.End.Character);
}

[Theory]
[InlineData(1, true)] // everything correct
[InlineData(100, true)] // file exists in workspace but mapping incorrect
[InlineData(1, false)] // file doesn't exist in workspace but mapping correct
public async Task FindReferencesWithLineMappingAcrossFilesReturnsRegularPosition_Razor(int mappingLine,
bool mappedFileExistsInWorkspace)
{
var testFiles = new List<TestFile>()
{
new TestFile("a.cshtml__virtual.cs", @"
public class Foo
{
public void b$$ar() { }
}
public class FooConsumer
{
public FooConsumer()
{
#line " + mappingLine + @" ""b.cs""
new Foo().bar();
#line default
}
}"),
};

if (mappedFileExistsInWorkspace)
{
testFiles.Add(new TestFile("b.cs",
@"// hello"));
}

var usages = await FindUsagesAsync(testFiles.ToArray());
Assert.Equal(2, usages.Count());

var regularResult = usages.ElementAt(0);
var mappedResult = usages.ElementAt(1);

Assert.EndsWith("a.cshtml__virtual.cs", regularResult.Uri.Path);
Assert.EndsWith("a.cshtml__virtual.cs", mappedResult.Uri.Path);

Assert.Equal(3, regularResult.Range.Start.Line);
Assert.Equal(10, mappedResult.Range.Start.Line);

// regular + mapped results have regular postition
Assert.Equal(32, regularResult.Range.Start.Character);
Assert.Equal(35, regularResult.Range.End.Character);

Assert.Equal(34, mappedResult.Range.Start.Character);
Assert.Equal(37, mappedResult.Range.End.Character);
}

[Fact]
public async Task CanFindReferencesWithNegativeLineMapping()
{
Expand Down

0 comments on commit 547015b

Please sign in to comment.