Skip to content

Commit

Permalink
(cake-buildGH-19) First pass at adding StyleCop
Browse files Browse the repository at this point in the history
  • Loading branch information
gep13 authored and Structed committed Aug 26, 2017
1 parent 4d4aa6b commit f9a30cd
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 167 deletions.
53 changes: 27 additions & 26 deletions src/Adornments/AdornmentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class AdornmentProvider : IWpfTextViewCreationListener
private const string PropertyName = "ShowWatermark";
private const double InitOpacity = 0.4D;

private static bool _isVisible, _hasLoaded;
private static bool _isVisible;
private static bool _hasLoaded;
private SettingsManager _settingsManager;

[Import]
Expand All @@ -30,6 +31,31 @@ class AdornmentProvider : IWpfTextViewCreationListener
[Import]
public SVsServiceProvider ServiceProvider { get; set; }

public void TextViewCreated(IWpfTextView textView)
{
ITextDocument document;

if (!TextDocumentFactoryService.TryGetTextDocument(textView.TextDataModel.DocumentBuffer, out document))
{
return;
}

LoadSettings();

var fileName = Path.GetFileName(document.FilePath).ToLowerInvariant();

// Check if filename is absolute because when debugging, script files are sometimes dynamically created.
if (string.IsNullOrEmpty(fileName) || !Path.IsPathRooted(document.FilePath))
{
return;
}

if (fileName.EndsWith(".cake"))
{
textView.Properties.GetOrCreateSingletonProperty(() => new LogoAdornment(textView, _isVisible, InitOpacity));
}
}

private void LoadSettings()
{
if (_hasLoaded)
Expand Down Expand Up @@ -59,30 +85,5 @@ private void AdornmentVisibilityChanged(object sender, bool isVisible)

wstore.SetBoolean(Vsix.Name, PropertyName, isVisible);
}

public void TextViewCreated(IWpfTextView textView)
{
ITextDocument document;

if (!TextDocumentFactoryService.TryGetTextDocument(textView.TextDataModel.DocumentBuffer, out document))
{
return;
}

LoadSettings();

var fileName = Path.GetFileName(document.FilePath).ToLowerInvariant();

// Check if filename is absolute because when debugging, script files are sometimes dynamically created.
if (string.IsNullOrEmpty(fileName) || !Path.IsPathRooted(document.FilePath))
{
return;
}

if (fileName.EndsWith(".cake"))
{
textView.Properties.GetOrCreateSingletonProperty(() => new LogoAdornment(textView, _isVisible, InitOpacity));
}
}
}
}
7 changes: 4 additions & 3 deletions src/Adornments/LogoAdornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ namespace Cake.VisualStudio.Adornments
{
class LogoAdornment
{
private readonly double _initOpacity;

private IAdornmentLayer _adornmentLayer;
private Image _adornment;
private readonly double _initOpacity;
private double _currentOpacity;

public LogoAdornment(IWpfTextView view, bool isVisible, double initOpacity)
public LogoAdornment(IWpfTextView view, bool isVisible, double initOpacity)
{
_adornmentLayer = view.GetAdornmentLayer(AdornmentLayer.LayerName);
_currentOpacity = isVisible ? initOpacity : 0;
Expand Down Expand Up @@ -62,7 +63,7 @@ private static ImageSource GetImage()
var folder = Path.GetDirectoryName(assembly);
var file = Path.Combine(folder, "Resources\\icon.png");

var url = new Uri(file, UriKind.Absolute);
var url = new Uri(file, UriKind.Absolute);
return BitmapFrame.Create(url);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Cake.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>Cake.VisualStudio.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\Cake.VisualStudio.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -69,6 +71,8 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>Cake.VisualStudio.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Release\Cake.VisualStudio.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Label="TemplateBuilder">
<TemplateBuilderTargets Condition=" '$(TemplateBuilderTargets)'=='' ">$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\TemplateBuilder.1.1.4.5-beta\tools\ligershark.templates.targets ))</TemplateBuilderTargets>
Expand Down Expand Up @@ -146,6 +150,7 @@
<SubType>Designer</SubType>
</Content>
<None Include="app.config" />
<None Include="Cake.VisualStudio.ruleset" />
<None Include="packages.config" />
<Content Include="..\packages\cake.core.0.21.1\cake.core.0.21.1.nupkg">
<IncludeInVSIX>true</IncludeInVSIX>
Expand Down Expand Up @@ -433,6 +438,11 @@
<IncludeOutputGroupsInVSIX>TemplateProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
Expand Down
77 changes: 77 additions & 0 deletions src/Cake.VisualStudio.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Microsoft Managed Recommended Rules" Description="These rules focus on the most critical problems in your code, including potential security holes, application crashes, and other important logic and design errors. It is recommended to include this rule set in any custom rule set you create for your projects." ToolsVersion="10.0">
<Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
<Name Resource="MinimumRecommendedRules_Name" />
<Description Resource="MinimumRecommendedRules_Description" />
</Localization>
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
<Rule Id="CA1049" Action="Warning" />
<Rule Id="CA1060" Action="Warning" />
<Rule Id="CA1061" Action="Warning" />
<Rule Id="CA1063" Action="Warning" />
<Rule Id="CA1065" Action="Warning" />
<Rule Id="CA1301" Action="Warning" />
<Rule Id="CA1400" Action="Warning" />
<Rule Id="CA1401" Action="Warning" />
<Rule Id="CA1403" Action="Warning" />
<Rule Id="CA1404" Action="Warning" />
<Rule Id="CA1405" Action="Warning" />
<Rule Id="CA1410" Action="Warning" />
<Rule Id="CA1415" Action="Warning" />
<Rule Id="CA1821" Action="Warning" />
<Rule Id="CA1900" Action="Warning" />
<Rule Id="CA1901" Action="Warning" />
<Rule Id="CA2002" Action="Warning" />
<Rule Id="CA2100" Action="Warning" />
<Rule Id="CA2101" Action="Warning" />
<Rule Id="CA2108" Action="Warning" />
<Rule Id="CA2111" Action="Warning" />
<Rule Id="CA2112" Action="Warning" />
<Rule Id="CA2114" Action="Warning" />
<Rule Id="CA2116" Action="Warning" />
<Rule Id="CA2117" Action="Warning" />
<Rule Id="CA2122" Action="Warning" />
<Rule Id="CA2123" Action="Warning" />
<Rule Id="CA2124" Action="Warning" />
<Rule Id="CA2126" Action="Warning" />
<Rule Id="CA2131" Action="Warning" />
<Rule Id="CA2132" Action="Warning" />
<Rule Id="CA2133" Action="Warning" />
<Rule Id="CA2134" Action="Warning" />
<Rule Id="CA2137" Action="Warning" />
<Rule Id="CA2138" Action="Warning" />
<Rule Id="CA2140" Action="Warning" />
<Rule Id="CA2141" Action="Warning" />
<Rule Id="CA2146" Action="Warning" />
<Rule Id="CA2147" Action="Warning" />
<Rule Id="CA2149" Action="Warning" />
<Rule Id="CA2200" Action="Warning" />
<Rule Id="CA2202" Action="Warning" />
<Rule Id="CA2207" Action="Warning" />
<Rule Id="CA2212" Action="Warning" />
<Rule Id="CA2213" Action="Warning" />
<Rule Id="CA2214" Action="Warning" />
<Rule Id="CA2216" Action="Warning" />
<Rule Id="CA2220" Action="Warning" />
<Rule Id="CA2229" Action="Warning" />
<Rule Id="CA2231" Action="Warning" />
<Rule Id="CA2232" Action="Warning" />
<Rule Id="CA2235" Action="Warning" />
<Rule Id="CA2236" Action="Warning" />
<Rule Id="CA2237" Action="Warning" />
<Rule Id="CA2238" Action="Warning" />
<Rule Id="CA2240" Action="Warning" />
<Rule Id="CA2241" Action="Warning" />
<Rule Id="CA2242" Action="Warning" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1101" Action="None" />
<Rule Id="SA1200" Action="None" />
<Rule Id="SA1309" Action="None" />
<Rule Id="SA1633" Action="None" />
</Rules>
</RuleSet>
48 changes: 26 additions & 22 deletions src/CakePackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,14 @@ namespace Cake.VisualStudio
public sealed partial class CakePackage : Package, IVsShellPropertyEvents
{
private static DTE2 _dte;
internal static DTE2 Dte => _dte ?? (_dte = (DTE2) GetGlobalService(typeof(DTE)));
internal static IVsUIShell Shell => _shell ?? (_shell = (IVsUIShell) GetGlobalService(typeof(IVsUIShell)));

uint _cookie;
private static IVsUIShell _shell;
internal static DTE2 Dte => _dte ?? (_dte = (DTE2)GetGlobalService(typeof(DTE)));

protected override void Initialize()
{
Logger.Initialize(this, Vsix.Name);
base.Initialize();
var shellService = GetService(typeof(SVsShell)) as IVsShell;
internal static IVsUIShell Shell => _shell ?? (_shell = (IVsUIShell)GetGlobalService(typeof(IVsUIShell)));

if (shellService != null)
{
ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this, out _cookie));
}
uint _cookie;

Menus.InstallBootstrapperCommand.Initialize(this);
Menus.InstallShellBootstrapperCommand.Initialize(this);
Menus.InstallConfigFileCommand.Initialize(this);
}
private static IVsUIShell _shell;

public static bool IsDocumentDirty(string documentPath, out IVsPersistDocData persistDocData)
{
Expand All @@ -69,26 +56,43 @@ public static bool IsDocumentDirty(string documentPath, out IVsPersistDocData pe
public int OnShellPropertyChange(int propid, object var)
{
// when zombie state changes to false, finish package initialization
if ((int)__VSSPROPID.VSSPROPID_Zombie == propid)
if (propid == (int)__VSSPROPID.VSSPROPID_Zombie)
{
if ((bool)var == false)
{
// zombie state dependent code
//// zombie state dependent code

// Dte = (DTE2)GetService(typeof(DTE));
// eventlistener no longer needed
//// Dte = (DTE2)GetService(typeof(DTE));
//// eventlistener no longer needed

var shellService = GetService(typeof(SVsShell)) as IVsShell;

if (shellService != null)

{
ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(_cookie));
}

_cookie = 0;
}
}

return VSConstants.S_OK;
}

protected override void Initialize()
{
Logger.Initialize(this, Vsix.Name);
base.Initialize();
var shellService = GetService(typeof(SVsShell)) as IVsShell;

if (shellService != null)
{
ErrorHandler.ThrowOnFailure(shellService.AdviseShellPropertyChanges(this, out _cookie));
}

Menus.InstallBootstrapperCommand.Initialize(this);
Menus.InstallShellBootstrapperCommand.Initialize(this);
Menus.InstallConfigFileCommand.Initialize(this);
}
}
}
1 change: 0 additions & 1 deletion src/Classifier/CakeClassificationDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Text.Classification;
using Microsoft.VisualStudio.Utilities;
Expand Down
32 changes: 17 additions & 15 deletions src/Classifier/CakeClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ internal CakeClassifier(IClassificationTypeRegistryService registry)
};
}

#region IClassifier

#pragma warning disable 67

/// <summary>
Expand Down Expand Up @@ -79,7 +77,7 @@ public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
return result; */

//create a list to hold the results
// create a list to hold the results
List<ClassificationSpan> classifications = new List<ClassificationSpan>();
var current = span.GetText();

Expand Down Expand Up @@ -115,18 +113,24 @@ public IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
{
Classify(classifications, current, span, predefinedType.Key, predefinedType.Value);
}
//Classify(classifications, current, span, _brightScriptLanguage.Quoted,
// _stringType);
//Classify(classifications, current, span, _brightScriptLanguage.KeyWords,
// _keywordType);
//Classify(classifications, current, span, _brightScriptLanguage.IdentifierTypes,
// _identifierType);
//Classify(classifications, current, span, _brightScriptLanguage.Numeric,
// _numericType);

////Classify(classifications, current, span, _brightScriptLanguage.Quoted,
//// _stringType);
////Classify(classifications, current, span, _brightScriptLanguage.KeyWords,
//// _keywordType);
////Classify(classifications, current, span, _brightScriptLanguage.IdentifierTypes,
//// _identifierType);
////Classify(classifications, current, span, _brightScriptLanguage.Numeric,
//// _numericType);
return classifications;
}
private void Classify(List<ClassificationSpan> classifications, string current,
SnapshotSpan span, List<string> matchList, IClassificationType classificationType)

private void Classify(
List<ClassificationSpan> classifications,
string current,
SnapshotSpan span,
List<string> matchList,
IClassificationType classificationType)
{
foreach (var item in matchList)
{
Expand All @@ -142,7 +146,5 @@ private void Classify(List<ClassificationSpan> classifications, string current,
}
}
}

#endregion
}
}
4 changes: 0 additions & 4 deletions src/Classifier/CakeClassifierProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ internal class CakeClassifierProvider : IClassifierProvider

#pragma warning restore 649

#region IClassifierProvider

/// <summary>
/// Gets a classifier for the given text buffer.
/// </summary>
Expand All @@ -40,7 +38,5 @@ public IClassifier GetClassifier(ITextBuffer buffer)
{
return buffer.Properties.GetOrCreateSingletonProperty<CakeClassifier>(creator: () => new CakeClassifier(_classificationRegistry));
}

#endregion
}
}
Loading

0 comments on commit f9a30cd

Please sign in to comment.