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

Don't remap line mappings in Razor files #2460

Merged
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
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