Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,24 @@ await _projectManager
private void AddDocumentToMiscProjectCore(ProjectSnapshotManager.Updater updater, string filePath)
{
var textDocumentPath = FilePathNormalizer.Normalize(filePath);
_logger.LogDebug($"Asked to add {textDocumentPath} to the miscellaneous files project, because we don't have project info (yet?)");

_logger.LogDebug($"Adding {filePath} to the miscellaneous files project, because we don't have project info (yet?)");
var miscFilesProject = _projectManager.GetMiscellaneousProject();

if (miscFilesProject.GetDocument(FilePathNormalizer.Normalize(textDocumentPath)) is not null)
if (_projectManager.TryResolveDocumentInAnyProject(textDocumentPath, _logger, out var document))
{
// Document already added. This usually occurs when VSCode has already pre-initialized
// open documents and then we try to manually add all known razor documents.
// Already in a known project, so we don't want it in the misc files project
_logger.LogDebug($"File {textDocumentPath} is already in {document.Project.Key}, so we're not adding it to the miscellaneous files project");
return;
}

var miscFilesProject = _projectManager.GetMiscellaneousProject();

// Representing all of our host documents with a re-normalized target path to workaround GetRelatedDocument limitations.
var normalizedTargetFilePath = textDocumentPath.Replace('/', '\\').TrimStart('\\');

var hostDocument = new HostDocument(textDocumentPath, normalizedTargetFilePath);
var textLoader = _remoteTextLoaderFactory.Create(textDocumentPath);

_logger.LogInformation($"Adding document '{filePath}' to project '{miscFilesProject.Key}'.");
_logger.LogInformation($"Adding document '{textDocumentPath}' to project '{miscFilesProject.Key}'.");

updater.DocumentAdded(miscFilesProject.Key, hostDocument, textLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,47 @@ public async Task AddDocumentToMiscProjectAsync_AddsDocumentToMiscellaneousProje
Assert.False(_projectManager.IsDocumentOpen(DocumentFilePath));
}

[Fact]
public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument()
{
// Arrange
const string ProjectFilePath = "C:/path/to/project.csproj";
const string IntermediateOutputPath = "C:/path/to/obj";
const string RootNamespace = "TestRootNamespace";
const string DocumentFilePath = "C:/path/to/document.cshtml";

await _projectService.AddProjectAsync(
ProjectFilePath, IntermediateOutputPath, RazorConfiguration.Default, RootNamespace, displayName: null, DisposalToken);
await _projectService.AddDocumentToPotentialProjectsAsync(DocumentFilePath, DisposalToken);

// Act
using var listener = _projectManager.ListenToNotifications();

// Act
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);

// Assert
listener.AssertNoNotifications();
}

[Fact]
public async Task AddDocumentToMiscProjectAsync_IgnoresKnownDocument_InMiscFiles()
{
// Arrange
const string DocumentFilePath = "C:/path/to/document.cshtml";

await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);

// Act
using var listener = _projectManager.ListenToNotifications();

// Act
await _projectService.AddDocumentToMiscProjectAsync(DocumentFilePath, DisposalToken);

// Assert
listener.AssertNoNotifications();
}

[Fact]
public async Task RemoveDocument_RemovesDocumentFromOwnerProject()
{
Expand Down