Skip to content

Commit

Permalink
Update MappedLocation to better simulate Razor scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Sep 9, 2021
1 parent f930f33 commit cbe330e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 6 additions & 1 deletion src/OmniSharp.Roslyn/OmniSharpWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,13 @@ public void SetAnalyzerReferences(ProjectId id, ImmutableArray<AnalyzerFileRefer

public void AddAdditionalDocument(ProjectId projectId, string filePath)
{
var documentId = DocumentId.CreateNewId(projectId);
var loader = new OmniSharpTextLoader(filePath);
AddAdditionalDocument(projectId, filePath, loader);
}

public void AddAdditionalDocument(ProjectId projectId, string filePath, TextLoader loader)
{
var documentId = DocumentId.CreateNewId(projectId);
var documentInfo = DocumentInfo.Create(documentId, Path.GetFileName(filePath), filePath: filePath, loader: loader);
OnAdditionalDocumentAdded(documentInfo);
}
Expand Down
37 changes: 21 additions & 16 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindReferencesFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,29 +421,34 @@ public Foo Clone() {
[Fact]
public async Task MappedLocationFileNameProperlyRooted()
{
var relativeFile = ".\\pages\\test.xaml";
var folderPath = Directory.GetCurrentDirectory();
var relativeFile = ".\\Pages\\Index.cshtml";
var mappedFilePath = Path.GetFullPath(Path.Combine(folderPath, relativeFile));

var testFiles = new[]
{
new TestFile("a.cs",
@"public class Bar : F$$oo {}"),
new TestFile("b.cs", $@"
#line 23 ""{relativeFile}""
public class Foo {{ }}
new TestFile("Constants.cs",
@"
public static class Constants
{
public const string My$$Text = ""Hello World"";
}"),
new TestFile("index_cshtml.generated.cs", $@"
#line 1 ""{relativeFile}""
Constants.MyText
#line default
#line hidden")
#line hidden"),
new TestFile(mappedFilePath,
@"<p>@Constants.MyText</p>")
};

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.generated.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));

Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath));
var quickFix = Assert.Single(usages.QuickFixes, location => location.FileName.Equals(mappedFilePath));
Assert.Empty(quickFix.Projects);
}

[Fact]
Expand Down Expand Up @@ -493,9 +498,9 @@ private Task<QuickFixResponse> FindUsagesAsync(string code, bool excludeDefiniti
return FindUsagesAsync(new[] { new TestFile("dummy.cs", code) }, false, excludeDefinition);
}

private async Task<QuickFixResponse> FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false)
private async Task<QuickFixResponse> FindUsagesAsync(TestFile[] testFiles, bool onlyThisFile, bool excludeDefinition = false, string folderPath = null)
{
SharedOmniSharpTestHost.AddFilesToWorkspace(testFiles);
SharedOmniSharpTestHost.AddFilesToWorkspace(folderPath, testFiles);
var file = testFiles.Single(tf => tf.Content.HasPosition);
var point = file.Content.GetPointFromPosition();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class GeneratedCode
"project.csproj",
new[] { "netcoreapp3.1" },
new[] { testFile },
otherFiles: null,
ImmutableArray.Create<AnalyzerReference>(reference));

var point = testFile.Content.GetPointFromPosition();
Expand Down
3 changes: 2 additions & 1 deletion tests/TestUtility/OmniSharpTestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public IEnumerable<ProjectId> 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)).ToArray());

foreach (var csxFile in testFiles.Where(f => f.FileName.EndsWith(".csx", StringComparison.OrdinalIgnoreCase)))
{
Expand Down
9 changes: 8 additions & 1 deletion tests/TestUtility/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public static void AddCsxProjectToWorkspace(OmniSharpWorkspace workspace, TestFi
workspace.AddDocument(documentInfo);
}

public static IEnumerable<ProjectId> AddProjectToWorkspace(OmniSharpWorkspace workspace, string filePath, string[] frameworks, TestFile[] testFiles, ImmutableArray<AnalyzerReference> analyzerRefs = default)
public static IEnumerable<ProjectId> AddProjectToWorkspace(OmniSharpWorkspace workspace, string filePath, string[] frameworks, TestFile[] testFiles, TestFile[] otherFiles = null, ImmutableArray<AnalyzerReference> analyzerRefs = default)
{
otherFiles ??= Array.Empty<TestFile>();

var versionStamp = VersionStamp.Create();
var references = GetReferences();
frameworks = frameworks ?? new[] { string.Empty };
Expand Down Expand Up @@ -84,6 +86,11 @@ public static IEnumerable<ProjectId> 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);
}

Expand Down

0 comments on commit cbe330e

Please sign in to comment.