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

Support #line directive in find references #1660

Merged
merged 7 commits into from Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@
All changes to the project will be documented in this file.

## [1.34.9] - not yet released
* Line pragma is now respected in find references ([#1649](https://github.com/OmniSharp/omnisharp-roslyn/issues/1649), PR:[#1660](https://github.com/OmniSharp/omnisharp-roslyn/pull/1660))
* Do not set mono paths when running in standalone mode ([omnisharp-vscode#3410](https://github.com/OmniSharp/omnisharp-vscode/issues/3410), [omnisharp-vscode#3340](https://github.com/OmniSharp/omnisharp-vscode/issues/3340), [#1650](https://github.com/OmniSharp/omnisharp-roslyn/issues/1650), PR:[#1656](https://github.com/OmniSharp/omnisharp-roslyn/pull/1656))

## [1.34.8] - 2019-11-21
Expand Down
6 changes: 3 additions & 3 deletions src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs
Expand Up @@ -13,7 +13,7 @@ public static QuickFix GetQuickFix(this Location location, OmniSharpWorkspace wo
if (!location.IsInSource)
throw new Exception("Location is not in the source tree");

var lineSpan = location.GetLineSpan();
var lineSpan = location.GetMappedLineSpan();
bjorkstromm marked this conversation as resolved.
Show resolved Hide resolved
var path = lineSpan.Path;
var documents = workspace.GetDocuments(path);

Expand All @@ -25,9 +25,9 @@ public static QuickFix GetQuickFix(this Location location, OmniSharpWorkspace wo
Text = text.Trim(),
FileName = path,
Line = line,
Column = lineSpan.StartLinePosition.Character,
Column = lineSpan.HasMappedPath ? 0 : lineSpan.StartLinePosition.Character, // when a #line directive maps into a separate file, assume columns (0,0)
EndLine = lineSpan.EndLinePosition.Line,
EndColumn = lineSpan.EndLinePosition.Character,
EndColumn = lineSpan.HasMappedPath ? 0 : lineSpan.EndLinePosition.Character,
Projects = documents.Select(document => document.Project.Name).ToArray()
};
}
Expand Down
73 changes: 73 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Expand Up @@ -116,6 +116,79 @@ public FooConsumer()
Assert.Equal(2, usages.QuickFixes.Count());
}

[Fact]
public async Task CanFindReferencesWithLineMapping()
{
const string code = @"
public class Foo
{
public void b$$ar() { }
}

public class FooConsumer
{
public FooConsumer()
{
#line 1
new Foo().bar();
#line default
}
}";

var usages = await FindUsagesAsync(code);
Assert.Equal(2, usages.QuickFixes.Count());

var mappedResult = usages.QuickFixes.FirstOrDefault(x => x.Line == 0);
var regularResult = usages.QuickFixes.FirstOrDefault(x => x.Line == 3);
Assert.NotNull(mappedResult);
Assert.NotNull(regularResult);

// regular result has regular postition
Assert.Equal(32, regularResult.Column);
Assert.Equal(35, regularResult.EndColumn);
}

[Fact]
public async Task CanFindReferencesWithLineMappingAcrossFiles()
{
var testFiles = new[]
{
new TestFile("a.cs", @"
public class Foo
{
public void b$$ar() { }
}

public class FooConsumer
{
public FooConsumer()
{
#line 1 ""b.cs""
new Foo().bar();
#line default
}
}"),
new TestFile("b.cs",
@"// hello")
};

var usages = await FindUsagesAsync(testFiles, onlyThisFile: false);
Assert.Equal(2, usages.QuickFixes.Count());

var mappedResult = usages.QuickFixes.FirstOrDefault(x => x.Line == 0 && x.FileName == "b.cs");
var regularResult = usages.QuickFixes.FirstOrDefault(x => x.Line == 3 && x.FileName == "a.cs");
Assert.NotNull(mappedResult);
Assert.NotNull(regularResult);

// regular result has regular postition
Assert.Equal(32, regularResult.Column);
Assert.Equal(35, regularResult.EndColumn);

// mapped result has column 0,0
Assert.Equal(0, mappedResult.Column);
Assert.Equal(0, mappedResult.EndColumn);
}

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