diff --git a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs index 643f58cb2d..eb6f941727 100644 --- a/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs +++ b/src/OmniSharp.Roslyn/OmniSharpWorkspace.cs @@ -62,12 +62,12 @@ public OmniSharpWorkspace(HostServicesAggregator aggregator, ILoggerFactory logg private void OnDirectoryRemoved(string path, FileChangeType changeType) { - if(changeType == FileChangeType.DirectoryDelete) + if (changeType == FileChangeType.DirectoryDelete) { var docs = CurrentSolution.Projects.SelectMany(x => x.Documents) .Where(x => x.FilePath.StartsWith(path + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)); - foreach(var doc in docs) + foreach (var doc in docs) { OnDocumentRemoved(doc.Id); } @@ -340,7 +340,8 @@ public IEnumerable GetDocuments(string filePath) { return CurrentSolution .GetDocumentIdsWithFilePath(filePath) - .Select(id => CurrentSolution.GetDocument(id)); + .Select(id => CurrentSolution.GetDocument(id)) + .OfType(); } public Document GetDocument(string filePath) @@ -542,8 +543,13 @@ public void SetAnalyzerReferences(ProjectId id, ImmutableArray@Constants.MyText

") }; - var usages = await FindUsagesAsync(testFiles, onlyThisFile: false); + var usages = await FindUsagesAsync(testFiles, onlyThisFile: false, folderPath: folderPath); - Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.EndsWith("b.cs")); + Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.EndsWith("Index.cshtml_virtual.cs")); Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.Equals(relativeFile)); - var project = SharedOmniSharpTestHost.Workspace.CurrentSolution.Projects.Single(); - var projectFolder = Path.GetDirectoryName(project.FilePath); - var mappedFilePath = Path.GetFullPath(Path.Combine(projectFolder, relativeFile)); + var quickFix = Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath)); + Assert.Empty(quickFix.Projects); + } + + [Fact] + public async Task MappedLocationFileNameProperlyRootedInMiscellaneousWorkspace() + { + var folderPath = Directory.GetCurrentDirectory(); + var relativeFile = ".\\Index.cshtml.cs"; + var mappedFilePath = Path.GetFullPath(Path.Combine(folderPath, relativeFile)); - Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath)); + var testFiles = new[] + { + new TestFile("Constants.cs", @" + public static class Constants + { + public const string My$$Text = ""Hello World""; + }"), + new TestFile("Index.cshtml_virtual.cs", $@" + #line 1 ""{relativeFile}"" + Constants.MyText + #line default + #line hidden") + }; + + var miscFile = new TestFile(mappedFilePath, "// Constants.MyText;"); + + SharedOmniSharpTestHost.AddFilesToWorkspace(folderPath, testFiles); + SharedOmniSharpTestHost.Workspace.TryAddMiscellaneousDocument( + miscFile.FileName, + TextLoader.From(TextAndVersion.Create(miscFile.Content.Text, VersionStamp.Create())), + LanguageNames.CSharp); + + var testFile = testFiles.Single(tf => tf.Content.HasPosition); + var usages = await FindUsagesAsync(testFile, onlyThisFile: false); + + Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.EndsWith("Index.cshtml_virtual.cs")); + Assert.DoesNotContain(usages.QuickFixes, location => location.FileName.Equals(relativeFile)); + + var quickFix = Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath)); + Assert.Empty(quickFix.Projects); } [Fact] @@ -493,11 +550,16 @@ private Task FindUsagesAsync(string code, bool excludeDefiniti return FindUsagesAsync(new[] { new TestFile("dummy.cs", code) }, false, excludeDefinition); } - private async Task FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false) + private Task FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false, string folderPath = null) + { + SharedOmniSharpTestHost.AddFilesToWorkspace(folderPath, testFiles); + var testFile = testFiles.Single(tf => tf.Content.HasPosition); + return FindUsagesAsync(testFile, onlyThisFile, excludeDefinition); + } + + private async Task FindUsagesAsync(TestFile testFile, bool onlyThisFile, bool excludeDefinition = false) { - SharedOmniSharpTestHost.AddFilesToWorkspace(testFiles); - var file = testFiles.Single(tf => tf.Content.HasPosition); - var point = file.Content.GetPointFromPosition(); + var point = testFile.Content.GetPointFromPosition(); var requestHandler = GetRequestHandler(SharedOmniSharpTestHost); @@ -505,8 +567,8 @@ private async Task FindUsagesAsync(TestFile[] testFiles, bool { Line = point.Line, Column = point.Offset, - FileName = file.FileName, - Buffer = file.Content.Code, + FileName = testFile.FileName, + Buffer = testFile.Content.Code, OnlyThisFile = onlyThisFile, ExcludeDefinition = excludeDefinition }; diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/SourceGeneratorFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/SourceGeneratorFacts.cs index 8ef4fd428d..e4cb5b1eef 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/SourceGeneratorFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/SourceGeneratorFacts.cs @@ -47,6 +47,7 @@ class GeneratedCode "project.csproj", new[] { "netcoreapp3.1" }, new[] { testFile }, + otherFiles: null, ImmutableArray.Create(reference)); var point = testFile.Content.GetPointFromPosition(); diff --git a/tests/TestUtility/OmniSharpTestHost.cs b/tests/TestUtility/OmniSharpTestHost.cs index 2a739749e6..391c044a86 100644 --- a/tests/TestUtility/OmniSharpTestHost.cs +++ b/tests/TestUtility/OmniSharpTestHost.cs @@ -144,7 +144,10 @@ public IEnumerable AddFilesToWorkspace(string folderPath, params Test Workspace, Path.Combine(folderPath, "project.csproj"), new[] { "net472" }, - testFiles.Where(f => f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)).ToArray()); + testFiles.Where(f => f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)).ToArray(), + testFiles.Where(f => !f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) + && !f.FileName.EndsWith(".csx", StringComparison.OrdinalIgnoreCase) + && !f.FileName.EndsWith(".cake", StringComparison.OrdinalIgnoreCase)).ToArray()); foreach (var csxFile in testFiles.Where(f => f.FileName.EndsWith(".csx", StringComparison.OrdinalIgnoreCase))) { diff --git a/tests/TestUtility/TestHelpers.cs b/tests/TestUtility/TestHelpers.cs index 93d0f7a2da..fd47b141d2 100644 --- a/tests/TestUtility/TestHelpers.cs +++ b/tests/TestUtility/TestHelpers.cs @@ -46,8 +46,10 @@ public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFi workspace.AddDocument(documentInfo); } - public static IEnumerable AddProjectToWorkspace(OmniSharpWorkspace workspace, string filePath, string[] frameworks, TestFile[] testFiles, ImmutableArray analyzerRefs = default) + public static IEnumerable AddProjectToWorkspace(OmniSharpWorkspace workspace, string filePath, string[] frameworks, TestFile[] testFiles, TestFile[] otherFiles = null, ImmutableArray analyzerRefs = default) { + otherFiles ??= Array.Empty(); + var versionStamp = VersionStamp.Create(); var references = GetReferences(); frameworks = frameworks ?? new[] { string.Empty }; @@ -84,6 +86,11 @@ public static IEnumerable AddProjectToWorkspace(OmniSharpWorkspace wo workspace.AddDocument(projectInfo.Id, testFile.FileName, TextLoader.From(TextAndVersion.Create(testFile.Content.Text, versionStamp)), SourceCodeKind.Regular); } + foreach (var otherFile in otherFiles) + { + workspace.AddAdditionalDocument(projectInfo.Id, otherFile.FileName, TextLoader.From(TextAndVersion.Create(otherFile.Content.Text, versionStamp))); + } + projectsIds.Add(projectInfo.Id); }