Skip to content

Commit

Permalink
Merge pull request #244 from tonyhallett/fix-coverage-colours-from-vs
Browse files Browse the repository at this point in the history
Fix coverage colours from vs
  • Loading branch information
tonyhallett authored Mar 4, 2022
2 parents 0622814 + 3eea0fb commit 89ff040
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ If you are using option 1) then project and global options will only be used whe

#### Options
```
CoverageColoursFromFontsAndColours Specify true to use Environment / Fonts and Colors / Text Editor for editor Coverage colouring.
CoverageColoursFromFontsAndColours Specify true to use Environment / Fonts and Colors / Text Editor for editor Coverage colouring ( if present).
Coverage Touched Area / Coverage Not Touched Area / Coverage Partially Touched Area.
When false colours used are Green, Red and Gold.
Enabled Specifies whether or not coverage output is enabled
Expand Down
63 changes: 44 additions & 19 deletions SharedProject/Impl/CoverageColorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,37 @@ namespace FineCodeCoverage.Impl
internal class CoverageColorProvider : ICoverageColoursProvider, ICoverageColours
{
private readonly IVsFontAndColorStorage fontAndColorStorage;
private Guid categoryWithCoverage = Guid.Parse("ff349800-ea43-46c1-8c98-878e78f46501");
private readonly ILogger logger;
private readonly uint storeFlags = (uint)(__FCSTORAGEFLAGS.FCSF_READONLY | __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS | __FCSTORAGEFLAGS.FCSF_NOAUTOCOLORS | __FCSTORAGEFLAGS.FCSF_PROPAGATECHANGES);
private System.Windows.Media.Color defaultCoverageTouchedArea = System.Windows.Media.Colors.Green;
private System.Windows.Media.Color defaultCoverageNotTouchedArea = System.Windows.Media.Colors.Red;
private System.Windows.Media.Color defaultCoveragePartiallyTouchedArea = System.Windows.Media.Color.FromRgb(255, 165, 0);
private readonly System.Windows.Media.Color defaultCoverageTouchedArea = System.Windows.Media.Colors.Green;
private readonly System.Windows.Media.Color defaultCoverageNotTouchedArea = System.Windows.Media.Colors.Red;
private readonly System.Windows.Media.Color defaultCoveragePartiallyTouchedArea = System.Windows.Media.Color.FromRgb(255, 165, 0);
private Guid categoryWithCoverage = Guid.Parse("ff349800-ea43-46c1-8c98-878e78f46501");
private bool coverageColoursFromFontsAndColours;
private bool dirty = true;
private bool canUseFontsAndColours = true;
public System.Windows.Media.Color CoverageTouchedArea { get; set; }

public System.Windows.Media.Color CoverageNotTouchedArea { get; set; }

public System.Windows.Media.Color CoveragePartiallyTouchedArea { get; set; }

private bool coverageColoursFromFontsAndColours;
private bool dirty = true;


[ImportingConstructor]
public CoverageColorProvider([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, IAppOptionsProvider appOptionsProvider)
public CoverageColorProvider(
[Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider,
IAppOptionsProvider appOptionsProvider,
ILogger logger
)
{
ThreadHelper.ThrowIfNotOnUIThread();
fontAndColorStorage = (IVsFontAndColorStorage)serviceProvider.GetService(typeof(IVsFontAndColorStorage));
Assumes.Present(fontAndColorStorage);
coverageColoursFromFontsAndColours = appOptionsProvider.Get().CoverageColoursFromFontsAndColours;
UseDefaultColoursIfNotFontsAndColours();
appOptionsProvider.OptionsChanged += AppOptionsProvider_OptionsChanged;
this.logger = logger;
}

private void AppOptionsProvider_OptionsChanged(IAppOptions appOptions)
Expand All @@ -50,15 +58,20 @@ private void UseDefaultColoursIfNotFontsAndColours()
{
if (!coverageColoursFromFontsAndColours)
{
CoverageTouchedArea = defaultCoverageTouchedArea;
CoverageNotTouchedArea = defaultCoverageNotTouchedArea;
CoveragePartiallyTouchedArea = defaultCoveragePartiallyTouchedArea;
UseDefaultColours();
}
}

private void UseDefaultColours()
{
CoverageTouchedArea = defaultCoverageTouchedArea;
CoverageNotTouchedArea = defaultCoverageNotTouchedArea;
CoveragePartiallyTouchedArea = defaultCoveragePartiallyTouchedArea;
}

public async Task PrepareAsync()
{
if (coverageColoursFromFontsAndColours && dirty)
if (coverageColoursFromFontsAndColours && canUseFontsAndColours && dirty)
{
await UpdateColoursFromFontsAndColorsAsync();
}
Expand All @@ -69,6 +82,7 @@ private async Task UpdateColoursFromFontsAndColorsAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var success = fontAndColorStorage.OpenCategory(ref categoryWithCoverage, storeFlags);
var usedFontsAndColors = false;
if (success == VSConstants.S_OK)
{
// https://github.com/microsoft/vs-threading/issues/993
Expand All @@ -80,18 +94,29 @@ System.Windows.Media.Color GetColor(string displayName)
{
return ParseColor(touchAreaInfo[0].crBackground);
}
throw new Exception("Failed to get color");
throw new NotSupportedException($"{getItemSuccess}");
}
try
{
// https://developercommunity.visualstudio.com/t/fonts-and-colors-coverage-settings-available-in-vs/1683898
CoverageTouchedArea = GetColor("Coverage Touched Area");
CoverageNotTouchedArea = GetColor("Coverage Not Touched Area");
CoveragePartiallyTouchedArea = GetColor("Coverage Partially Touched Area");
usedFontsAndColors = true;

}catch(NotSupportedException)
{
logger.Log("No coverage settings available from Fonts and Colors");
}

CoverageTouchedArea = GetColor("Coverage Touched Area");
CoverageNotTouchedArea = GetColor("Coverage Not Touched Area");
CoveragePartiallyTouchedArea = GetColor("Coverage Partially Touched Area");
}
else

fontAndColorStorage.CloseCategory();
if (!usedFontsAndColors)
{
//throw ?
canUseFontsAndColours = false;
UseDefaultColours();
}
fontAndColorStorage.CloseCategory(); // only for success ?

}

private System.Windows.Media.Color ParseColor(uint color)
Expand Down

0 comments on commit 89ff040

Please sign in to comment.