set file colour in sln explorer based on git status

This commit is contained in:
Matt Parker
2025-10-30 00:18:24 +10:00
parent b5aa964106
commit 4cff69abad
5 changed files with 44 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode, IFileOrFolder
public bool IsCshtmlFile => Path.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase);
public bool IsCsharpFile => Path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase);
public bool IsRoslynWorkspaceFile => IsCsharpFile || IsRazorFile || IsCshtmlFile;
public GitStatus GitStatus { get; set; } = GitStatus.Unaltered;
public required ReactiveProperty<bool> IsDirty { get; init; }
public required bool SuppressDiskChangeEvents { get; set; } // probably has concurrency issues
public required DateTimeOffset? LastIdeWriteTime { get; set; }

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using LibGit2Sharp;
namespace SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
@@ -11,7 +12,34 @@ public static class VsPersistenceMapper
var intermediateModel = await IntermediateMapper.GetIntermediateModel(solutionFilePath, cancellationToken);
var solutionModel = new SharpIdeSolutionModel(solutionFilePath, intermediateModel);
using var repo = new Repository(solutionModel.DirectoryPath);
var status = repo.RetrieveStatus(new StatusOptions());
foreach (var entry in status.Where(s => s.State is not FileStatus.Ignored))
{
// Assumes solution file is at git repo root
var filePath = new FileInfo(Path.Combine(solutionModel.DirectoryPath, entry.FilePath)).FullName; // used to normalise path separators
var fileInSolution = solutionModel.AllFiles.SingleOrDefault(f => f.Path.Equals(filePath, StringComparison.OrdinalIgnoreCase));
if (fileInSolution is null) continue;
var mappedGitStatus = entry.State switch
{
FileStatus.NewInIndex | FileStatus.ModifiedInWorkdir => GitStatus.Added, // I've seen these appear together
FileStatus.NewInIndex or FileStatus.NewInWorkdir => GitStatus.Added,
FileStatus.ModifiedInIndex or FileStatus.ModifiedInWorkdir => GitStatus.Modified,
_ => GitStatus.Unaltered // TODO: handle other kinds?
};
fileInSolution.GitStatus = mappedGitStatus;
}
return solutionModel;
}
}
public enum GitStatus
{
Unaltered,
Modified,
Added
}