Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support having a file be both a document and additional file. #20558

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/EditorFeatures/Core/Shared/Preview/PreviewWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override void OpenDocument(DocumentId documentId, bool activate = true)
{
if (this.CurrentSolution.ContainsAdditionalDocument(documentId))
{
OpenAdditionalDocument(documentId, activate);
OpenAdditionalDocument(documentId);
return;
}

Expand All @@ -60,6 +60,11 @@ public override void OpenDocument(DocumentId documentId, bool activate = true)
this.OnDocumentOpened(documentId, text.Container);
}

/// <summary>
/// Puts the specified additional document into the open state.
/// </summary>
/// <param name="documentId">The <see cref="DocumentId"/> to open.</param>
/// <param name="activate">Ignored - not necessary for additional documents.</param>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the statement for the param here more "this doesn't matter in the preview workspace?"

public override void OpenAdditionalDocument(DocumentId documentId, bool activate = true)
{
var document = this.CurrentSolution.GetAdditionalDocument(documentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal abstract partial class AbstractProject : ForegroundThreadAffinitizedObj
private readonly List<ProjectReference> _projectReferences = new List<ProjectReference>();
private readonly List<VisualStudioMetadataReference> _metadataReferences = new List<VisualStudioMetadataReference>();
private readonly Dictionary<DocumentId, IVisualStudioHostDocument> _documents = new Dictionary<DocumentId, IVisualStudioHostDocument>();
private readonly Dictionary<string, IVisualStudioHostDocument> _documentMonikers = new Dictionary<string, IVisualStudioHostDocument>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, (IVisualStudioHostDocument document, int refCount)> _documentMonikers = new Dictionary<string, (IVisualStudioHostDocument, int)>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, VisualStudioAnalyzer> _analyzers = new Dictionary<string, VisualStudioAnalyzer>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<DocumentId, IVisualStudioHostDocument> _additionalDocuments = new Dictionary<DocumentId, IVisualStudioHostDocument>();

Expand Down Expand Up @@ -410,8 +410,9 @@ public IVisualStudioHostDocument GetCurrentDocumentFromPath(string filePath)
{
lock (_gate)
{
_documentMonikers.TryGetValue(filePath, out var document);
return document;
return _documentMonikers.TryGetValue(filePath, out var value)
? value.document
: null;
}
}

Expand Down Expand Up @@ -892,7 +893,7 @@ private static void OnDocumentUpdatedOnDisk(object sender, EventArgs e)
}
}

private static void OnAdditionalDocumentOpened(object sender, bool isCurrentContext)
private static void OnAdditionalDocumentOpened(object sender, bool isCurrentContextIgnored)
{
IVisualStudioHostDocument document = (IVisualStudioHostDocument)sender;
AbstractProject project = (AbstractProject)document.Project;
Expand All @@ -901,7 +902,7 @@ private static void OnAdditionalDocumentOpened(object sender, bool isCurrentCont

if (project._pushingChangesToWorkspaceHosts)
{
project.ProjectTracker.NotifyWorkspaceHosts(host => host.OnAdditionalDocumentOpened(document.Id, document.GetOpenTextBuffer(), isCurrentContext));
project.ProjectTracker.NotifyWorkspaceHosts(host => host.OnAdditionalDocumentOpened(document.Id, document.GetOpenTextBuffer()));
}
else
{
Expand Down Expand Up @@ -951,6 +952,7 @@ protected void AddFile(
filePath: filename,
sourceCodeKind: sourceCodeKind,
getFolderNames: getFolderNames,
isAdditionalFile: false,
canUseTextBuffer: CanUseTextBuffer,
updatedOnDiskHandler: s_documentUpdatedOnDiskEventHandler,
openedHandler: s_documentOpenedEventHandler,
Expand Down Expand Up @@ -1014,7 +1016,7 @@ internal void AddDocument(IVisualStudioHostDocument document, bool isCurrentCont
lock (_gate)
{
_documents.Add(document.Id, document);
_documentMonikers.Add(document.Key.Moniker, document);
AddMoniker(document);
}

if (_pushingChangesToWorkspaceHosts)
Expand Down Expand Up @@ -1053,22 +1055,22 @@ internal void RemoveDocument(IVisualStudioHostDocument document)
lock (_gate)
{
_documents.Remove(document.Id);
_documentMonikers.Remove(document.Key.Moniker);
RemoveMoniker(document);
}

UninitializeDocument(document);
OnDocumentRemoved(document.Key.Moniker);
}
}

internal void AddAdditionalDocument(IVisualStudioHostDocument document, bool isCurrentContext)
internal void AddAdditionalDocument(IVisualStudioHostDocument document)
{
AssertIsForeground();

lock (_gate)
{
_additionalDocuments.Add(document.Id, document);
_documentMonikers.Add(document.Key.Moniker, document);
AddMoniker(document);
}

if (_pushingChangesToWorkspaceHosts)
Expand All @@ -1077,7 +1079,7 @@ internal void AddAdditionalDocument(IVisualStudioHostDocument document, bool isC

if (document.IsOpen)
{
this.ProjectTracker.NotifyWorkspaceHosts(host => host.OnAdditionalDocumentOpened(document.Id, document.GetOpenTextBuffer(), isCurrentContext));
this.ProjectTracker.NotifyWorkspaceHosts(host => host.OnAdditionalDocumentOpened(document.Id, document.GetOpenTextBuffer()));
}
}

Expand All @@ -1096,12 +1098,48 @@ internal void RemoveAdditionalDocument(IVisualStudioHostDocument document)
lock (_gate)
{
_additionalDocuments.Remove(document.Id);
_documentMonikers.Remove(document.Key.Moniker);
RemoveMoniker(document);
}

UninitializeAdditionalDocument(document);
}

private void AddMoniker(IVisualStudioHostDocument document)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"AddToMonikerToDocumentMap"?

{
var moniker = document.Key.Moniker;
if (_documentMonikers.TryGetValue(moniker, out var value))
{
value.refCount++;
_documentMonikers[moniker] = (value.document, value.refCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must admit: using tuples here seems really terrifying and really likely to make a mistake....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't the right hand side just be "value" since you already have the tuple in hand though?

}
else
{
_documentMonikers.Add(moniker, (document, 1));
}
}

private void RemoveMoniker(IVisualStudioHostDocument document)
{
var moniker = document.Key.Moniker;
if (_documentMonikers.TryGetValue(moniker, out var value))
{
Debug.Assert(value.document.Equals(document));
value.refCount--;
if (value.refCount == 0)
{
_documentMonikers.Remove(moniker);
}
else
{
_documentMonikers[moniker] = (value.document, value.refCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't the right hand side just be "value" since you already have the tuple in hand though?

}
}
else
{
Debug.Fail($"Couldn't find '{moniker}' in {nameof(_documentMonikers)} to remove it.");
}
}

public virtual void Disconnect()
{
AssertIsForeground();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void AddAdditionalFile(string additionalFilePath, Func<IVisualStudioHostD
sourceCodeKind: SourceCodeKind.Regular,
getFolderNames: _ => SpecializedCollections.EmptyReadOnlyList<string>(),
canUseTextBuffer: _ => true,
isAdditionalFile: true,
updatedOnDiskHandler: s_additionalDocumentUpdatedOnDiskEventHandler,
openedHandler: s_additionalDocumentOpenedEventHandler,
closingHandler: s_additionalDocumentClosingEventHandler);
Expand All @@ -146,7 +147,7 @@ public void AddAdditionalFile(string additionalFilePath, Func<IVisualStudioHostD
return;
}

AddAdditionalDocument(document, isCurrentContext: getIsInCurrentContext(document));
AddAdditionalDocument(document);
}

public void RemoveAdditionalFile(string additionalFilePath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Diagnostics;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Shell.Interop;
Expand All @@ -14,38 +15,47 @@ namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
///
/// Immutable, since this object is used as a key into some dictionaries.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(),nq")]
internal class DocumentKey : IEquatable<DocumentKey>
{
private readonly IVisualStudioHostProject _hostProject;
private readonly string _moniker;
public IVisualStudioHostProject HostProject { get; }
public string Moniker { get; }
public bool IsAdditionalFile { get; }

public IVisualStudioHostProject HostProject { get { return _hostProject; } }
public string Moniker { get { return _moniker; } }

public DocumentKey(IVisualStudioHostProject hostProject, string moniker)
public DocumentKey(IVisualStudioHostProject hostProject, string moniker, bool isAdditionalFile)
{
Contract.ThrowIfNull(hostProject);
Contract.ThrowIfNull(moniker);

_hostProject = hostProject;
_moniker = moniker;
HostProject = hostProject;
Moniker = moniker;
IsAdditionalFile = isAdditionalFile;
}

public bool Equals(DocumentKey other)
{
return other != null &&
HostProject == other.HostProject &&
Moniker.Equals(other.Moniker, StringComparison.OrdinalIgnoreCase);
Moniker.Equals(other.Moniker, StringComparison.OrdinalIgnoreCase) &&
IsAdditionalFile.Equals(other.IsAdditionalFile);
}

public override int GetHashCode()
{
return Hash.Combine(HostProject.GetHashCode(), StringComparer.OrdinalIgnoreCase.GetHashCode(Moniker));
return Hash.Combine(HostProject.GetHashCode(),
Hash.Combine(
StringComparer.OrdinalIgnoreCase.GetHashCode(Moniker),
IsAdditionalFile.GetHashCode()));
}

public override bool Equals(object obj)
{
return this.Equals(obj as DocumentKey);
}

private string GetDebuggerDisplay()
{
return $"{Moniker} (additional: {IsAdditionalFile})";
}
}
}
Loading