Skip to content

Commit

Permalink
Merge pull request dotnet#33264 from jasonmalinowski/fix-document.fol…
Browse files Browse the repository at this point in the history
…ders-not-populating

Compute relative folder information if we aren't given it
  • Loading branch information
jasonmalinowski authored Feb 13, 2019
2 parents 5ee28ca + 53c3b9b commit 25f0c8f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.Interop;
using Microsoft.VisualStudio.LanguageServices.UnitTests.ProjectSystemShim.Framework;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -29,5 +31,71 @@ public void IgnoreAdditionsOfXomlFiles()
project.OnSourceFileRemoved("Goo.xoml");
}
}

[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
public void AddFileExWithLinkPathUsesThatAsAFolder()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");

project.AddFileEx(@"C:\Cat.cs", linkMetadata: @"LinkFolder\Cat.cs");

var document = environment.Workspace.CurrentSolution.Projects.Single().Documents.Single();

Assert.Equal(new[] { "LinkFolder" }, document.Folders);
}
}

[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
public void AddFileExWithLinkPathWithoutFolderWorksCorrectly()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");

project.AddFileEx(@"C:\Cat.cs", linkMetadata: @"Dog.cs");

var document = environment.Workspace.CurrentSolution.Projects.Single().Documents.Single();

Assert.Empty(document.Folders);
}
}

[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
public void AddFileExWithNoLinkPathComputesEmptyFolder()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");
var projectFolder = Path.GetDirectoryName(environment.Workspace.CurrentSolution.Projects.Single().FilePath);

project.AddFileEx(Path.Combine(projectFolder, "Cat.cs"), null);

var document = environment.Workspace.CurrentSolution.Projects.Single().Documents.Single();

Assert.Empty(document.Folders);
}
}

[WpfFact]
[Trait(Traits.Feature, Traits.Features.ProjectSystemShims)]
public void AddFileExWithNoLinkPathComputesRelativeFolderPath()
{
using (var environment = new TestEnvironment())
{
var project = CSharpHelpers.CreateCSharpProject(environment, "Test");
var projectFolder = Path.GetDirectoryName(environment.Workspace.CurrentSolution.Projects.Single().FilePath);

project.AddFileEx(Path.Combine(projectFolder, "RelativeFolder", "Cat.cs"), null);

var document = environment.Workspace.CurrentSolution.Projects.Single().Documents.Single();

Assert.Equal(new[] { "RelativeFolder" }, document.Folders);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ internal abstract partial class AbstractLegacyProject : ForegroundThreadAffiniti
protected IProjectCodeModel ProjectCodeModel { get; set; }
protected VisualStudioWorkspace Workspace { get; }

/// <summary>
/// The path to the directory of the project. Read-only, since although you can rename
/// a project in Visual Studio you can't change the folder of a project without an
/// unload/reload.
/// </summary>
private readonly string _projectDirectory = null;

private static readonly char[] PathSeparatorCharacters = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };

#region Mutable fields that should only be used from the UI thread
Expand Down Expand Up @@ -64,6 +71,11 @@ public AbstractLegacyProject(
projectFilePath = null;
}

if (projectFilePath != null)
{
_projectDirectory = Path.GetDirectoryName(projectFilePath);
}

var projectFactory = componentModel.GetService<VisualStudioProjectFactory>();
VisualStudioProject = projectFactory.CreateAndAddToWorkspace(
projectSystemName,
Expand Down Expand Up @@ -163,7 +175,13 @@ protected void AddFile(
if (!string.IsNullOrEmpty(linkMetadata))
{
var linkFolderPath = Path.GetDirectoryName(linkMetadata);
folders = linkFolderPath.Split(PathSeparatorCharacters).ToImmutableArray();
folders = linkFolderPath.Split(PathSeparatorCharacters, StringSplitOptions.RemoveEmptyEntries).ToImmutableArray();
}
else if (!string.IsNullOrEmpty(VisualStudioProject.FilePath))
{
var relativePath = PathUtilities.GetRelativePath(_projectDirectory, filename);
var relativePathParts = relativePath.Split(PathSeparatorCharacters);
folders = ImmutableArray.Create(relativePathParts, start: 0, length: relativePathParts.Length - 1);
}

VisualStudioProject.AddSourceFile(filename, sourceCodeKind, folders);
Expand Down

0 comments on commit 25f0c8f

Please sign in to comment.