From 28805c89c1383c0953d0b27f7143f519dbb8c72e Mon Sep 17 00:00:00 2001 From: filipw Date: Fri, 29 Nov 2019 16:57:34 +0100 Subject: [PATCH 1/5] support line mapping in find usages --- src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs index c844c2c6c3..4aec9de6eb 100644 --- a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs +++ b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs @@ -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(); var path = lineSpan.Path; var documents = workspace.GetDocuments(path); From 96af533626a30e592fb93ed861462c8061728c37 Mon Sep 17 00:00:00 2001 From: filipw Date: Fri, 29 Nov 2019 16:57:51 +0100 Subject: [PATCH 2/5] tests for line remapping --- .../FindReferencesFacts.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs index 1c39a37c5c..207547565b 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs @@ -116,6 +116,62 @@ public FooConsumer() Assert.Equal(2, usages.QuickFixes.Count()); } + [Fact] + public async Task CanFindReferencesWithLineRemapping() + { + 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()); + Assert.True(usages.QuickFixes.Count(x => x.Line == 0) == 1, "One result should be resolve to line 0"); + Assert.True(usages.QuickFixes.Count(x => x.Line == 3) == 1, "One result should be resolve to line 3"); + } + + [Fact] + public async Task CanFindReferencesWithLineRemappingAcrossFiles() + { + 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()); + Assert.True(usages.QuickFixes.Count(x => x.Line == 0) == 1, "One result should be resolved to line 1"); + Assert.True(usages.QuickFixes.Count(x => x.FileName == "b.cs") == 1, "One result should be resolved to 'b'cs"); + Assert.True(usages.QuickFixes.Count(x => x.Line == 3) == 1, "One result should be resolved to line 4"); + } + [Fact] public async Task ExcludesMethodDefinition() { From 704fef5f6df9a477976a05d272d3cb2b57367f82 Mon Sep 17 00:00:00 2001 From: filipw Date: Fri, 29 Nov 2019 17:20:31 +0100 Subject: [PATCH 3/5] improved tests --- .../Helpers/LocationExtensions.cs | 4 +-- .../FindReferencesFacts.cs | 31 ++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs index 4aec9de6eb..e798a4b734 100644 --- a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs +++ b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs @@ -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() }; } diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs index 207547565b..962e16eb00 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs @@ -117,7 +117,7 @@ public FooConsumer() } [Fact] - public async Task CanFindReferencesWithLineRemapping() + public async Task CanFindReferencesWithLineMapping() { const string code = @" public class Foo @@ -137,12 +137,19 @@ public FooConsumer() var usages = await FindUsagesAsync(code); Assert.Equal(2, usages.QuickFixes.Count()); - Assert.True(usages.QuickFixes.Count(x => x.Line == 0) == 1, "One result should be resolve to line 0"); - Assert.True(usages.QuickFixes.Count(x => x.Line == 3) == 1, "One result should be resolve to line 3"); + + 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 CanFindReferencesWithLineRemappingAcrossFiles() + public async Task CanFindReferencesWithLineMappingAcrossFiles() { var testFiles = new[] { @@ -167,9 +174,19 @@ public FooConsumer() var usages = await FindUsagesAsync(testFiles, onlyThisFile: false); Assert.Equal(2, usages.QuickFixes.Count()); - Assert.True(usages.QuickFixes.Count(x => x.Line == 0) == 1, "One result should be resolved to line 1"); - Assert.True(usages.QuickFixes.Count(x => x.FileName == "b.cs") == 1, "One result should be resolved to 'b'cs"); - Assert.True(usages.QuickFixes.Count(x => x.Line == 3) == 1, "One result should be resolved to line 4"); + + 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] From 8b913fe547f97535c55db64490cbe362a7f4ea6b Mon Sep 17 00:00:00 2001 From: filipw Date: Fri, 29 Nov 2019 17:22:25 +0100 Subject: [PATCH 4/5] added line pragma support to CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35cf9e274d..50b68723a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From cf180b9f1497f2c08bd4745e3820cc9a334bfec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Bj=C3=B6rkstr=C3=B6m?= Date: Thu, 5 Dec 2019 00:11:22 +0200 Subject: [PATCH 5/5] Exclude Cake files from line span mapping --- src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs index e798a4b734..bbc1e8e082 100644 --- a/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs +++ b/src/OmniSharp.Roslyn.CSharp/Helpers/LocationExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -13,7 +14,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 = location.GetMappedLineSpan(); + var lineSpan = Path.GetExtension(location.SourceTree.FilePath).Equals(".cake", StringComparison.OrdinalIgnoreCase) + ? location.GetLineSpan() + : location.GetMappedLineSpan(); var path = lineSpan.Path; var documents = workspace.GetDocuments(path);