Skip to content

Commit

Permalink
Merge pull request #30527 from tmeschter/NewAddFileApis-181015
Browse files Browse the repository at this point in the history
Implement new interfaces for adding files
  • Loading branch information
tmeschter authored Nov 9, 2018
2 parents ea69e38 + 5b250f6 commit 7f47498
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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;
using System.Runtime.InteropServices;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.Interop
{
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("38B39ADD-D4D6-47E1-B996-7ED16D0295A5")]
internal interface IProjectSiteEx
{
void StartBatch();
void EndBatch();

void AddFileEx([MarshalAs(UnmanagedType.LPWStr)] string filePath, [MarshalAs(UnmanagedType.LPWStr)] string linkMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal abstract partial class AbstractLegacyProject : ForegroundThreadAffiniti
protected IProjectCodeModel ProjectCodeModel { get; set; }
protected VisualStudioWorkspace Workspace { get; }

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

#region Mutable fields that should only be used from the UI thread

private readonly VsENCRebuildableProjectImpl _editAndContinueProject;
Expand Down Expand Up @@ -134,6 +136,29 @@ protected void AddFile(
VisualStudioProject.AddSourceFile(filename, sourceCodeKind, folders);
}

protected void AddFile(
string filename,
string linkMetadata,
SourceCodeKind sourceCodeKind)
{
// We have tests that assert that XOML files should not get added; this was similar
// behavior to how ASP.NET projects would add .aspx files even though we ultimately ignored
// them. XOML support is planned to go away for Dev16, but for now leave the logic there.
if (filename.EndsWith(".xoml"))
{
return;
}

var folders = ImmutableArray<string>.Empty;
if (!string.IsNullOrEmpty(linkMetadata))
{
var linkFolderPath = Path.GetDirectoryName(linkMetadata);
folders = linkFolderPath.Split(PathSeparatorCharacters).ToImmutableArray();
}

VisualStudioProject.AddSourceFile(filename, sourceCodeKind, folders);
}

protected void RemoveFile(string filename)
{
AssertIsForeground();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.Interop;
using Roslyn.Utilities;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.Legacy
{
internal abstract partial class AbstractLegacyProject : IProjectSiteEx
{
private readonly Stack<VisualStudioProject.BatchScope> _batchScopes = new Stack<VisualStudioProject.BatchScope>();

public void StartBatch()
{
_batchScopes.Push(VisualStudioProject.CreateBatchScope());
}

public void EndBatch()
{
Contract.ThrowIfFalse(_batchScopes.Count > 0);
var scope = _batchScopes.Pop();
scope.Dispose();
}

public void AddFileEx([MarshalAs(UnmanagedType.LPWStr)] string filePath, [MarshalAs(UnmanagedType.LPWStr)] string linkMetadata)
{
// TODO: uncomment when fixing https://github.com/dotnet/roslyn/issues/5325
//var sourceCodeKind = extension.Equals(".csx", StringComparison.OrdinalIgnoreCase)
// ? SourceCodeKind.Script
// : SourceCodeKind.Regular;
AddFile(filePath, linkMetadata, SourceCodeKind.Regular);
}
}
}

0 comments on commit 7f47498

Please sign in to comment.